# Adaptive GAM smooths in mgcv

Simon Wood's book on GAMs and his associated R package mgcv are both highly detailed and informative when it comes to GAM theory and model-fitting to real and simulated data.

For 1D smooths, there is really not much to worry about, save for deciding whether to implement cyclic vs. adaptive basis functions, which can give very different prediction results compared to cubic, thin-plate and P-spline smooths, because, in the adaptive case, multiple GAMs are fitted to different regions along a spline. As far as I can tell, cyclic bases are common in time-series modelling, whereas adaptive smooths should be considered when data vary a lot with respect to the response variable; however, adaptive smooths should be used "sparingly and with care".

I have been investigating GAMs for some time now, and given my research question, I am finding myself changing my mind a lot when it comes to which smooth to implement. mgcv includes 17 different smooths to choose from (by my count). I have considered both cubic and P-spline smooths.

My question now is: when should adaptive smooths be considered over non-adaptive counterparts, if the eventual goal is to use fitted GAMs for prediction purposes? For my purposes, I am sticking with the default GCV smoothness criterion, even though it has the tendency to under-smooth.

The literature is growing in applied ecological GAMs, but I have yet to come across a study that implements an adaptive smooth.

Any advice is appreciated.

## 1 Answer

Most of the extra smooths in the mgcv toolbox are really there for specialist applications — you can largely ignore them for general GAMs, especially univariate smooths (you don't need a random effect spline, a spline on the sphere, a Markov random field, or a soap-film smoother if you have univariate data for example.)

## If you can bear the setup cost, use thin-plate regression splines (TPRS).

These splines are optimal in an asymptotic MSE sense, but require one basis function per observation. What Simon does in mgcv is generate a low-rank version of the standard TPRS by taking the full TPRS basis and subjecting it to an eigendecomposition. This creates a new basis where the first k basis function in the new space retain most of the signal in the original basis, but in many fewer basis functions. This is how mgcv manages to get a TPRS that uses only a specified number of basis functions rather than one per observation. This eigendecomposition preserves much of the optimality of the classic TPRS basis, but at considerable computational effort for large data sets.

## If you can't bear the setup cost of TPRS, use cubic regression splines (CRS)

This is a quick basis to generate and hence is suited to problems with a lot of data. It is knot-based however, so to some extent the user now needs to choose where those knots should be placed. For most problems there is little to be gained by going beyond the default knot placement (at the boundary of the data and spaced evenly in between), but if you have particularly uneven sampling over the range of the covariate, you may choose to place knots evenly spaced sample quantiles of the covariate, for example.

Every other smooth in mgcv is special, being used where you want isotropic smooths or two or more covariates, or are for spatial smoothing, or which implement shrinkage, or random effects and random splines, or where covariates are cyclic, or the wiggliness varies over the range of a covariate. You only need to venture this far into the smooth toolbox if you have a problem that requires special handling.

## Shrinkage

There are shrinkage versions of both the TPRS and CRS in mgcv. These implement a spline where the perfectly smooth part of the basis is also subject to the smoothness penalty. This allows the smoothness selection process to shrink a smooth back beyond even a linear function essentially to zero. This allows the smoothness penalty to also perform feature selection.

## Duchon splines, P splines and B splines

These splines are available for specialist applications where you need to specify the basis order and the penalty order separately. Duchon splines generalise the TPRS. I get the impression that P splines were added to mgcv to allow comparison with other penalized likelihood-based approaches, and because they are splines used by Eilers & Marx in their 1996 paper which spurred a lot of the subsequent work in GAMs. The P splines are also useful as a base for other splines, like splines with shape constraints, and adaptive splines.

B splines, as implemented in mgcv allow for a great deal of flexibility in setting up the penalty and knots for the splines, which can allow for some extrapolation beyond the range of the observed data.

## Cyclic splines

If the range of values for a covariate can be thought of as on a circle where the end points of the range should actually be equivalent (month or day of year, angle of movement, aspect, wind direction), this constraint can be imposed on the basis. If you have covariates like this, then it makes sense to impose this constraint.

## Adaptive smoothers

Rather than fit a separate GAM in sections of the covariate, adaptive splines use a weighted penalty matrix, where the weights are allowed to vary smoothly over the range of the covariate. For the TPRS and CRS splines, for example, they assume the same degree of smoothness across the range of the covariate. If you have a relationship where this is not the case, then you can end up using more degrees of freedom than expected to allow for the spline to adapt to the wiggly and non-wiggly parts. A classic example in the smoothing literature is the

library('ggplot2')
theme_set(theme_bw())
library('mgcv')
data(mcycle, package = 'MASS')
pdata <- with(mcycle,
data.frame(times = seq(min(times), max(times), length = 500)))

ggplot(mcycle, aes(x = times, y = accel)) + geom_point()


These data clearly exhibit periods of different smoothness - effectively zero for the first part of the series, lots during the impact, reducing thereafter.

if we fit a standard GAM to these data,

m1 <- gam(accel ~ s(times, k = 20), data = mcycle, method = 'REML')


we get a reasonable fit but there is some extra wiggliness at the beginning and end the range of times and the fit used ~14 degrees of freedom

plot(m1, scheme = 1, residuals = TRUE, pch= 16)


To accommodate the varying wiggliness, an adaptive spline uses a weighted penalty matrix with the weights varying smoothly with the covariate. Here I refit the original model with the same basis dimension (k = 20) but now we have 5 smoothness parameters (default is m = 5) instead of the original's 1.

m2 <- gam(accel ~ s(times, k = 20, bs = 'ad'), data = mcycle, method = 'REML')


Notice that this model uses far fewer degrees of freedom (~8) and the fitted smooth is much less wiggly at the ends, whilst still being able to adequately fit the large changes in head acceleration during the impact.

What's actually going on here is that the spline has a basis for smooth and a basis for the penalty (to allow the weights to vary smoothly with the covariate). By default both of these are P splines, but you can also use the CRS basis types too (bs can only be one of 'ps', 'cr', 'cc', 'cs'.)

As illustrated here, the choice of whether to go adaptive or not really depends on the problem; if you have a relationship for which you assume the functional form is smooth, but the degree of smoothness varies over the range of the covariate in the relationship then an adaptive spline can make sense. If your series had periods of rapid change and periods of low or more gradual change, that could indicate that an adaptive smooth may be needed.

• Thanks Gavin! This is really an excellent answer that I think will also help others with the same concern. – compbiostats Sep 28 '17 at 18:26
• For my problem, I fit a monotonic increasing curve that eventually plateaus to a horizontal asymptote. At the start of my curve, data increase quite rapidly, before eventually slowing toward an asymptote. In some cases, extrapolation (close/far) beyond the range of observed data is necessary. My models are very simple, but I realize that predictions here are speculative at best. – compbiostats Sep 28 '17 at 18:35