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
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\).
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
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.
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.)
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)
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
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.
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}\).
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
\[ 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)\):
- 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\);
- 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}; \]
- update \(\hat{\boldsymbol\eta} = \mathbf{X}\hat{\boldsymbol\beta}\) and repeat to convergence.
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.
model \[ \log \mathrm{E}(\mathrm{Volume}_i) = \alpha + f_1(\mathrm{Girth}_i) + f_2(\mathrm{Height}_i)\,, \] \(\mathrm{Volume}_i \sim \text{Gamma}\). exponentiating, expected volume is a product \(e^\alpha e^{f_1} e^{f_2}\), and \(\operatorname{Var}(Y) = \phi\mu^2\) lets big trees carry bigger absolute errors — both closer to the geometry (\(\log V \approx \text{const} + 2\log G + \log H\); a log–log linear fit gives slopes 1.98 and 1.12).
irls quantities: \(g’(\mu) = 1/\mu\), \(V(\mu) = \mu^2\), so \(w_i = 1/(\hat\mu_i^2 \cdot \hat\mu_i^{-2}) = 1\) and \(z_i = (y_i - \hat\mu_i)/\hat\mu_i + \hat\eta_i\). the weights vanish from the problem: each step is just am.fit(z, ...) on pseudo-data, and \(z_i - \hat\eta_i = r_i^P\), so its returned GCV already is the Pearson form. (for a poisson log-link model \(w_i = \hat\mu_i\) instead.)
gam.fit <- function(y, x, v, sp, k = 10) {
eta <- log(y); old.gcv <- -100
repeat {
mu <- exp(eta); z <- (y - mu) / mu + eta # pseudo-data, w = 1
fit <- am.fit(z, x, v, sp, k) # one penalised LS step
if (abs(fit$gcv - old.gcv) < 1e-5 * fit$gcv) break
old.gcv <- fit$gcv; eta <- fit$fitted
}
fit$fitted <- exp(fit$fitted); fit
}
with GCV minimised over \(\log\lambda\): edf about 4.5–4.7, GCV \(0.00808\). 𐃏 \(\hat f_1(\mathrm{Girth})\) runs from \(-0.81\) to \(0.93\) and is now concave on the log scale (\(2\log G\) would span \(1.82\); the fit spans \(1.74\)); \(\hat f_2(\mathrm{Height})\) is again a line, \(-0.21\) to \(0.18\), slope \(\approx 0.016\) per foot.
reading on the response scale, since a change \(\Delta\) in a smooth multiplies the mean by \(e^\Delta\): thinnest to thickest tree multiplies expected volume by \(e^{1.74} \approx 5.7\); each extra foot of height by \(e^{0.016}\), about \(1.6\%\), and the full height range by \(e^{0.39} \approx 1.5\). the effects multiply — thick and tall is \(\approx 8.5\) times thin and short — where the additive model could only add fixed increments of 59 and 9 ft\(^3\).
proofs
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.
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
| fit | change | edf (terms; total) | GCV |
|---|---|---|---|
s(Height) + s(Girth) | defaults (thin plate, k=10) | 1.00, 2.42; 4.42 | 0.008082 |
same, bs = "cr" | cubic regression spline basis | 1.00, 2.42; 4.42 | 0.008081 |
s(Height, bs = "cr", k = 20) … | larger basis for height | 1.00, 2.42; 4.42 | 0.008082 |
same as first, gamma = 1.5 | heavier charge per df | 1.00, 2.12; 4.12 | 0.009539 (n.c.) |
s(Girth, Height, k = 25) | isotropic 2-d smooth | 4.79; 5.79 | 0.009358 |
te(Girth, Height, k = 5) | tensor product 2-d smooth | 3; 4 | 0.008197 |
- basis: thin plate is the default but computationally heavy;
bs = "cr"gives an indistinguishable fit. the basis is a computational device. - basis dimension
kis the ceiling on a term’s df (\(k - 1\) after the constraint); the penalty decides how many are used. raisingkonce it is big enough changes nothing; too small akcannot represent a wiggly effect at all. gammamultiplies 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 withk = 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\)).
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
- splines and smoothing — the one-predictor smoother each term is built from, and effective degrees of freedom
- generalised linear models — the irls loop that penalised irls extends by one penalty term
- regularised regression — ridge is penalised least squares with \(\mathbf{S} = \mathbf{I}\); the augmented-data trick works there too
- cross-validation — the general method behind ocv and GCV
- model selection — choosing between parametric and smooth terms
- curse of dimensionality — why additivity, not a full surface \(f(x_1, \dots, x_p)\), is the default
- locally weighted regression — the other route to a smooth term (
lo()in the gam package)
Backlinks (4)
1. Analysis of Variance /wiki/ml/supervised/regression/anova/
2. Splines and Smoothing /wiki/ml/supervised/regression/splines/
3. Locally Weighted Regression /wiki/ml/supervised/regression/locally-weighted/
a straight line is too rigid for a wiggly world, and a global degree-9 polynomial is a hostage negotiation. 𐃏 locally weighted regression (LWR — and its robust cousin LOWESS) takes the diplomatic route: fit the simplest possible model, but fit it freshly at every query point, paying attention only to the training points nearby.
motivation
- linear regression commits to one \(\theta\) for the whole input space. if the true \(f\) bends, the residue of that commitment is bias everywhere.
- the fix need not be a fancier global family. any smooth function is locally linear — taylor says so — so a linear fit weighted toward a neighbourhood of \(x_0\) can track an arbitrary smooth \(f\).
- the price: there is no longer a “trained model”. LWR is memory-based and non-parametric — like k-nearest neighbours, it keeps the entire training set and does all of its work at prediction time. training is \(O(1)\); every query costs a fresh weighted least-squares solve. 𐃏
the estimator
weighted least squares at a query point
fix a query \(x_0\). assign each training point a weight \(w_i(x_0) \ge 0\) that decays with distance from \(x_0\), then solve the weighted least-squares problem
Comments