rr-inla

Error “dsparseModelMatrix” object: superclass "xMatrix" not defined


I am running a Bayesian model with INLA that was working yesterday and not today anymore (how frustrating). So I took a tutorial that I checked previously and that was also working (https://becarioprecario.bitbucket.io/inla-gitbook/ch-spatial.html#spatial-models-using-stochastic-partial-differential-equations). But similarly, it did not work again fot the same reason:

options(repos = c(getOption("repos"),
                  INLA="https://inla.r-inla-download.org/R/stable"))
install.packages("INLA", dep = TRUE)

library(sp)
library(gstat)
library(INLA)
data(meuse)


coordinates(meuse) <- ~x+y
proj4string(meuse) <- CRS("+init=epsg:28992")

data(meuse.grid)
coordinates(meuse.grid) = ~x+y
proj4string(meuse.grid) <- CRS("+init=epsg:28992")
gridded(meuse.grid) = TRUE

library(maptools)
meuse.bdy <- unionSpatialPolygons(
  as(meuse.grid, "SpatialPolygons"), rep (1, length(meuse.grid))
)

pts <- meuse.bdy@polygons[[1]]@Polygons[[1]]@coords
mesh <- inla.mesh.2d(loc.domain = pts, max.edge = c(150, 500),
                     offset = c(100, 250) )

meuse.spde <- inla.spde2.matern(mesh = mesh, alpha = 2)
A.meuse <- inla.spde.make.A(mesh = mesh, loc = coordinates(meuse))
s.index <- inla.spde.make.index(name = "spatial.field",
                                n.spde = meuse.spde$n.spde)
meuse.stack <- inla.stack(data  = list(zinc = meuse$zinc),
                          A = list(A.meuse, 1),
                          effects = list(c(s.index, list(Intercept = 1)),
                                         list(dist = meuse$dist)),
                          tag = "meuse.data")

A.pred <- inla.spde.make.A(mesh = mesh, loc = coordinates(meuse.grid))
meuse.stack.pred <- inla.stack(data = list(zinc = NA),
                               A = list(A.pred, 1),
                               effects = list(c(s.index, list (Intercept = 1)),
                                              list(dist = meuse.grid$dist)),
                               tag = "meuse.pred")

join.stack <- inla.stack(meuse.stack, meuse.stack.pred)

form <- log(zinc) ~ -1 + Intercept + dist + f(spatial.field, model = spde)

m1 <- inla(form, data = inla.stack.data(join.stack, spde = meuse.spde),
           family = "gaussian",
           control.predictor = list(A = inla.stack.A(join.stack), compute = TRUE),
           control.compute = list(cpo = TRUE, dic = TRUE))

with the error :

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 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.

As advised here : Error in validObject(.Object) : invalid class “dsparseModelMatrix” object: superclass "Mnumeric", https://groups.google.com/g/r-inla-discussion-group/c/Pz7cpf4L1zU I reinstall R and it still do not work. I also reinstall Matrix package without any results.

Do you have any clue ?

Ubuntu 22.04

R version 4.3.2 (2023-10-31) -- "Eye Holes"

INLA version 23.09.09


Solution

  • You must re-install MatrixModels from sources after changing between Matrix < 1.6-2 and Matrix >= 1.6-2.

    install.packages("MatrixModels", type = "source")