d <- sim_school()
head(d, 4) school hours score
1 S01 4.6 47.4
2 S01 4.0 53.1
3 S01 0.8 37.6
4 S01 6.3 69.6
Learning by doing with R + lme4
lmer() and read the outputTip
Detailed explanations and exercises are in the hands-on book
Study time and test score recorded for students in 20 schools
school hours score
1 S01 4.6 47.4
2 S01 4.0 53.1
3 S01 0.8 37.6
4 S01 6.3 69.6
What we want to know: if study time increases by one hour, how many points does the score go up?
Upward trend, but large scatter
Every school slopes upward, but the level differs by school
Grouped structures are everywhere
Schools ⊃ students, hospitals ⊃ patients, stores ⊃ sales, repeated measures on the same person …
Randomly assign a “tutoring program” that actually has no effect at the school level, and test 300 times
Key points
\[ \text{score}_i = \beta_0 + \beta_1 \cdot \text{hours}_i + \varepsilon_i, \qquad \varepsilon_i \sim N(0, \sigma^2) \]
Overall positive / negative depending on the school → the “school offset” is mixed into the error
| Approach | Differences between schools | Problem |
|---|---|---|
| Complete pooling | Ignored | Independence breaks |
| No pooling | Completely separate for each school | Unstable with little data |
| Partial pooling | Different but similar | ← linear mixed model |
The assumption of partial pooling:
The intercepts differ from school to school, but they come from a single normal distribution
The less data a school has, the more it is pulled toward the overall mean
Estimate a single value common to everyone
Differ by group and vary according to a distribution
“Mixed” = a model that mixes these two kinds of effects
\[ \text{score}_{ij} = \underbrace{\beta_0 + \beta_1 \cdot \text{hours}_{ij}}_{\text{fixed effects}} + \underbrace{u_j}_{\text{school offset}} + \underbrace{\varepsilon_{ij}}_{\text{individual variation}} \]
\[ u_j \sim N(0, \tau^2), \qquad \varepsilon_{ij} \sim N(0, \sigma^2) \]
| Part | Meaning |
|---|---|
score ~ hours |
Fixed effects (same as lm()) |
(1 | school) |
The intercept (1) varies by school |
Left of | = what varies; right = what it varies by
Linear mixed model fit by REML. t-tests use Satterthwaite's method [
lmerModLmerTest]
Formula: score ~ hours + (1 | school)
Data: d
REML criterion at convergence: 2119.3
Scaled residuals:
Min 1Q Median 3Q Max
-2.52549 -0.63282 -0.00379 0.65028 2.52623
Random effects:
Groups Name Variance Std.Dev.
school (Intercept) 62.97 7.935
Residual 40.54 6.367
Number of obs: 315, groups: school, 20
Fixed effects:
Estimate Std. Error df t value Pr(>|t|)
(Intercept) 46.0878 1.9390 24.7389 23.77 <2e-16 ***
hours 3.2296 0.1299 297.5316 24.86 <2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Correlation of Fixed Effects:
(Intr)
hours -0.338
Groups Name Std.Dev.
school (Intercept) 7.9352
Residual 6.3668
(Intercept) hours
46.087796 3.229567
ranef(): school offsets \(\hat u_j\); coef(): the line for each schoolThe proportion of the total variation accounted for by differences between schools
\[ \text{ICC} = \frac{\tau^2}{\tau^2 + \sigma^2} \]
18 people restricted to 3 hours of sleep per night; reaction time (ms) measured for 10 days
Not only the intercept, but also the pace of deterioration (slope) differs by person
\[ \text{Reaction}_{ij} = (\beta_0 + u_{0j}) + (\beta_1 + u_{1j}) \cdot \text{Days}_{ij} + \varepsilon_{ij} \]
Three variation parameters are estimated:
| Syntax | Meaning |
|---|---|
(1 | g) |
Random intercept only |
(x | g) |
Intercept + slope, correlation also estimated |
(x || g) |
Intercept + slope, correlation assumed to be 0 |
(0 + x | g) |
Random slope only |
(Intercept) Days
251.40510 10.46729
Groups Name Std.Dev. Corr
Subject (Intercept) 24.7407
Days 5.9221 0.066
Residual 25.5918
(1 | school/class)(1 | subject) + (1 | item)| Method | When to use |
|---|---|
| REML (default) | Final estimates, comparing random effects |
| ML | Comparing models with different fixed effects |
anova() automatically refits with ML Estimate Std. Error df t value Pr(>|t|)
(Intercept) 251.40510 6.824597 16.99973 36.838090 1.171558e-17
Days 10.46729 1.545790 16.99998 6.771481 3.263824e-06
Data: sleepstudy
Models:
fit0: Reaction ~ 1 + (Days | Subject)
fit_rs: Reaction ~ Days + (Days | Subject)
npar AIC BIC logLik -2*log(L) Chisq Df Pr(>Chisq)
fit0 5 1785.5 1801.4 -887.74 1775.5
fit_rs 6 1763.9 1783.1 -875.97 1751.9 23.537 1 1.226e-06 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Data: sleepstudy
Models:
fit_ri2: Reaction ~ Days + (1 | Subject)
fit_rs: Reaction ~ Days + (Days | Subject)
npar AIC BIC logLik -2*log(L) Chisq Df Pr(>Chisq)
fit_ri2 4 1794.5 1807.2 -893.23 1786.5
fit_rs 6 1755.6 1774.8 -871.81 1743.6 42.837 2 4.99e-10 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Warning
“Variance = 0” is at the edge of the possible values → p-values are conservative (too large)
df AIC
fit_ri2 4 1794.465
fit_nocor 5 1753.669
fit_rs 6 1755.628
boundary (singular) fitA variance was estimated as 0 / a correlation as ±1
(x | g) → (x || g) → (1 | g)Model failed to convergeThe optimization does not converge
lmerControl(optimizer = "bobyqa")allFit()Reaction time increased on average by 10.5 ms per day of sleep restriction (95% CI [7.4, 13.5], t(17.0) = 6.77, p < .001). The SD of individual differences in the slope was 5.9 ms/day.
lmer(y ~ x + (1 | g)): random interceptlmer(y ~ x + (x | g)): random slopeanova() / AIC; diagnose with residual plotsOrthodont data)glmer(..., family = binomial)brms package