inlabru-org / inlabru

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

Issue running inlabru example scripts #97

Closed jamesgrecian closed 3 years ago

jamesgrecian commented 3 years ago

I've been working through the documentation for the latest release of inlabru, but have run into issues trialling the examples given on the new https://inlabru-org.github.io/ page.

When running the lgcp gorilla example I get the following error when trying to generate predictions:

lambda <- predict(fit, pixels(gorillas$mesh), ~ exp(mySmooth + Intercept))
Error in INLA::inla.posterior.sample(n = n, result = result, seed = seed,  : 
  unused argument (parallel.configs = FALSE)

I've tried updating to newer versions of INLA but that doesn't resolve the issue. Has anyone else had the same issue?

> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

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

other attached packages:
[1] INLA_20.03.17 foreach_1.5.1 Matrix_1.3-2  inlabru_2.2.4 sp_1.4-4      ggplot2_3.3.3

loaded via a namespace (and not attached):
 [1] rstudioapi_0.13    magrittr_2.0.1     splines_3.6.3      tidyselect_1.1.0   munsell_0.5.0      colorspace_2.0-0   lattice_0.20-41    R6_2.5.0           rlang_0.4.10      
[10] dplyr_1.0.2        tools_3.6.3        rgdal_1.5-19       grid_3.6.3         gtable_0.3.0       withr_2.3.0        MatrixModels_0.4-1 iterators_1.0.13   ellipsis_0.3.1    
[19] tibble_3.0.4       lifecycle_0.2.0    crayon_1.3.4       purrr_0.3.4        codetools_0.2-18   vctrs_0.3.6        glue_1.4.2         compiler_3.6.3     pillar_1.4.7      
[28] generics_0.1.0     scales_1.1.1       pkgconfig_2.0.3   
finnlindgren commented 3 years ago

The parallel.configs argument was introduced in a more recent INLA version; inlabru does specify the testing repository for inla, so should give you version 20.12.10 of INLA, which should solve the problem, as well as solve various unrelated bugs, both in inla itself, and in support for the new rgdal/sp packages handling of PROJ4/6 issues. However, in the current devel version of inlabru I added a check (I think last week) to work around this specific issue, which should allow older INLA versions to run with newer inlabru versions. Please check if remotes::install_github("inlabru-org/inlabru", ref = "devel") solves the problem, and let me know.

finnlindgren commented 3 years ago

Fixed in 9d2f2af

jamesgrecian commented 3 years ago

Thanks Finn, after installing the development version of inlabru I can generate predictions. However, when trying to plot the predictions the last section of code generates a different figure to the one shown on the webpage:

# Plot the result
ggplot() + 
  gg(lambda) +
  gg(gorillas$nests, color = "red", size = 0.2) +
  coord_equal() +
  ggtitle("Nest intensity per km squared")

Rplot01

finnlindgren commented 3 years ago

Can you be more specific? Is this for https://inlabru-org.github.io/inlabru/articles/web/2d_lgcp.html ?

finnlindgren commented 3 years ago

OK, now I recognised the code from the README... What's class(lambda) ? What's head(lambda) ?

finnlindgren commented 3 years ago

Here's the output on my (Ubuntu 20.04) machine:

> head(lambda)
Object of class SpatialPixelsDataFrame
Object of class SpatialPixels
Grid topology:
  cellcentre.offset   cellsize cells.dim
x          580.1737 0.04056724       149
y          673.8885 0.03446247       150
SpatialPoints:
            x        y
[1,] 583.4596 673.8885
[2,] 583.5002 673.8885
[3,] 583.5408 673.8885
[4,] 583.5813 673.8885
[5,] 583.6219 673.8885
[6,] 583.6625 673.8885
Coordinate Reference System (CRS) arguments: +proj=utm +zone=32 +datum=WGS84 +units=km +no_defs 

Data summary:
      mean              sd            q0.025            median           q0.975           smin        
 Min.   :0.8459   Min.   :1.058   Min.   :0.03202   Min.   :0.4382   Min.   :3.503   Min.   :0.02118  
 1st Qu.:0.8518   1st Qu.:1.086   1st Qu.:0.03881   1st Qu.:0.4394   1st Qu.:3.713   1st Qu.:0.02194  
 Median :0.8665   Median :1.131   Median :0.04709   Median :0.4453   Median :3.996   Median :0.02274  
 Mean   :0.8670   Mean   :1.142   Mean   :0.04481   Mean   :0.4480   Mean   :3.983   Mean   :0.02300  
 3rd Qu.:0.8728   3rd Qu.:1.191   3rd Qu.:0.05159   3rd Qu.:0.4556   3rd Qu.:4.271   3rd Qu.:0.02356  
 Max.   :0.9012   Max.   :1.252   Max.   :0.05353   Max.   :0.4627   Max.   :4.424   Max.   :0.02582  
      smax             cv             var       
 Min.   :5.021   Min.   :1.251   Min.   :1.120  
 1st Qu.:5.535   1st Qu.:1.263   1st Qu.:1.179  
 Median :6.318   Median :1.293   Median :1.280  
 Mean   :6.697   Mean   :1.317   Mean   :1.309  
 3rd Qu.:7.560   3rd Qu.:1.357   3rd Qu.:1.418  
 Max.   :9.277   Max.   :1.432   Max.   :1.567  
> sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.1 LTS

Matrix products: default
BLAS:   /home/flindgre/local/R-4.0.3/lib/R/lib/libRblas.so
LAPACK: /home/flindgre/local/R-4.0.3/lib/R/lib/libRlapack.so

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C               LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
 [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8    LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] INLA_20.12.10      foreach_1.5.1      Matrix_1.2-18      inlabru_2.2.4.9000 sp_1.4-4           ggplot2_3.3.3     
[7] reprex_0.3.0       usethis_2.0.0     

loaded via a namespace (and not attached):
 [1] pillar_1.4.7        compiler_4.0.3      iterators_1.0.13    tools_4.0.3         digest_0.6.27      
 [6] lifecycle_0.2.0     tibble_3.0.4        gtable_0.3.0        lattice_0.20-41     pkgconfig_2.0.3    
[11] rlang_0.4.10        rstudioapi_0.13     rgdal_1.5-18        yaml_2.2.1          xfun_0.19          
[16] withr_2.3.0         dplyr_1.0.2         knitr_1.30          MatrixModels_0.4-1  generics_0.1.0     
[21] fs_1.5.0            vctrs_0.3.6         stats4_4.0.3        rprojroot_2.0.2     grid_4.0.3         
[26] prompt_1.0.0        tidyselect_1.1.0    glue_1.4.2          here_1.0.1          R6_2.5.0           
[31] sn_1.6-2            farver_2.0.3        purrr_0.3.4         magrittr_2.0.1      splines_4.0.3      
[36] scales_1.1.1        clisymbols_1.2.0    codetools_0.2-18    ellipsis_0.3.1      mnormt_2.0.2       
[41] colorspace_2.0-0    numDeriv_2016.8-1.1 labeling_0.4.2      munsell_0.5.0       tmvnsim_1.0-2      
[46] crayon_1.3.4  
jamesgrecian commented 3 years ago

Hi Finn, not sure what is going on. I'm tempted to update R to 4.0 today to see if this resolves the issue. Maybe it's an R 3.6.3 and PROJ issue?

> head(lambda)
       mean        sd     q0.025    median   q0.975       smin     smax       cv       var
1 0.8325980 1.0423994 0.04956669 0.4663908 4.301379 0.03192357 4.576310 1.251984 1.0865966
2 0.8248406 1.0202702 0.05254805 0.4614071 4.233156 0.02969932 4.807245 1.236930 1.0409514
3 0.8255502 1.0171695 0.04975489 0.4736492 4.117812 0.02864141 5.306447 1.232111 1.0346339
4 0.8369182 1.0442203 0.04759382 0.4627597 4.086048 0.02762119 5.857489 1.247697 1.0903961
5 0.8461447 1.0542468 0.04667729 0.4520154 4.023112 0.02654149 5.845364 1.245941 1.1114363
6 0.8200682 0.9515292 0.05067724 0.4660484 3.796091 0.02650315 4.739562 1.160305 0.9054078
> class(lambda)
[1] "prediction" "data.frame"
> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

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

other attached packages:
[1] INLA_20.03.17      foreach_1.5.1      Matrix_1.3-2       inlabru_2.2.4.9000 sp_1.4-4           ggplot2_3.3.3     

loaded via a namespace (and not attached):
 [1] rstudioapi_0.13    magrittr_2.0.1     splines_3.6.3      tidyselect_1.1.0   munsell_0.5.0      colorspace_2.0-0   lattice_0.20-41    R6_2.5.0           rlang_0.4.10      
[10] dplyr_1.0.2        tools_3.6.3        rgdal_1.5-19       grid_3.6.3         gtable_0.3.0       withr_2.3.0        MatrixModels_0.4-1 iterators_1.0.13   ellipsis_0.3.1    
[19] digest_0.6.27      tibble_3.0.4       lifecycle_0.2.0    crayon_1.3.4       farver_2.0.3       purrr_0.3.4        codetools_0.2-18   vctrs_0.3.6        glue_1.4.2        
[28] labeling_0.4.2     compiler_3.6.3     pillar_1.4.7       generics_0.1.0     scales_1.1.1       pkgconfig_2.0.3   
> 
finnlindgren commented 3 years ago

There was a similar issue recently, where either the pixels output wasn't the intended, or it didn't propagate the spatial aspect in the predict output; That issue was fixed, but I wonder if this is a variant of it. What's the output from pixels(gorillas$mesh) ?

finnlindgren commented 3 years ago

I has a suspicion of the cause; are you able to do debugonce(inlabru:::expand_to_dataframe) and then step through that function when running the predict() call? If the pixels output itself was a SpatialPixelsDataFrame, so should the result of expand_to_dataframe. The cbind(x, data) towards the end is where I suspect R4 and R3.6 might behave differently.

jamesgrecian commented 3 years ago

The output of pixels(gorillas$mesh) is a SpatialPixelsDataFrame with the following warnings:

Warning messages:
1: In drawGTree(x) : reached elapsed time limit
2: In proj4string(x) : CRS object has comment, which is lost in output

I've stepped through debugonce(inlabru:::expand_to_dataframe) to the point where result <- cbind(x, data). At this point x is a SpatialPixelsDataFrame while data is a data frame with 0 columns and 14216 rows

finnlindgren commented 3 years ago

And after the cbind, what is "result"? A plain data.frame?

finnlindgren commented 3 years ago

If that's the case, this is the cuplrit, and a workaround would be to do

pix <- pixels(...)
pred <- predict(...)
pred <- SpatialPixelsDataFrame(
                 SpatialPixels(coordinates(pix), proj4string = fm_sp_get_crs(pix)),
                 data = as.data.frame(pred)
             )

(I haven't seen drawGTree before; I don't think it's related)

jamesgrecian commented 3 years ago

I'm not sure that I've done this right, but the R output looks like this:

Browse[2]> n
debug: result <- cbind(x, data)
Browse[2]> n
debug: result
Browse[2]> result
     [,1]
[1,] ?   
finnlindgren commented 3 years ago

Interesting... Can you try this:


 
--

> a<-sp::SpatialPixelsDataFrame(matrix(1:6,3,2),data=data.frame(a=1:3))
> cbind(a,data.frame(b=21:23))
Object of class SpatialPixelsDataFrame
Object of class SpatialPixels
Grid topology:
          cellcentre.offset cellsize cells.dim
coords.x1                 1        1         3
coords.x2                 4        1         3
SpatialPoints:
     coords.x1 coords.x2
[1,]         1         4
[2,]         2         5
[3,]         3         6
Coordinate Reference System (CRS) arguments: NA 

Data summary:
       a             b       
 Min.   :1.0   Min.   :21.0  
 1st Qu.:1.5   1st Qu.:21.5  
 Median :2.0   Median :22.0  
 Mean   :2.0   Mean   :22.0  
 3rd Qu.:2.5   3rd Qu.:22.5  
 Max.   :3.0   Max.   :23.0  
jamesgrecian commented 3 years ago

On closer inspection result is a list of 1 containing the SpatialPixelsDataFrame in the first slot

jamesgrecian commented 3 years ago
> a<-sp::SpatialPixelsDataFrame(matrix(1:6,3,2),data=data.frame(a=1:3))
> a
Object of class SpatialPixelsDataFrame
Object of class SpatialPixels
Grid topology:
          cellcentre.offset cellsize cells.dim
coords.x1                 1        1         3
coords.x2                 4        1         3
SpatialPoints:
     coords.x1 coords.x2
[1,]         1         4
[2,]         2         5
[3,]         3         6
Coordinate Reference System (CRS) arguments: NA 

Data summary:
       a      
 Min.   :1.0  
 1st Qu.:1.5  
 Median :2.0  
 Mean   :2.0  
 3rd Qu.:2.5  
 Max.   :3.0  
> cbind(a,data.frame(b=21:23))
  [,1] [,2]     
b ?    Integer,3
finnlindgren commented 3 years ago

And with sp:::cbind.Spatial(a, data.frame(b=21:23)) ?

jamesgrecian commented 3 years ago
sp:::cbind.Spatial(a, data.frame(b=21:23))
Object of class SpatialPixelsDataFrame
Object of class SpatialPixels
Grid topology:
          cellcentre.offset cellsize cells.dim
coords.x1                 1        1         3
coords.x2                 4        1         3
SpatialPoints:
     coords.x1 coords.x2
[1,]         1         4
[2,]         2         5
[3,]         3         6
Coordinate Reference System (CRS) arguments: NA 

Data summary:
       a             b       
 Min.   :1.0   Min.   :21.0  
 1st Qu.:1.5   1st Qu.:21.5  
 Median :2.0   Median :22.0  
 Mean   :2.0   Mean   :22.0  
 3rd Qu.:2.5   3rd Qu.:22.5  
 Max.   :3.0   Max.   :23.0  
finnlindgren commented 3 years ago

OK, then I think it's a namespace issue for generic methods. Load the sp package explicitly with library(sp) and try the cbind(a, ...) version again.

jamesgrecian commented 3 years ago
library(sp)
> cbind(a,data.frame(b=21:23))
  [,1] [,2]     
b ?    Integer,3
finnlindgren commented 3 years ago

I see. The cbind.Spatial documentation seems to be missing (it points to the Spatial class documentation, but isn't mentioned). You can hopefully use the workaround for now, but I'll update the package to use cbind.Spatial explicitly, which should solve it.

jamesgrecian commented 3 years ago

Thanks @finnlindgren