Statistical Computing Seminar
Introduction to Multilevel Modeling Using MLwiN

This seminar is based on the examples in the paper Using SAS Proc Mixed to Fit Multilevel Models, Hierarchical Models, and Individual Growth Models by Judith Singer and can be downloaded from Professor Singer's web site.

MLwiN data files, hsb12.ws , hsb12a.ws and willett.ws

Outline


Starting MLwiN and opening data

To open an existing MLwiN worksheet, you can do the following.

Once you have the data open, you can see the variable names, number of cases (valid and missing) and min/max like this.

You can view the data in a kind of spreadsheet like this.


Model 1: Predicting MathAch from meanses

This model is from page 331 of the Singer article.

This model uses a school level predictor, meanses, to predict MathAch.  Further, we recognize that each school may have a different intercept, so the intercept is random at level 2 and level 1.  We can write this model using multiple equations as shown below.

Level 1: MathAchij β0j + rij  
Level 2: β0j =  γ00 + γ01(MeanSES) + u0j

Combining the two equations into one by substituting the level 2 equation into the level 1 equation, we have the equation below, with the random effects identified by placing them in square brackets.

MathAchij  γ00 + γ01(MeanSES) + [ u0j + rij ]

We can create and run this model in MLwiN as shown below.

The Equations window should then look like the one below. 

The results of model should look like this below.

Comments:


Model 2: Predicting MathAch from cses

This model is from page 335 of the Singer article.

This model is referred to as a random-coefficient model by Raudenbush and Bryk. Pretend that we will a run regression of MathAch on centered ses on each school; that is, we are going to run 160 regressions. We can then ask the following questions.

  1. What would be the average of the 160 intercepts, (both intercept and slope)?
  2. How much do the regression equations vary from school to school?
  3. What is the correlation between the intercepts and slopes?

Here is the model expressed using multiple equations.

Level 1: MathAchij β0j + β1j (cses) + rij  
Level 2: β0j =  γ00  + u0j
Level 2: β1j =  γ10  + u1j

Combining the two equations into one by substituting the level 2 equation to level 1 equation, we have the following with the random effects identified by placing them in square brackets.

MathAchij γ00  + γ10(cses) +  [ u1j(cses) + u0j + rij ]   

Instead of using meanses we would like to use centered SES or cses to predict math achievement.  Change meanses to cses and make it random at level 2. The Equation window should look like this.

The results of model should look like this below.

Comments:


Model 3: Predicting MathAch from meanses, sector, cses, meansesBYcses and sectorBYcses

This model is shown in the Singer article on pages 337 and 338.

This model is referred as an intercepts and slopes-as-outcomes model by Raudenbush and Bryk. The questions that we are interested in are:

  1. Do meanses and sector significantly predict the intercept?
  2. Do meanses and sector significantly predict the within-school slopes?
  3. How much variation in the intercepts and the slopes is explained by meanses and sector?

We can express this as a multiple equation model like this.

Level 1: MathAchij β0j + β1j (cses) + rij  
Level 2: β0j =  γ00  + γ01(MeanSES) + γ02(Sector) + u0j
Level 2: β1j =  γ10  + γ11(MeanSES) + γ12(Sector) + u1j

Combining the two equations into one by substituting the level 2 equations into the level 1 equation, we have the following equation.  The random effects are identified by placing them in square brackets.

MathAchij  γ00  + γ01(MeanSES) + γ02(Sector) + γ10 (cses) + γ11(MeanSES*cses) +  γ12(Sector*cses) + [  u0j + u1j(cses) +  rij ]

We can do this in MLwiN like this.

Your model should look like this (but maybe out of order).

The results of model should look like this below.

Let us focus on the interactions with cses. The sectorBYcses interaction is significant.  When sector is 0 (public), the slope for cses is 2.939 and when sector is 1 (Catholic) the slope for cses is 2.939 + -1.644 = 1.295. We can view this interaction by generating the predicted values and graphing them.

Click Apply

The slope of cses for public schools (sector 0) is 2.939 because this is the coefficient for cses.  Catholic schools (sector = 1) have a slope of 2.939 +  -1.644 = 1.295 (coefficient for cses plus the coefficient for csesBYsector, illustrated in the graph above).

The meansesBYcses interaction is tricky to interpret.  The interaction term is 1.042, which means that for every one unit increase in the average SES for a school, the slope between cses and MathAch is expected to increase by 1.042.  In short, as the SES for a school increases, we predict the slope of cses would increase.  We can visually see this as shown below.

Viewing predicted values for meansesBYcses interaction

Click Apply.

The slope of cses when meanses is 0 is 12.114.  As meanses increases, so does cses, because the meansesBYcses interaction term is positive (1.042).  In fact, when meanses increases by 1 point, the slope for cses increases by 1.042.  So if a school has a meanses of 1, then the predicted slope for cses would be 12.114 + 1.042.


Repeating the Examples

In the examples above, we have simplified matters in a number of ways to help focus on the process of creating models in the MLwiN equation editor and getting the results.  However, we have concealed a number of tasks that needed to be performed to be able to run the models; namely, how to input the data into MLwiN and how to create some of the variables used in the models.  We will repeat the steps of running examples 1, 2 and 3 again but this time show how to input the data into MLwiN and how to create the variables.


Inputting Data

We believe that the easiest way to get your data into MLwiN is to convert it from a Stata data file to an MLwiN data file.  There are other methods for getting your data into MLwiN, but we have found them to be much more difficult than this method.  We have the data file as a Stata data file called hsb12a.dta .  Notice this is called hsb12a -- this file is unlike the prior file not only because it is a Stata file, but also because it does not have lots of the variables already computed for us, namely meanses, cses, meansesBYcses and sectorBYcses

We use a Stata command called stata2mlwin to help convert the Stata data file to an MLwiN data file.  You can get stata2mlwin from within Stata like this.

net from http://www.ats.ucla.edu/stat/stata/ado/analysis
net install stata2mlwin

Assuming you have downloaded hsb12a.dta as c:\alda\hsb12a.dta you can convert the file like this.

stata2mlwin using hsb12a

Then, you can complete the conversion by starting MLwiN and doing the following steps.


Model 1 (again): Predicting MathAch from meanses

This model is from page 331 of the Singer article.

This process is much like before, however we need to do a couple of extra steps that were done for us before:
  1) we must sort the data on school and then student (our level 2 and level 1 variables)
  2) We need to create meanses

The Equations window should then look like the one below. 

The results of model should look like this below.


Model 2 (again): Predicting MathAch from cses

This model is from page 335 of the Singer article.

We need to add an extra step that we did not perform before -- we need to create cses (the group mean centered version of ses).  We do this by taking ses minus the average value of ses within the school meanses.

The Equations window should then look like the one below. 

The results of model should look like this below.


Model 3 (again): Predicting MathAch from meanses, sector, cses, meansesBYcses and sectorBYcses

This model is shown in the Singer article on pages 337 and 338.

To run this model we need to create meansesBYcses by multiplying meanses by cses.  We also need to create the interaction of cses by sector (a continuous by categorical interaction).

Your model should look like this (but maybe out of order).

The results of model should look like this below.


For more information on multilevel modeling and MLwiN, please see the following:

How to cite this page

Report an error on this page or leave a comment

The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.