- Introduced by
- Gelman & Hill (2006); Raudenbush & Bryk (2002) for frequentist HLM; Bayesian treatment consolidated by Gelman et al.
- Year
- 2006
- Type
- Bayesian multilevel linear model
- Family
- Regression / GLM
What does it do?
The Bayesian Hierarchical Linear Model (Bayesian HLM) estimates linear relationships in nested or clustered data by placing prior distributions on all model parameters and updating them with observed data.
It models variation within groups and between groups at the same time and expresses uncertainty as posterior distributions over the parameters, rather than as sampling-distribution approximations based on large-sample theory. In practice these posteriors are usually obtained by sampling, and variational approximations, when used, yield approximate rather than exact posteriors.
The intuition behind the method
Think of data where students are nested within classrooms, which are nested within schools. A Bayesian HLM fits a regression line for each group, but instead of treating each group as completely independent, it pools information across groups through prior distributions. Groups with little data borrow strength from the overall pattern, while groups with abundant data stay closer to their own estimates. Because the model is Bayesian, every coefficient and variance component comes with a full posterior distribution rather than only a point estimate and a standard error, so uncertainty is reported for small groups and sparse data as well as large ones.
History
Hierarchical linear models have a long frequentist tradition, in which variance components are estimated by maximum likelihood or restricted maximum likelihood (REML). The Bayesian treatment is associated with Gelman and colleagues: Gelman and Hill (2006) cover multilevel regression. Probabilistic programming languages such as WinBUGS, JAGS and Stan later made these models practical for applied researchers.
When to use it
Choose Bayesian HLM when the outcome is continuous and the observations are nested or clustered, for example students in classrooms, patients in hospitals, or repeated measures per subject. It is especially useful when group sizes are unequal or some groups are small, because partial pooling gives more stable group estimates than fitting a separate regression to each group. Choose the Bayesian version when you need probability statements about parameters, want to include prior information, or need predictions with credible intervals. Do not use it when observations are not genuinely grouped, when the outcome is not continuous (the model here is a normal linear model), or when a frequentist mixed model answers the question and the extra sampling cost is not justified, for instance when there are only a few large, balanced groups whose effects are already well determined.
Limitations and considerations
- Sampling is computationally expensive for large datasets or many groups, because each group deviation u_0j is an additional parameter that must be sampled.
- Estimates of τ² can be sensitive to the prior when the number of groups is small, since the data carry limited information about the spread of the group effects.
- The model assumes normally distributed group deviations u_0j and normally distributed residuals ε_ij; departures from these assumptions are not corrected automatically and must be checked, for example with posterior predictive checks.
- Model checking, convergence diagnostics and prior sensitivity analysis require more expertise than fitting a standard mixed-effects model.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Partial pooling: each group intercept β_0j is shrunk toward the grand mean γ_00 by an amount that depends on the between-group variance τ² and on the group's own data, which reduces noise in small groups.
- The between-group variance τ² and the within-group variance σ² receive their own posterior distributions, so uncertainty about how much groups differ is reported directly rather than only as a point estimate.
- Credible intervals can be read as probability statements about the parameters given the model and priors, unlike frequentist confidence intervals.
- Prior information can enter through the priors on γ, σ² and τ², which helps when the data are sparse.
- The same probabilistic framework supports more complex variance structures, such as cross-level interactions, within one model.
Applications
- Educational research: modelling student achievement nested within classrooms and schools while accounting for school-level heterogeneity.
- Clinical trials with multiple sites: estimating treatment effects while allowing site-specific baselines and borrowing strength across centres.
- Longitudinal studies: modelling repeated measurements per subject as level-1 units with subject-level random intercepts and slopes.
- Ecology: species-level responses nested within habitats, with partial pooling across sparse species observations.
- Survey research: multilevel modelling of respondents nested within regions or sampling clusters.
- Meta-analysis: combining study-level estimates under a hierarchical prior that allows heterogeneity across studies.
How it works
How it works
At level 1, the outcome y for observation i in group j is a linear function of the predictor x plus a normally distributed residual with variance σ². The likelihood allows the slope β_1j to differ by group, so each group can have its own line, while the residual variance σ² is common to all observations. Whether the slope actually varies is set by the level-2 specification described below.
Formula
y_{ij} = \beta_{0j} + \beta_{1j} x_{ij} + \varepsilon_{ij}, \quad \varepsilon_{ij} \sim \mathcal{N}(0, \sigma^2)At level 2, the group intercept β_0j is modelled as the grand mean γ_00 plus a group deviation u_0j drawn from a normal distribution with variance τ². This partial-pooling structure pulls group intercepts toward γ_00, and the pull is stronger when τ² is small or when a group has little data. The slope β_1j needs its own level-2 specification. Either it is fixed (β_1j = γ_10, giving a random-intercept model with a common slope), or it is written as β_1j = γ_10 + u_1j with (u_0j, u_1j) jointly normal, which gives a random-slope model.
Formula
\beta_{0j} = \gamma_{00} + u_{0j}, \quad u_{0j} \sim \mathcal{N}(0, \tau^2)Prior distributions are placed on all hyperparameters: the fixed coefficients γ, the within-group standard deviation σ and the between-group standard deviation τ (equivalently the variances σ² and τ²). Coefficients are commonly given normal priors centred at zero with a wide scale, and the standard deviations are given half-normal or half-Cauchy priors. Weakly informative priors are generally preferred to flat priors on variance components, because flat priors can yield improper posteriors. Default prior scales such as half-Cauchy(0, 2.5) assume that the outcome and predictors are standardised. When the data are in their original units, the prior scales should be adjusted to the units of the outcome, and the choice should be stated and justified.
Formula
p(\gamma, \sigma^2, \tau^2) \propto \text{prior distributions on all hyperparameters}Bayes' theorem combines the likelihood with the priors to give the joint posterior over the group effects u, the coefficients γ, and the variances σ² and τ². This posterior summarises what the data and priors jointly imply about each parameter, including every group-specific intercept.
Formula
p(\gamma, u, \sigma^2, \tau^2 \mid y) \propto p(y \mid \gamma, u, \sigma^2)\, p(\gamma, \sigma^2, \tau^2)
The joint posterior generally has no closed form, so samples are drawn with Markov chain Monte Carlo (MCMC), or the posterior is approximated with variational methods. Variational methods are faster but give approximate results rather than exact posteriors. Posterior means, medians and credible intervals are reported together with convergence diagnostics such as R-hat and effective sample size.
Common pitfalls
- Placing flat or improper priors on the variance components τ² or σ², which can yield improper posteriors or poor sampling behaviour. Weakly informative half-normal or half-Cauchy priors on standard deviations are safer defaults, but their scale must match the units of the outcome. The default scale of half-Cauchy(0, 2.5) assumes standardised data.
- Ignoring convergence diagnostics: R-hat values above 1.01 or low effective sample sizes indicate that the MCMC chains have not mixed well, and the reported estimates are then unreliable.
- Conflating credible intervals with frequentist confidence intervals in reporting. Their interpretations differ and should be stated precisely.
- Adding levels or group-specific slopes without a theoretical reason. Each addition increases the number of parameters to sample and makes the model harder to interpret.
- Skipping posterior predictive checks, which can hide misspecification of the normal residual or group-effect assumptions that residual inspection would often reveal in a frequentist analysis.
Frequently asked questions
What is the difference between Bayesian HLM and a frequentist mixed-effects model?
Both use a hierarchical / multilevel structure, but a frequentist mixed-effects model estimates variance components by maximum likelihood (REML) and provides asymptotic confidence intervals. Bayesian HLM places priors on all parameters and produces posterior distributions under the stated model and priors. These posteriors are usually approximated by MCMC, with convergence checks, or by faster variational methods that give approximate results. The Bayesian approach allows prior knowledge to be incorporated, but its conclusions still depend on the model assumptions and priors.
How do I choose priors for the variance components?
Weakly informative priors are usually safest: a half-normal(0, 1) or half-Cauchy(0, 2.5) on standard deviation parameters keeps them positive and lets the data dominate while avoiding implausibly large values. These scales assume standardised data. On the original scale of the outcome, set the scale to match its units and justify the choice. Avoid flat or uniform priors on variances, as they can produce improper posteriors when group sizes are small.
How many groups do I need for a Bayesian HLM to work well?
There is no fixed minimum number of groups. With a small number of groups, the data carry little information about the between-group variance τ², so the prior on τ² has a larger influence on the results. Weakly informative priors help stabilise estimation, but they cannot make a very small number of groups informative about the spread of group effects. When the number of groups is small, report how sensitive the conclusions are to the prior, and consider a fixed-effects or simpler model if the grouping structure cannot support estimating τ².
What software can I use to fit a Bayesian HLM?
Stan (via rstan or brms in R, or CmdStanPy in Python) is the most widely recommended, offering the No-U-Turn Sampler (NUTS) for efficient MCMC. brms provides a formula-based interface similar to lme4, making the transition from frequentist HLM straightforward. PyMC is a popular Python alternative.
How do I assess model fit in a Bayesian HLM?
Use posterior predictive checks to compare simulated data from the fitted model against the observed data. Information criteria such as WAIC or LOO-CV (leave-one-out cross-validation via the loo package) allow comparison across candidate models. Also inspect R-hat and effective sample size to confirm MCMC convergence.
Related methods
Sources
Gelman, A., & Hill, J. (2006). Data Analysis Using Regression and Multilevel/Hierarchical Models. Cambridge University Press.
ISBN: 978-0521686891Gelman, 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