viewof model = html`<select style="font-size:15px;padding:4px 8px">
<option value="pooled">Single line — everything fixed (one line for all)</option>
<option value="rint" selected>Random intercept — parallel lines, different baselines</option>
<option value="rslope">Random slope — same start, different rates</option>
<option value="both">Random intercept + slope — fully separate lines</option>
</select>`Random Intercepts & Slopes
Info sheet · Statistics for Psychology & Neuroscience
Warning✎ Editing notes — to do / to check
Working notes for the author — not shown to students once collapsed; remove before publishing.
- [ ]
What you’ll get from this sheet
The two ways a random effect can act. By the end you should be able to:
- Explain what random intercepts and random slopes are.
- Read the equation for a model with intercepts, slopes, or both.
- Recognise when random effects add realism.
A random intercept lets groups start at different baselines; a random slope lets groups change at different rates. You can have either, or both — the most realistic (and most complex) option.
The problem with one line
A general linear model predicts an outcome as a straight line: \(y_i = \beta_0 + \beta_1 x_i + \varepsilon_i\), with \(\beta_0\) the intercept (baseline) and \(\beta_1\) the slope (rate of change). Take the London housing model — predict price from area, bedrooms, and house type — where everything was a fixed effect. That forces one slope for everybody: the effect of area on price was pinned at a single value across all house types. But in reality flats usually cost far more per square foot than houses. One line can’t say that. Random effects can — by letting a group’s intercept and/or slope vary.
Three ways to let lines vary
Same houses, coloured by type (flat, house, new development). Switch the random-effects structure and watch the fitted lines change.
- Single line (all fixed). One intercept, one slope — the flats are systematically above the line and the houses below it, because a single line can’t reflect that they differ. This is ordinary regression.
- Random intercept. \(y_{ij} = (\beta_0 + u_{0j}) + \beta_1 x_{ij} + \varepsilon_{ij}\) — the intercept varies by group \(j\), slopes stay equal. Three parallel lines: same rate, different baselines.
- Random slope. \(y_{ij} = \beta_0 + (\beta_1 + u_{1j}) x_{ij} + \varepsilon_{ij}\) — one baseline, slopes differ. Lines fan out from a shared start.
- Random intercept + slope. \(y_{ij} = (\beta_0 + u_{0j}) + (\beta_1 + u_{1j}) x_{ij} + \varepsilon_{ij}\) — both vary. Separate lines per group — the fullest, most realistic model.
The \(u_{0j}\) and \(u_{1j}\) terms are the random deviations for group \(j\): how far its intercept and slope stray from the overall \(\beta_0\) and \(\beta_1\). The model doesn’t estimate each one freely — it assumes they’re drawn from a normal distribution and estimates its variance, which is what lets the effect generalise to groups you didn’t observe.
See it in code
In lme4, the bit in parentheses is the random effect: (1 | group) is a random intercept, (0 + x | group) a random slope, (1 + x | group) both.
library(lme4)
lmer(price ~ area + (1 | type), data = london) # random intercept
lmer(price ~ area + (0 + area | type), data = london) # random slope
lmer(price ~ area + (1 + area | type), data = london) # both (intercept + slope)import statsmodels.formula.api as smf
smf.mixedlm("price ~ area", london, groups=london["type"]).fit() # random intercept
smf.mixedlm("price ~ area", london, groups=london["type"],
re_formula="~area").fit() # intercept + slopeStatic reference — lme4/statsmodels aren’t in the in-page runtime.
TipCheck your understanding
Flats cost more per square foot than houses, and they start at a higher base price. Which random effect(s) do you need?
Both. The “more per square foot” part is a random slope — the effect of area varies by house type. The “higher base price” part is a random intercept — the baseline varies by type. So you’d fit price ~ area + (1 + area | type), letting each type have its own intercept and its own slope.
A “maximal” random structure (intercepts + slopes + their correlation for every grouping) is often not estimable from the data you have — the model fails to converge or reports a singular fit. Random slopes are especially data-hungry: you need a decent number of observations per group. When a model won’t converge, simplify — drop the slope, or the intercept–slope correlation — rather than forcing it.
Where this shows up next
See Chapter (Mixed-Effects Models) for the full derivation and the London housing worked example. Next up: actually building and evaluating one of these models in R.