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.
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.
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.
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.
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.
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)
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.
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.
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.
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.
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.
lmer(Y ~ X + (1 + X | Group), data = df). With small J and small n_j, ρ may be poorly identified → try the || syntax.
lmer(Y ~ X + (1 | Group), data = df).lmer(Y ~ X + (1 + X | Group), data = df).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.
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.
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.
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.