Hierarchical Bayesian model

Model continuous outcomes nested within groups while sharing information across those groups.

Method lesson · Updated October 6, 2026

Concepts and reasoning

Learning across related groups

Patients may be nested in clinics, students in schools, or measurements in laboratories. Observations from one group often share conditions and are not interchangeable with independent observations from different groups. A hierarchical model represents both within-group and between-group variation.

Complete pooling estimates one common group mean, ignoring group differences. No pooling estimates every group separately, which can make small groups highly variable. Partial pooling treats group effects as related draws from a shared distribution. Each group contributes its own evidence while borrowing information from the others.

This borrowing is strongest when a group's data are imprecise relative to the between-group spread. It is weaker when the group has substantial information or the population of groups is heterogeneous. Shrinkage is a consequence of a model and its estimated uncertainties, not a manual correction applied only to inconvenient outliers.

Exchangeability is a substantive assumption

A common group distribution assumes the groups are exchangeable conditional on included predictors: before observing their individual outcomes, their unexplained effects are modeled similarly. If referral centers and community clinics differ systematically, group-level covariates or another structure may be needed.

The Stan multilevel modeling guide discusses partial pooling and group models. A hierarchy can improve estimation for sparse groups, but it can also conceal relevant structure if the groups are not comparable.

Mathematical foundations

On a small screen, scroll wide equations and tables horizontally.

A varying-intercept model

A simple Gaussian hierarchy is

Here σ describes within-group residual variation, τ describes between-group intercept variation, and β contains common slopes. Priors are also specified for μ, τ, σ, and β. Uncertainty in these higher-level parameters propagates into the group estimates.

For intuition, suppose μ, τ, and σ are known and there are no predictors. A group's sample mean has variance σ²/nⱼ. Its conditional posterior mean is

The posterior variance is

These formulas show the mechanism. A full fitted model averages over uncertainty in μ, τ, and σ, so substituting point estimates into this simple calculation is only an approximation to the full posterior.

In an intercept-only random-effects population model, the intraclass correlation is τ²/(τ² + σ²). It measures how much of the modeled outcome variation is between groups.

Worked example

Hypothetical data for learning; independent of your analysis inputs.

Worked example: two groups with the same raw mean

Suppose the shared mean is μ = 50, between-group variance τ² = 4, and within-group variance σ² = 36. Two clinics both have sample mean 60.

Clinic sample sizeWeight on its own meanConditional posterior mean
24/(4 + 18) = 0.18251.818
184/(4 + 2) = 0.66756.667
904/(4 + 0.4) = 0.90959.091

For the two-person clinic, most of the raw difference from 50 could be sampling noise, so the estimate moves strongly toward the shared mean. The 90-person clinic has more precise local evidence and is moved much less.

For n = 2, conditional posterior variance is (1/4 + 2/36)⁻¹ = 3.273, with SD 1.809. For n = 18 it is (1/4 + 18/36)⁻¹ = 1.333, with SD 1.155. These calculations condition on known population parameters; actual uncertainty is wider or otherwise different when those quantities are estimated.

The modeled intraclass correlation is 4/(4 + 36) = 0.10. Grouping matters even though most individual variation occurs within groups.

For a new clinic with no observations, its intercept is drawn from the population distribution rather than estimated as exactly μ. A prediction for a new patient in that clinic must include uncertainty in the new clinic's effect as well as individual residual variation.

Interpreting the results

Partial pooling balances group and shared information

A hierarchical model estimates group-specific deviations while linking them through a population distribution. Groups with little information are usually pulled more toward the shared mean, whereas well-informed groups can retain larger differences. This shrinkage reflects the model's uncertainty trade-off, not a correction that assumes all groups are identical.

A group standard deviation describes residual between-group variation after the included predictors. It is different from individual residual standard deviation. A small estimate can reflect genuinely similar groups or limited information about heterogeneity, so its interval and prior sensitivity matter.

Predictions for an observed group condition on its estimated deviation. Predictions for a new group must include uncertainty in an unobserved group effect. Those are different targets. Group rankings can be highly uncertain even when their posterior means differ; report uncertainty and the scale of variation rather than a league table alone.

Assumptions and limitations

Exchangeability needs a substantive argument

Partial pooling assumes groups can reasonably be modeled as draws from a shared distribution after accounting for included predictors. A hospital with a fundamentally different service or population may not be exchangeable with the others. Group-level predictors or a different structure may be needed.

A varying-intercept model allows baseline differences while keeping predictor slopes common across groups. It cannot represent group-specific slopes merely by including more groups. Within-group and between-group predictor relationships can also differ; centering choices affect their interpretation.

Gaussian individual errors and the group-effect distribution are modeling assumptions, not consequences of nesting. Few groups provide limited information about the between-group scale. Priors on that scale can meaningfully influence pooling, especially with small or unbalanced groups. Stan's regression guide develops hierarchical regression structures.

Diagnostics and sensitivity analysis

Check fit at both levels

Review computational diagnostics for population coefficients, group scale, and group effects. Hierarchical geometry can be difficult near a small group variance; good intercept traces alone do not establish convergence for every group. Divergences and low effective sample sizes should be investigated before ranking groups.

Compare predictive behavior by group as well as across the full sample. A good overall mean can conceal systematic underprediction in small groups or unexplained within-group spread. Examine influential groups and the effect of unbalanced group sizes.

Repeat with plausible group-scale priors and consider whether common slopes are defensible. For a new-group prediction question, use a validation strategy that holds out entire groups rather than only random observations. Report the distinction between variation among individuals, uncertainty in group means, and variation expected across new groups.

References and other material

Read the methodological references and supporting tutorials below. The examples in this lesson are original teaching calculations, not findings from these sources.

Related method guides