5 Random-slope models
Code cells can be edited and re-run with “Run Code” (R is running inside your browser). The first run takes several tens of seconds to load the packages. Feel free to change numbers and see how the results change.
In the previous chapter we assumed that “only the intercept differs by school (the lines are parallel)”. But the slope may also differ by group.
5.1 Today’s data: a sleep deprivation experiment (sleepstudy)
This is a real dataset that comes with the lme4 package. 18 participants spent 10 days with their sleep restricted to 3 hours per night, and their reaction time (Reaction, milliseconds) was measured every day.
Reaction: reaction time (ms). Larger means slower reactionsDays: number of days of sleep restriction (0–9)Subject: participant ID
Because the same person is measured 10 times, the participant is the group (repeated-measures data).
We can see that, for each participant,
- the initial reaction time (intercept) differs
- the pace at which they get slower over the days (the slope) also differs. Some people barely change, while others deteriorate rapidly
5.2 The model equation
For the measurement of participant \(j\) on day \(i\),
\[ \text{Reaction}_{ij} = (\beta_0 + u_{0j}) + (\beta_1 + u_{1j}) \cdot \text{Days}_{ij} + \varepsilon_{ij} \]
- \(u_{0j}\): the offset in participant \(j\)’s intercept
- \(u_{1j}\): the offset in participant \(j\)’s slope
We assume that \(u_{0j}\) and \(u_{1j}\) jointly follow a two-dimensional normal distribution:
\[ \begin{pmatrix} u_{0j} \\ u_{1j} \end{pmatrix} \sim N\left( \begin{pmatrix} 0 \\ 0 \end{pmatrix}, \begin{pmatrix} \tau_0^2 & \rho\tau_0\tau_1 \\ \rho\tau_0\tau_1 & \tau_1^2 \end{pmatrix} \right) \]
It looks complicated, but only the following three quantities are estimated.
| Symbol | Meaning |
|---|---|
| \(\tau_0\) | Size of individual differences in the intercept (SD) |
| \(\tau_1\) | Size of individual differences in the slope (SD) |
| \(\rho\) (rho) | Correlation between intercept and slope. E.g., positive if “people who were slower to begin with also deteriorate faster” |
5.3 How to write it in R
lmer(Reaction ~ Days + (Days | Subject), data = sleepstudy)(Days | Subject) is shorthand for (1 + Days | Subject) and means “the intercept and the slope of Days vary by participant”.
| Syntax | Meaning |
|---|---|
(1 | g) |
Random intercept only |
(x | g) = (1 + x | g) |
Random intercept and slope, and their correlation is also estimated |
(x || g) |
Random intercept and slope, correlation assumed to be 0 (fewer parameters) |
(0 + x | g) |
Random slope only |
5.4 Fitting
5.4.1 How to read it
Random effects
- Std.Dev. of
Subject (Intercept)→ \(\hat\tau_0\): individual differences in reaction time on day 0 - Std.Dev. of
Subject Days→ \(\hat\tau_1\): individual differences in the pace of deterioration per day Corr→ \(\hat\rho\): correlation between intercept and slopeResidual→ \(\hat\sigma\)
Fixed effects
(Intercept): reaction time on day 0 for an average person (ms)Days: deterioration per day for an average person (ms/day)
On average, reactions get slower by about 10.5 ms per day. However, the SD of individual differences in the slope is about 5.9 ms/day, so by the properties of the normal distribution, roughly 95% of people’s slopes are expected to lie in the range mean ± 2 SD ≈ -1.4 to 22.3 ms/day. In other words, there are people ranging from “hardly affected at all” to “strongly affected”.
5.5 Check with a plot: random intercept vs random slope
The random-intercept model (common slope) does not fit well for people who deteriorate quickly (e.g., 308) or slowly (e.g., 335). The random-slope model gives each person a slope that fits them.
5.6 Shrinkage happens here too
Let’s compare with the slopes from fitting lm separately for each participant (no pooling).
When there are multiple grouping factors, you can use the same notation.
- Nested: students ⊂ classes ⊂ schools →
(1 | school/class)(same as(1 | school) + (1 | school:class)) - Crossed: participants × stimulus words (everyone responds to every word) →
(1 | subject) + (1 | item)
- Random-slope model: the slope also varies by group
- In R:
(x | group). Estimates the SDs of the intercept and slope, and their correlation - The slope SD tells you how large “individual differences in the effect” are