inlabru-org / inlabru

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

inlabru : matrix dimensions problem #163

Closed Mouniaz96 closed 1 year ago

Mouniaz96 commented 1 year ago

Dear all,

I've sent recently a question about spatial covariates effects varying over time (here named Shop), and since then I've tried to compute something like this :

formula = Cars ~ Shop_time (hour.index, Shop, model='ar', order=2) + ...

inlabru.shop=bru(formula , data = data.est , family = "poisson",
                 options=list(inla.mode="experimental",control.compute=list(cpo = TRUE)))

predict(inlabru.shop, data.pred, ~ exp(Intercept + Shop_time + ...)

and I have this error message :

'Error: Matrices must have same dimensions in Matrix(e1) * e2'

The prediction data 'data.pred' has the same names and number of columns as data.

I have this message only when I include this Shop_time term.

The code is the following :

require(INLA)
require(inlabru)

data1.csv data2.csv

data.est=read.csv('data1.csv', header=TRUE, sep=',')
data_pred.mds=read.csv('data2.csv', header=TRUE, sep=',')  #Prediction data

#Formula
shop.mds  <- Cars ~ Shop_time(hour.index, Shop, model='ar', order=2)+
  noise(main=loc.index, model = "iid", constr = TRUE) + # noise
  sitehour(main=cbind(Xutm, Yutm), model = prior_spde.mds, group=hour.index,  # space-hour interaction
           control.group = list(model = "ar", order=2, scale.model= TRUE)) + 
  hourday(main=hour.index, model='rw2', scale.model = TRUE, cyclic=TRUE,     # space-day interaction
          group=day.index, control.group = list(model = "ar", order=2, scale.model= TRUE))

shopp.mds <- bru(shop.mds , data = data.est, family = "poisson", 
                 options=list(inla.mode="experimental",control.compute=list(cpo = TRUE)))

# CPO - DIC  - PIT
shopp.mds$dic$dic 
-mean(log(shopp.mds$cpo$cpo))
shopp.mds$failure
hist(shopp.mds$cpo$pit)
shopp.mds$waic$waic 

##########################################
# PROBLEM Error: Matrices must have same dimensions in Matrix(e1) * e2
##########################################

myPred.mds <- predict(shopp.mds, data_pred.mds, ~ exp(Intercept + Shop_time + sitehour + hourday ))

[#####################################################] Do you know how I can fix this ? Thank you for your help. Best regards.

Mounia

finnlindgren commented 1 year ago

Hi, can you supply the prior_spde.mds object as well so I can test the code here?

Also the traceback() output after you get the error, and your sessionInfo().

Mouniaz96 commented 1 year ago

Sorry i forgot, here are the mesh and the prior_spde.mds object

nb_sens=17
mesh <- inla.mesh.2d(loc=data.est[1:nb_sens,c(2,1)], max.edge= c(0.4,2), offset=0.4, cutoff = 0.1)
prior_spde.mds <- inla.spde2.pcmatern(
  mesh, alpha=3/2,
  prior.range = c(1.8, 0.5), # Probability(range < 1.8) = 0.5
  prior.sigma = c(0.6, 0.7)  # Probability(sigma > 0.6) = 0.7
)
Mouniaz96 commented 1 year ago

traceback() 19: stop(gettextf("Matrices must have same dimensions in %s", deparse(sys.call(sys.parent()))), call. = FALSE, domain = NA) 18: dimCheck(e1, e2) 17: Matrix(e1) e2 16: Matrix(e1) e2 15: eval(call, parent.frame()) 14: eval(call, parent.frame()) 13: callGeneric(Matrix(e1), e2) 12: val[["weights"]] A 11: val[["weights"]] A 10: amatrix_eval.component(x, data = data, ...) 9: amatrix_eval(x, data = data, ...) 8: FUN(X[[i]], ...) 7: lapply(components, function(x) amatrix_eval(x, data = data, ...)) 6: amatrix_eval.component_list(model$effects[included], data = data, inla_f = TRUE) 5: amatrix_eval(model$effects[included], data = data, inla_f = TRUE) 4: evaluate_model(model = object$bru_info$model, state = state, data = data, predictor = formula, include = include, exclude = exclude) 3: generate.bru(object, data = data, formula = formula, n.samples = n.samples, seed = seed, num.threads = num.threads, include = include, exclude = exclude, ...) 2: predict.bru(shopp.mds, data_pred.mds, ~exp(Intercept + Shop_time + sitehour + hourday)) 1: predict(shopp.mds, data_pred.mds, ~exp(Intercept + Shop_time + sitehour + hourday)) ##################### sessionInfo()

R version 4.2.1 (2022-06-23 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19044)

Matrix products: default

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

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

other attached packages: [1] inlabru_2.5.2 INLA_22.07.23 sp_1.5-0 foreach_1.5.2 Matrix_1.4-1

loaded via a namespace (and not attached): [1] Rcpp_1.0.9 plyr_1.8.7 pillar_1.8.0 compiler_4.2.1 RColorBrewer_1.1-3 [6] viridis_0.6.2 remotes_2.4.2 iterators_1.0.14 tools_4.2.1 lifecycle_1.0.1
[11] tibble_3.1.8 gtable_0.3.0 lattice_0.20-45 viridisLite_0.4.0 pkgconfig_2.0.3
[16] rlang_1.0.4 cli_3.3.0 rstudioapi_0.13 rgdal_1.5-32 gridExtra_2.3
[21] dplyr_1.0.9 MatrixModels_0.5-0 generics_0.1.3 vctrs_0.4.1 stats4_4.2.1
[26] grid_4.2.1 tidyselect_1.1.2 glue_1.6.2 R6_2.5.1 fansi_1.0.3
[31] sn_2.0.2 tidyr_1.2.0 ggplot2_3.3.6 purrr_0.3.4 magrittr_2.0.3
[36] splines_4.2.1 scales_1.2.0 codetools_0.2-18 mnormt_2.1.0 colorspace_2.0-3
[41] numDeriv_2016.8-1.1 utf8_1.2.2 munsell_0.5.0

finnlindgren commented 1 year ago

I haven't been able to reproduce the problem when running on a lower resolution mesh (to make it faster to get to the predict() part). I tried several inlabru versions, both with your Matrix version and the current version. I'm rerunning with your original mesh now, to see if that triggers the problem.

Can you try upgrading inlabru to at least the CRAN version 2.5.3 and see if that makes a difference?

finnlindgren commented 1 year ago

I also get no error with the full mesh, for inlabru 2.5.2, Matrix 1.4-1.

finnlindgren commented 1 year ago

I notice that trying to save the estimated object to an .rds file generates a huge file (10GB and it hasn't even finished saving it yet). Not sure if that's related, but I think that's a general problem.

finnlindgren commented 1 year ago

I traced the memory use to the inla "configs" output, which is required for predict() to work. To reduce the memeory needed when testing, add the option control.inla=list(int.strategy="eb").

Mouniaz96 commented 1 year ago

I tried the 2.5.3 version and I have the same problem. Ok, I will try to reduce the memory right now

Mouniaz96 commented 1 year ago

Hi, it didn't help either unfortunately. Is it working for you ? Should I run this on another laptop ? Thank you.

finnlindgren commented 1 year ago

I haven't been able to reproduce the problem here. Can you first try the inlabru development version, in case there's some strange issue that's been resolved there? devtools::install_github("inlabru-org/inlabru")

finnlindgren commented 1 year ago

Also reinstall and/or upgrade the Matrix package, since you have an old version of that.

Mouniaz96 commented 1 year ago

Hi again, I think I found the problem, the class of my covariate Shop in the data_pred.mds is 'matrix array' and it is simply coming from that. Thank you for your help.

finnlindgren commented 1 year ago

Interesting; can you confirm that if you run exactly the code you posted above that you don't get the error (as your own code must be reading the variables in some other way to create an array)? A simple Shop_time(hour.index, as.vector(Shop), ...) should fix it.

I'll keep this issue open to remember to check to what extent the inlabru code might coerce inputs to avoid similar issues, or give more informative messages than the built-in R error.

Mouniaz96 commented 1 year ago

Hi Finn, I tried to run the same code that I posted and it was incredibly slow and I think I didn't get the same error. So I believe that it is just coming from the table, and the way I saved it that transformed the covariate for some reason...