Apr 11, 2016

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

Download the complete do-file including data here.
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
  // .. Skip a couple of thousand observations
end

// Rename outcome variables for -reshape-:
rename langPOST score1 
rename aritPOST score2

// Convert data into long format:
reshape long score, i(pupilNR_new) j(outcome)

// Create dummy variables that identify outcome variable:
quietly tab outcome, gen(outcome) 

// Results in Table 16.1:
mixed score outcome1 outcome2, nocons ///
   || schoolnr: outcome1 outcome2, nocons cov(un) ///
   || pupilNR_new: , nocons cov(un) residuals(un, t(outcome))

// Results in Table 16.2: 
mixed score outcome1 outcome2 ///
      outcome#c.IQ_verb outcome#c.ses ///
   outcome#c.sch_iqv outcome#c.sch_ses ///
   outcome#c.IQ_verb#c.ses outcome#c.sch_iqv#c.sch_ses, nocons ///
   || schoolnr: outcome1 outcome2, nocons cov(un) ///
   || pupilNR_new:, nocons cov(un) residuals(un, t(outcome)) 

Reference

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