paul-buerkner/brms
brms R package for Bayesian generalized multivariate non-linear multilevel models using Stan
📦 brms – Bayesian Regression Models using Stan (R)
What it is
- An R package that lets you fit Bayesian generalized (non‑linear) multilevel models by writing the model in a familiar lme4‑style formula and automatically translating it to Stan code.
- Stan does the heavy lifting: it runs Hamiltonian Monte Carlo (NUTS) to draw samples from the posterior.
Why it matters
- Gives statisticians and data scientists a high‑level, user‑friendly interface to sophisticated Bayesian inference without having to write Stan code by hand.
- Supports a huge variety of response families (Gaussian, Poisson, binomial, ordinal, zero‑inflated, custom mixtures, etc.) and model components (smooth terms, autocorrelation, censored data, missing‑value imputation, distributional regression, multivariate responses).
- Flexible prior specification and built‑in tools for model checking, cross‑validation, and Bayes‑factor comparison.
Key features (as described in the README)
- Formula syntax similar to
lme4→ low learning curve for R users. - Automatic generation of Stan code (
stancodemethod) and compilation. - Multilevel (hierarchical) modeling with arbitrary grouping structures.
- Non‑linear, spline (
s,t2) and smooth terms via mgcv. - Posterior predictive checks,
loo/kfoldcross‑validation, Bayes factors. - Visualization helpers (
plot,conditional_effects, integration with bayesplot, ggplot2). - Compatibility with rstan, rstanarm, bridgesampling, loo, bayesplot, mgcv, etc.
Typical workflow (excerpt from README)
library(brms)
# Fit a Poisson multilevel model
fit1 <- brm(count ~ zAge + zBase * Trt + (1|patient),
data = epilepsy, family = poisson())
# Summarise posterior draws
summary(fit1)
# Visualise coefficients
plot(fit1, variable = c("b_Trt1", "b_zBase"))
# Predict on new data (ignoring random effects)
newdata <- data.frame(Trt = c(0,1), zAge = 0, zBase = 0)
predict(fit1, newdata = newdata, re_formula = NA)
The README also shows how to compare two models with approximate leave‑one‑out cross‑validation (loo).
Installation
- Stable CRAN release:
install.packages("brms") - Development version:
remotes::install_github("paul-buerkner/brms") - Requires a C++ compiler (Rtools on Windows, Xcode on macOS) because Stan must be compiled.
Documentation & learning resources
- Official website with vignettes: https://paulbuerkner.com/brms/
- Introductory paper (JSS) and advanced multilevel modeling paper (R Journal).
- Blog posts, Stan Discourse forum, and GitHub issue tracker for help.
Citation When using brms, cite the 2017 JSS paper (Bürkner) and the Stan papers, plus any auxiliary packages you employ.
Bottom line
brms is a mature, widely‑used bridge between R’s regression formula ecosystem and Stan’s powerful Bayesian inference engine, making state‑of‑the‑art Bayesian multilevel modeling accessible to a broad audience.
Related
- Project
- Project
- Project
- Project
- Project