Bayesian Hierarchical Model

Statistics
Introduced by
Gelman & Hill (2006); Bayesian multilevel tradition
Year
2006
Type
hierarchical probabilistic model

What does it do?

Bayesian hierarchical modelling is a Bayesian approach to nested data, such as students within schools within districts.

It estimates parameters at each level and lets groups share information through partial pooling, so each group's estimate depends partly on its own data and partly on the wider set of groups. The between-group variance is treated as an unknown quantity with its own hyperprior, so the degree of sharing is learned from the data rather than fixed in advance. Gelman and Hill (2006/2007) is an applied reference for multilevel regression, and Gelman et al. (2013) covers the Bayesian treatment.

The intuition behind the method

Imagine estimating the average test score for thirty schools, some of which have only two or three students in the sample. A fully independent ('no pooling') analysis would trust each school's raw average, even if it rests on three data points. Complete pooling would collapse all schools into one grand mean and ignore their differences. Partial pooling finds the middle ground: each school's estimate is pulled toward the overall mean by an amount that reflects how much data that school contributed — small schools are pulled hard, large schools barely at all. Bayesian hierarchical modelling implements this shrinkage through a prior on the group means whose spread is itself estimated from the data via hyperpriors, so the degree of pooling is learned rather than assumed.

History

Hierarchical models for nested data predate the texts usually cited for them. Gelman and Hill's Data Analysis Using Regression and Multilevel/Hierarchical Models (Cambridge University Press, first published 2007, cited here as 2006) is an applied manual covering linear and nonlinear regression and multilevel models, including multilevel logistic regression. The Bayesian treatment of hierarchical models is developed in Gelman et al., Bayesian Data Analysis (3rd ed., 2013).

When to use it

Choose this method when observations are nested or clustered and you need estimates at more than one level, for example group-level effects together with the variation between groups. It is most useful when some groups contain few observations, because partial pooling stabilises their estimates by borrowing information from the other groups. It is also a stronger choice than a classical hierarchical linear model when you need full posterior uncertainty for every group-level parameter, or when prior information about the between-group variance exists and should enter the analysis. Be cautious when there are only a few groups (fewer than about five as a rule of thumb), because τ² then rests mainly on its hyperprior; a simpler Bayesian regression may be preferable. Avoid it when computation time is a tight constraint, since MCMC sampling is considerably heavier than fitting a classical model. The formula on this page covers continuous outcomes, so non-continuous outcomes need a likelihood chosen separately.

Limitations and considerations

  • With few groups (fewer than about five as a rule of thumb), τ² is poorly identified and its posterior largely reflects the chosen hyperprior.
  • When the total sample is small, shrinkage pulls every group estimate strongly toward the grand mean, so individual group estimates can be unreliable.
  • Posterior sampling with MCMC is heavier than fitting a classical hierarchical linear model and can take substantially longer as the model grows.
  • Results depend on the hyperprior for τ²; a prior-sensitivity analysis should be run to confirm that conclusions do not change under reasonable alternatives.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Partial pooling shrinks estimates for groups with few observations toward the overall mean, which is the main reason these estimates are more stable than fitting each group separately.
  • The amount of shrinkage is governed by the between-group variance τ², which is estimated from the data through its hyperprior rather than fixed by the analyst.
  • Every parameter, including group means, variance components and predictions, comes with a posterior distribution summarised by credible intervals.
  • Nested structures such as students within schools can be modelled in one fit, so group-level and individual-level estimates are obtained together.

Applications

  • Educational research: estimating school or teacher effects on student outcomes while accounting for student-level predictors.
  • Clinical trials and medicine: comparing treatment effects across hospitals or study sites when site sample sizes vary widely.
  • Social and political science: modelling opinion or behaviour nested within geographic or institutional units.
  • Longitudinal studies: decomposing within-person and between-person sources of variation in repeated-measures data.
  • Ecology and environmental science: species abundance modelling across sites with heterogeneous sampling effort.

How it works

How it works

1

Bayes' rule operates at two levels simultaneously. The likelihood describes how observed data depend on group-level parameters θ (e.g., school means). A first-level prior ties those group parameters together through shared hyperparameters φ (e.g., a grand mean and between-group standard deviation). A hyperprior on φ completes the hierarchy. The joint posterior over all unknowns is proportional to the product of the likelihood, the group-level prior, and the hyperprior, so the model learns the degree of pooling from the data itself.

Formula
p(\theta, \phi \mid y) \propto p(y \mid \theta)\, p(\theta \mid \phi)\, p(\phi)
2

In the formula above, θ corresponds to the group means μ_j, and φ to the hyperparameters μ₀ and τ². Each observation y_ij in group j is Normally distributed around that group's mean μ_j with within-group variance σ². The group means μ_j are drawn from a Normal distribution centred on the grand mean μ₀ with between-group variance τ². Hyperpriors are placed on μ₀ and on the between-group spread: typically a weakly informative Normal for μ₀, and for τ either a Half-Cauchy prior on τ or an Inverse-Gamma prior on τ². These are alternative choices for the same variance component, not both applied at once, and they are different priors that can lead to different results when groups are few. The group structure must be defined explicitly. As a rule of thumb, with fewer than about five groups τ is poorly identified and the hyperprior has a large influence; check prior sensitivity whatever the number of groups. The formula given here covers a continuous outcome only; binary or categorical outcomes require a separately chosen likelihood.

Formula
y_{ij} \sim \mathcal{N}(\mu_j, \sigma^2), \quad \mu_j \sim \mathcal{N}(\mu_0, \tau^2)
3

Multiplying the likelihood by all priors gives the joint posterior over group means μ_j, grand mean μ₀, and variance components σ² and τ². This posterior is rarely available in closed form, so it is usually sampled via Markov chain Monte Carlo, typically the No-U-Turn Sampler (NUTS), a variant of Hamiltonian Monte Carlo. Common working criteria are R-hat below 1.01, effective sample size above 400, and trace plots showing well-mixed chains; check these before using any summary. Posterior summaries (means, medians, and 95% credible intervals) are then extracted for each group and each variance component. Variational inference is a faster approximate alternative, but its uncertainty estimates are generally less reliable than those from MCMC. Finally, run a posterior predictive check: simulate replicated datasets from posterior draws and compare summaries of them (means, spread, group-level extremes) with the observed data. If the observed values fall far outside the replicated distribution, the model is misspecified.

Formula
p(\mu_j, \mu_0, \tau^2, \sigma^2 \mid y) \propto p(y \mid \mu_j, \sigma^2)\, p(\mu_j \mid \mu_0, \tau^2)\, p(\mu_0)\, p(\tau^2)\, p(\sigma^2)

Common pitfalls

  • Skipping convergence checks: R-hat, effective sample size, and trace plots must all be inspected before a posterior summary is trusted.
  • Fitting with very few groups, where the between-group variance cannot be estimated well from the data and the hyperprior effectively decides the amount of pooling.
  • Choosing a τ² hyperprior that is too tight, which forces strong pooling regardless of the true between-group variation.
  • Omitting a posterior predictive check, which shows whether the model generates data resembling the observed sample.
  • Applying the Normal likelihood from this page to binary or categorical outcomes, for which the formula given here does not apply.
  • Reading a 95% credible interval as a frequentist confidence interval; it is a probability statement about the parameter under the stated model and priors.

Frequently asked questions

How is this different from a classical hierarchical linear model (HLM / mixed-effects model)?

Classical HLM is usually fitted by restricted maximum likelihood (REML), which gives point estimates of the variance components with standard errors. The Bayesian hierarchical model places hyperpriors on those variance components and samples the full joint posterior, so every parameter, including τ², has a credible interval. With small groups the prior helps stabilise the between-group variance estimate; with large, balanced samples the two approaches usually give similar results.

What is partial pooling and why does it matter?

Partial pooling is the middle ground between treating each group as completely independent ('no pooling', high variance) and collapsing all groups into one estimate ('complete pooling', high bias). In the Bayesian hierarchical model, each group's posterior mean is a weighted combination of its own data and the grand mean, with the weight determined by τ² relative to σ²/n_j. Groups with few observations are pooled more strongly; groups with many observations are barely affected. This shrinkage typically reduces prediction error and is the main practical advantage of the hierarchical structure.

How many groups do I need?

There is no strict threshold. As a rule of thumb, with fewer than about five groups the between-group variance τ² is poorly identified and the hyperprior dominates, so results are sensitive to that choice. With more groups the data inform τ² better, but a prior-sensitivity check is worthwhile in any case. There is no strict upper limit on the number of groups.

What hyperprior should I use for the between-group standard deviation τ?

A weakly informative Half-Cauchy prior on τ is a common choice: it places little restriction near zero while its heavy tails do not rule out large between-group variation. Set its scale relative to the units of the outcome and check it with a prior predictive simulation; a scale of about 1 to 5 is only an illustrative default, not a recommendation from the cited sources. An Inverse-Gamma prior on τ² is an alternative. Whichever prior is chosen, re-fit with an alternative hyperprior and check whether the posterior group means shift substantially.

Related methods

Sources

  1. Gelman, A. & Hill, J. (2006). Data Analysis Using Regression and Multilevel/Hierarchical Models. Cambridge University Press.

    Open DOI reference
  2. Gelman, A., Carlin, J. B., Stern, H. S., Dunson, D. B., Vehtari, A. & Rubin, D. B. (2013). Bayesian Data Analysis (3rd ed.). CRC Press.

    ISBN: 978-1439840955