This page shows an example of a two level multilevel model. First a multilevel model is shown using HLM and then using Stata, and then the same data are analyzed using Mplus using a multilevel model. The Mplus output is related to the multilevel model results. We suggest that you view this page using two web browsers so you can show the page side by side showing the Stata output in one browser and the corresponding Mplus output in the other browser.
This example is drawn from the Mplus User’s Guide (example 6.1) and we suggest that you see the Mplus User’s Guide for more details about this example. We thank the kind people at Muthén & Muthén for permission to use examples from their manual.
Example Using HLM
Each subject is observed on the variable Y at four different times. Conceptualized as a multilevel model, the variable time is a level 1 variable. Time is coded 0, 1, 2, and 3. The intercept is the predicted value when time is 0. Each subject has their own intercept and slope, expressed as random effects at level 2. We can write this model using multiple equations as shown below. This uses the ex61.mdm file.
Level 1: Y_{ij }= β_{0j }+ β_{1j}Time + r_{ij}
Level 2: β_{0j }= γ_{00 }+ u_{0j }β_{1j }= γ_{10 }+ u_{1j}
Here is the output from HLM, condensed to save space. Footnotes are included for relating the output to Mplus.
Summary of the model specified (in equation format)  Level1 Model Y = B0 + B1*(TIME) + R Level2 Model B0 = G00 + U0 B1 = G10 + U1 Iterations stopped due to small change in likelihood function ******* ITERATION 2 ******* Sigma_squared = 0.48774^{F} Tau INTRCPT1,B0 0.98667^{C} 0.13242 TIME,B1 0.13242^{E} 0.22750^{D } Tau (as correlations) INTRCPT1,B0 1.000 0.279 TIME,B1 0.279 1.000 Final estimation of fixed effects:  Standard Approx. Fixed Effect Coefficient Error Tratio d.f. Pvalue  For INTRCPT1, B0 INTRCPT2, G00 0.522793^{A} 0.051538 10.144 499 0.000 For TIME slope, B1 INTRCPT2, G10 1.026268^{B} 0.025497 40.250 499 0.000 
Example Using Stata
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.
MathAch_{ij }= γ_{00 }+ γ_{10}(MeanSES) + [ u_{0j }+ r_{ij} ]

The term u_{0j }is a random effect at level 2, representing random variation in the average math achievement among (between) schools.

The term r_{ij }is a random effect at level 1, representing random variation in the math achievement of students within schools.
Here is an example using Stata.
infile y0 y1 y2 y3 using https://stats.idre.ucla.edu/stat/mplus/output/ex6.1.dat, clear generate id = _n reshape long y, i(id) j(time) xtmixed y time  id: time, cov(un) var mle
Mixedeffects ML regression Number of obs = 2000 Group variable: id Number of groups = 500 Obs per group: min = 4 avg = 4.0 max = 4 Wald chi2(1) = 1623.34 Log likelihood = 3016.6973 Prob > chi2 = 0.0000  y  Coef. Std. Err. z P>z [95% Conf. Interval] + time  1.026268^{B} .0254715 40.29 0.000 .9763442 1.076191 _cons  .522793^{A} .0514865 10.15 0.000 .4218814 .6237047   Randomeffects Parameters  Estimate Std. Err. [95% Conf. Interval] + id: Unstructured  var(time)  .2268527^{D} .0209755 .1892514 .2719247 var(_cons)  .9840141^{C} .0852067 .8304146 1.166025 cov(time,_cons)  .1324435^{E} .0300523 .0735421 .191345 + var(Residual)  .4877361^{F} .0218122 .446805 .5324169  LR test vs. linear regression: chi2(3) = 1601.32 Prob > chi2 = 0.0000 Note: LR test is conservative and provided only for reference
Mplus Example
Here is the same example analyzed as a multilevel model using Mplus based on the ex61l.dat data file.
Title: Multilevel model Data: File is ex61l.dat ; Variable: Names are id time y; WITHIN = time ; CLUSTER = id; ANALYSIS: TYPE = TWOLEVEL RANDOM; MODEL: %WITHIN% s  y ON time; %BETWEEN% y s ; y with s; SUMMARY OF ANALYSIS Number of observations 2000 SUMMARY OF DATA Number of clusters 500 Average cluster size 4.000 Estimated Intraclass Correlations for the Y Variables Intraclass Intraclass Variable Correlation Variable Correlation Y 0.452 TESTS OF MODEL FIT Loglikelihood H0 Value 3016.699 Information Criteria Number of Free Parameters 6 Akaike (AIC) 6045.397 Bayesian (BIC) 6079.003 SampleSize Adjusted BIC 6059.940 (n* = (n + 2) / 24) MODEL RESULTS Estimates S.E. Est./S.E. Within Level Residual Variances Y 0.487^{F} 0.024 20.455 Between Level Y WITH S 0.131^{E} 0.032 4.078 Means Y 0.523^{A} 0.051 10.154 S 1.026^{B} 0.025 40.291 Variances Y 0.986^{C} 0.081 12.130 S 0.227^{D} 0.020 11.262
 This is γ_{00} in the multilevel model. It is the predicted value of y when time is 0.
 This is γ_{10} in the multilevel model. It is the predicted value increase in y for a one unit increase in time.
 This is the variance of the intercept, the variance of u_{0j} in the multilevel model.
 This is the variance of the slope, the variance of u_{1j} in the multilevel model.
 This is the covariance of the intercept and slope, the covariance β_{0j }and β_{1j} from the multilevel model.
 This is the residual variance at level 1.