- Introduced by
- Hastie & Tibshirani (GAM framework, 1990); Bayesian formulation developed through work by Wood, Fahrmeir, Lang, and others
- Year
- 1990s–2000s
- Type
- Semiparametric Bayesian regression
- Family
- Regression / GLM
What does it do?
A Bayesian Generalized Additive Model (Bayesian GAM) places prior distributions on the smooth functions and other parameters of a generalized additive model.
Each nonlinear effect is estimated as a posterior distribution rather than a single fitted curve, so uncertainty about the shape of every effect is reported directly. The degree of smoothing is governed by variance hyperparameters that are learned from the data, and the model can be extended to hierarchical or mixed-effects structures.
The intuition behind the method
A classical GAM lets each predictor have its own flexible curve rather than a rigid straight line, and then sums these curves to predict the outcome. The Bayesian version treats each curve as a random function drawn from a Gaussian process prior. The data update these priors into posteriors, so the output is not only a best-fitting curve for each predictor but a distribution over plausible curves. This shows how certain or uncertain each nonlinear relationship is. The amount of smoothing is set by variance hyperparameters that are estimated from the data.
History
Generalized Additive Models were introduced by Hastie and Tibshirani in their 1990 monograph as a flexible extension of GLMs, in which the linear predictor is replaced by a sum of smooth functions. Bayesian formulations of semiparametric regression, which place priors on these smooth functions, developed within the statistics literature; Bayesian nonparametric regression is covered in Bayesian Econometrics (p. 236–43). Software now supports fitting such models in a Bayesian framework: brms fits Bayesian multilevel models in Stan across a wide range of distributions, link functions and non-linear model structures (Bürkner, 2017), and R-INLA implements integrated nested Laplace approximation for approximate Bayesian inference.
When to use it
Choose a Bayesian GAM when you expect nonlinear relationships between predictors and the outcome, the response is non-Gaussian, and you need uncertainty about each smooth effect rather than only a point estimate. It is particularly useful when prior information about smoothness is available, when the sample is moderate in size, or when the fitted model will feed decisions that require calibrated uncertainty. It also suits settings where the smooth terms must sit inside a larger hierarchical structure. Do not choose it when the relationship is expected to be genuinely linear and the interpretation of additive slopes is the main goal; a Bayesian GLM is simpler and faster in that case. Avoid it when computing resources are very limited, because MCMC sampling for models with many smooth terms is considerably slower than frequentist REML estimation.
Limitations and considerations
- MCMC inference is computationally expensive, and models with many smooth terms or large datasets can be slow to sample.
- Smooth curves are harder to interpret than linear coefficients, so communicating nonlinear effects to non-technical audiences requires careful visualisation.
- In small samples, the prior on the smoothing variances and the choice of response distribution can noticeably influence the estimated curves, so sensitivity analysis is advisable.
- Identifiability constraints, such as sum-to-zero conditions, must be imposed on smooth terms to prevent confounding with the intercept.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Captures nonlinear predictor effects without fixing a parametric form, and can be applied to a range of response distributions through the choice of link function.
- Full posterior distributions over each smooth give credible intervals that reflect uncertainty in the smoothing parameters as well as in the coefficients.
- Smoothing variances are learned from the data through hyperpriors, so the degree of smoothing need not be fixed in advance; the basis dimension, however, must still be set generously and checked, as the pitfalls and FAQ describe.
- Fits naturally within hierarchical and mixed-effects models, allowing random smooth effects across groups.
- Posterior predictive checks give a direct way to assess model criticism and fit.
Applications
- Environmental science: estimating nonlinear dose-response curves relating pollution levels to health outcomes with full uncertainty bands.
- Epidemiology: modelling time trends in disease incidence while accounting for seasonal nonlinearity and overdispersion.
- Ecology: species distribution models with nonlinear climate covariate effects fitted to count or presence-absence data.
- Economics: estimating nonparametric Engel curves or income-consumption relationships where linearity is implausible.
- Neuroscience: modelling EEG or fMRI signal as smooth functions of time or stimulus intensity with credible envelopes.
- Public health: projecting hospitalization rates as smooth functions of calendar time during epidemics.
How it works
How it works
The linear predictor is an additive sum of smooth functions f_j, one for each covariate. It is linked to the outcome's conditional mean through a link function g that suits the response distribution, such as the identity for a Gaussian response, the logit for a binary response, or the log for counts.
Formula
g(\mu_i) = \alpha + \sum_{j=1}^{p} f_j(x_{ij})Each smooth function f_j is assigned a zero-mean Gaussian process prior, scaled by a smoothing variance tau_j^2 and a penalty matrix K_j, such as the penalty derived from a thin-plate spline or B-spline basis. The penalty expresses the belief that nearby values of f_j should be similar, which penalises overly wiggly curves.
Formula
f_j \sim \mathcal{GP}(0,\, \tau_j^2 K_j)The smoothing variances tau_j^2 are given their own priors, commonly weakly informative half-Cauchy or inverse-gamma distributions. The degree of smoothing is therefore learned from the data rather than fixed in advance.
Formula
\tau_j^2 \sim \pi(\tau_j^2)
The joint posterior over all smooth functions and smoothing variances is proportional to the likelihood multiplied by the smooth priors and the hyperpriors. It summarises the uncertainty about all nonlinear effects at once.
Formula
p(\mathbf{f}, \boldsymbol{\tau} \mid \mathbf{y}) \propto p(\mathbf{y} \mid \mathbf{f})\, p(\mathbf{f} \mid \boldsymbol{\tau})\, p(\boldsymbol{\tau})Posterior inference is carried out by Hamiltonian Monte Carlo, for example through Stan or brms, or by integrated nested Laplace approximation (INLA). Both routes target the joint posterior p(f, tau | y) from the posterior step: MCMC approximates it with posterior draws, while INLA approximates the marginal posteriors directly. Posterior predictive checks assess model fit, and posterior summaries such as means and credible intervals for each smooth are plotted as effect curves with uncertainty bands.
Common pitfalls
- Choosing a basis dimension (number of knots) too small to capture the true wiggliness. The basis dimension should be set generously, with the smoothness prior doing the regularisation.
- Skipping posterior predictive checks. A curve that looks smooth can still fit poorly in regions where data are sparse.
- Treating INLA credible intervals as equivalent to MCMC-based posteriors without checking that the Laplace approximation is adequate for the data.
- Reading a posterior correlation between two smooth terms as evidence of an interaction when no interaction term was included in the model.
Frequently asked questions
How does a Bayesian GAM differ from a frequentist GAM fitted by REML?
Frequentist GAMs estimate smooth functions by penalised likelihood and select smoothing parameters by restricted maximum likelihood (REML), producing point estimates and approximate confidence intervals. Bayesian GAMs place priors on the smooth functions and smoothing variances, yielding a full posterior distribution. Bayesian credible intervals can reflect uncertainty in the smoothing parameters themselves, whereas frequentist confidence intervals are often computed conditional on the estimated smoothing parameter.
What software fits Bayesian GAMs?
The brms R package fits Bayesian multilevel models through Stan, and smooth terms can be included in its model formulas. The mgcv package is a widely used frequentist GAM tool in R that estimates smooth terms by penalised likelihood and selects smoothing parameters by REML or ML, so it is a penalised-likelihood tool rather than a fully Bayesian sampler. R-INLA uses integrated nested Laplace approximation for fast approximate Bayesian inference on GAMs. PyMC is a further probabilistic programming option, and Gaussian process components can in principle represent smooth terms; this is offered as an example of what is possible rather than a capability verified against the sources.
How do I choose the basis dimension (number of knots)?
Set the basis dimension generously, since the smoothness prior shrinks the function toward a simpler form when the data support it. After fitting, run a basis adequacy check, such as the k-check available in mgcv, to confirm that the chosen dimension is not limiting flexibility.
Can a Bayesian GAM handle random effects?
Yes. Random intercepts and slopes for grouped data can be written as smooth terms with a ridge-type penalty, which corresponds to an identity covariance. Bayesian GAMs in brms or INLA can include such terms in the same model as nonlinear smooth terms.
When is INLA preferable to MCMC for Bayesian GAMs?
INLA is much faster and scales better to large datasets, which makes it practical when MCMC would be prohibitively slow. However, INLA relies on a Laplace approximation that can be inaccurate for highly non-Gaussian or heavy-tailed posteriors. MCMC, through Stan or brms, is more reliable when the posterior is irregular or when exact credible intervals are critical.
Related methods
Sources
Wood, S. N. (2017). Generalized Additive Models: An Introduction with R (2nd ed.). CRC Press.
ISBN: 9781498728331Bürkner, P.-C. (2017). brms: An R Package for Bayesian Multilevel Models Using Stan. Journal of Statistical Software, 80(1), 1–28.
Open DOI referenceBayesian Econometrics, p. 236–43