Error in fitting spatial poisson BYM models: conflicts between inla function and Matrix package??

887 views
Skip to first unread message

Beth Purse

unread,
Feb 8, 2017, 9:35:06 AM2/8/17
to R-inla discussion group
Hi there,

I am running some spatial BYM models in INLA.
This code used to run fine on R and package versions in August but is now throwing an error during the inla function which I think may be related to package conflicts between INLA and the Matrix or MatrixModels package.

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

This error is thrown whether I am fitting an intercept only INLA model or a model with covariates in it.

My code is as follows and I have attached the workspace and the session info is also given below. Another colleague can get it to run on his machine and his session info is also given.
Any assistance would be very much appreciated.
Best wishes

Beth

#####################################

load("Workspace_test_09Feb2017.Rdata")
require(INLA)
require(spdep)
require(nlme)
temp <- poly2nb(sind)
#The order of the areas needs to be the same between the data and the spatial polygon object obtained importing the shapefile, so we re-order the data.
str(temp)
ID<-seq(1,80)
nb2INLA("South_India.graph", temp)
Yc$ID <- c(1:80)
load("South_India_data_Feb2017_centred.RData")
nullmod <- inla(bt ~ 1 +f(ID, model="bym",graph="South_India.graph"),
data=Yc,
family="poisson",
#param = c(prior.iid, prior.besag),
#initial = c(initial.iid, initial.besag),
#control.data = list(link="log"),
control.compute=list(dic=TRUE))


My session info is as follows:

Session 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

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

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

other attached packages:
[1] spdep_0.6-9         nlme_3.1-130        INLA_0.0-1485844051
[4] Matrix_1.2-7.1      sp_1.2-3          

loaded via a namespace (and not attached):
 [1] lattice_0.20-34    deldir_0.1-12      gtools_3.5.0       MASS_7.3-45      
 [5] grid_3.3.2         MatrixModels_0.4-1 coda_0.19-1        gdata_2.17.0     
 [9] LearnBayes_2.15    gmodels_2.16.2     boot_1.3-18        splines_3.3.2    
[13] tools_3.3.2        compiler_3.3.2


A colleague managed to get the models to run successfully, his session info was:


R version 3.3.2 (2016-10-31)

Platform: x86_64-w64-mingw32/x64 (64-bit)

Running under: Windows 7 x64 (build 7601) Service Pack 1

 

locale:

[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252    LC_MONETARY=English_United Kingdom.1252

[4] LC_NUMERIC=C                            LC_TIME=English_United Kingdom.1252   

attached base packages:

[1] stats     graphics  grDevices utils     datasets  methods   base    

other attached packages:

[1] spdep_0.6-9         INLA_0.0-1485844051 Matrix_1.2-7.1      maptools_0.8-39     sp_1.2-3          


loaded via a namespace (and not attached):

[1] lattice_0.20-34    deldir_0.1-12      gtools_3.5.0       MASS_7.3-45        grid_3.3.2         MatrixModels_0.4-1

[7] nlme_3.1-128       coda_0.19-1        gdata_2.17.0       LearnBayes_2.15    gmodels_2.16.2     boot_1.3-18      

[13] splines_3.3.2      tools_3.3.2        foreign_0.8-67     compiler_3.3.2   



Workspace_test_09Feb2017.Rdata

INLA help

unread,
Feb 8, 2017, 9:59:26 PM2/8/17
to Beth Purse, R-inla discussion group
Beth,

thanks for your email. I ran your example here without any problems on
linux and on a Mac.

Yes, there is certainly some package crash. I would suggest just to
remove your R and all packages you have installed, and simply reinstall
everything. In my experience, it is way easier to bypass the problem
than to try to solve it.

B



On Wed, 2017-02-08 at 06:35 -0800, Beth Purse wrote:
>
> load("Workspace_test_09Feb2017.Rdata")
> require(INLA)
> require(spdep)
> require(nlme)
> temp <- poly2nb(sind)
> #The order of the areas needs to be the same between the data and the
> spatial polygon object obtained importing the shapefile, so we re-
> order the data.
> str(temp)
> ID<-seq(1,80)
> nb2INLA("South_India.graph", temp)
> Yc$ID <- c(1:80)
> load("South_India_data_Feb2017_centred.RData")
> nullmod <- inla(bt ~ 1 +f(ID, model="bym",graph="South_India.graph"),
> data=Yc,
> family="poisson",
> #param = c(prior.iid, prior.besag),
> #initial = c(initial.iid, initial.besag),
> #control.data = list(link="log"),
> control.compute=list(dic=TRUE))
>

--
Håvard Rue
he...@r-inla.org

Beth Purse

unread,
Feb 14, 2017, 4:19:17 AM2/14/17
to R-inla discussion group, bethp...@gmail.com, he...@r-inla.org

Dear Harvard

Thanks so much for taking a look at this for me. I re-installed everything and it works fine now.
Good to know that it is best to "bypass" in these situations.
Thanks again for your time.

Beth

Pedro Garrett

unread,
Feb 21, 2017, 9:39:46 AM2/21/17
to R-inla discussion group
Hi,

I also had the same problem and just updated the "Matrix" package and it worked.

Pedro

Gustavo Machado

unread,
Jun 16, 2017, 4:57:36 PM6/16/17
to R-inla discussion group
I have the same and by updating MATRIX the problem was fixed

Hossameldin Mahmoud

unread,
Nov 24, 2017, 9:57:04 AM11/24/17
to R-inla discussion group
Kindly be informed that I am getting the following error: 

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

when I run the following code 

library(rqpd)

data(bwd)

### A fixed-effects model:

# formula is specified as
fe.form <- dbirwt ~ smoke + dmage + agesq + 
   novisit + pretri2 + pretri3 | as.factor(momid3) 

# estimate the model:
fe.fit <- rqpd(fe.form, panel(lambda=0.5), data=bwd)


can you help me please? 

Finn Lindgren

unread,
Nov 24, 2017, 9:59:37 AM11/24/17
to Hossameldin Mahmoud, R-inla discussion group
rqpd does not appear to be related to the INLA package.

Finn
--
You received this message because you are subscribed to the Google Groups "R-inla discussion group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to r-inla-discussion...@googlegroups.com.
To post to this group, send email to r-inla-disc...@googlegroups.com.
Visit this group at https://groups.google.com/group/r-inla-discussion-group.
For more options, visit https://groups.google.com/d/optout.

Ema Biscaia

unread,
Jul 28, 2023, 3:11:34 PM7/28/23
to R-inla discussion group
Hello everyone!

I just updated my R and RStudio versions to the latest ones and although I was able to install INLA, I'm getting this message when I run "library(INLA)":

  • Not a validObject(): no slot of name "srs" for this object of class "RasterLayer"
I have already updated the Matrix package but it continues to give the error.
I found this error by noticing that the formula for my model had "unknown" written in the environment.

Can you help me?

Thank you a lot!
Have a nice weekend
Ema

Finn Lindgren

unread,
Jul 28, 2023, 6:50:58 PM7/28/23
to Ema Biscaia, R-inla discussion group
What lead you to think it has to do with the Matrix package? The RasterLayer message rather seems related to the raster package. Please provide the traceback() output you can get after the error message, and also the full sessionInfo() output.

Finn

Finn Lindgren

unread,
Jul 28, 2023, 6:52:07 PM7/28/23
to Ema Biscaia, R-inla discussion group
Also make sure you run in a clean R environment, with no automatically loaded .RData session data etc.

Finn

Ema Biscaia

unread,
Jul 29, 2023, 11:15:54 AM7/29/23
to R-inla discussion group
Thank you for your answer, Finn!

Well, as I read in this topic that upgrading the matrix package did work in the past I tried it if I could fix my problem but I couldn't.
I tried next to upgrade the raster package with no success either.

I was able to open the package INLA withou any problem but now I have something like this:
r_ema.PNG

and I can't run my INLA model which I did using the R version 4.1.1.

If there's no solution I think I will just return to the older version where everything worked fine!

Ema

Finn Lindgren

unread,
Jul 29, 2023, 11:38:32 AM7/29/23
to Ema Biscaia, R-inla discussion group
Hi, that's a warning message on a specific line of code and not an error on library(INLA). First of all, check if its the as() call that gives the warning or if its the inla.sp2segment() call, and let me know.

Finn

Finn Lindgren

unread,
Jul 29, 2023, 12:44:36 PM7/29/23
to Ema Biscaia, R-inla discussion group
Can you send me you bound.pt object so I can test it here?
Finn

Finn Lindgren

unread,
Jul 29, 2023, 3:13:34 PM7/29/23
to Ema Biscaia, R-inla discussion group
Correction, it's the pol.PT object I need.
What kind of object is it?
I suspect the error comes from the raster package, but it's not clear why that gets involved here, if pol.Pt is a polygon object of some type.

Finn
--
Finn Lindgren
email: finn.l...@gmail.com

Ema Biscaia

unread,
Jul 31, 2023, 5:20:11 PM7/31/23
to R-inla discussion group
Hi Finn

My pol.PT is an "sf" "data.frame" object that I created from reading with st_read() the shapefile I send above.

Shapefiles.zip

Ema Biscaia

unread,
Jul 31, 2023, 5:30:00 PM7/31/23
to R-inla discussion group
About this, I rerunned the code and it didn't gave me errors when compiling the bound.pt but it continues to give this massive error when I run my model that works perfectly when I use R 4.1.1.
r_mod.PNG
I don't know if it helps but my "form" that's a formula object has written in the environment this: (unknow), but it runs I doesn't seem to be the problem because, again, in R 4.1.1 it has the same thing and the model works. 

I'm very grateful for your help!
Ema

Finn Lindgren

unread,
Jul 31, 2023, 6:08:24 PM7/31/23
to Ema Biscaia, R-inla discussion group
Please show str(form)

Finn

Ema Biscaia

unread,
Aug 1, 2023, 10:04:27 AM8/1/23
to R-inla discussion group
This is the str(form):
form.PNG

Finn Lindgren

unread,
Aug 1, 2023, 10:17:24 AM8/1/23
to Ema Biscaia, R-inla discussion group
Where’s the “unknown” part you mentioned before?

Finn

On 1 Aug 2023, at 15:04, Ema Biscaia <e.bi...@campus.fct.unl.pt> wrote:

This is the str(form):

Finn Lindgren

unread,
Aug 1, 2023, 10:19:30 AM8/1/23
to Ema Biscaia, R-inla discussion group
But if the conversion to bound.pt no longer failed, what did you change?
It’s difficult to help when the problem seems to be changing all the time.

Finn

On 1 Aug 2023, at 15:17, Finn Lindgren <finn.l...@gmail.com> wrote:

Where’s the “unknown” part you mentioned before?

Ema Biscaia

unread,
Aug 11, 2023, 5:38:52 AM8/11/23
to R-inla discussion group
Hello, Finn

I'm sorry I'm only replying now but I have been trying to understand what is happening to R and the new version and all of that.

I'm now able to run every line of my code before the model. When I try to run the model with this code:

inla(form, family = "weibullsurv", data = inla.stack.data(stk),
    control.predictor = list(A = inla.stack.A(stk), compute = TRUE),
    control.compute=list(config = TRUE))


I get the following error:

Error in inla.core(formula = formula, family = family, contrasts = contrasts, : With control.fixed = list(expand.factor.strategy='model.matrix'), then NA's in factor are not allowd. Please use strategy 'inla' instead.
*** inla.core.safe: inla.program has crashed: rerun to get better initial values. try=1/2 Error in inla.core(formula = formula, family = family, contrasts = contrasts, : With control.fixed = list(expand.factor.strategy='model.matrix'), then NA's in factor are not allowd. Please use strategy 'inla' instead. *** inla.core.safe: inla.program has crashed: rerun to get better initial values. try=2/2 Error in inla.core(formula = formula, family = family, contrasts = contrasts, : With control.fixed = list(expand.factor.strategy='model.matrix'), then NA's in factor are not allowd. Please use strategy 'inla' instead. 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.  

I then added control.fixed=list(expand.factor.strategy='inla') and it worked but I'm not sure that is what I want because I want to work with dummy variables in my factor variables (var1 and var2). Does this added line change the results of the model?
Besides all this I don't have any NA in my factor variables. I only have NA in my time2 variable, indicating the right censorship. Is this maybe the problem?

Thank you again for all your time spent
Ema

Finn Lindgren

unread,
Aug 11, 2023, 5:51:43 AM8/11/23
to Ema Biscaia, R-inla discussion group
I'm not sure about the time1 NAs, but for Var1 and Var2, the NAs are introduced by the inla.stack() construction.  You're much better off doing the conversion to dummy (0/1) variables yourself first, since that would give you complete control over how the factor variables are interpreted. E.g. first calling "MyVars = model.matrix(~ 1 + Var1 + Var2, data = ...)", and then using "... ~ 0 + MyVars" instead of "... ~ Var1 + Var2" in the inla formula, and as (matrix) input to inla.stack would probably give you what you want.

Finn

Ema Biscaia

unread,
Aug 11, 2023, 6:13:03 AM8/11/23
to R-inla discussion group
Well, yes, I don't think the problem is with the NA's in time2 because I changed them to Inf and I still have the error.

I applied your suggestion to the formula but I don't understand how to do it in the stack. Should I change the "A" argument or the "effects" one?

Thank you
Ema

Finn Lindgren

unread,
Aug 11, 2023, 6:28:21 AM8/11/23
to Ema Biscaia, R-inla discussion group
The effects part. MyVars=MyVars instead of Var1=Var1. It’s been a while since I used it with a matrix, so there may be some detail I’ve forgotten, but essentially a matrix should work just as a regular variable.

Finn

On 11 Aug 2023, at 11:13, Ema Biscaia <e.bi...@campus.fct.unl.pt> wrote:

Well, yes, I don't think the problem is with the NA's in time2 because I changed them to Inf and I still have the error.

Ema Biscaia

unread,
Aug 11, 2023, 6:55:31 AM8/11/23
to R-inla discussion group
It worked!! Thank you a lot, Finn!

I now have to compare the results with this version of R (4.3.1) with the older one I was working on. Is it expected that the results differ?

Ema

Reply all
Reply to author
Forward
0 new messages