Level 2 residuals for ML cumulative logit 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
vivian1234
Posts: 30
Joined: Tue Apr 12, 2016 10:54 am

Level 2 residuals for ML cumulative logit model

Post by vivian1234 »

Hi, I was trying to replicate the analysis from module 9 Practice using R2MLwiN.
I used the following codes to calculate the residual for random intercept and slope:

Code: Select all

## to calculate random intercept residual
u0 <- model03@residual$lev_2_resi_est_Intercept.123/2 

## to calculate random slope residual
u1 <- model03@residual$lev_2_resi_est_agecen50.123/2
However, when I compared with the residuals calculated using MLwiN, u0 obtained from the above code correspond to the random slope residual (agecen50.123) from MLwiN while u1 obtained from R2MLwiN correspond to the random intercept (cons.123) from MLwiN.


Thanks a lot.

Regards,
Vivian
vivian1234
Posts: 30
Joined: Tue Apr 12, 2016 10:54 am

Re: Level 2 residuals for ML cumulative logit model

Post by vivian1234 »

The problem also exists in the random part estimations.

Result obtained by MLwiN

Code: Select all

Random Part			
Level: state_long			
cons.123/cons.123		0.24907945	0.06827134	1
agecen50^1.123/cons.123		0.00349785	0.0013245	0.65697408
agecen50^1.123/agecen50^1.123	0.00011381	0.00003864	1
Result by R2MLwin

Code: Select all

The random part estimates at the state1 level: 
var_Intercept_123		0.00013	0.00005
cov_Intercept_123_agecen50_123	0.004	0.00161
var_agecen50_123		0.28027	0.08268
In this case, should I reverse the random effect obtained from R2MLwin?

Thanks a lot.
ChrisCharlton
Posts: 1351
Joined: Mon Oct 19, 2009 10:34 am

Re: Level 2 residuals for ML cumulative logit model

Post by ChrisCharlton »

Thank you for letting us know about this. I will look into it as soon as I get the chance. Can you please provide the syntax for the model that corresponds to these results?
vivian1234
Posts: 30
Joined: Tue Apr 12, 2016 10:54 am

Re: Level 2 residuals for ML cumulative logit model

Post by vivian1234 »

Hi, I'm using data (9.1wsz) from Module 9. The data is converted to dta and then imported to R. The categorical variable (occtype) is converted into dummy variables. Here is the syntax for replicating the random slope model:

Code: Select all

data9.1$occtype0 <- dummy.code(data9.1$occtype)

RSmodel <- runMLwiN(logit(electint, cons, 4) ~ 1 + female[1:3] +  agecen50[1:3] + 
                      agesq[1:3] + occtype01[1:3] + occtype02[1:3] +
                      occtype03[1:3] + occtype04[1:3] + occtype05[1:3] +
                      (1[1:3] + agecen50[1:3]  | state), 
                    D = "Ordered Multinomial", data = data9.1,
                    estoptions = list(EstM = 1, mcmcMeth = list(iterations = 5000, burnin = 500), 
                                      resi.store = TRUE, resi.store.levs = 2))
                                      
## to calculate random intercept residual
u0 <- RSmodel@residual$lev_2_resi_est_Intercept.123/2 

## to calculate random slope residual
u1 <- RSmodel@residual$lev_2_resi_est_agecen50.123/2    
Thank you very much.

Regards,
Vivian
ChrisCharlton
Posts: 1351
Joined: Mon Oct 19, 2009 10:34 am

Re: Level 2 residuals for ML cumulative logit model

Post by ChrisCharlton »

I have had an initial look into this and it appears that the problem is that cons[1:3] term is in the random part, but not the fixed part. R2MLwiN is assuming that the parameters in MLwiN are in the order that they appear in the equation, however as agecen50[1:3] is in the fixed part, but 1[1:3] isn't the order is reversed due to the way the variables are added to the model. I will attempt to fix this for the next version of the software but in the mean time the workaround is to reorder the terms in the random part so that any terms shared with the fixed part come first and are in the same order. In your case this would mean changing the model to the following:

Code: Select all

RSmodel <- runMLwiN(logit(electint, cons, 4) ~ 1 + female[1:3] +  agecen50[1:3] + 
                      agesq[1:3] + occtype01[1:3] + occtype02[1:3] +
                      occtype03[1:3] + occtype04[1:3] + occtype05[1:3] +
                      ( agecen50[1:3] + 1[1:3]  | state), 
                    D = "Ordered Multinomial", data = data9.1,
                    estoptions = list(EstM = 1, mcmcMeth = list(iterations = 5000, burnin = 500), 
                                      resi.store = TRUE, resi.store.levs = 2))
Thanks again for letting us know about this, and sorry for the inconvenience.
ChrisCharlton
Posts: 1351
Joined: Mon Oct 19, 2009 10:34 am

Re: Level 2 residuals for ML cumulative logit model

Post by ChrisCharlton »

Just to let you know we have just released a new version of R2MLwiN (0.8-5) to CRAN which should fix both of the bugs that you reported. Please let us know if you find any further problems.
Post Reply