inlabru-org / inlabru

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

dsparseModelMatrix error in vignettes #218

Closed MarieAugerMethe closed 10 months ago

MarieAugerMethe commented 11 months ago

Hello,

I have used inlabru in the past (it's a great package), and wanted to refresh my mind before jumping into an analysis. I was going through the vignettes found here: https://inlabru-org.github.io/inlabru/articles/

I tried the first 3 vignettes and got an error for all of them.

At first, I thought it was the Intercept(1) without covariates in vignettes 1 & 2, but I get the same error for vignette 3. Then I figured it was a problem with the INLA installation or the Matrix package. So I updated R and reinstalled all of the latest versions of the packages. I still get the same errors.

Here is my session info:

R version 4.3.2 (2023-10-31) -- "Eye Holes" Copyright (C) 2023 The R Foundation for Statistical Computing Platform: x86_64-apple-darwin20 (64-bit)

INLA: v 23.09.09 inlabru: v 2.10.0 Matrix: v 1.6-2

library(inlabru)
library(INLA)
library(mgcv)
library(ggplot2)
bru_safe_sp(force = TRUE)

data(gorillas, package = "inlabru")
nests <- gorillas$nests
mesh <- gorillas$mesh
boundary <- gorillas$boundary

ggplot() +
  geom_fm(data = mesh) +
  gg(nests) +
  gg(boundary, fill = "red", alpha = 0.2) +
  ggtitle("Points")

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

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

fit <- lgcp(cmp, nests, samplers = boundary, domain = list(coordinates = mesh))

Error in validObject(.Object) : invalid class “dsparseModelMatrix” object: superclass "xMatrix" not defined in the environment of the object's class

inla.core.safe: inla.program has crashed: rerun to get better initial values. try=1/1 Error in validObject(.Object) : invalid class “dsparseModelMatrix” object: superclass "xMatrix" not defined in the environment of the object's class Warning in iinla(model = info[["model"]], lhoods = info[["lhoods"]], options = info[["options"]]) : iinla: Problem in inla: Error in inla.core.safe(formula = formula, family = family, contrasts = contrasts, : Failed to get good enough initial values. Maybe it is due to something else. iinla: Problem in inla: 1: lgcp(cmp, nests, samplers = boundary, domain = list(coordinates = mesh)) 2: bru(components, lik, options = options, .envir = .envir) 3: iinla(model = info[["model"]], lhoods = info[["lhoods"]], options = info[[ [...] 4: fm_try_callstack(...) 5: do.call(INLA::inla, inla.options.merged, envir = environment(model$effects)) 6: (function (formula = NULL, family = "gaussian", contrasts = NULL, data = NULL, quantiles = c(0.025, 0.5, 0.975), E = NULL, offset = NULL, scale = NULL, weights = NULL, Ntrials = NULL, strata = NULL, lp.scale = NULL, link.covariates = NULL, verbose = inla [...] lincomb = NULL, selection = NULL, control.compute = list(), control.predictor = list(), control.family = list(), control.inla = list(), control.fi [... truncated] iinla: Giving up and returning last successfully obtained result for diagnostic purposes.

bru_safe_inla()

returns TRUE

Any help would be great!

finnlindgren commented 11 months ago

Can you try the latest INLA version (I think you may have the latest "stable" version, but the "testing" version is more recent) to see if it was a bug that has already been fixed? I don't directly recognise this particular error, but there were some similar errors when Matrix was being updated, and that were fixed already.

MarieAugerMethe commented 11 months ago

Thanks @finnlindgren for the quick reply. I tried the testing version with no luck. I get the same error. Update: one sec. I get a different error.

MarieAugerMethe commented 11 months ago

Sorry, I spoke too fast above. But I think the problem remains.

The error does not stop lgcp() to run, but it returns the warning:

Warning in iinla(model = info[["model"]], lhoods = info[["lhoods"]], options = info[["options"]]) : iinla: Problem in inla: Error in validObject(.Object) : invalid class “dsparseModelMatrix” object: superclass "xMatrix" not defined in the environment of the object's class iinla: Problem in inla: 1: lgcp(cmp, nests, samplers = boundary, domain = list(coordinates = mesh)) 2: bru(components, lik, options = options, .envir = .envir) 3: iinla(model = info[["model"]], lhoods = info[["lhoods"]], options = info[[ [...] 4: fm_try_callstack(...) 5: do.call(INLA::inla, inla.options.merged, envir = environment(model$effects)) 6: (function (formula = NULL, family = "gaussian", contrasts = NULL, data = NULL, quantiles = c(0.025, 0.5, 0.975), E = NULL, offset = NULL, scale = NULL, weights = NULL, Ntrials = NULL, strata = NULL, lp.scale = NULL, link.covariates = NULL, verbose = inla [...] lincomb = NULL, selection = NULL, control.compute = list(), control.predictor = list(), control.family = list(), control.inla = list(), control.fixed = list(), [... truncated] iinla: Giving up and returning last successfully obtained result for diagnostic purposes.

And when I do

fit$error[1]

I get the same error: "Error in validObject(.Object) : \n invalid class “dsparseModelMatrix” object: superclass \"xMatrix\" not defined in the environment of the object's class\n"

finnlindgren commented 11 months ago

What's the names or titles of the vignettes you tried? I'll trigger a rebuild of the website; that will show if it's a general problem or more machine specific.

MarieAugerMethe commented 11 months ago

Thanks Finn!

The code above is from https://inlabru-org.github.io/inlabru/articles/2d_lgcp.html

I have also tried:

https://inlabru-org.github.io/inlabru/articles/1d_lgcp.html https://inlabru-org.github.io/inlabru/articles/2d_lgcp_covars.html

And I get the same problem. I just re-ran all of them with the testing version of INLA and get the same thing now a warning with an error when using fit$error

finnlindgren commented 11 months ago

Ok, thanks! I see the Matrix version is brand new, so they may have introduced an incompatibility. I'll see what the website build reveals, and look into it tomorrow.

MarieAugerMethe commented 11 months ago

Thank you!!!

finnlindgren commented 10 months ago

The issue was indeed with the new Matrix package version, but in a solvable way due to the order in which packages were installed; you need to rebuild some other packages that use/link to Matrix but were installed/built before the new Matrix version. Just reinstalling the MatrixModels package (a simple install.packages("MatrixModels")) and restarting the R session solved the problem on my machine. There may be other packages that also need reinstalling, but that one is the most obvious one.

MarieAugerMethe commented 10 months ago

Thanks Finn,

it's not working on my side. I tried a few things, but here is the latest attempt. I've uninstalled: Matrix, MatrixModels, maxtrixStats, INLA, INLAspacetime, and inlabru.

Then, I've restarted R, and installed first Matrix. In an abundance of caution restarted R, and installed MatrixModels, then INLA with dependencies using install.packages("INLA",repos=c(getOption("repos"),INLA="https://inla.r-inla-download.org/R/testing"), dep=TRUE), then inlabru.

Again in an abundance of caution restarted R, and ran vignette 2 (https://inlabru-org.github.io/inlabru/articles/2d_lgcp.html) and I got the same warning and error.

Am I missing something crucial?

Many thanks!!

finnlindgren commented 10 months ago

Your on mac, which sometimes has its own issues, but from what I can see on CRAN, MatrixModels latest version has been built for your version (but not for all other macos versions) What version do you now have installed? (getNamespaceVersion("MatrixModels"))

MarieAugerMethe commented 10 months ago

Yes, on mac. The version of MatrixModels installed is "0.5-3"

finnlindgren commented 10 months ago

That is the latest MatrixModels version, so that's not the problem at least.

Is your system configured to save&load the R workspace between sessions? If it is, turn that off, so you start with a clean R session; I wonder if you accidentally are using some remnants of objects or methods computed/stored from a previous session.

MarieAugerMethe commented 10 months ago

Hi Finn, no my session does not save and load workspace. I double-checked the Global Options from R studio. And re-did the installation outside of a R studio project, where I also cleared the environment. And still no luck...

finnlindgren commented 10 months ago

I noticed that MatrixModels had a new version just a few days ago. I suspect what happened is that the binary build for macOS was done with the old version of Matrix, and that CRAN hasn't rebuilt it for the new Matrix version yet. You may need to install the previous Matrix version as a temporary fix, unless you're able to install the MatrixModels package from source (this is the normal method on Linux, which is why I didn't encounter this problem myself; reinstalling on my system always links to the other installed packages every time).

MarieAugerMethe commented 10 months ago

Thank you Finn!!!! Yes, installing MatrixModels (and matrixStats, inlabru, to be safe) from source after installing Matrix worked! Thank you!!!!

finnlindgren commented 10 months ago

Great! I pointed people on the r-inla-discussion list to this page so hopefully others with the same issue can find the solution here as well.

ebabcock commented 6 months ago

Hi, I've started getting this error again as of today, and this fix didn't work. I'm in Posit cloud. Is there a new update of Matrix?

ebabcock commented 6 months ago

Never mind. The solution here worked https://github.com/hrue/r-inla/issues/86