inlabru-org / inlabru

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

Error with summary() on model returned by bru() #82

Closed ASeatonSpatial closed 3 years ago

ASeatonSpatial commented 3 years ago

This is probably just a small bug, I'm not sure it really affects the summary output. When I run summary() on a model fitted with bru() and an inla.spde2.pcmatern model component, I see the following error at the bottom of the output:

Error in if (eff$model == "spde2") { : argument is of length zero

Here is a reproducible example:

require(gamair)   # has aral sea chlorophyll data
data(aral)
data(aral.bnd)
library(inlabru)
library(INLA)

# Mesh stuff
bnd <- inla.mesh.segment(cbind(aral.bnd$lon, aral.bnd$lat))
loc <- cbind(aral$lon, aral$lat)

mesh <- inla.mesh.2d(boundary=bnd,
                     max.edge=c(0.384, 0.5),
                     min.angle=c(30, 21),
                     max.n=c(48000, 16000), ## Safeguard against large meshes.
                     max.n.strict=c(128000, 128000), ## Don't build a huge mesh!
                     cutoff=0.01, ## Filter away adjacent points.
                     offset=c(0.1, 0.3)) ## Offset for extra boundaries, if needed.

# spde model
spde <- inla.spde2.pcmatern(mesh=mesh,
                            prior.range=c(0.1, 0.5),
                            prior.sigma=c(10, 0.5))

# fit model
formula <- chl ~ Intercept + grf(main = c("lon", "lat"), model=spde)
fit = bru(formula,
          family = "gaussian",
          data = aral)

# summary of model
summary(fit)

Which outputs the following summary

> summary(fit)

--- Likelihoods ----------------------------------------------------------------------------------

--- Criteria -------------------------------------------------------------------------------------

Watanabe-Akaike information criterion (WAIC):   2.524e+03
Deviance Information Criterion (DIC):       2.524e+03

--- Fixed effects -------------------------------------------------------------------------------- 

              mean        sd 0.025quant 0.5quant 0.975quant    mode signif
Intercept 7.126369 0.1476909   6.836283 7.126366   7.416199 7.12637   TRUE

--- Random effects ------------------------------------------------------------------------------- 

grf ranges: mean = [-0.015573, -0.00279093], sd = [9.81306, 22.1313], quantiles = [-50.3478 : 50.3715]

--- All hyper parameters (internal representation) ----------------------------------------------- 

                                               mean           sd 0.025quant   0.5quant
Precision for the Gaussian observations  0.09475057  0.006080073 0.08323349 0.09460216
Range for grf                            0.22504556  0.423184487 0.02203839 0.11058469
Stdev for grf                           13.29680729 13.050600539 0.89893700 9.44926520
                                        0.975quant       mode
Precision for the Gaussian observations  0.1071693 0.09436951
Range for grf                            1.1318961 0.04708621
Stdev for grf                           48.1460019 2.45499929

Error in if (eff$model == "spde2") { : argument is of length zero`

My session info is

> sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.5 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C               LC_TIME=en_GB.UTF-8       
 [4] LC_COLLATE=en_GB.UTF-8     LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] 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.10.12-1    foreach_1.5.1      Matrix_1.2-18      inlabru_2.1.14.900
[5] sp_1.4-4           ggplot2_3.3.2      gamair_1.0-2      

loaded via a namespace (and not attached):
 [1] rstudioapi_0.11    magrittr_1.5       splines_4.0.3      tidyselect_1.1.0  
 [5] munsell_0.5.0      colorspace_1.4-1   lattice_0.20-41    R6_2.4.1          
 [9] rlang_0.4.8        dplyr_1.0.2        tools_4.0.3        rgdal_1.5-18      
[13] grid_4.0.3         gtable_0.3.0       withr_2.3.0        MatrixModels_0.4-1
[17] iterators_1.0.13   ellipsis_0.3.1     yaml_2.2.1         tibble_3.0.4      
[21] lifecycle_0.2.0    crayon_1.3.4       purrr_0.3.4        codetools_0.2-16  
[25] vctrs_0.3.4        glue_1.4.2         compiler_4.0.3     pillar_1.4.6      
[29] generics_0.0.2     scales_1.1.1       pkgconfig_2.0.3 
finnlindgren commented 3 years ago

Yeah, this was a known bug; the whole function should be replaced, but I've made a minimal update that at least gets rid of the direct errors.