# Estimating HLM Models Using R: Part 3

## 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.

```
hsb <- hsb %>%
mutate(grp_cent_ses = ses - meanses)
```

Start with the level-1 equation:

\[ 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 the results.

```
mod_3 <- lmer(mathach ~ grp_cent_ses + (grp_cent_ses | schid), data = hsb)
summary(mod_3)
```

```
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: mathach ~ grp_cent_ses + (grp_cent_ses | schid)
## Data: hsb
##
## REML criterion at convergence: 46714.2
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -3.09684 -0.73201 0.01854 0.75386 2.89920
##
## Random effects:
## Groups Name Variance Std.Dev. Corr
## schid (Intercept) 8.6838 2.947
## grp_cent_ses 0.6939 0.833 0.02
## Residual 36.7001 6.058
## Number of obs: 7185, groups: schid, 160
##
## Fixed effects:
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) 12.6493 0.2445 156.7014 51.73 <2e-16 ***
## grp_cent_ses 2.1932 0.1283 155.2158 17.10 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr)
## grp_cent_ss 0.013
## convergence code: 0
## Model failed to converge with max|grad| = 0.00751199 (tol = 0.002, component 1)
```