Skip to content

Instantly share code, notes, and snippets.

@oliviergimenez
Created October 13, 2020 09:31
Show Gist options
  • Save oliviergimenez/15457f4c25e88e5126ff60ba58bf5d7e to your computer and use it in GitHub Desktop.
Save oliviergimenez/15457f4c25e88e5126ff60ba58bf5d7e to your computer and use it in GitHub Desktop.
mimic what mgcv::predict.gam() does
# simple example to mimic what predict.gam does
library(mgcv) # GAM package, see Wood's book https://www.taylorfrancis.com/books/9781315370279
dat <- MASS::mcycle # get mcycle data
head(dat)
# set up a smoother
sm <- smoothCon(s(times, k = 10), data = dat, knots = NULL)[[1]]
# use it to fit a regression spline model
beta <- coef(lm(dat$accel ~ sm$X - 1))
# plot data
plot(dat$times, dat$accel)
# create prediction times
times <- seq(0, 60, length = 200)
# get matrix mapping beta to spline prediction at'times'
Xp <- PredictMat(sm, data.frame(times = times))
# add smooth to plot
lines(times, Xp%*%beta)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment