level 1 variance poisson model

Welcome to the forum for R2MLwiN users. Feel free to post your question about R2MLwiN here. The Centre for Multilevel Modelling take no responsibility for the accuracy of these posts, we are unable to monitor them closely. Do go ahead and post your question and thank you in advance if you find the time to post any answers!

Go to R2MLwiN: Running MLwiN from within R >> http://www.bris.ac.uk/cmm/software/r2mlwin/
Post Reply
med151991
Posts: 14
Joined: Tue Dec 11, 2018 11:52 am

level 1 variance poisson model

Post by med151991 »

Hello,

I am trying to fit a three level poisson model. WHen I just do it as two level model it runs fine, but when I add the third level in, i get an error message saying 'variables cannot be made random at level one in poisson models' but I have not included any random level 1 variables. Here is my code:

Code: Select all

model<-log(death)~1+gender+education+parentdeath+offset(log(personyears))+(1|region)+(1|household)
model1<runMLwiN(Formula=model, D="Poisson", data=dataset, estoptions=list(EstM=1))

I have tried to sort the data using levID = c("zone", "household", "id") but when i include that in the model I still get the error about level 1 random and also another error saying 'syntax has been superseded, see help for guidance'.

Can anyone give me some pointers on where I am going wrong here?

Many thanks,

Megan
ChrisCharlton
Posts: 1348
Joined: Mon Oct 19, 2009 10:34 am

Re: level 1 variance poisson model

Post by ChrisCharlton »

Other than having < instead of <- in your second line, which shouldn't have an effect on the model, I can't see anything wrong with your specification. Can you check that example 11.3 in chapter 11 of the MCMC guide replication examples on https://www.bristol.ac.uk/cmm/software/ ... /examples/ works for you as this should be an equivalent model?

The option levID doesn't sort the data but is related to an older version of the function syntax so you shouldn't need to use that. If you did want to change options related to the data sorting then these are sort.force and sort.ignore. These will either sort the data for you based on what the function thinks the hierarchy is, or suppress any checks on how it is sorted.
med151991
Posts: 14
Joined: Tue Dec 11, 2018 11:52 am

Re: level 1 variance poisson model

Post by med151991 »

Hi Chris,

Thanks very much for your reply, that was a mistake in my first post i do have a <- instead. I have tried the example data and code and that worked absolutely fine, but mine still does not. I have tried a simple version of the model with this code

Code: Select all

(model<-runMLwiN(log(death)~1+gender+offset(log(personyears))+(1|region)+(1|household), D="Poisson", estoptions=list(Est=1), data=dataset)
and i get the error - "data is not sorted according to the model hierarchy". I have tried the same code with versions of sort.force and sort.ignore and neither work and I get the error again saying "variables cannot be made random at level one in Poisson models" and an additional error saying "this syntax has been superseded see help for guidance on converting it" and also "number of items to replace is not a multiple of replacement length"
ChrisCharlton
Posts: 1348
Joined: Mon Oct 19, 2009 10:34 am

Re: level 1 variance poisson model

Post by ChrisCharlton »

If you look at the code for the runMLwiN function at https://github.com/r-forge/r2mlwin/blob ... runMLwiN.R you will see that the first message comes from line 601 and is only triggered if the levID option has been set. By default this is NULL and your syntax doesn't appear to set the option so I'm not sure why this is happening. One thing I do notice about your most recent code is that you are missing an closing bracket, but this might have just been missed when you did the copy/paste.

Do you have version of some data that has this same problem that you are able to share so that I can debug this in more detail?
med151991
Posts: 14
Joined: Tue Dec 11, 2018 11:52 am

Re: level 1 variance poisson model

Post by med151991 »

Hi Chris,

Yes this was happening before I tried using levID anyway. Unfortunately the dataset is in a server with secure access so I cannot take any of the data out. As it is working with the example I wonder if it is something to do with my data. It is already in long format but I haven't done any sorting of any kind, does this need to be done?

I've had success with running a two level model in this way by using the group_by function in R, but the trouble is I don't know how to do that for a three level model so this has left me stuck
ChrisCharlton
Posts: 1348
Joined: Mon Oct 19, 2009 10:34 am

Re: level 1 variance poisson model

Post by ChrisCharlton »

To get the right answers the data will need to be sorted (or you could potentially turn on the xc option as you are using MCMC). This should be as simple as:

Code: Select all

dataset <- dataset[order(dataset$region, dataset$household), ]
Setting sort.force to TRUE is supposed to do this for you within the function.

If you set sort.ignore to TRUE then this will stop the check being run, so a model should fit but you will probably see the number of higher-level units reported is wrong with corresponding incorrect model results.

You could potentially try generating a fake dataset with a structure similar to yours and testing with that to see whether it works, which might indicate whether there is a particular aspect causing the problem.
med151991
Posts: 14
Joined: Tue Dec 11, 2018 11:52 am

Re: level 1 variance poisson model

Post by med151991 »

Sorting the data with that code worked! My model is now running, thank you! :)
laurasmith
Posts: 2
Joined: Tue May 23, 2023 6:46 am

Re: level 1 variance poisson model

Post by laurasmith »

Himapquest driving directions
To specify this correctly, you need to nest the "household" random intercept within the "region" random intercept. You can modify your model formula as follows:
model <- log(death) ~ 1 + gender + education + parentdeath +
offset(log(personyears)) + (1 | region / household)
This specifies that "household" is nested within "region" and is at level 3.
Post Reply