Skip to main content

Try it in R: Multiple Comparisons

Building and checking models
Intermediate
Build an empirical-Bayes multilevel shrinkage estimator in base R and compare its false discoveries and accuracy against no pooling.
Published

November 11, 2026

Modified

November 15, 2026

Classical multiple-comparisons corrections (like Bonferroni) widen confidence intervals to keep you from making false claims, but leave the noisy point estimates untouched. Gelman, Hill, and Yajima (2012) show that multilevel modeling (partial pooling) solves the problem more directly: it estimates how much the true group effects vary (\(\tau\)) relative to the measurement noise (\(\sigma\)), and pulls noisy extremes toward the overall mean.

Step 1: Fifty groups and the “winner’s curse”

Suppose we measure \(J = 50\) groups (schools, hospitals, states, or subgroups). Their true effects come from \(\theta_j \sim \mathrm{N}(0, \tau^2)\) with \(\tau = 0.1\), and each group is measured with noise \(\sigma = 0.5\). Let’s simulate one world and inspect the group with the largest raw estimate.

Step 2: Three lines of R for partial pooling

The variance of the observed estimates \(y_j\) is \(\tau^2 + \sigma^2\). Empirical Bayes estimates \(\hat\tau^2 = \max\!\big(0,\, \text{Var}(y) - \sigma^2\big)\) and shrinks each group toward the grand mean \(\bar y\) by the shrinkage factor \(B = \hat\tau^2 / (\hat\tau^2 + \sigma^2)\):

Predict → Modify → Observe

  1. Pure noise (\(\tau = 0\)). In Step 1, change tau <- 0 and re-run both Step 1 and Step 2. How many “significant” claims does no-pooling make out of 50 groups, and how far do the blue multilevel intervals pull back toward zero?
  2. Strong real differences (\(\tau = 0.8\)). Change tau <- 0.8 (now the true group differences are larger than the measurement noise sigma = 0.5) and re-run both cells. Watch how B approaches 1 and the blue multilevel intervals automatically stay wide and close to the raw estimates!

Step 3: Repeat across 1,000 simulated worlds

Averaging over 1,000 worlds shows the long-run number of wrong-sign claims per 50 groups as \(\tau\) varies from \(0\) to \(0.5\).

Your turn: compare root-mean-squared error (RMSE) across all 50 groups

Partial pooling doesn’t just prevent false-positive claims; by James–Stein theory, it also estimates the 50 true effects \(\theta_1, \dots, \theta_{50}\) much more accurately overall.

Complete the code below to compute the root-mean-squared error \(\sqrt{\frac{1}{J}\sum_{j=1}^J (\hat\theta_j - \theta_j)^2}\) for both y (no pooling) and pm (multilevel partial pooling).

Use sqrt(mean((y - theta)^2)) for no pooling and sqrt(mean((pm - theta)^2)) for the multilevel estimates.

rmse_no_pooling <- sqrt(mean((y - theta)^2)) rmse_multilevel <- sqrt(mean((pm - theta)^2)) round(c(no_pooling = rmse_no_pooling, multilevel = rmse_multilevel), 3)
Show simulation code
rmse_no_pooling <- sqrt(mean((y - theta)^2))
rmse_multilevel <- sqrt(mean((pm - theta)^2))

round(c(no_pooling = rmse_no_pooling, multilevel = rmse_multilevel), 3)

Where next

Back to top