Apr 12, 2016

Snijders and Bosker's (2012) chapter on multivariate multilevel analysis using -runmlwin-

version 13.1
clear

// Data set for Example 16.1
// mlbook2, data set with pupils having missings on ses or IQ_verb or (langPOST and aritPOST) excluded.
input schoolnr pupilNR_new langPOST aritPOST ses IQ_verb IQ_perf Minority denomina sch_ses sch_iqv sch_min
  1.00    1.00     .  12.00 -17.73  -1.37  -3.75   1.00   1.00 -14.035 -1.4039  0.630
  1.00    3.00  46.00  24.00  -4.73   3.13   1.25   0.00   1.00 -14.035 -1.4039  0.630
  1.00    4.00  45.00  19.00 -17.73   2.63  -1.08   1.00   1.00 -14.035 -1.4039  0.630
  1.00    5.00  33.00  24.00 -12.73  -2.37  -0.08   0.00   1.00 -14.035 -1.4039  0.630
  1.00    6.00  46.00  26.00  -4.73  -0.87  -1.08   0.00   1.00 -14.035 -1.4039  0.630
  1.00    7.00  20.00   9.00 -17.73  -3.87  -4.41   0.00   1.00 -14.035 -1.4039  0.630
  1.00    8.00  30.00  13.00 -17.73  -2.37  -2.08   1.00   1.00 -14.035 -1.4039  0.630
// .. Skip a few cases ..
end

// 1. State Mlwin path: 
global MLwiN_path C:\Program Files (x86)\MLwiN v2.31\i386\mlwin.exe

// 2. Create constant for models:
generate cons = 1

// 3. Fit models:
//    Results of Table 16.1:
runmlwin (langPOST cons, eq(1)) (aritPOST cons, eq(2)), ///
         level2(schoolnr: (cons, eq(1)) (cons, eq(2))) ///
         level1(pupilNR_new: (cons, eq(1)) (cons, eq(2))) ///
   nopause
  
//    Population correlation coefficients:
di [RP2]cov(cons_1\cons_2) / sqrt([RP2]var(cons_1)*[RP2]var(cons_2)) // School-level
di [RP1]cov(cons_1\cons_2) / sqrt([RP1]var(cons_1)*[RP1]var(cons_2)) // Student-level

//    This one seems a bit off in the book:
di ([RP2]cov(cons_1\cons_2) + [RP1]cov(cons_1\cons_2)) / sqrt( ([RP2]var(cons_1) + [RP1]var(cons_1)) ///
                                                             * ([RP2]var(cons_2) + [RP1]var(cons_2)))

//    Correlation between group means for groups of a hypothetical size n = 30
di ([RP2]cov(cons_1\cons_2) + [RP1]cov(cons_1\cons_2) /30) / sqrt( ([RP2]var(cons_1) + [RP1]var(cons_1) /30) ///
                                                                 * ([RP2]var(cons_2) + [RP1]var(cons_2) /30))
   
//    Results of Table 16.2:
//    - Create interaction terms (-runmlwin- doesn't accept factor variables):
generate iqXses = IQ_verb * ses 
generate schiqXschses = sch_iqv * sch_ses
//    - Fit model:
runmlwin (langPOST cons IQ_verb ses sch_iqv sch_ses iqXses schiqXschses, eq(1)) ///
         (aritPOST cons IQ_verb ses sch_iqv sch_ses iqXses schiqXschses, eq(2)), ///
         level2(schoolnr: (cons, eq(1)) (cons, eq(2))) ///
         level1(pupilNR_new: (cons, eq(1)) (cons, eq(2))) ///
   nopause

References

Leckie, George, and Chris Charlton. 2013. "runmlwin. A Program to Run the MLwiN Multilevel Modeling Software from within Stata." Journal of Statistical Software 52(11):1-40.

Snijders, Tom, and Roel Boskers. 2012. Multilevel Analysis. An Introduction to Basic and Advanced Multilevel Modeling, 2nd ed. Sage.