## 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.09680 -0.73194 0.01858 0.75388 2.89928
##
## Random effects:
## Groups Name Variance Std.Dev. Corr
## schid (Intercept) 8.682 2.9465
## grp_cent_ses 0.694 0.8331 0.02
## Residual 36.700 6.0581
## Number of obs: 7185, groups: schid, 160
##
## Fixed effects:
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) 12.6493 0.2445 156.7391 51.73 <2e-16 ***
## grp_cent_ses 2.1932 0.1283 155.2180 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.012
```

Still have questions? Contact us!