GAM standards using mgcv in R. Use when fitting gam(), specifying smooth terms, selecting basis types, or diagnosing concurvity.
library(mgcv)
model <- gam(outcome ~ s(x1) + s(x2) + factor_var,
family = quasipoisson(), data = df, method = "REML")
| Term | Usage | Description |
|---|---|---|
s(x) | Univariate smooth | Thin plate spline by default |
s(x, bs = "cr") | Cubic regression spline | Faster, good for medium data |
s(x, bs = "ps") | P-spline | Good for penalized DLNM |
te(x1, x2) | Tensor product | Full interaction (separate smoothing per dimension) |
ti(x1, x2) | Tensor interaction | Pure interaction (main effects excluded) |
s(x, k = 10) | Set basis dimension | k sets max wiggliness (default: 10) |
bs = | Name | Best For |
|---|---|---|
"tp" | Thin plate (default) | General purpose, isotropic |
"cr" | Cubic regression | Faster computation, 1D |
"ps" | P-spline | DLNM penalty integration |
"cc" | Cyclic cubic | Seasonal effects (circular) |
"re" | Random effect | Random intercepts/slopes |
method = "REML" — most stable, less prone to under-smoothing.method = "GCV.Cp" — faster but can under-smooth.method = "ML" — for model comparison with AIC().# Check basis dimension adequacy
gam.check(model) # k-index < 1 suggests k is too low
# Concurvity (collinearity for smooths)
concurvity(model, full = TRUE) # > 0.8 is concerning
# Summary with effective df
summary(model) # edf ≈ 1 means nearly linear
gam() with paraPen = list(cb = cbPen(cb)).s() smooths for confounders with parametric cross-basis.crosspred() works with both glm() and gam() objects.k: If gam.check() says basis dimension is too low, increase k.bs for DLNM: Use "cr" or "ps" with cbPen(), not "tp".