- Introduced by
- Gelman, Hill, and the broader Bayesian hierarchical modeling tradition
- Year
- 1990s–2000s (modern Bayesian MCMC era)
- Type
- Bayesian regression model
- Family
- Regression / GLM
What does it do?
The Bayesian mixed effects model extends the classical mixed effects framework by placing prior distributions on all parameters: the fixed effects, the covariance of the random effects, and the residual variance.
Updating these priors with data yields full posterior distributions, so uncertainty is reported for both population-level and group-level quantities. It is used when observations are grouped or repeated and the analyst wants that uncertainty expressed probabilistically.
The intuition behind the method
Imagine measuring student test scores across many classrooms. A mixed effects model lets each classroom have its own intercept (random effect) while estimating a shared slope for a treatment (fixed effect). The Bayesian version adds the insight that we are never certain about the variance of classroom effects — so it treats that variance itself as an unknown with a prior belief, then revises it based on data. The payoff is that every quantity, including the random effect variances and predictions for new classrooms, comes with a probability distribution rather than a point estimate, giving honest uncertainty bands even when data are sparse.
History
Mixed effects models emerged in biometric and agricultural statistics in the mid-20th century. Bayesian estimation of these models became practical in the 1990s, when MCMC methods such as Gibbs sampling, and later Hamiltonian Monte Carlo, made high-dimensional posteriors computable. Gelman and Hill's 2007 textbook is a widely used treatment of multilevel and Bayesian regression that helped bring the approach to applied fields; the brms and rstanarm packages, released in the 2010s, made Bayesian mixed effects models easier to fit.
When to use it
Choose a Bayesian mixed effects model when observations are grouped or repeated (for example students within schools, repeated measures within patients, or patients within hospitals) and the analysis must report uncertainty for the variance components as well as the coefficients. It is useful when some groups have few observations, because partial pooling pulls their estimates toward the overall distribution, or when prior knowledge from earlier studies should enter the analysis explicitly. It is also the natural choice when you need posterior predictive distributions for new data or new groups. Do not use it when there is no grouping structure for the random effects to model, when the dataset is very large and MCMC runtime would be prohibitive, or when a frequentist lme4 fit gives reliable estimates for the question at hand and the extra Bayesian machinery would add cost without a benefit you need.
Limitations and considerations
- MCMC sampling can take minutes to hours for large datasets or complex random-effect structures, which is a practical constraint when models must be refitted repeatedly.
- When there are few groups, the between-group variance Σ_b is informed largely by its prior, so its posterior can be sensitive to the prior choice and should be checked with a prior sensitivity analysis.
- Specifying the model and diagnosing convergence (R-hat, effective sample size, trace plots) requires more expertise than fitting the equivalent model with a frequentist tool.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Every parameter, including the random-effect variances, receives a full posterior distribution, so interval statements for variance components do not rely on asymptotic approximations.
- Partial pooling through the random-effect distribution shrinks estimates for small groups toward the overall mean, which limits overfitting when group sizes are small or unequal.
- Uncertainty in Σ_b and β propagates into predictions and derived quantities, including predictions for a group that is not in the sample.
- Priors let external information enter the model explicitly and can regularise fixed-effect estimates when the number of predictors is large relative to the data.
Applications
- Educational research: estimating school and classroom effects on student outcomes while controlling for student-level covariates.
- Clinical trials with repeated measures: modelling patient trajectories over time while accounting for between-subject variability.
- Ecology: species abundance models with sites as random effects, where prior information from earlier surveys can inform the site-level variance. Abundance counts call for a count likelihood rather than the Gaussian form described on this page.
- Psycholinguistics: reaction-time experiments with crossed random effects for subjects and items. Reaction times are usually skewed, so a transformed or non-Gaussian likelihood is normally needed.
- Epidemiology: multilevel disease-risk models with patients nested in hospitals, propagating uncertainty about hospital-level variance into predictions. For a binary disease outcome, a logistic likelihood is used within the same random-effect structure.
- Meta-analysis: treating studies as random effects and using informative priors based on historical heterogeneity estimates.
How it works
How it works
For observation i in group j, the outcome is modelled as a fixed-effect part Xβ plus a group-specific deviation Zb_j, with residual error ε_ij. This form assumes a Gaussian outcome: the residuals are ε_ij ~ N(0, σ²), and the group deviations b_j are drawn from a multivariate normal distribution with covariance Σ_b, which is unknown and must be estimated. The residual variance σ² is the parameter that receives the prior described next. If the outcome is binary, a count, or strongly skewed (many reaction times are), the normal likelihood does not apply; a non-Gaussian likelihood with the same random-effect structure is needed, as described in bayesian-generalized-linear-model. The same structure extends to several grouping factors, each with its own random-effect covariance, for example subjects and items in a psycholinguistic experiment. Crossed factors need enough levels of each factor to estimate their variances.
Formula
y_{ij} = X_{ij}\beta + Z_{ij}b_j + \varepsilon_{ij},\quad b_j \sim \mathcal{N}(0, \Sigma_b)Priors are placed on the fixed-effect coefficients β, the random-effect covariance Σ_b, and the residual variance σ². For variance components, weakly informative priors such as half-normal or half-Cauchy distributions on standard deviations are commonly used, keeping estimates in a plausible range without overriding informative data.
Formula
\beta \sim p(\beta),\quad \Sigma_b \sim p(\Sigma_b),\quad \sigma^2 \sim p(\sigma^2)
Bayes' theorem combines the likelihood and the priors into a joint posterior over β, the group effects b, Σ_b and σ². This posterior is proportional to the product of these terms, and because it is high-dimensional it usually cannot be integrated analytically.
Formula
p(\beta, b, \Sigma_b, \sigma^2 \mid y) \propto p(y \mid \beta, b, \sigma^2)\,p(b \mid \Sigma_b)\,p(\beta)\,p(\Sigma_b)\,p(\sigma^2)
Markov chain Monte Carlo draws samples from this posterior. In common software such as brms or rstanarm, which call Stan, the sampler is Hamiltonian Monte Carlo. Sampling quality is checked with R-hat statistics and trace plots before the draws are used.
Posterior summaries such as means, medians and credible intervals are computed from the retained draws. Predictions for new observations or new groups come from the posterior predictive distribution, which carries forward the uncertainty in all parameters, including Σ_b.
Common pitfalls
- Using default flat or improper priors on variance parameters: these can cause convergence failures or unrealistic posteriors — prefer weakly informative half-normal or half-Cauchy priors.
- Ignoring convergence diagnostics (R-hat > 1.01, low bulk or tail ESS) and treating MCMC output as valid when chains have not mixed.
- Treating the posterior mean as a point estimate without reporting credible intervals, losing the main advantage over frequentist models.
- Over-specifying random-effect structures (e.g., full correlation matrices for many random slopes) with insufficient data, causing non-identifiability.
- Forgetting to check the posterior predictive distribution against observed data — a prior–likelihood mismatch can produce a technically converged but poorly fitting model.
Frequently asked questions
How does the Bayesian mixed effects model differ from the frequentist lme4 model?
Both estimate fixed effects and random effects, but the Bayesian version treats all parameters, including variance components, as random variables with prior distributions. This yields full posterior distributions rather than point estimates, and it handles small-group settings more robustly through prior regularisation. The frequentist counterpart, lme4, fits the same kind of structure by maximum likelihood or restricted maximum likelihood (Bates et al., 2015).
Which software packages implement Bayesian mixed effects models?
The most common R options are brms, a high-level interface to Stan, and rstanarm, which provides pre-compiled Stan models. Both use MCMC, typically Hamiltonian Monte Carlo, to draw from the posterior.
How do I choose priors for random-effect standard deviations?
A weakly informative prior, such as a half-normal or half-Cauchy distribution on each standard deviation, is commonly recommended. Such priors keep the variance from taking implausibly large values while letting informative data dominate. Default priors differ between packages and versions, so check the documentation of the software you use and run a sensitivity check with an alternative prior.
What does R-hat tell me about my Bayesian mixed effects model?
R-hat (potential scale reduction factor) compares variance within chains to variance between chains. Values near 1.00 indicate convergence; values above 1.01 suggest chains have not mixed and the posterior samples should not be trusted. Also check bulk and tail effective sample sizes (ESS > 400 is a common threshold).
When should I prefer a Bayesian multilevel model over a simple Bayesian regression?
Whenever observations are not independent, because they share a group (classroom, patient, site), ignoring that grouping inflates Type I error and underestimates uncertainty. The random effects account for within-group correlation, and the Bayesian framework quantifies how much variance is attributable to group differences.
Related methods
Sources
Gelman, A., & Hill, J. (2007). Data Analysis Using Regression and Multilevel/Hierarchical Models. Cambridge University Press.
ISBN: 978-0521686891Bates, D., Mächler, M., Bolker, B., & Walker, S. (2015). Fitting Linear Mixed-Effects Models Using lme4. Journal of Statistical Software, 67(1), 1–48.
Open DOI reference