inlabru-org / inlabru

inlabru
https://inlabru-org.github.io/inlabru/
76 stars 21 forks source link

Problem predicting from bru/lgcp model: "Error in samples[[i]] : subscript out of bounds" #66

Closed esnightingale closed 4 years ago

esnightingale commented 4 years ago

I've gotten this error with my own data and when running the gorillas example code on https://rdrr.io/cran/inlabru/man/predict.bru.html. Any idea what the problem is?

if (require("INLA", quietly = TRUE)) {

  # Load the Gorilla data

  data(gorillas, package ="inlabru")

  # Plot the Gorilla nests, the mesh and the survey boundary

  ggplot() +
    gg(gorillas$mesh) +
    gg(gorillas$nests) +
    gg(gorillas$boundary) +
    coord_fixed()

  # Define SPDE prior

  matern <- inla.spde2.pcmatern(gorillas$mesh,
                                prior.sigma = c(0.1, 0.01),
                                prior.range = c(5, 0.01))

  # Define domain of the LGCP as well as the model components (spatial SPDE effect and Intercept)

  cmp <- coordinates ~ mySmooth(map = coordinates, model = matern) + Intercept

  # Fit the model
  fit <- lgcp(cmp, gorillas$nests, samplers = gorillas$boundary)

  # Once we obtain a fitted model the predict function can serve various purposes. 
  # The most basic one is to determine posterior statistics of a univariate
  # random variable in the model, e.g. the intercept

  icpt <- predict(fit, NULL, ~ Intercept)

That last line yields the error

Error in samples[[i]] : subscript out of bounds In addition: Warning message: In inla.posterior.sample.interpret.selection(selection, result) : Some selections are not used:

sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17134)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252 
[2] LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] parallel  stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
[1] inlabru_2.1.9 ggplot2_3.1.0 INLA_18.10.29 sp_1.3-1      Matrix_1.2-14

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.2             polyCub_0.7.0          pillar_1.4.1          
 [4] compiler_3.5.1         plyr_1.8.4             tools_3.5.1           
 [7] rpart_4.1-13           goftest_1.1-1          digest_0.6.20         
[10] nlme_3.1-137           surveillance_1.16.2    tibble_2.1.3          
[13] gtable_0.2.0           lattice_0.20-35        mgcv_1.8-24           
[16] pkgconfig_2.0.2        rlang_0.3.4            rstudioapi_0.8        
[19] rgdal_1.3-6            spatstat.data_1.4-0    withr_2.1.2           
[22] dplyr_0.8.1            knitr_1.20             MatrixModels_0.4-1    
[25] spatstat.utils_1.13-0  htmlwidgets_1.3        grid_3.5.1            
[28] tidyselect_0.2.5       glue_1.3.1             data.table_1.11.8     
[31] R6_2.4.0               polyclip_1.9-1         tensor_1.5            
[34] deldir_0.1-15          purrr_0.2.5            magrittr_1.5          
[37] MASS_7.3-50            scales_1.0.0           htmltools_0.3.6       
[40] splines_3.5.1          abind_1.4-5            spatstat_1.57-1       
[43] assertthat_0.2.1       fanplot_3.4.1          xtable_1.8-3          
[46] colorspace_1.4-1       labeling_0.3           lazyeval_0.2.1        
[49] munsell_0.5.0          hhh4addon_0.0.0.0.9006 crayon_1.3.4 
finnlindgren commented 4 years ago

This bug was fixed in version 2.1.10; from https://cran.r-project.org/web/packages/inlabru/news/news.html,

inlabru 2.1.10 Fix internal sampling bug due to INLA changes