Skip to content

Instantly share code, notes, and snippets.

@dill
Last active May 20, 2020 10:32
Show Gist options
  • Save dill/5419eec66b2490de8990f5fdbfa86e49 to your computer and use it in GitHub Desktop.
Save dill/5419eec66b2490de8990f5fdbfa86e49 to your computer and use it in GitHub Desktop.
Projection matrices with fit=FALSE
library(mgcv)
set.seed(2) ## simulate some data...
dat <- gamSim(1,n=400,dist="normal",scale=2)
b <- gam(y~s(x1), data=dat, fit=FALSE)
newpred <- data.frame(x1=seq(0, 10, length.out=100))
# Lp for the data
lp_data <- PredictMat(b$smooth[[1]], dat)
# same? (ignore intercept)
all.equal(b$X[,-1], lp_data, check.attributes=FALSE)
# compare to actually fitting the model
bfit <- gam(y~s(x1),data=dat)
lp_data_fit <- predict(bfit, dat, type="lpmatrix")
# need to drop first column (intercept!)
all.equal(lp_data_fit[,-1], lp_data, check.attributes=FALSE)
# Lp for prediction grid
# unfit
lp_pred <- PredictMat(b$smooth[[1]], newpred)
# fit
lp_pred_fit <- predict(bfit, newpred, type="lpmatrix")
# same?
all.equal(lp_pred_fit[,-1], lp_pred, check.attributes=FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment