Skip to content

GAMs: a glass box that bends

One smooth curve per feature, added together. Every prediction can be read off its curves. See when that's enough, and what the additive assumption can't catch.

Applied ~11 min8 steps

What runs in CodeSplash

You will be able to

  • Describe an additive model of penalized smooth curves
  • Read effective degrees of freedom and the smoothing parameter
  • Tell from a comparison when interactions are what's missing

Read first

01

Curves that add

Read

A linear model gives each feature a slope. A generalized additive model gives each one a curve:

g(E[y])=β0+f1(x1)+f2(x2)+⋯+fp(xp)g\big(\mathbb{E}[y]\big) = \beta_0 + f_1(x_1) + f_2(x_2) + \dots + f_p(x_p)

Each fjf_j is a P-splineP-splineA smooth curve built from many small bumps, with a penalty on wiggliness that decides how flexible it really is.: many local bumps, with a penalty on how wiggly they get. To explain a prediction, read each curve at that row's value and add up.

That's why the card rates Generalized additive model at the top for interpretability.

02

Read one curve

Explore

Each panel shows one feature's effect on the prediction scale, with a band. Where is it close to a straight line? Where does it bend?

GAM partial effectsWhat does each feature add to an additive model's prediction?
View data
FeatureValueEffectLowerUpper
Temperature56−1.200−1.400−1
Temperature57.370−0.9950−1.188−0.8020
Temperature58.730−0.8160−1.003−0.6290
Temperature60.100−0.6610−0.8410−0.4810
Temperature61.470−0.5290−0.7020−0.3560
Temperature62.830−0.4170−0.5840−0.2500
Temperature64.200−0.3230−0.4830−0.1630
Temperature65.570−0.2460−0.4000−0.0930
Temperature66.930−0.1840−0.3300−0.0370
Temperature68.300−0.1340−0.27400.0060
Temperature69.670−0.0950−0.22800.0380
Temperature71.030−0.0650−0.19200.0620
Temperature72.400−0.0420−0.16200.0780
Temperature73.770−0.0240−0.13700.0900
Temperature75.130−0.0090−0.11500.0980
Temperature76.5000.0050−0.09500.1050
Temperature77.8700.0190−0.08700.1260
Temperature79.2300.0370−0.07700.1500
Temperature80.6000.0580−0.06200.1780
Temperature81.9700.0860−0.04000.2130
Temperature83.3300.1230−0.01100.2560
Temperature84.7000.16900.02900.3090
Temperature86.0700.22800.08200.3750
Temperature87.4300.30100.14800.4540
Temperature88.8000.39000.23000.5500
Temperature90.1700.49700.33000.6640
Temperature91.5300.62400.45000.7970
Temperature92.9000.77200.59200.9520
Temperature94.2700.94500.75801.131
Temperature95.6301.1420.94901.336
Temperature971.3681.1681.568
Wind20.55000.35000.7500
Wind2.6300.45000.25600.6430
Wind3.2700.36000.17300.5460
Wind3.9000.27900.09900.4590
Wind4.5300.20600.03200.3790
Wind5.1700.1400−0.02700.3070
Wind5.8000.0810−0.07900.2410
Wind6.4300.0280−0.12600.1810
Wind7.070−0.0200−0.16700.1260
Wind7.700−0.0630−0.20300.0770
Wind8.330−0.1020−0.23500.0310
Wind8.970−0.1370−0.2640−0.0100
Wind9.600−0.1680−0.2880−0.0480
Wind10.230−0.1960−0.3100−0.0830
Wind10.870−0.2220−0.3290−0.1150
Wind11.500−0.2450−0.3450−0.1450
Wind12.130−0.2650−0.3720−0.1590
Wind12.770−0.2840−0.3970−0.1700
Wind13.400−0.3000−0.4200−0.1800
Wind14.030−0.3150−0.4420−0.1890
Wind14.670−0.3290−0.4620−0.1960
Wind15.300−0.3410−0.4810−0.2010
Wind15.930−0.3520−0.4990−0.2050
Wind16.570−0.3620−0.5150−0.2080
Wind17.200−0.3710−0.5310−0.2110
Wind17.830−0.3790−0.5450−0.2120
Wind18.470−0.3860−0.5590−0.2120
Wind19.100−0.3920−0.5720−0.2120
Wind19.730−0.3980−0.5850−0.2110

First 60 of 93 rows.

Read the band before you read the ends of a curve: few rows there means the shape is mostly the penalty talking.

03

Bending versus interacting

Predict

Predict first

Ozone RMSE: a straight-line model has 22.9, and a random forest has 19.0. A GAM lets every feature bend, but doesn't let them interact. Where will it land?

Matches the forestBarely beats the line, and the forest stays aheadWorse than the line
Reveal

It barely beats the line: 22.3. Curves alone don't close the gap. Ozone's big values need hot and still weather together, and that's an interaction, which an additive model can't represent. A forest finds it by itself.

04

How wiggly?

Read

The penalty decides, through Smoothing. The default chooses it by generalized cross-validation.

Effective degrees of freedom count how much wiggle was spent: about 1 for a straight line, and more as a curve bends. For ozone, the fit used 10.8 in total at a penalty of 1.

Fix Penalty (λ) yourself only when you have a reason, for example to compare with another study.

05

When it's the right model

Read

On Pima, with four features and no interactions: AUC 0.853, against 0.857 for logistic on seven and 0.843 for boosting on seven. When effects are additive, a GAM is as good as anything, and you can show the curves to a regulator.

Use it when you need to explain each input's effect, and can accept no interactions unless you add them yourself.

06

Check your understanding

Checkpoint

Check yourself

Which pattern can't a plain GAM represent?

Show the answer

Answer: c

Each curve involves one feature, so effects add. A dependency between two features is an interaction, which needs a joint term.

Check yourself

A smooth has effective degrees of freedom close to 1. What does it look like?

Show the answer

Answer: a

One degree of freedom is one slope. More means more bends. A strong penalty pushes a curve towards the line.

Check yourself

A GAM matches the linear model, yet a forest beats both clearly. What's the best next thing to try?

Show the answer

Answer: b

If bending didn't help and a model with interactions did, the missing structure is probably joint. A product term may bring the glass box back into contention.

07

Fit one

In the app

In the Learners step, select Generalized additive model. Basis functions per curve sets the budget and Smoothing picks the penalty. It handles numbers and two classes, and in Setup the Objective can be Poisson, Gamma or Tweedie for counts and amounts.

Python
import numpy as np
import pandas as pd
from sklearn.linear_model import RidgeCV
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import SplineTransformer

df = pd.read_csv("airquality.csv").dropna()
X, y = df[["Solar.R", "Wind", "Temp"]], df["Ozone"]
# One spline basis per column, added together, under one penalty.
gam = make_pipeline(SplineTransformer(n_knots=6), RidgeCV(alphas=np.logspace(-3, 3, 13)))
print(-cross_val_score(gam, X, y, cv=5, scoring="neg_root_mean_squared_error").mean())
R
library(mgcv)
df <- na.omit(airquality)
set.seed(1)
fold <- sample(rep(1:5, length.out = nrow(df)))
rmse <- sapply(1:5, function(k) {
  fit <- gam(Ozone ~ s(Solar.R, k = 8) + s(Wind, k = 8) + s(Temp, k = 8),
             data = df[fold != k, ], method = "GCV.Cp")
  sqrt(mean((df$Ozone[fold == k] - predict(fit, df[fold == k, ]))^2))
})
mean(rmse)

Each library has its own basis and penalty. The app runs one fit in both engines, and that's why its predictions agree between them.

08

Recap

Recap
  • A GAM adds one penalized smooth curve per feature, so every prediction can be read.
  • Effective degrees of freedom say how much each curve bends.
  • If a GAM ties a line and a forest wins, look for interactions.

Concepts in this lesson