Dear All,
I have fitted 2 level multivariate multilevel model using runmlwin.
I used the output to compute the level 1 residual manually instead of using the automated residual generation command in runmlwin so I can include only significant covariates:
Residual level1 = y-(b0+0*age-0.43*female +...+0.04*BMI), where y is the observed, b0 is the constant term in the model and others are the covariates. The covariate which is not significant is multiplied by zero as can be seen with age above.
I wish to do same for level 2 residual but I am having a little difficulty about how to do it.
Please, any help will be highly appreciated.
Many thanks.
Jascalla
Computing Level 2 residuals manually
-
- Site Admin
- Posts: 432
- Joined: Fri Apr 01, 2011 2:14 pm
Re: Computing Level 2 residuals manually
Hi J,
Interesting question. Below I show you how to calculate the level-2 and level-1 residuals manually. I also show you how to check that they are the same as the ones that runmlwin will automatically give you if you use the residuals() option.
In terms of your specific query of setting some of the estimated regression coefficients, simply adjust the line of code starting with generate xb_ij.
Best wishes
George
Interesting question. Below I show you how to calculate the level-2 and level-1 residuals manually. I also show you how to check that they are the same as the ones that runmlwin will automatically give you if you use the residuals() option.
In terms of your specific query of setting some of the estimated regression coefficients, simply adjust the line of code starting with generate xb_ij.
Best wishes
George
Code: Select all
* Load data
use "http://www.bristol.ac.uk/cmm/media/runmlwin/tutorial.dta", clear
* Fit model and store level-2 shrunken residuals in a new variable called u0
* and store the level-1 residual in a new variable called e0
runmlwin normexam cons standlrt girl i.schgend, ///
level2(school: cons, residuals(u)) ///
level1(student: cons, residuals(e)) ///
nopause
* Generate fixed part prediction
generate xb_ij = _b[cons] + _b[standlrt]*standlrt + _b[girl]*girl ///
+ _b[_2_schgend]*i2.schgend + _b[_3_schgend]*i3.schgend
* Generate raw total residual
generate r_ij = normexam - xb_ij
* Generate raw level-2 residual
bysort school: egen r_j = mean(r_ij)
* Generate number of level-1 units per level-2 unit
bysort school: generate n_j = _N
* Store level-2 variance
scalar sigma2_u = [RP2]var(cons)
* Store level-1 variance
scalar sigma2_e = [RP1]var(cons)
* Generate shrinkage factor
generate s_j = sigma2_u/(sigma2_u + sigma2_e/n_j)
* Generate shrunken level-2 residual
generate u_j = s_j*r_j
* Confirm that our manually calculated u_j equals u0 calculated by MLwiN to 3
* decimal places
assert round(u_j,0.001)==round(u0,0.001)
* Generate level-1 reidual
generate e_ij = normexam - xb_ij - u_j
* Confirm that our manually calculated e_ij equals e0 calculated by MLwiN to 3
* decimal places
assert round(e_ij,0.001)==round(e0,0.001)
Re: Computing Level 2 residuals manually
Hi George,
Many thanks for this and I will give it a go.
I do appreciate your timely response always.
Thank you
Many thanks for this and I will give it a go.
I do appreciate your timely response always.
Thank you