Mixed & Multilevel Models (GLMMs)
Data is often grouped: students within schools, patients within hospitals, repeated measurements within people. Those observations aren't independent, and pretending they are breaks your standard errors. Mixed (multilevel) models handle grouping head-on, and they come with a genuinely magical trick: groups borrow strength from each other.
Fixed effects vs. random effects
A mixed model has two kinds of terms. Fixed effects are the usual coefficients: effects assumed constant across the whole population. Random effects let each group have its own intercept (and sometimes its own slope), drawn from a distribution the model estimates. Instead of estimating every group in isolation, the model learns "how much do groups vary?" and uses that to inform each one.
How much of the variation is the grouping?
Two spreads come out of a fitted mixed model: τ, the standard deviation between group means, and σ, the spread of individuals inside a group. Their ratio has a name you will meet everywhere clustered data is discussed. The intraclass correlation is ICC = τ² / (τ² + σ²), the share of the total variation that lives between groups rather than within them, and equivalently the correlation you expect between two observations drawn from the same group.
In the playground below, σ is fixed at 11 and the τ slider is therefore an ICC dial. At its default τ = 8 the ICC is .35, so a third of the variation is between groups. Slide τ down to 1 and it collapses to .008, which is the regime where groups barely differ and shrinkage pulls everything to the grand mean. Push τ to 30 and it reaches .88, where each group is nearly its own population. The same number governs how badly clustering hurts a survey, which is why sampling methods uses it to compute the price of cluster sampling.
The three ways to handle groups
- Complete pooling: ignore the groups entirely; one estimate for everyone. Simple, but throws away real group differences.
- No pooling: estimate each group on its own. Honest, but tiny or noisy groups get wild, unreliable estimates.
- Partial pooling is the mixed-model compromise: each group's estimate is pulled toward the overall mean, more so when the group has little data or lots of noise. This is shrinkage.
🎮 Shrinkage in Action
Each row is a group. The open circle is its own raw mean (no pooling); the filled purple circle is the partial-pooled estimate. Drag the group-variation slider down and watch the estimates pulled toward the overall mean (dashed line); small groups move most.
Why shrinkage is a good thing
It feels like cheating to move a group's estimate away from its own data — but it improves accuracy on average. A group of 3 with a freakishly high mean is probably just lucky; pulling it toward the overall mean is usually closer to the truth. This is the same logic behind why a rookie's hot first week doesn't predict their season. Partial pooling formalizes "regression to the mean."
Notice in the playground: when τ is large (groups really do differ a lot), there's little shrinkage: the data is trusted. When τ is small (groups are mostly alike), estimates collapse toward the overall mean. And at any setting, the small groups shrink more than the big ones, because they carry less independent information.
The "G" in GLMM
Combine this grouping machinery with a link function and you get a generalized linear mixed model (GLMM): random effects on top of logistic or Poisson regression. That covers an enormous swath of real research: yes/no outcomes measured repeatedly within people, event counts nested within regions, and far more.
Why it matters: mixed models are now the default for grouped and repeated-measures data across psychology, ecology, medicine, and education. They respect the data's structure and make better predictions for small groups. They're a fitting place to end the course: the full power of the linear model, finally set loose on the messy, nested data of the real world.
Problem 27 of the practice problems starts one step earlier, with a researcher deciding whether to run 90 people through one design each or 30 people through all three, which is the design choice that determines whether you need any of this machinery.
Common questions
What is the difference between fixed and random effects?
Fixed effects are coefficients estimated for effects you care about specifically and would keep in a replication (treatment, age, condition). Random effects model sampled clusters (these particular schools, participants, litters) as draws from a population, estimating how much clusters vary rather than each one in isolation. Litmus test: would new data bring the same levels (fixed) or new ones (random)?
Why doesn't my mixed model print p-values?
Because for a mixed model nobody agrees what the denominator degrees of freedom should be. In a balanced classical ANOVA the error df are countable; once you have unequal group sizes, crossed random effects and partial pooling, the effective df fall between two defensible numbers and the F ratio is only approximately F-distributed. R's lme4 therefore declines to guess, which surprises everyone the first time. The accepted ways forward: report the fixed effects with their confidence intervals and let those carry the inference, use Satterthwaite or Kenward-Roger approximate df (the lmerTest package adds them, and SPSS and JASP apply Satterthwaite by default, which is why they show p-values and lme4 does not), or compare nested models with a likelihood-ratio test. Whichever you choose, say which one in the write-up, because the three do not always agree at the margin.
How many groups do I need to fit random effects?
Rules of thumb converge on: fewer than ~5 groups, don't — the model can't estimate between-group variance from 3 numbers (use fixed dummy codes instead); 10–20 groups works but estimates variance components roughly; 30+ is comfortable, and 50+ is preferred for random slopes or when the variance components are themselves the research question.