Multilevel & Mixed Models

Dr. R. Düsing · University of Osnabrück
N = 50 students J = 5 classes (n_j = 10) X = study time (h/week) Y = exam performance
Seed: —
Why Multilevel? — Complete, No & Partial Pooling
Does one shared line across all 50 students suffice — Complete Pooling? Or must each class be treated strictly separately — No Pooling? Or a middle ground — Partial Pooling?
Data by class & regression lines
Scenarios
Pooling mode (toggle)
Two extremes without shrinkage:
Partial Pooling (LMM) — the same shrinkage, two variants depending on what gets shrunk:
Variance decomposition
Between 30%
Within 70%
ICC = τ₀ = · σ =
γ₀₀
γ₁₀ (Fixed Slope)
τ₀² (Between)
σ² (Within)
LMM — Random Intercepts
Yij = γ₀₀ + γ₁₀·Xij + u0j + εij
u0j ~ N(0,τ₀²)  ·  εij ~ N(0,σ²)
What you see

J=5 school classes (colors), n_j=10 students, X = study time, Y = exam points. The regression lines are initially hidden — activate the pooling modes individually (the "Controls" tab) to show them: first one alone, then compared.

▸ Learn more

Complete Pooling (red, dashed) → activate
A single global OLS line through all N=50 data points. Group structure is completely ignored. Advantage: simple. Problem: standard errors are underestimated, because observations within a class aren't independent.

No Pooling (group colors, dotted) → activate
Each class gets its own regression line. In practice: a model with a group × predictor interaction — for J groups this yields 2J parameters (J intercepts + J slopes). This quickly becomes unwieldy for large J; no information sharing between groups. At n_j=4, estimates become unstable.

Partial Pooling happens twice — remember the principle, not two methods
Both of the following modes are Partial Pooling: an estimate is shrunk toward its grand mean, weighted by how much of its own evidence the group has (see the formula below). The only difference is which estimate gets shrunk — just the intercept, or intercept and slope together. No Pooling provides the unshrunk starting point in both cases.

… intercept only (group colors, solid) → activate
The group intercepts are shrunk toward the grand mean, but all groups share the same slope γ₁₀ (Random Intercepts) — the lines are therefore parallel. The blue dashed line shows the fixed effect (γ₀₀ + γ₁₀·X). Correct standard errors, information sharing between groups.

… intercept + slope (group colors, dashed) → activate
In addition to the intercept, the slope per group is now also shrunk (Random Slopes). Activate both modes at once: the same (shrunk) starting points, but slopes are now free to differ individually. This module starts with the same population parameters as Tab — the same data points, the same true slope variance τ₁. Switch to Tab to freely change τ₁ and the intercept-slope correlation ρ (that only affects Tab ③'s own display, not this tab retroactively).

What to observe
· High ICC: CP and NP lines lie far apart. PP/RS lie in between.
· Small groups: NP lines fluctuate strongly, PP/RS lines stay stable.
· Simpson: the CP line shows a negative trend, NP/PP show a positive within effect. Module ④ explains why.

Empirical Bayes shrinkage — the math behind it
The partial-pooling intercept of group j is a weighted average of the no-pooling estimate and the grand mean: α̂_jPP = λ_j · α̂_jNP + (1–λ_j) · γ̂₀₀, where the shrinkage factor is λ_j = τ₀² / (τ₀² + σ²/n_j). λ → 1 (no shrinkage): large groups (n_j↑) or large between-variance (τ₀²↑) — the group has enough of its own evidence. λ → 0 (strong shrinkage): small groups or dominant within-variance — the prior (grand mean) dominates.

How strongly are group intercepts pulled toward the grand mean? Slide τ₀ and σ and watch the effect live.
Caterpillar plot — group intercepts (95% CI)
Shrinkage: No Pooling → Partial Pooling
Between-SD τ₀ 8.0
Within-SD σ 8.0
Group size n_j 10
ICC = τ₀² / (τ₀² + σ²)
Between 50%
Within 50%
ICC = λ̄ =
ICC
λ (shrinkage factor)
Max. shrinkage
Avg. |NP – PP|
Shrinkage formula
λ_j = τ₀² / (τ₀² + σ²/n_j)
α̂_jPP = λ_j·α̂_jNP + (1–λ_j)·γ̂₀₀
What you see

Caterpillar plot (top): each class a point (◆ = Partial Pooling, ○ = No Pooling) with a 95% CI, dashed line = γ̂₀₀. Shrinkage diagram (bottom): arrows show the shift NP → PP. Slide τ₀, σ and n_j (the "Controls" tab) to watch the effect live.

▸ Learn more

Caterpillar plot (top) in detail
Each class has a point (◆ = Partial Pooling, ○ = No Pooling) and a 95% CI. The dashed line is γ̂₀₀ (grand mean). Observe: PP points always lie closer to the dashed line than NP points — that's shrinkage.

Shrinkage diagram (bottom)
Arrows show the shift NP → PP. The factor λ is shown at the arrow: λ = 1.0 means no shrinkage (the class has enough of its own evidence), λ = 0.2 means strong shrinkage (grand mean dominates). Note: λ is identical for all classes of equal size — yet the arrows look different lengths. That's because λ only sets the proportion of shrinkage (aPP = λ·aNP + (1−λ)·γ̂₀₀); the absolute arrow length additionally depends on how far the raw NP value was from γ̂₀₀ in the first place. A class with an extreme NP value (like K3) is pulled by the same proportion, but by considerably more in absolute points, than a class near the mean.

Slider τ₀ (between-SD)
Increase τ₀: classes differ more strongly → ICC rises → λ rises → less shrinkage. Estimates "trust" the group structure.

Slider σ (within-SD)
Increase σ: more noise within classes → ICC falls → λ falls → more shrinkage. The class-specific information is less reliable.

Slider n_j
Large groups (n_j = 30): λ ≈ 1, no shrinkage needed. Small groups (n_j = 3): λ small, strong shrinkage. A core advantage of LMM: it automatically adapts to the amount of data available per group.

ICC rules of thumb
ICC < 0.05: group structure negligible. · ICC 0.05–0.20: moderate cluster effects, LMM recommended. · ICC > 0.20: strong effects, LMM essential. Education context: typically ICC ≈ 0.10–0.20.

Random SlopesEffect Heterogeneity Between Classes
Does study time have the same effect in every class? τ₁ lets slopes vary, ρ controls the intercept-slope correlation.
Raw NP lines (toggle)
Slope var. τ₁ 1.5
Correlation ρ 0.00
Fixed Slope γ₁₀ 3.0
γ₁₀ (avg. slope)
τ₁ (slope SD)
ρ(u₀,u₁)
Slope range (RS)
Random Intercepts + Slopes
Yij = (γ₀₀+u0j) + (γ₁₀+u1j)·X + ε
Σ = [[τ₀²,ρτ₀τ₁],[ρτ₀τ₁,τ₁²]]
What you see

Colored solid = shrunk random-slopes estimate per class (the same model shown on tab ① in "Intercept+Slope" mode). Gray dashed line = the fixed slope across all points. Use the button below the plot to also show the raw (unshrunk) NP lines. Slide τ₁ and ρ (the "Controls" tab) to explore slope variability, the intercept-slope correlation, and the shrinkage effect live.

▸ Learn more

What you're seeing in detail
Colored solid = class-specific random-slopes estimate after shrinkage — exactly the estimate tab ① shows in "Intercept+Slope" mode for the data synced there by default. Gray dashed line = the estimated fixed slope (OLS across all points, N=J·n_j) — a good approximation of the true value γ₁₀, because it uses far more observations than any single colored line. Use the button below the plot to also show the raw NP lines (one per class, unshrunk, estimated only from the n_j observations of that class) — comparing the two shows how strongly the solid lines were pulled back toward γ₁₀. At τ₁=0, the true slopes of all classes are identical — the raw NP lines can still look slightly different, because each one is estimated from only n_j=10 noisy observations (pure sampling noise, not real heterogeneity). That's exactly why random-slope shrinkage exists: it pulls these randomly noisy NP slopes back toward γ₁₀ instead of taking them at face value — the smaller τ₁ is relative to the noise, the further the shrunk line moves from the raw NP line, toward the fixed slope.

Slider τ₁ (slope variance)
τ₁=0: all classes benefit equally from study time — Random Intercepts suffices.
τ₁>0: the slopes fan out (or in). Substantive question: is the study-time effect stronger in some classes than in others?

Slider ρ (intercept-slope correlation)
ρ>0 (fan-out): classes with a high starting level also show a stronger study-time effect. Richer classes get richer — Matthew principle.
ρ<0 (compensation): classes with a low starting level benefit more from study time — instruction evens things out.
ρ=0: slope and intercept vary independently.

When do I need Random Slopes?
Theoretically: when the predictor effect shouldn't be the same for all groups.
Statistically: model comparison via LRT or LOOIC. But: random-slope models need more data (especially large n_j) and can be numerically unstable. When in doubt: Random Intercepts as default.

Tip: uncorrelated RE (||)
If ρ is hard to identify (small J), you can decouple the intercept and slope random effects. In lme4: (1 + X || Class). In brms: (1 | Class) + (0 + X | Class). This reduces parameters and improves convergence.

R code (lme4 / brms)
Random Intercepts: lmer(Y ~ X + (1 | Class), data=df)
Random Slopes (correlated): lmer(Y ~ X + (1 + X | Class), data=df)
Uncorrelated (||): lmer(Y ~ X + (1 + X || Class), data=df)
Bayesian: brm(Y ~ X + (1 + X | Class), family=gaussian(), data=df)

Simpson's Paradoxthe Aggregation Error
The overall OLS trend can contradict the within-group trend — a classic case of confounded group structure.
Within effect (dotted) vs. OLS overall = Complete Pooling (dashed)
Scenarios
Reference line (toggle)
Within effect β_W 3.0
Between offset β_B −6.0
Group spacing 7.0
β OLS overall (= Complete Pooling)
β within (avg.)
Sign flip
Decomposition of the OLS coefficient
β_OLS = β_W·w_W + β_B·w_B
w_W = share of within-variance in X
β_OLS = −: even though β_W > 0 !
What you see

5 classes (colors). Within each class: positive trend (β_W > 0). Between classes: negative trend (β_B < 0) — the dashed OLS line only sees the negative between trend and estimates β < 0. Slide β_W, β_B and the group spacing (the "Controls" tab) to explore the sign flip live.

▸ Learn more

How does the paradox arise?
OLS mixes two different effects: the within-group effect (interesting!) and the between-group effect (confounded!). If the between effect dominates the sign and the between-variance in X is large, the sign reverses.

How does LMM solve the problem?
The fixed slope γ₁₀ in the LMM estimates the within effect — controlled for the group intercepts. That's exactly the causally relevant effect: "if a student in the same class studies one more hour, what changes?"

Explore the sliders
· β_B → 0: no between confounding, OLS and within agree.
· Group spacing ↓: classes overlap in X, between-variance drops → less Simpson effect.
· β_W → negative: the within effect is negative too — no longer a paradox, just a genuinely poor study-time effect.

Connection to fixed effects
In econometrics: the fixed-effects model (within-group demeaning) likewise estimates only within effects. LMM (partial pooling) sits in between: it accounts for within effects but also allows level-2 predictors (modeling between effects explicitly).

Reference line "Fixed Effect / Grand Mean"
Activate the line below the scenarios to see the correctly specified within effect (slope β_W, anchored at the grand mean) directly alongside the misleading OLS-overall line — the contrast this module is really about: OLS overall in this plot is Complete Pooling (the same global line, ignoring the group structure entirely), just under a different name.

Historical examples
UC Berkeley (1973): women had lower admission rates in aggregate. Stratified by department: equal or better in most departments. Women applied more often to competitive departments.
Kidney stone treatment: treatment A looks worse than B. Stratified by stone size: A is better in both groups. B was preferred for easier cases — confounding by indication.

Extreme Shrinkage — When Pooling Really Helps
📋 Standalone Example — Extreme Shrinkage
20 school classes of varying size — from n_j=3 to n_j=40 students — take the same test. A small class of just 3 students shows a strikingly high (or low) mean. Is this a genuine difference — or just chance, because extreme means occur much more easily with so few observations? Exactly this argument (illustrated, among others, by Gelman & Hill, 2007, using baseball batting averages and cancer rates per county) is the core of Partial Pooling: small, extreme estimates are a priori less plausible than moderate ones — so they're shrunk more strongly toward the grand mean, which on average leads to better predictions.
Estimate by group size (funnel plot)
No repetitions run yet.
True SD τ 8.0
Noise σ 15.0
Avg. error No Pooling
Avg. error Partial Pooling
…at n≤6: No Pooling
…at n≤6: Partial Pooling
τ̂² (estimated)
Empirical Bayes estimate of the group mean
ŷ_j = λ_j·ȳ_j + (1−λ_j)·γ̂
λ_j = τ̂² / (τ̂² + σ²/n_j) — the same formula as in Module ②, here without a slope, just for group means
What you see

20 classes, sorted by group size (x-axis). Gray circle = true mean (known only here, because simulated). Orange circle = No Pooling. Blue dot = Partial Pooling (shrunk). On the left (small n), the orange points scatter much wider — a classic "funnel" shape.

▸ Learn more

What you're seeing in detail
20 classes, sorted by group size (x-axis). Gray circle = true mean (known only here, because simulated). Orange circle = No Pooling (raw class mean). Blue dot = Partial Pooling (shrunk). The dotted line connects No Pooling and Partial Pooling for the same class.

The funnel
On the left (small n), the orange No-Pooling points scatter much further around γ̂ than on the right (large n) — a classic "funnel" shape. This is due purely to sample size: the same true difference τ produces much greater estimation uncertainty at small n_j (SE = σ/√n_j).

The punchline
The blue Partial-Pooling points lie, on average, closer to the gray true value than the orange No-Pooling points — especially on the left, at small n. This isn't a claim, it's directly verifiable: see "avg. error" at the bottom left, or click "500 Simulations" for proof across many repetitions.

Sliders
· τ (true SD) ↓: classes barely differ in truth → every deviation is more likely noise → more shrinkage pays off.
· σ (noise) ↑: individual measurements become less reliable → shrinkage increases for all groups, most for small ones.

Gelman's argument
Andrew Gelman uses this pattern, among others, with the example of kidney cancer rates per US county: counties with the highest rates are almost always sparsely populated — but so are the counties with the lowest rates. This isn't a substantive contradiction but pure sampling noise: small counties (small n) simply have the largest estimation variance and therefore produce both extremes disproportionately often. The same logic applies to small school classes, small branches, short baseball seasons, and so on. Partial Pooling actively exploits this insight: extreme estimates from small groups are pulled more strongly toward the grand mean, because they're the least plausible a priori. Reference: Gelman & Hill (2007), Data Analysis Using Regression and Multilevel/Hierarchical Models, Ch. 12. This exact "record-holding communities are tiny" pattern — independent of Partial Pooling, purely as a probability phenomenon — is explored in depth in a dedicated tool: → Law of Small Numbers.

Flashcards — Multilevel & Mixed Models
ICC — Intraclass Correlation
The ICC (Intraclass Correlation Coefficient) measures the share of the total variance that lies between groups: ICC = τ₀² / (τ₀² + σ²). ICC = 0 means groups aren't any more similar than random samples — OLS suffices. ICC > 0.10–0.20 means a strong cluster structure: individuals within a group are more similar to each other. Then OLS leads to standard errors that are too small and incorrect inference. Rule of thumb: consider LMM above ICC > 0.05; LMM is essential above ICC > 0.10.
Complete · No · Partial Pooling
Complete Pooling ignores groups — one global OLS line, SE underestimated. No Pooling is modeled as a group × predictor interaction (2J parameters) — stable only with large n_j and small J, no information sharing. Partial Pooling (LMM) is the compromise: group intercepts (Random Intercepts) or also slopes (Random Slopes) are shrunk toward the grand mean (Empirical Bayes). Strength of shrinkage: λ = τ₀²/(τ₀²+σ²/n_j). Small groups get adjusted more strongly. Result: a regularized estimator with better out-of-sample prediction and correct SE.
Shrinkage & Empirical Bayes
Partial Pooling is equivalent to Empirical Bayes: group intercepts are treated as randomly drawn from N(0,τ₀²). Shrinkage factor λ_j = τ₀²/(τ₀²+σ²/n_j). At τ₀² ↑ or n_j ↑, λ → 1 (little shrinkage). For small groups or small τ₀², λ → 0 (strong shrinkage). The caterpillar plot shows shrunk intercepts with CI. Groups far from the grand mean have wider CIs and less shrinkage — because they bring more of their own evidence.
A random-intercept model assumes all groups have the same slope. Random-slope models allow this effect to vary: τ₁ = SD of the slope distribution. The correlation parameter ρ between u₀j and u₁j matters substantively: ρ > 0 (fan-out) = high groups benefit more; ρ < 0 (compensation) = low groups benefit more. In lme4: lmer(Y ~ X + (1 + X | Group), data = df). With small J and small n_j, ρ may be poorly identified → try the || syntax.
Simpson's Paradox & Within/Between
The aggregated OLS estimator mixes within effects (causally interesting) with between effects (often confounded). Simpson's paradox arises when the two effects have opposite signs and between-variance in X dominates. LMM's fixed slope γ₁₀ estimates the within effect, adjusted for group differences. Historical examples: UC Berkeley admissions (1973), kidney stone treatment. Anyone relying on OLS alone may draw causally wrong conclusions.
Model Formula & R Syntax
Random Intercepts: lmer(Y ~ X + (1 | Group), data = df).
Random Slopes: lmer(Y ~ X + (1 + X | Group), data = df).
Bayesian (brms): brm(Y ~ X + (1 | Group), family = gaussian(), data = df).
summary() shows fixed effects (γ₀₀, γ₁₀) and random effects (τ₀, σ). ICC via performance::icc(model). Model comparison: anova(m_ri, m_rs) or loo_compare(). Conditional R²: r2_nakagawa() from the performance package.
Shrinkage as a Prediction Improvement
The real benefit of Partial Pooling isn't an aesthetic preference but measurably better out-of-sample prediction: because small groups are highly likely to produce extreme estimates purely through sampling noise, an estimator shrunk toward the grand mean is, on average, closer to the true value (lower expected squared error) than the unshrunk No-Pooling estimator — a classic result from decision theory (related to the James-Stein estimator). Gelman & Hill (2007) illustrate this with, among other things, baseball batting averages and cancer rates per county. See Module ⑤.
Going further: (G)LMM — Mixed Models Interactive
This tool covers the core ideas — for a much more extensive deep dive (including generalized linear mixed models, GLMM, with binary/count outcomes, further random-effect structures, and model comparisons), the BTL sister project offers the interactive tool "(G)LMM — Mixed Models Interactive" (German version).
? Help — Multilevel & Mixed Models

What does this tool show?

The multilevel model tool explains Linear Mixed Models (LMM) for hierarchically structured data. Example: J=5 school classes with n_j=10 students each, X=study time, Y=exam points. Five interactive modules lead from the pooling question through shrinkage and random slopes to Simpson's paradox and the question of when shrinkage actually improves prediction.

The five modules

  • ① Pooling Comparison — compare Complete, No and Partial Pooling live. ICC bar shows variance decomposition. Scenarios at the click of a button.
  • ② Shrinkage & ICC — caterpillar plot + shrinkage arrow diagram. Sliders for τ₀, σ, n_j show how λ controls shrinkage.
  • ③ Random Slopes — τ₁ and ρ control slope variability and intercept-slope correlation. Fan-out / compensation live.
  • ④ Simpson's Paradox — set within and between effects. Experience the sign flip of the OLS coefficient live.
  • ⑤ Extreme Shrinkage — 20 groups of varying size, with the true value visible. Shows directly that Partial Pooling is, on average, closer to the truth than No Pooling — especially for small groups.

Terms

  • ICC: τ₀² / (τ₀² + σ²) — share of between-variance in total variance.
  • γ₀₀: grand intercept (fixed effect) — overall mean across all groups.
  • γ₁₀: fixed slope — shared within-group effect of X on Y.
  • u₀j: random intercept of group j — deviation from the grand mean.
  • u₁j: random slope of group j — deviation from the fixed slope.
  • τ₀ / τ₁: SD of the random-intercept / -slope distribution between groups.
  • σ: residual SD (within-group error).
  • λ: shrinkage factor = τ₀² / (τ₀² + σ²/n_j).
▸ Method-of-moments estimator (for the curious)

This tool uses simple moment estimators (not REML / ML). The within-slope γ₁₀ is the Sxx-weighted average of the group-specific OLS slopes. The residual variance σ² is computed from the residuals to the shared slope. τ₀² = max(0, Var(NP intercepts) − σ²/n_j). These estimators are consistent but less efficient than REML — sufficient for teaching purposes.

Further reading

References

Gelman, A. & Hill, J. (2007). Data Analysis Using Regression and Multilevel/Hierarchical Models. Cambridge University Press.

Hox, J. J., Moerbeek, M. & van de Schoot, R. (2018). Multilevel Analysis: Techniques and Applications (3rd ed.). Routledge.

West, B. T., Welch, K. B. & Galecki, A. T. (2022). Linear Mixed Models: A Practical Guide Using Statistical Software (3rd ed.). Chapman & Hall/CRC.