Additive Models

an additive model replaces each linear term \(\beta_j x_j\) of linear regression with an unknown smooth function \(f_j(x_j)\), and adds them up. a generalised additive model (gam) does the same inside the link of a generalised linear model. the single-predictor smoother is the building block and lives on splines and smoothing; this page is about what changes when there are several smooths in one model: identifiability, one penalty per term, choosing several smoothing parameters at once, and the non-gaussian fit. 𐃏

the recipe in one line: give each smooth a basis and a penalty, fit by penalised least squares (additive model) or penalised irls (gam), and pick the penalties by generalised cross-validation.

the additive model

Definition (Additive model)

with two covariates \(x\) and \(v\), \[ y_i = \beta_0 + f_1(x_i) + f_2(v_i) + \varepsilon_i, \qquad \varepsilon_i \overset{\text{iid}}{\sim} N(0, \sigma^2), \quad i = 1, \dots, n, \] with \(f_1, f_2\) smooth and unknown. more predictors add more terms in the same way.

additivity is a strong assumption: \(f_1(x) + f_2(v)\) is a very special surface. the interaction \(xv\) is not of this form, since every additive surface has \(\partial^2 / \partial x \partial v = 0\) while \(xv\) has mixed derivative \(1\).

Remark (Non-identifiability)

for any constant \(c\), \((\beta_0, f_1 + c, f_2 - c)\) gives exactly the same fitted values as \((\beta_0, f_1, f_2)\); constants can also be traded with \(\beta_0\). the data cannot separate them, so least squares has no unique minimiser until a constraint pins each \(f_j\) vertically.

basis and penalty for each term

the notes use the most transparent basis: piecewise linear “tent” functions on \(k_1\) evenly spaced knots \(x_1^* < \dots < x_{k_1}^*\) with spacing \(h\), \(b_j(x) = \max(0, 1 - |x - x_j^*|/h)\), so that \[ f_1(x) = \sum_{j=1}^{k_1} b_j(x)\, \delta_j, \qquad f_2(v) = \sum_{j=1}^{k_2} \mathcal{B}_j(v)\, \gamma_j . \] two properties carry everything below. interpolation: \(b_i(x_j^*) = 1\) if \(i = j\) and \(0\) otherwise, so \(\delta_j = f_1(x_j^*)\) and \(f_1\) is the broken line through the points \((x_j^*, \delta_j)\). partition of unity: \(\sum_j b_j(x) = 1\) on the knot range. at the data, \(\mathbf{f}_1 = \mathbf{X}_1 \boldsymbol\delta\) with \([\mathbf{X}_1]_{ij} = b_j(x_i)\). 𐃏

the roughness penalty is the sum of squared second differences of the knot values, \[ \boldsymbol\delta^\top \mathbf{S}_1 \boldsymbol\delta = \|\mathbf{D}_1 \boldsymbol\delta\|^2 = \sum_{j=2}^{k_1-1} (\delta_{j-1} - 2\delta_j + \delta_{j+1})^2\,, \] with \(\mathbf{D}_1\) the \((k_1 - 2) \times k_1\) second-difference matrix. it is zero exactly when \(f_1\) is a straight line. any other basis–penalty pair (cubic regression splines, thin plate splines; see splines and smoothing) slots into the same algebra.

the identifiability constraint

Proposition (Sum-to-zero constraint by centring)

impose \(\sum_{i=1}^n f_1(x_i) = 0\) by centring the columns of the basis matrix over the data, \[ \hat{\mathbf{X}}_1 = \mathbf{X}_1 - \tfrac{1}{n} \mathbf{1}\mathbf{1}^\top \mathbf{X}_1, \qquad \hat{\mathbf{X}}_1 \boldsymbol\delta = \mathbf{f}_1 - c\,\mathbf{1}, \quad c = \tfrac1n \textstyle\sum_i f_1(x_i). \] the fitted curve keeps its shape and is only shifted vertically (the intercept absorbs \(c\)). centring drops the rank of the basis matrix from \(k_1\) to \(k_1 - 1\), so one coefficient is set to zero (one column dropped).

of all the linear constraints that would work, sum-to-zero gives the narrowest confidence bands: it pins the curve where the data are rather than at one end. the visible consequence is the “bow-tie” band of a term estimated as a straight line (see the mgcv example below).

penalised least squares

stack the constrained bases into one design, \(\mathbf{X} = (\mathbf{1}, \mathbf{X}_1, \mathbf{X}_2)\) with \(\boldsymbol\beta = (\beta_0, \boldsymbol\delta^\top, \boldsymbol\gamma^\top)^\top\), and pad each penalty with zeros (\(\bar{\mathbf{S}}_1, \bar{\mathbf{S}}_2\)) so it acts on \(\boldsymbol\beta\) but touches only its own block.

Theorem (Penalised least squares for an additive model)

the minimiser of \[ \|\mathbf{y} - \mathbf{X}\boldsymbol\beta\|^2 + \lambda_1 \boldsymbol\beta^\top \bar{\mathbf{S}}_1 \boldsymbol\beta + \lambda_2 \boldsymbol\beta^\top \bar{\mathbf{S}}_2 \boldsymbol\beta \] is \[ \hat{\boldsymbol\beta} = (\mathbf{X}^\top\mathbf{X} + \lambda_1 \bar{\mathbf{S}}_1 + \lambda_2 \bar{\mathbf{S}}_2)^{-1} \mathbf{X}^\top \mathbf{y}\,, \] with hat matrix \[ \mathbf{H} = \mathbf{X}(\mathbf{X}^\top\mathbf{X} + \lambda_1 \bar{\mathbf{S}}_1 + \lambda_2 \bar{\mathbf{S}}_2)^{-1}\mathbf{X}^\top\,. \] equivalently, \(\hat{\boldsymbol\beta}\) is ordinary least squares on the augmented data \[ \tilde{\mathbf{y}} = \begin{bmatrix} \mathbf{y} \\ \mathbf{0} \end{bmatrix}, \qquad \tilde{\mathbf{X}} = \begin{bmatrix} \mathbf{X} \\ \mathbf{B} \end{bmatrix}, \qquad \mathbf{B} = \begin{bmatrix} \mathbf{0} & \sqrt{\lambda_1}\mathbf{D}_1 & \mathbf{0} \\ \mathbf{0} & \mathbf{0} & \sqrt{\lambda_2}\mathbf{D}_2 \end{bmatrix}, \] and the first \(n\) diagonal entries of the augmented hat matrix are the \(H_{ii}\).

this is ridge regression with a structured penalty matrix in place of \(\lambda \mathbf{I}\) (compare regularised regression). the intercept is never penalised, and each \(\lambda_j\) acts only on its own function: \(\lambda_1 \to \infty\) forces \(\mathbf{D}_1\boldsymbol\delta = \mathbf{0}\), a straight-line \(f_1\), while leaving \(f_2\) free. \(\mathbf{H}\) is no longer a projection, but \(\operatorname{tr}(\mathbf{H})\) is still the effective degrees of freedom, ranging from \(3\) (intercept plus two lines) up to \(1 + (k_1 - 1) + (k_2 - 1)\) (no penalty). the augmented form is how it is computed: lm() does the QR decomposition and \(\mathbf{X}^\top\mathbf{X}\) is never formed.

choosing the smoothing parameters

the target is \(M = \frac1n \sum_i (\hat f_i - f_i)^2\), unknowable since \(f\) is. leave-one-out cross-validation, \(\mathcal{V}_{\mathrm{CV}} = \frac1n \sum_i (\hat f_i^{[-i]} - y_i)^2\), estimates \(\mathrm{E}[M] + \sigma^2\) up to the leave-one-out swap: the cross term \(\mathrm{E}[(\hat f_i^{[-i]} - f_i)\varepsilon_i]\) vanishes precisely because the left-out fit never saw \(\varepsilon_i\). (on the general idea see cross-validation.)

Theorem (Leave-one-out without refitting; GCV)

for any linear smoother \(\hat{\mathbf{f}} = \mathbf{H}\mathbf{y}\) fitted by penalised least squares, \[ \mathcal{V}_{\mathrm{CV}} = \frac1n \sum_{i=1}^n \frac{(y_i - \hat f_i)^2}{(1 - H_{ii})^2}. \] replacing every \(H_{ii}\) by their mean \(\operatorname{tr}(\mathbf{H})/n\) gives the generalised cross-validation score \[ \mathcal{V}_g = \frac{n \sum_{i=1}^n (y_i - \hat f_i)^2}{[n - \operatorname{tr}(\mathbf{H})]^2} = \hat\sigma^2 \cdot \frac{n}{n - \operatorname{tr}(\mathbf{H})}, \qquad \hat\sigma^2 = \frac{\mathrm{RSS}}{n - \operatorname{tr}(\mathbf{H})}. \]

small \(\lambda\) shrinks the rss but inflates \(\operatorname{tr}(\mathbf{H})\); large \(\lambda\) the reverse; \(\mathcal{V}_g\) balances them, needs one trace rather than \(n\) leverages, and (unlike ocv) is invariant to rotations of the data. with two or more \(\lambda\)’s a grid is wasteful, so minimise \(\mathcal{V}_g\) numerically over \(\log \lambda_j\) — the log keeps every \(\lambda_j > 0\) and the search unconstrained.

in R, and the trees data

am.fit <- function(y, x, v, sp, k = 10) {
  # tf.XD (lecture helper): tent basis at the data, last column dropped, columns centred; plus D
  xdx <- tf.XD(x, seq(min(x), max(x), length = k))
  xdv <- tf.XD(v, seq(min(v), max(v), length = k))
  nD <- nrow(xdx$D) * 2; sp <- sqrt(sp)
  X <- cbind(c(rep(1, nrow(xdx$X)), rep(0, nD)),           # unpenalised intercept
             rbind(xdx$X, sp[1] * xdx$D, xdv$D * 0),
             rbind(xdv$X, xdx$D * 0, sp[2] * xdv$D))       # augmented design
  b <- lm(c(y, rep(0, nD)) ~ X - 1)                       # OLS on augmented data
  n <- length(y); trA <- sum(influence(b)$hat[1:n])       # tr(H)
  rss <- sum((y - fitted(b)[1:n])^2)
  list(b = coef(b), edf = trA, fitted = fitted(b)[1:n], gcv = rss / (n - trA) * n / (n - trA))
}
am.gcv <- function(lsp, y, x, v, k) am.fit(y, x, v, exp(lsp), k)$gcv      # GCV as a function of log sp
fit.am <- optim(c(0, 0), am.gcv, y = trees$Volume, x = trees$Girth, v = trees$Height, k = 15)
Example (Additive model for timber volume)

R’s trees: \(n = 31\) black cherry trees, diameter Girth (8.3–20.6 in), Height (63–87 ft), Volume (10.2–77 ft\(^3\)). fit \[ \mathrm{Volume}_i = \alpha + f_1(\mathrm{Girth}_i) + f_2(\mathrm{Height}_i) + \varepsilon_i \] with \(k = 15\) knots per term (\(1 + 14 + 14 = 29\) coefficients), minimising GCV over the log smoothing parameters.

  • \(\hat\lambda_1 = e^{3.14} \approx 23\), but \(\hat\lambda_2 = e^{23.3} \approx 1.3 \times 10^{10}\): the height penalty runs to infinity, so \(\hat f_2\) is exactly a straight line.
  • \(\operatorname{tr}(\mathbf{H}) = 4.70\) (intercept 1, height line 1, girth curve about 2.7), \(\mathrm{RSS} = 188.9\), \(\mathcal{V}_g = 8.47\).
  • \(\hat\alpha = 30.17 = \bar y\) exactly: with centred bases the column of ones is orthogonal to every other column and unpenalised.
  • \(\hat f_1\) rises from \(-17.9\) to \(40.9\), convex; \(\hat f_2\) from \(-4.9\) to \(4.1\). both cross zero mid-range, as the constraint demands.

reading: holding height fixed, expected volume rises with girth at an increasing rate, by about 59 ft\(^3\) over the observed range; holding girth fixed, it rises linearly with height, about 9 ft\(^3\) (\(\approx 0.4\) ft\(^3\) per foot). the weakness shows: physically volume is closer to \(\text{girth}^2 \times \text{height}\), a product.

proofs

Proof (Sum-to-zero constraint by centring)

since \(\mathbf{1}^\top\mathbf{1} = n\),

\begin{align} \mathbf{1}^\top \hat{\mathbf{X}}_1 \boldsymbol\delta &= \mathbf{1}^\top\mathbf{X}_1\boldsymbol\delta - \tfrac1n (\mathbf{1}^\top\mathbf{1})\, \mathbf{1}^\top\mathbf{X}_1\boldsymbol\delta = 0 . \end{align}

rank: the tents partition unity, so every row of \(\mathbf{X}_1\) sums to one, \(\mathbf{X}_1\mathbf{1} = \mathbf{1}\). then

\begin{align} \hat{\mathbf{X}}_1 \mathbf{1} &= \mathbf{X}_1\mathbf{1} - \tfrac1n \mathbf{1}\,\mathbf{1}^\top \mathbf{X}_1 \mathbf{1} = \mathbf{1} - \tfrac1n \mathbf{1}\, n = \mathbf{0}, \end{align}

so the columns are dependent and \(\boldsymbol\delta\), \(\boldsymbol\delta + a\mathbf{1}\) give the same fit (adding \(a\) to every knot value is the vertical shift being removed). dropping one column restores full rank.

Proof (Penalised least squares and the augmented model)

write \(\mathbf{P} = \lambda_1\bar{\mathbf{S}}_1 + \lambda_2\bar{\mathbf{S}}_2\) (symmetric, positive semi-definite). the criterion is

\begin{align} Q(\boldsymbol\beta) &= \mathbf{y}^\top\mathbf{y} - 2\boldsymbol\beta^\top\mathbf{X}^\top\mathbf{y} + \boldsymbol\beta^\top(\mathbf{X}^\top\mathbf{X} + \mathbf{P})\boldsymbol\beta, \\ \nabla Q &= -2\mathbf{X}^\top\mathbf{y} + 2(\mathbf{X}^\top\mathbf{X} + \mathbf{P})\boldsymbol\beta = \mathbf{0} \;\Rightarrow\; \hat{\boldsymbol\beta} = (\mathbf{X}^\top\mathbf{X} + \mathbf{P})^{-1}\mathbf{X}^\top\mathbf{y}, \end{align}

a minimum since the hessian \(2(\mathbf{X}^\top\mathbf{X} + \mathbf{P})\) is positive definite (\(\mathbf{X}\) has full column rank after the column drops). for the augmented form, block multiplication gives \(\mathbf{B}^\top\mathbf{B} = \mathbf{P}\), hence \(\tilde{\mathbf{X}}^\top\tilde{\mathbf{X}} = \mathbf{X}^\top\mathbf{X} + \mathbf{P}\) and \(\tilde{\mathbf{X}}^\top\tilde{\mathbf{y}} = \mathbf{X}^\top\mathbf{y}\), so \(\|\tilde{\mathbf{y}} - \tilde{\mathbf{X}}\boldsymbol\beta\|^2 = Q(\boldsymbol\beta)\) term by term. the top-left \(n \times n\) block of \(\tilde{\mathbf{X}}(\tilde{\mathbf{X}}^\top\tilde{\mathbf{X}})^{-1}\tilde{\mathbf{X}}^\top\) is then \(\mathbf{X}(\mathbf{X}^\top\mathbf{X} + \mathbf{P})^{-1}\mathbf{X}^\top = \mathbf{H}\).

Proof (Leave-one-out without refitting)

fix \(i\) and let \(\mathbf{y}^*\) equal \(\mathbf{y}\) except \(y_i^* = \hat f_i^{[-i]}\). the penalised criterion for \(\mathbf{y}^*\) is the leave-one-out criterion plus \((y_i^* - f(x_i))^2 \ge 0\), which is zero at \(f = \hat f^{[-i]}\); so \(\hat f^{[-i]}\) minimises it, i.e. \(\hat{\mathbf{f}}^{[-i]} = \mathbf{H}\mathbf{y}^*\). reading row \(i\):

\begin{align} \hat f_i^{[-i]} &= \sum_{j \ne i} H_{ij} y_j + H_{ii}\hat f_i^{[-i]} = \hat f_i - H_{ii} y_i + H_{ii}\hat f_i^{[-i]} \\ \Rightarrow\; \hat f_i^{[-i]} - y_i &= \frac{\hat f_i - y_i}{1 - H_{ii}} . \end{align}

square and average. GCV substitutes \(H_{ii} \to \operatorname{tr}(\mathbf{H})/n\): \[ \frac1n \sum_i \frac{(y_i - \hat f_i)^2}{(1 - \operatorname{tr}\mathbf{H}/n)^2} = \frac{n\,\mathrm{RSS}}{(n - \operatorname{tr}\mathbf{H})^2}\,. \]

generalised additive models

Definition (Generalised additive model)

\[ g(\mathrm{E}[y_i]) = \beta_0 + f_1(x_{i1}) + \dots + f_p(x_{ip}), \] with \(g\) a known smooth monotone link and \(y_i\) from any exponential family. gams are to additive models what glms are to linear models; the additive model is the identity-link, gaussian case. the logit link gives a logistic gam, the log link a log-additive model for counts or positive data.

penalised irls

a gam is fitted by penalised likelihood: maximise the log-likelihood minus \(\tfrac12\sum_j \lambda_j \boldsymbol\beta^\top\bar{\mathbf{S}}_j\boldsymbol\beta\). in practice that is the glm’s irls loop, unchanged except that each weighted least-squares step carries the penalty. from the current \(\hat{\boldsymbol\eta}\), \(\hat\mu_i = g^{-1}(\hat\eta_i)\):

  1. weights and working response, exactly as for a glm: \(w_i = 1/[V(\hat\mu_i)\, g’(\hat\mu_i)^2]\), \(z_i = g’(\hat\mu_i)(y_i - \hat\mu_i) + \hat\eta_i\);
  2. solve the penalised weighted least squares problem, \[ \min \|\sqrt{\mathbf{W}}(\mathbf{z} - \mathbf{X}\boldsymbol\beta)\|^2 + \sum_j \lambda_j \boldsymbol\beta^\top\bar{\mathbf{S}}_j\boldsymbol\beta\,, \] whose solution is \[ \hat{\boldsymbol\beta} = (\mathbf{X}^\top\mathbf{W}\mathbf{X} + \lambda_1\bar{\mathbf{S}}_1 + \lambda_2\bar{\mathbf{S}}_2)^{-1}\mathbf{X}^\top\mathbf{W}\mathbf{z}; \]
  3. update \(\hat{\boldsymbol\eta} = \mathbf{X}\hat{\boldsymbol\beta}\) and repeat to convergence.
Proposition (GCV for a GAM uses Pearson residuals)

take the GCV score of the converged working model. it equals the additive-model formula with Pearson residuals in place of raw ones, \[ \mathcal{V}_g = \frac{n\sum_i (r_i^P)^2}{[n - \operatorname{tr}(\mathbf{H})]^2}, \qquad r_i^P = \frac{y_i - \hat\mu_i}{\sqrt{V(\hat\mu_i)}}, \] so the numerator is \(n\) times the Pearson statistic and \(\mathbf{H}\) is the hat matrix of the weighted penalised fit.

proofs

Proof (GCV for a GAM uses Pearson residuals)

the converged working model regresses \(\sqrt{\mathbf{W}}\mathbf{z}\) on \(\sqrt{\mathbf{W}}\mathbf{X}\); its residual sum of squares is \(\sum_i w_i (z_i - \hat\eta_i)^2\), and

\begin{align} w_i (z_i - \hat\eta_i)^2 &= \frac{\bigl(g’(\hat\mu_i)(y_i - \hat\mu_i)\bigr)^2}{V(\hat\mu_i)\, g’(\hat\mu_i)^2} = \frac{(y_i - \hat\mu_i)^2}{V(\hat\mu_i)} = (r_i^P)^2 . \end{align}

substitute into the GCV formula with the weighted penalised hat matrix.

gams in practice: mgcv

mgcv::gam() has the glm() interface plus smooth terms s(x); by default each term’s smoothness is chosen by GCV (for the families used here), with a thin plate regression spline basis of dimension k = 10.

library(mgcv)
gam.mod <- gam(Volume ~ s(Height) + s(Girth), family = Gamma(link = log), data = trees)
Estimated degrees of freedom:
1.00 2.42  total = 4.42
GCV score: 0.008082356

height gets edf \(1.00\) (a centred line has one free parameter, its slope; the penalty has removed every wiggle), girth \(2.42\) (a little more than a quadratic), and the total adds one for the intercept. this matches the hand-built gam despite a different basis: the data choose the shape, not the basis. plot(gam.mod) draws each \(\hat f_j\) with \(\pm 2\) s.e. bands and partial residuals, \(\hat f_j(x_{ij}) + r_i^P\) — the data with every other term removed, on the working scale.

Remark (The pinched band)

the band for s(Height,1) closes to zero width at \(\bar h = 76\) ft, exactly where the line crosses zero. under sum-to-zero a fitted line \(a + bh\) must satisfy \(na + b\sum_i h_i = 0\), so \(\hat f_2(h) = b(h - \bar h)\) and \(\operatorname{Var}\hat f_2(h) = (h - \bar h)^2 \operatorname{Var}(b)\), zero at the mean. the girth curve (edf 2.42) has more than one free direction, so its band is narrowest mid-range but never closes.

the defaults, one at a time

fitchangeedf (terms; total)GCV
s(Height) + s(Girth)defaults (thin plate, k=10)1.00, 2.42; 4.420.008082
same, bs = "cr"cubic regression spline basis1.00, 2.42; 4.420.008081
s(Height, bs = "cr", k = 20) …larger basis for height1.00, 2.42; 4.420.008082
same as first, gamma = 1.5heavier charge per df1.00, 2.12; 4.120.009539 (n.c.)
s(Girth, Height, k = 25)isotropic 2-d smooth4.79; 5.790.009358
te(Girth, Height, k = 5)tensor product 2-d smooth3; 40.008197
  • basis: thin plate is the default but computationally heavy; bs = "cr" gives an indistinguishable fit. the basis is a computational device.
  • basis dimension k is the ceiling on a term’s df (\(k - 1\) after the constraint); the penalty decides how many are used. raising k once it is big enough changes nothing; too small a k cannot represent a wiggly effect at all.
  • gamma multiplies the df in the GCV denominator, \(n\sum_i (r_i^P)^2 / [n - \gamma\operatorname{tr}(\mathbf{H})]^2\), charging each df \(\gamma\) times to counter GCV’s tendency to overfit. scores at different \(\gamma\) are not comparable (n.c.).
  • several variables: s(x, v) is an isotropic smooth — rotation-invariant but not scale-invariant, so it struggles when covariates are in different units (inches against feet); te(x, v) is a tensor product with one penalty per direction — more parameters, but invariant to rescaling. its basis dimension is the product of marginal ones (\(5 \times 5 = 25\), comparable with k = 25).

reading the table: the plain additive fits have the lowest GCV, and the 2-d smooths buy nothing — the empirical form of the multiplicative argument (on the log scale girth and height already act additively), with the isotropic smooth worst, as its unit-sensitivity predicts. an edf of 1.00 for height also suggests the semi-parametric Volume ~ Height + s(Girth); gam() mixes parametric and smooth terms freely.

backfitting, and the gam package

the tutorial uses Hastie’s gam package on ISLR’s Wage data. with natural-spline terms a gam is just one large least-squares regression on basis columns (lm(wage ~ ns(year, 4) + ns(age, 5) + education)). with smoothing-spline terms s(age, 5) it is fitted by backfitting: cycle through the terms, re-smoothing each partial residual \(y_i - \beta_0 - \sum_{k \ne j}\hat f_k(x_{ik})\) against \(x_{ij}\) until nothing changes. its summary() tests linear against non-linear per term (year \(p = 0.35\): linear suffices; age \(p < 2 \times 10^{-16}\)), and nested \(F\)-tests agree (no year to linear year \(F = 14.48\), \(p = 0.00014\); linear to spline \(F = 1.10\), \(p = 0.35\)).

Example (Separation inside a logistic GAM)

gam(I(wage > 250) ~ year + s(age, df = 5) + education, family = binomial). none of the 268 men without a high-school diploma earns over 250, so that education level’s fitted probability is driven to zero and its coefficient to \(-\infty\): the logistic regression separation problem, now inside a gam. the symptom is an education panel with axis \(\pm 400\) and a vast error bar on one level; dropping the level (subset = (education != "1. < HS Grad")) restores a sane fit.

see also

Comments