Scaffolding
Building and checking models
Method or technique
Advanced
Understanding your model by comparing it to related models.
What it is
Gelman starts from Winston Churchill’s remark that the truth is sometimes so precious it must be attended by a bodyguard of lies: “Similarly, for a model to be believed, it must, except in the simplest of cases, be accompanied by similar models that either give similar results or, if they differ, do so in a way that can be understood.” He distinguishes scaffolding from model selection and averaging: “we would want to compare our results to simpler models, even if we know that our chosen model is correct. Models of even moderate complexity can be extremely difficult to understand on their own.”
A single fitted model is hard to understand on its own. Statistical scaffolding means fitting a family of related models, simpler and more complex versions of the one you care about, and comparing their results. Builders take down the scaffold once the building stands. Likewise, the extra models don’t need to appear in the final report, but they are how you came to understand the final one.
Why it matters
Complex models such as multilevel regressions, models with interactions or causal models produce numbers whose sources are hard to trace. By moving from simple to complex, you can see which modeling choice moved which estimate. A coefficient that flips sign when you add a predictor, or a group estimate that shrinks a lot under partial pooling, tells you how the data and the model interact. Without that, a single complex model is a black box that you cannot defend or debug.
How it works
For a multilevel model of group means, a classic scaffold has three steps:
- Complete pooling. Ignore groups and use one overall mean.
- No pooling. Use each group’s own mean.
- Partial pooling. Use the multilevel model, which lies between the two and pulls small groups further toward the overall mean.
Plotting all three side by side shows how much each group is shrunk and why: shrinkage depends on group size and on the between-group variance. You can add further rungs, such as group-level predictors or varying slopes, the same way.
Simulated example
Ten groups of very different sizes. The plot traces each group’s estimate across the three rungs of the scaffold.
Show simulation code
The R version uses base R, and the Python version uses NumPy and Matplotlib. Where the simulation draws random numbers, each language draws its own, so exact numbers differ slightly but show the same pattern. The numbered markers in the R code are explained under it.
import {rng, range, mean, sum, fmt} from "../js/stats.js"
viewof tau = Inputs.range([0.05, 1], {value: 0.3, step: 0.05, label: "True between-group sd"})
viewof seed = Inputs.range([1, 500], {value: 21, step: 1, label: "Random seed"})
scaffold = {
const r = rng(seed);
const sizes = [2, 3, 5, 8, 12, 20, 35, 60, 100, 200];
const raw = sizes.map((n, j) => {
const theta = r.normal(0, tau), y = r.normals(n, theta, 1);
return {group: `G${j + 1} (n=${n})`, n, theta, ybar: mean(y), v: 1 / n};
});
const grand = sum(raw.map(d => d.ybar * d.n)) / sum(raw.map(d => d.n));
const mu = sum(raw.map(d => d.ybar / d.v)) / sum(raw.map(d => 1 / d.v));
const tau2 = Math.max(0.002, mean(raw.map(d => (d.ybar - mu) ** 2)) - mean(raw.map(d => d.v)));
return raw.flatMap(d => [
{group: d.group, n: d.n, model: "1. Complete pooling", est: grand},
{group: d.group, n: d.n, model: "2. No pooling", est: d.ybar},
{group: d.group, n: d.n, model: "3. Partial pooling", est: mu + tau2 / (tau2 + d.v) * (d.ybar - mu)}
]);
}
Plot.plot({
ariaLabel: "Scaffolding: simulation chart",
ariaDescription: "Vertical axis: Estimated group mean. The chart updates with the controls above it, and the summary below the charts states the result in words.",
height: 340,
x: {label: null, domain: ["1. Complete pooling", "2. No pooling", "3. Partial pooling"], padding: 0.3},
y: {label: "Estimated group mean", grid: true},
color: {type: "log", scheme: "blues", legend: true, label: "Group size"},
marks: [
Plot.line(scaffold, {x: "model", y: "est", z: "group", stroke: "n", strokeWidth: 1.5}),
Plot.dot(scaffold, {x: "model", y: "est", fill: "n", r: 4}),
Plot.text(scaffold.filter(d => d.model === "3. Partial pooling"), {x: "model", y: "est", text: "group", dx: 8, textAnchor: "start", fontSize: 9})
]
})# Scaffolding: complete pooling, no pooling, then partial pooling.
1tau <- 0.3 # true between-group sd
2set.seed(21)
n <- c(2, 3, 5, 8, 12, 20, 35, 60, 100, 200) # group sizes
theta <- rnorm(10, 0, tau)
4ybar <- sapply(1:10, function(j) mean(rnorm(n[j], theta[j], 1)))
v <- 1 / n
3grand <- sum(ybar * n) / sum(n) # 1. complete pooling
mu <- sum(ybar / v) / sum(1 / v)
tau2 <- max(0.002, mean((ybar - mu)^2) - mean(v))
5partial <- mu + tau2 / (tau2 + v) * (ybar - mu) # 3. partial pooling
est <- cbind(complete = grand, none = ybar, partial = partial)
rownames(est) <- paste0("G", 1:10, " (n=", n, ")")
print(round(est, 2))
6matplot(1:10, est,
type = "b", pch = 19, lty = 1, xaxt = "n",
col = c("grey50", "#d6604d", "#2166ac"), xlab = "Group (size)",
ylab = "Estimate"
)
axis(1, 1:10, n)
legend("topright", c("Complete pooling", "No pooling", "Partial pooling"),
col = c("grey50", "#d6604d", "#2166ac"), pch = 19, bty = "n"
)- 1
- The page’s control: the true spread between the ten groups.
- 2
- Fixes the random numbers so the run is reproducible.
- 3
- The first scaffold: complete pooling, one estimate for everyone.
- 4
- The second: no pooling, each group’s own mean, noisy for the small groups.
- 5
- The model you were building toward: partial pooling, which sits between the two scaffolds.
- 6
- All three estimates for each group, from the smallest to the largest. The scaffolds show what the final model is doing.
# Scaffolding: complete pooling, no pooling, then partial pooling.
import numpy as np
import matplotlib.pyplot as plt
tau = 0.3 # true between-group sd
rng = np.random.default_rng(21)
n = np.array([2, 3, 5, 8, 12, 20, 35, 60, 100, 200]) # group sizes
theta = rng.normal(0, tau, 10)
ybar = np.array([rng.normal(t, 1, k).mean() for t, k in zip(theta, n)])
v = 1 / n
grand = np.sum(ybar * n) / n.sum() # 1. complete pooling
mu = np.sum(ybar / v) / np.sum(1 / v)
tau2 = max(0.002, np.mean((ybar - mu) ** 2) - v.mean())
partial = mu + tau2 / (tau2 + v) * (ybar - mu) # 3. partial pooling
for k, y, p in zip(n, ybar, partial):
print(f"n = {k:3d}: complete {grand:.2f}, none {y:.2f}, "
f"partial {p:.2f}")
x = np.arange(10)
plt.plot(x, np.full(10, grand), "o-", color="0.5", label="Complete pooling")
plt.plot(x, ybar, "o-", color="#d6604d", label="No pooling")
plt.plot(x, partial, "o-", color="#2166ac", label="Partial pooling")
plt.xticks(x, n)
plt.xlabel("Group (size)")
plt.ylabel("Estimate")
plt.legend()
plt.show()Key takeaways
- Understand a complex model by fitting the simpler models around it and comparing.
- Trace how each estimate changes from rung to rung. Changes reveal what the model is doing.
- The scaffold may not appear in the final paper, but it is how you know the paper is right.
Source
- Andrew Gelman, Winston Churchill on statistical modeling (2009).