Yes! You can do that in mgcv

David L Miller

Biomathematics and Statistics Scotland

UK Centre for Ecology and Hydrology

https://converged.yt/talks/lancaster-gam-talk

Motivation

All models are wrong, so why not start with one you actually understand?

Jenny Bryan

What is mgcv?

TL;DR: you can fit generalized additive models

  • R package, by Simon Wood (Edinburgh)
  • Uses familiar glm()-type syntax
  • Recommended (ships with R)
  • Very fast (and parallelisable)

What is a GAM?

\[ \mathbb{E}(Y_i) = g^{-1}\left[\beta_0 + \sum_j s_j(x_{ij})\right] \]

  • \(Y_i \sim\) some (extended?) exponential family
  • \(g\) is a link function
  • \(\beta_0\) is an intercept
  • What about the \(s_j\)s?

Smoothing

  • \(s_j\)s are smooths (usually splines)

\[ s(x) = \sum_k \beta_k b_k(x) \]

  • Built from basis functions \(b_k(x)\)
  • Only need to estimate \(\beta_k\)s

Basis-penalty smoothers

  • How do we avoid overfitting?
  • Measure wigglyness, then penalise!

\[ \lambda \int_\mathbb{R} \left( \frac{\partial^2 s(x)}{\partial x^2}\right)^2 \text{d}x\\ \]

where \(\lambda\) is a smoothing parameter

Basis-penalty smoothers

  • How do we avoid overfitting?
  • Measure wigglyness, then penalise!

Splines can be fancy

Okay…

  • More things than you think are GAMs
  • … or can be shoe-horned into a GAM
  • Let’s see some examples

Things that look like splines

  • Random effects
  • Markov random fields
  • Gaussian processes

INLA’s SPDE model

  • Fit Matérn covariance by equivalence to SPDE
  • We can write that as a basis-penalty smoother too!

Fancy smoothers

(Linear functionals)

Varying-coefficients

  • Extend to \(x\beta \Rightarrow x \beta(z)\)
  • Moderate a covariate based on another?

Factor-smooth interactions

  • Often data are grouped
  • What if we want to share information?

Factor-smooth interactions

  • Often data are grouped
  • What if we want to share information?
  • Smooth back to some group level
  • Share smoothing parameters

Signal regression

  • Remote sensing data is a pain
  • What place or time had an effect?
  • 💭 can rasters be functions?
  • use functional ideas

Distributed lags

  • What if the effect is cumulative?
  • Aphids care about this!
  • Smooth all the lags
  • Sum them for effect

Other models

Poisson processes

  • Approximate PP likelihood with a GLM
  • Berman-Turner device
  • (same idea as in inlabru)

Multiple likelihoods

  • “Data integration” has been an open problem for longer than I’ve been a statistician
  • Essential component is different response distributions
  • (If only for different hyperparameters)

Ordered categories

  • Sometimes we move between states
  • Birds/seals molt etc
  • Model the transitions

What about Big Data?

  • Fit “big” additive models
  • Parallelise both IRLS and penalization
  • Marginal discretization of coefficients
  • GAM goes brrrrr

How hard is it to do these things?

How can I do it?

So far, we’ve just fiddled with:

  • mgcv function (gam/gamm/bam)
  • basis (bs=)
  • response distribution (family=)
  • the data

Generally write:

m <- gam(y ~ s(x, bs="tp"), family=gaussian, data=df)

We can go a bit further…

Penalty \(\approx\) prior precision

Are we Bayesians now? (Yes)

Bayesian models

Prior on parameters:

\[ \boldsymbol{\beta} \sim \text{MVN}(\mathbf{0}, \mathbf{S}^{-}/\lambda) \]

Cool! … where does that get us?

  • Posterior sampling!
  • Confidence intervals (credible intervals) with good properties
  • Patch-in to Stan, JAGS, Nimble, TMB etc

Posterior sampling example

Phew! That’s it!

Thanks

Conclusions

  • mgcv does a lot of stuff!
  • It might be a good start point
  • We don’t have to use the most complicated tool!
    • (it doesn’t have to be mgcv!)
    • Work from where you know and expand out

We can just have a chill time and build models we understand

converged.yt/talks/lancaster-gam-2023