Bayesian Linear Regression

Author

Mark Andrews

Abstract

In-depth coverage of Bayesian linear regression using brms. We work through models with multiple predictors, examine MCMC diagnostics, explore the posterior distribution using the tools in brms and bayesplot, and look at prior specification and sensitivity.

Fitting a linear model

The brm function takes the same formula interface as lm. For a model of weight from height and gender:

The summary output from brm reports the posterior mean, standard deviation, and credible intervals for each parameter. The Rhat column is a convergence diagnostic: values close to 1.0 indicate that the chains have converged. The Bulk_ESS and Tail_ESS columns report effective sample size, measuring how many independent samples the chains are equivalent to.

Visualising MCMC output

brms provides mcmc_plot for a range of posterior visualisations.

Trace plots show the sampling path of each chain over iterations. A well-mixed chain looks like a horizontal fuzzy caterpillar with no trends or stuck periods. When multiple chains are run, they should all overlap in the same region.

Working with posterior samples directly

The function as_draws_df extracts all MCMC samples as a data frame. This gives direct access to the posterior distribution and allows any custom summary to be computed.

Fixed effects summaries

The fixef function returns a tidy table of posterior summaries for the fixed effect parameters.

MCMC diagnostics in depth

The trace plot is the primary visual diagnostic. For a model with four chains, each chain should produce a separate trace that mixes freely with the others. Chains that do not mix indicate poor convergence, typically caused by a poorly specified prior, identifiability problems, or an overly complex model.

Rhat summarises convergence numerically. Values above 1.01 suggest the chains have not converged. Effective sample size measures the information content of the MCMC output: a chain of 4000 samples with high autocorrelation may be equivalent to only a few hundred independent samples.

The joint posterior distribution for two parameters can be visualised using mcmc_plot with type = "scatter" or by plotting the draws directly. This shows correlations between parameters that the marginal summaries do not capture.

Inspecting the underlying Stan code

brms translates the model specification to Stan code, which is then compiled and run. Inspecting this code is useful for understanding what brms is actually fitting.

prior_summary shows what prior distributions brms has assigned to each parameter by default. get_prior shows the prior structure for a model formula before fitting.