Skip to main content

The Secret Weapon

Building and checking models
Method or technique
Intermediate
Fitting a statistical model repeatedly on several different datasets and then displaying all these estimates together.
Published

November 9, 2026

Modified

November 13, 2026

Entry #2 in Andrew Gelman’s Handy Statistical Lexicon. Original discussion: The secret weapon.

Filed under Building and checking models · Method or technique · Intermediate.

Related terms: small multiples, separate regressions by year, coefficient plot.

Source check: ✓ The explanation was checked against the source.

What it is

When your data come in natural batches, such as survey years, countries, schools or experiments, fit the same model to each batch separately. Then plot the coefficient of interest from every fit side by side, with its uncertainty interval. The result is a picture of how the estimate varies across batches.

Gelman calls it the secret weapon “because it seems to be done much less often than it could be. I suspect the technique is not used more because people are fixated on point estimates and don’t realize that a graph can tell a clearer story.” He also describes it as “an approximation to multilevel modeling, with the partial pooling done by eye on the graphs rather than using a full statistical model.” It needs no new methodology, just a loop and a graph.

Why it matters

A single model fit to pooled data gives you one number and hides everything else. Do the batches agree? Is there a trend over time? Is one batch an outlier driving the result? Fitting separately and plotting together answers these questions at a glance. It is also an honest first step before building a multilevel model: the plot shows you whether, and how much, pooling is warranted.

How it works

  1. Split the data by the batching variable \(g = 1, \dots, G\).
  2. Fit \(y_i = \alpha_g + \beta_g x_i + \epsilon_i\) in each batch and record \(\hat\beta_g\) and its standard error.
  3. Plot \(\hat\beta_g \pm 1\,\text{se}\) against \(g\). Year on the x-axis is the classic choice.
  4. Look for structure. A smooth trend suggests modeling \(\beta_g\) as a function of time. Scatter consistent with the error bars suggests the batches share a common coefficient.

Simulated example

A survey is run every two years from 1972 to 2024, and in each year we regress an outcome on a predictor. The true coefficient drifts smoothly over time. Try shrinking the per-year sample size: each estimate becomes noisier, but the trend is still visible across the whole set.

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, lm} from "../js/stats.js"

viewof nPerYear = Inputs.range([20, 2000], {value: 300, step: 10, label: "Respondents per survey"})
viewof trend = Inputs.radio(["Rising", "Flat", "Rise and fall"], {value: "Rising", label: "True pattern"})
viewof seed = Inputs.range([1, 500], {value: 11, step: 1, label: "Random seed"})

fits = {
  const r = rng(seed);
  const years = range(27).map(i => 1972 + 2 * i);
  const truth = yr => {
    const t = (yr - 1972) / 52;
    return trend === "Rising" ? 0.1 + 0.5 * t : trend === "Flat" ? 0.3 : 0.1 + 1.2 * t * (1 - t) * 1.6;
  };
  return years.map(year => {
    const beta = truth(year);
    const x = r.normals(nPerYear);
    const y = x.map(v => 1 + beta * v + r.normal(0, 1));
    const f = lm(x, y);
    return {year, beta, est: f.b, lo: f.b - f.seB, hi: f.b + f.seB};
  });
}

Plot.plot({
  ariaLabel: "The Secret Weapon: simulation chart",
  ariaDescription: "Horizontal axis: Survey year. Vertical axis: Estimated coefficient (±1 se). The chart updates with the controls above it, and the summary below the charts states the result in words.",
  height: 320,
  y: {label: "Estimated coefficient (±1 se)", grid: true},
  x: {label: "Survey year", tickFormat: "d"},
  marks: [
    Plot.ruleX(fits, {x: "year", y1: "lo", y2: "hi", stroke: "#2166ac"}),
    Plot.dot(fits, {x: "year", y: "est", fill: "#2166ac"}),
    Plot.line(fits, {x: "year", y: "beta", stroke: "#d6604d", strokeDasharray: "5 3"}),
    Plot.ruleY([0])
  ]
})
# The Secret Weapon: fit the same model to each year, then plot them all.
1n_per_year <- 300 # respondents per survey
trend <- "Rising" # or "Flat", "Rise and fall"
2set.seed(11)

years <- seq(1972, 2024, by = 2)
3truth <- function(yr) {
  t <- (yr - 1972) / 52
  switch(trend,
    Rising = 0.1 + 0.5 * t,
    Flat = 0.3 + 0 * t,
    "Rise and fall" = 0.1 + 1.2 * t * (1 - t) * 1.6
  )
}
4fits <- t(sapply(years, function(year) {
  x <- rnorm(n_per_year)
  y <- 1 + truth(year) * x + rnorm(n_per_year)
  summary(lm(y ~ x))$coefficients["x", 1:2] # estimate and se
}))
5inside <- abs(fits[, 1] - truth(years)) <= fits[, 2]
cat(
  sum(inside), "of", length(years), "intervals (+/- 1 se) cover the",
  "true coefficient\n"
)

6plot(years, fits[, 1],
  pch = 19,
  ylim = range(fits[, 1] - fits[, 2], fits[, 1] + fits[, 2]),
  xlab = "Year", ylab = "Coefficient estimate"
)
segments(years, fits[, 1] - fits[, 2], years, fits[, 1] + fits[, 2])
lines(years, truth(years), col = "#d6604d", lwd = 2, lty = 2)
1
The page’s controls: respondents per survey and the true pattern over time.
2
Fixes the random numbers so the run is reproducible.
3
The true coefficient in each year.
4
The secret weapon: fit the same regression separately to each year’s survey and keep the coefficient and its standard error.
5
How many of the one-standard-error intervals cover the truth (about two thirds is expected).
6
All the estimates plotted together against the true trend. No single year shows the pattern; the set of years does.
# The Secret Weapon: fit the same model to each year, then plot them all.
import numpy as np
import matplotlib.pyplot as plt

n_per_year = 300  # respondents per survey
trend = "Rising"  # or "Flat", "Rise and fall"
rng = np.random.default_rng(11)

years = np.arange(1972, 2025, 2)


def truth(yr):
    t = (yr - 1972) / 52
    return {"Rising": 0.1 + 0.5 * t, "Flat": 0.3 + 0 * t,
            "Rise and fall": 0.1 + 1.2 * t * (1 - t) * 1.6}[trend]


est, se = [], []
for year in years:
    x = rng.normal(size=n_per_year)
    y = 1 + truth(year) * x + rng.normal(size=n_per_year)
    (b, a), cov = np.polyfit(x, y, 1, cov="unscaled")
    resid = y - (a + b * x)
    est.append(b)
    se.append(np.sqrt(cov[0, 0] * resid @ resid / (n_per_year - 2)))
est, se = np.array(est), np.array(se)
inside = np.abs(est - truth(years)) <= se
print(f"{inside.sum()} of {len(years)} intervals (+/- 1 se) cover the "
      "true coefficient")

plt.errorbar(years, est, yerr=se, fmt="o", color="black")
plt.plot(years, truth(years), "--", color="#d6604d", linewidth=2)
plt.xlabel("Year")
plt.ylabel("Coefficient estimate")
plt.show()

Key takeaways

  • Fit the same model to each batch, then plot all the estimates together. It’s cheap and it shows a lot.
  • The display shows variation over time or across groups that a single pooled estimate hides.
  • It is a natural stepping stone to a multilevel model that partially pools across batches.

Source

Back to top