Vasco Yasenov
  • About Me
  • CV
  • Blog
  • Research
  • Software
  • Others
    • Methods Map
    • Kids Books
    • TV Series Ratings

On this page

  • Background
  • Notation
  • A Closer Look
    • What Makes a GAM?
    • How Smoothness Is Estimated
    • Why Rank Reduction Matters
    • Beyond the Mean
    • Hypothesis Testing
  • An Example
  • Bottom Line
  • Where to Learn More
  • References

Generalized Additive Models: What You Need to Know

regression
statistical inference
Published

February 12, 2026

Background

Generalized Additive Models (GAMs) are one of the most powerful and flexible tools in a data scientist’s toolbox for modeling complex, nonlinear relationships between covariates and an outcome. They generalize linear models by allowing smooth, nonparametric functions of the predictors while still maintaining interpretability and manageable computation. The core idea is instead of forcing relationships to be straight lines, let the data speak for itself.

This article explains what you really need to know about GAMs, following the excellent review by Simon Wood (2025). We’ll go over the basics of how GAMs work, how smoothness is controlled, the computational strategies involved, and key pitfalls to watch out for. We’ll also walk through a code example in both R and Python to show how to fit and interpret these models in practice.

Notation

Consider an outcome variable \(y\) and predictors \(x_1, x_2, \dots, x_p\). The simplest linear model is:

\[ y = \beta_0 + \sum_{j=1}^p \beta_j x_j + \varepsilon. \]

The GAM replaces the linear terms \(\beta_j x_j\) with smooth functions \(f_j(x_j)\):

\[ y = \beta_0 + \sum_{j=1}^p f_j(x_j) + \varepsilon. \]

More generally, for non-Gaussian outcomes, GAMs use a link function \(g(\cdot)\):

\[ g(\mathbb{E}[y]) = \beta_0 + \sum_{j=1}^p f_j(x_j). \]

Each \(f_j\) is estimated from the data and constrained to be “smooth” through penalization.

A Closer Look

What Makes a GAM?

The backbone of a GAM is its smooth terms. These are typically represented using splines — basis functions that piece together polynomials smoothly at specified knots. But not just any spline will do! In GAMs, smoothness is enforced through penalty terms that discourage excessive wiggliness.

For example, for a cubic spline, the penalty is usually the integral of the squared second derivative:

\[ \int (f''(x))^2 \, dx. \]

In coefficient form, estimation solves

\[ \min_{f_1,\dots,f_p} \left\{ \sum_{i=1}^n \left(y_i - \beta_0 - \sum_{j=1}^p f_j(x_{ij})\right)^2 + \sum_{j=1}^p \lambda_j \int \left(f_j''(x)\right)^2 \, dx \right\}, \]

where \(S_j\) encodes the roughness penalty and \(\lambda_j\) is the smoothing parameter.

Everything in a GAM flows from this penalized least-squares (or penalized likelihood) objective. The balance between fitting the data and keeping the function smooth is controlled by smoothing parameters (\(\lambda\)). This is regularization: in particular, the standard spline roughness penalties are quadratic (ridge-like). A higher \(\lambda\) makes the function flatter; a lower \(\lambda\) allows more flexibility.

How Smoothness Is Estimated

Model selection in GAMs involves three related but distinct questions:

  • How smooth should each function be? (smoothing parameter selection, \(\lambda\))
  • How flexible is the basis? (choice of basis dimension \(k\))
  • Which smooth terms should be included at all? (term selection, \(f_j\))

The basis dimension \(k\) controls the maximum possible flexibility (how rich the spline basis is), while the smoothing parameter \(\lambda\) controls how much of that flexibility is actually used. Intuitively, \(k\) sets the size of the function space you search over; \(\lambda\) determines the effective degrees of freedom (wiggliness) within that space. In practice, you choose \(k\) “large enough” and let \(\lambda\) do the regularization; if \(k\) is too small, the smooth can be forced to underfit no matter how you tune \(\lambda\).

There are two main strategies to estimate \(\lambda\):

  1. Cross-validation (CV): Minimize prediction error by holding out parts of the data. You are familiar with this from traditional machine learning models.
  2. Marginal likelihood (REML): An empirical Bayes approach that tends to perform well in practice.

The marginal likelihood approach treats smooth coefficients as random effects with Gaussian priors (a mixed-model representation), and often yields better-behaved uncertainty quantification than ad hoc tuning.

Similarly, there are two common tools for model selection. The well-known Akaike Information Criterion (AIC) controls the trade-off between goodness of fit and model complexity. Alternatively, one can employ hypothesis testing to check whether each \(f_j\) is significantly different from zero.

With \(\lambda\), \(k\), and \(f_j\) selected, we can fit the GAM and make predictions. Let’s shift the focus on a few more nuianced, but important, topics.

Why Rank Reduction Matters

Full spline bases can be large and computationally expensive. To address this, GAMs often use low-rank spline bases (e.g., thin plate regression splines): you represent each smooth with a modest number of basis functions (controlled by \(k\)), rather than using a very large “full” basis. This keeps computation tractable while retaining most of the flexibility practitioners want. Consequently, GAM fitting scales better to larger datasets while preserving interpretability.

Beyond the Mean

GAMs aren’t limited to modeling the mean and naturally extend to modeling other aspects of the distribution. They can handle location, scale, and shape modeling — meaning that the variance, skewness, or other distributional parameters can also depend on smooth functions of predictors. This generalization brings GAMs into the world of generalized additive models for location, scale, and shape (GAMLSS).

They can even be extended to quantile regression and non-exponential family distributions, making them incredibly versatile. However, while GAMs allow flexible modeling of conditional expectations, they do not by themselves address common thorny issues such as endogeneity, causal identification, or selection bias. They simply allow for more depth in modeling the relationship between the outcome and the covariates and thus should be utilized in the context of machine learning/prediction.

Hypothesis Testing

Testing whether a smooth term is zero corresponds to testing whether its associated function is identically zero. Because smooth terms are penalized, the effective degrees of freedom are estimated from the data, and the resulting test statistics rely on large-sample approximations. The reported \(p\)-values are therefore approximate and should be interpreted as heuristic diagnostics rather than exact finite-sample guarantees.

An Example

  • R
  • Python
library(mgcv)
set.seed(1988)
n <- 200
x <- runif(n, 0, 10)
y <- sin(x) + rnorm(n, 0, 0.3)
model <- gam(y ~ s(x), method = "REML")
summary(model)
plot(model, residuals = TRUE)
import numpy as np
import statsmodels.api as sm
import matplotlib.pyplot as plt
from statsmodels.gam.api import GLMGam, BSplines

np.random.seed(1988)
n = 200
x = np.random.uniform(0, 10, n)
y = np.sin(x) + np.random.normal(0, 0.3, n)

# Build a cubic B-spline basis for x
X = x[:, None]
bs = BSplines(X, df=[10], degree=[3], knot_kwds=[{"lower_bound": x.min(), "upper_bound": x.max()}])

# Gaussian GAM (identity link) via the GLM-GAM interface
exog = np.ones((n, 1))  # intercept only
gam = GLMGam(y, smoother=bs, exog=exog).fit()
print(gam.summary())

plt.figure()
XX = np.linspace(x.min(), x.max(), 200)[:, None]
exog_pred = np.ones((len(XX), 1))
plt.plot(XX[:, 0], gam.predict(exog=exog_pred, exog_smooth=XX), label="GAM fit")
plt.scatter(x, y, alpha=0.3)
plt.legend()
plt.show()

Bottom Line

  • GAMs allow flexible, nonlinear modeling while retaining interpretability.
  • Smoothness is controlled by penalties, estimated via CV or marginal likelihood (REML).
  • Rank reduction makes GAMs computationally feasible even with large datasets.
  • GAMs generalize beyond means to scale, shape, and quantile modeling.

Where to Learn More

The recent review by Simon Wood (2025) is the most comprehensive and readable guide to modern GAMs. For practical hands-on work, Wood’s book Generalized Additive Models: An Introduction with R (2017) remains the go-to resource. See also Hastie (2017). For Bayesian extensions check Rue et al. (2009).

References

  • Hastie, T. J. (2017). Generalized additive models. Statistical models in S, 249-307.

  • Rue, H., Martino, S., & Chopin, N. (2009). Approximate Bayesian inference for latent Gaussian models by using integrated nested Laplace approximations. Journal of the Royal Statistical Society Series B: Statistical Methodology, 71(2), 319-392.

  • Wood, S. N. (2025). Generalized Additive Models. Annual Review of Statistics and Its Application, 12, 497–526.

  • Wood, S. N. (2017). Generalized Additive Models: An Introduction with R. CRC Press.

© 2025 Vasco Yasenov

 

Powered by Quarto