inlabru-org / inlabru

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

Bug when including factor variable in prediction of lgcp model equation #86

Closed jdselwyn closed 3 years ago

jdselwyn commented 3 years ago

When I fit an LGCP model with a factor variable as a covariate I get the following error when predicting the model onto the results of pixels.

Error in Matrix::sparseMatrix(i = which(ok), j = val[ok], x = rep(weights,  : 
  all(dims >= dims.min) is not TRUE

I'm able to predict using ipoints or without the factor variable in the prediction equation. Below is code to reproduce the error using the gorillas dataset.

library(inlabru)
library(INLA)
library(ggplot2)

# Load the data
data(gorillas, package = "inlabru")

# Construct latent model components
matern <- inla.spde2.pcmatern(gorillas$mesh, 
                              prior.sigma = c(0.1, 0.01), 
                              prior.range = c(5, 0.01))
cmp <- coordinates ~ mySmooth(main = coordinates,
                              model = matern) +
  vegetation(main = gorillas$gcov$vegetation, model = "iid", n = 6) +
  Intercept

# Fit LGCP model
fit <- lgcp(cmp,
            data = gorillas$nests,
            samplers = gorillas$boundary,
            domain = list(coordinates = gorillas$mesh),
            options = list(control.inla = list(int.strategy = "eb")))

#Predict Number of Nests
integration_points <- ipoints(gorillas$boundary, gorillas$mesh)

predict(fit, integration_points, ~ sum(weight * exp(mySmooth + Intercept + vegetation)))
#This works

# Predict Gorilla nest intensity
lambda <- predict(fit, pixels(gorillas$mesh, mask = gorillas$boundary), ~ exp(mySmooth + Intercept + vegetation))
#This doesn't

#Prediction without vegetation covariate
lambda <- predict(fit, pixels(gorillas$mesh, mask = gorillas$boundary), ~ exp(mySmooth + Intercept))
#This works
> sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

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

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

other attached packages:
[1] INLA_20.10.12-1    foreach_1.5.0      Matrix_1.2-18      inlabru_2.1.14.900 sp_1.4-4          
[6] ggplot2_3.3.2     

loaded via a namespace (and not attached):
 [1] compiler_4.0.2        pillar_1.4.6          iterators_1.0.12      remotes_2.1.1        
 [5] tools_4.0.2           rpart_4.1-15          goftest_1.2-2         lifecycle_0.2.0      
 [9] tibble_3.0.4          gtable_0.3.0          nlme_3.1-148          lattice_0.20-41      
[13] mgcv_1.8-31           pkgconfig_2.0.3       rlang_0.4.8           rstudioapi_0.11      
[17] rgdal_1.5-18          spatstat.data_1.4-3   withr_2.3.0           dplyr_1.0.2          
[21] MatrixModels_0.4-1    generics_0.0.2        vctrs_0.3.4           spatstat.utils_1.17-0
[25] stats4_4.0.2          tidyselect_1.1.0      grid_4.0.2            glue_1.4.2           
[29] R6_2.5.0              sn_1.6-2              polyclip_1.10-0       purrr_0.3.4          
[33] deldir_0.1-25         tensor_1.5            magrittr_1.5          codetools_0.2-16     
[37] scales_1.1.1          ellipsis_0.3.1        MASS_7.3-51.6         splines_4.0.2        
[41] mnormt_2.0.2          abind_1.4-5           spatstat_1.64-1       colorspace_1.4-1     
[45] numDeriv_2016.8-1.1   munsell_0.5.0         tmvnsim_1.0-2         crayon_1.3.4
finnlindgren commented 3 years ago

Thanks! I had missed some nrow() calls that needed to be NROW() to handle more kinds of input.