Skip to content

Instantly share code, notes, and snippets.

@dill
Created December 29, 2015 23:27
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 dill/57c07217a347f91f517a to your computer and use it in GitHub Desktop.
Save dill/57c07217a347f91f517a to your computer and use it in GitHub Desktop.
cyclic-random effects tensor for Noam
# cyclic-random effects tensor for Noam
library(mgcv)
# code adapted from ?gam.model
dat <- gamSim(1,n=400,scale=2) ## simulate 4 term additive truth
## Now add some random effects to the simulation. Response is
## grouped into one of 20 groups by `fac' and each groups has a
## random effect added....
fac <- as.factor(sample(1:20,400,replace=TRUE))
dat$X <- model.matrix(~fac-1)
b <- rnorm(20)*.5
dat$y <- dat$y + dat$X%*%b
# model in the documentation
rm1 <- gam(y ~ s(fac,bs="re")+s(x0)+s(x1)+s(x2)+s(x3),data=dat,method="ML")
# what about a smooth-re interaction?
rm2 <- gam(y ~ te(x2,fac,bs=c("tp","re"))+s(x0)+s(x1)+s(x3),data=dat,method="ML")
# for a cyclic spline need to switch "tp" for "cc" and read
# ?smooth.construct.cc.smooth.spec for info on end points
# not sure how model checking works on such a model!!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment