# Estimating HLM Models Using R: Part 4

Jeremy Albright

Posted on
ANOVA HLM R

## Intercepts- and Slopes-as-Outcomes

R&B present a final model that includes one further generalization of the random coefficients model. Start again with the level-1 model.

$Y_{ij} = \beta_{0j} + +\beta_{1j}(\text{SES}_{ij}) + e_{ij}$ Next, model the intercept, $$\beta_{0j}$$, as a function of school-level characteristics mean SES and school type (public or private).

\begin{aligned} \beta_{0j} &= \gamma_{00} + \gamma_{01}(\text{Mean SES}_j) + \gamma_{02}(\text{School Type}_j) + u_{0j} \\ \beta_{1j} &= \gamma_{10} + \gamma_{11}(\text{Mean SES}_j) + \gamma_{12}(\text{School Type}_j) + u_{1j} \end{aligned}

Substituting:

\begin{aligned} Y_{ij} = &\gamma_{00} +\gamma_{01}(\text{Mean SES}_j) + \gamma_{02}(\text{School Type}_j) + \gamma_{10}(\text{SES}_{ij}) + \\ &\gamma_{11}(\text{Mean SES}_j)(\text{SES}_{ij}) + \gamma_{12}(\text{School Type}_j)(\text{SES}_{ij}) + \\ &u_{0j} + u_{1j}(\text{SES}_{ij}) + e_{ij} \end{aligned} where $$\text{SES}_{ij}$$ is again group-mean centered. $$(\text{Mean SES}_j)(\text{SES}_{ij})$$ and $$(\text{School Type}_j)(\text{SES}_{ij})$$ represent cross-level interactions estimating how much the effect of student-level SES on math achievement varies by school-level SES and school type. Remaining between-school variability in the outcome is captured with the $$u_{0j}$$ random effect, and remaining between-school variability in the effect of student-level SES is captured with the random effect $$u_{1j}$$.

The following code fits the model.

mod_4 <- lmer(mathach ~ meanses*grp_cent_ses + schtype*grp_cent_ses + (1 + grp_cent_ses | schid), data = hsb)
summary(mod_4)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: mathach ~ meanses * grp_cent_ses + schtype * grp_cent_ses + (1 +
##     grp_cent_ses | schid)
##    Data: hsb
##
## REML criterion at convergence: 46503.7
##
## Scaled residuals:
##      Min       1Q   Median       3Q      Max
## -3.15921 -0.72319  0.01706  0.75439  2.95822
##
## Random effects:
##  Groups   Name         Variance Std.Dev. Corr
##  schid    (Intercept)   2.3819  1.5433
##           grp_cent_ses  0.1014  0.3184   0.39
##  Residual              36.7211  6.0598
## Number of obs: 7185, groups:  schid, 160
##
## Fixed effects:
##                      Estimate Std. Error       df t value Pr(>|t|)
## (Intercept)           12.1136     0.1988 159.8921  60.931  < 2e-16 ***
## meanses                5.3391     0.3693 150.9689  14.457  < 2e-16 ***
## grp_cent_ses           2.9388     0.1551 139.3042  18.948  < 2e-16 ***
## schtype                1.2167     0.3064 149.5994   3.971 0.000111 ***
## meanses:grp_cent_ses   1.0389     0.2989 160.5528   3.476 0.000656 ***
## grp_cent_ses:schtype  -1.6426     0.2398 143.3449  -6.850 2.01e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
##             (Intr) meanss grp_c_ schtyp mns:__
## meanses      0.245
## grp_cent_ss  0.080  0.020
## schtype     -0.697 -0.356 -0.056
## mnss:grp_c_  0.019  0.079  0.282 -0.028
## grp_cnt_ss: -0.056 -0.029 -0.694  0.082 -0.351

Note that the $$*$$ operator in R automatically creates both the main effects and multiplicative terms for the interactions.