UCLA Academic Technology Services HomeServicesClassesContactJobs

Stata FAQ
How can I convert a multilevel model to a mixed model?

Many multilevel models can be estimated using mixed model procedures however the syntax will be rather different. The ml2mixed command (findit ml2mixed) can be used to help you convert from the multivel model specification to the xtmixed syntax. The ml2mixed command will work for multilevel models with either two or three levels. For two level models ml2mixed allows for both random intercepts and random slopes at level 2 while the three level model only allows for random intercepts.

We will begin with a simple example of a 2-level unconditional model with random intercepts. The dependent variable is placed in the dep() option and the level-2 cluster variable is put into the l2id().

ml2mixed, dep(math) l2id(class)

Multilevel Model
Level 1 Model
  math = 
Level 2 Model -- id = class
  [int] = 

Stata Mixed Model -- Stata 11 notation
  xtmixed math     /// 
          || class:  
Now we can add two level-1 predictors hmwk and gender using the l1() option.
ml2mixed, dep(math) l1(hmwk gender) l2id(class)

Multilevel Model
Level 1 Model
  math = hmwk gender
Level 2 Model -- id = class
  [int] = 

Stata Mixed Model -- Stata 11 notation
  xtmixed math hmwk gender    /// 
          || class: 
For the next example we will keep the two level-1 predictors and add a level-2 predictor of the random intercept. The level-2 intercpt predictor is placed in the l2i() option. In addition, we will include the notes option which displays some hopefully helpful comments.
ml2mixed, dep(math) l1(hmwk gender) l2id(class) l2i(meanses) notes

Multilevel Model
Level 1 Model
  math = hmwk gender
Level 2 Model -- id = class
  [int] = meanses

Stata Mixed Model -- Stata 11 notation
  xtmixed math hmwk gender meanses   /// 
          || class:  

Stata Notes
1) Categorical predictors need the i. prefix.
2) Continuous variables in interactions need the c. prefix.
3) Use var option to get variances instead of standard deviations.
4) If outcome variable is binary use -xtmelogit- command.
5) If outcome variable is a count use -xtmepoisson- command.
Next is a model with a random slope for hmwk in additon to having a random intercept. The random slope is indicated by putting the level-1 variable inside square brackets within the l2s() option. This example also includes the sas and spss options with provide the syntax for the SAS proc mixed and for the SPSS mixed procedures respectively.
ml2mixed, dep(math) l1(hmwk) l2id(class) l2i(meanses) l2s([hmwk]) sas spss

Multilevel Model
Level 1 Model
  math = hmwk
Level 2 Model -- id = class
  [int] = meanses
  slope[hmwk] = 

Stata Mixed Model -- Stata 11 notation
  xtmixed math hmwk meanses   /// 
          || class: hmwk , cov(unstr)

SAS Proc Mixed
proc mixed;
  class class  [...];
  model math = hmwk meanses   / solution;
  random intercept hmwk / subject=class type=un;
run;

SPSS Mixed
mixed math [by...] with hmwk meanses
  /print = solution
  /fixed = hmwk meanses  
  /random = intercept hmwk | subject(class) covtype(un).
In the next model we include a predictor for the random slopes. We do this by including the variable names after the square brackets in the l2s() option. Please note that the ses#meanses term in the model is Stata 11 syntax. If you are running an earlier version of Stata you will need to create interations using the xi or create them manually.
ml2mixed, dep(math) l1(hmwk ses) l2id(class) l2i(meanses) l2s([ses] meanses [hmwk] meanses) notes

Multilevel Model
Level 1 Model
  math = hmwk ses
Level 2 Model -- id = class
  [int] = meanses
  slope[ses] = meanses 
  slope[hmwk] = meanses 

Stata Mixed Model -- Stata 11 notation
  xtmixed math hmwk ses meanses  ses#meanses hmwk#meanses  /// 
          || class: ses hmwk , cov(unstr)

Stata Notes
1) Categorical predictors need the i. prefix.
2) Continuous variables in interactions need the c. prefix.
3) Use var option to get variances instead of standard deviations.
4) If outcome variable is binary use -xtmelogit- command.
5) If outcome variable is a count use -xtmepoisson- command.
The final model is a 3-level model with a level-3 variable, poverty, predicting the random intercept for hmwk. The level-3 identified is indicated with the l3id() option and the level-3 predictor variable with the l3i() option.
ml2mixed, dep(math) l1(hmwk) l2id(class) l2i(meanmath) l2s([hmwk] meanmath) l3id(school) l3i(poverty)

Multilevel Model
Level 1 Model
  math = hmwk
Level 2 Model -- id = class
  [int] = meanmath
  slope[hmwk] = meanmath 
Level 3 Model -- id = school
  [int] = poverty

Stata Mixed Model -- Stata 11 notation
  xtmixed math hmwk meanmath poverty hmwk#meanmath  /// 
          || school: || class: hmwk , cov(unstr)

How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


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.