# Estimating HLM Models Using Stata: Part 3

Jeremy Albright

Posted on
ANOVA HLM Stata

## Random Coefficient Model

Next, R&B present a model in which student-level SES is included instead of average SES, and they treat the slope of student SES as random. One complication is that R&B present results after group-mean centering student SES. Group-mean centering means that the average SES for each student’s school is subtracted from each student’s individual SES. The following creates a new, group mean centered SES variable.

gen grp_cent_ses = ses - meanses

$Y_{ij} = \beta_{0j} + +\beta_{1j}(\text{SES}_{ij}) + e_{ij}$

where $$\text{SES}_{ij}$$ is the student-level SES variable that has been centered around the school mean. The intercept $$\beta_{0j}$$ can be modeled as a grand mean $$\gamma_{00}$$ plus random error, $$u_{0j}$$. Similarly, the slope $$\beta_{1j}$$ can be modelled as having a grand mean $$\gamma_{10}$$ plus random error $$u_{1j}$$.

\begin{aligned} \beta_{0j} &= \gamma_{00} + u_{0j} \\ \beta_{1j} &= \gamma_{10} + u_{1j} \end{aligned}

Combining equations produces:

$Y_{ij} = \gamma_{00} + \gamma_{10}(\text{SES}_{ij}) + u_{0j} + u_{1j}(\text{SES}_{ij}) + e_{ij}$

There are now two random effects, $$u_{0j}$$ and $$u_{1j}$$. Treating the random effects covariances as unstructured, which is the default in lme4, produces a variance component for each effect as well as their covariance. The following code returns both of the results.

mixed mathach grp_cent_ses  || schid: grp_cent_ses , reml cov(un)
Performing EM optimization:

Iteration 0:   log restricted-likelihood =  -23357.18
Iteration 1:   log restricted-likelihood = -23357.118
Iteration 2:   log restricted-likelihood = -23357.118

Computing standard errors:

Mixed-effects REML regression                   Number of obs     =      7,185
Group variable: schid                           Number of groups  =        160

Obs per group:
min =         14
avg =       44.9
max =         67

Wald chi2(1)      =     292.40
Log restricted-likelihood = -23357.118          Prob > chi2       =     0.0000

------------------------------------------------------------------------------
mathach |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
grp_cent_ses |   2.193192   .1282585    17.10   0.000      1.94181    2.444574
_cons |   12.64934    .244514    51.73   0.000      12.1701    13.12858
------------------------------------------------------------------------------

------------------------------------------------------------------------------
Random-effects Parameters  |   Estimate   Std. Err.     [95% Conf. Interval]
-----------------------------+------------------------------------------------
schid: Unstructured          |
var(grp_ce~s) |   .6939836   .2807834       .314019    1.533707
var(_cons) |   8.681695   1.079634      6.803794    11.07791
cov(grp_ce~s,_cons) |   .0507475    .406395      -.745772    .8472671
-----------------------------+------------------------------------------------
var(Residual) |    36.7002   .6257439      35.49403    37.94735
------------------------------------------------------------------------------
LR test vs. linear model: chi2(3) = 1065.70               Prob > chi2 = 0.0000

Note: LR test is conservative and provided only for reference.

Note that a second option, cov(un) has been added. By default, Stata will assume that the two random effects are not correlated. The cov(un) option tells Stata to treat the random effects covariance matrix as unstructured, meaning that the covariance may also be estimated.

It is common to report the association between random effects as correlations rather than covariances. The postestimation command estat sd will report the random effects variances in standard deviation units and the covariance in correlation units.

estat sd
------------------------------------------------------------------------------
Random-effects Parameters  |   Estimate   Std. Err.     [95% Conf. Interval]
-----------------------------+------------------------------------------------
schid: Unstructured          |
sd(grp_ce~s) |   .8330568    .168526       .560374    1.238429
sd(_cons) |   2.946472    .183208      2.608408    3.328349
corr(grp_ce~s,_cons) |   .0206746   .1655372     -.2948849    .3321681
-----------------------------+------------------------------------------------
sd(Residual) |   6.058069   .0516455      5.957686    6.160142
------------------------------------------------------------------------------