Skip to content

Instantly share code, notes, and snippets.

@timcdlucas
Last active March 26, 2021 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timcdlucas/1cf69746db414d0aa0b2c6ae057774ea to your computer and use it in GitHub Desktop.
Save timcdlucas/1cf69746db414d0aa0b2c6ae057774ea to your computer and use it in GitHub Desktop.
categorical vars in gam
library(mgcv)
# From the man page without categorical.
set.seed(2) ## simulate some data...
dat <- gamSim(1,n=400,dist="normal",scale=2)
b <- gam(y~s(x0)+s(x1)+s(x2)+s(x3),data=dat)
summary(b)
plot(b,pages=1,residuals=TRUE) ## show partial residuals
preds <- predict(b, newdata = dat)
# Now add categorical
dat2 <- cbind(dat,
categ = factor(sample(letters[1:3], nrow(dat), replace = TRUE)),
categ2 = factor(sample(letters[1:3], nrow(dat), replace = TRUE)))
b2 <- gam(y~s(x0)+s(x1)+s(x2)+s(x3) + categ + categ2, data = dat2)
summary(b2)
plot(b2, pages=1, residuals=TRUE) ## show partial residuals
preds <- predict(b2, newdata = dat2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment