### Name: gam.models ### Title: Specifying generalized additive models ### Aliases: gam.models ### Keywords: models regression ### ** Examples set.seed(10) ## simulate date from y = f(x2)*x1 + error dat <- gamSim(3,n=400) b<-gam(y ~ s(x2,by=x1),data=dat) plot(b,pages=1) summary(b) ## Factor `by' variable example (with a spurious covariate x0) ## simulate data... dat <- gamSim(4) ## fit model... b <- gam(y ~ fac+s(x2,by=fac)+s(x0),data=dat) plot(b,pages=1) summary(b) ## note that the preceding fit is the same as.... b1<-gam(y ~ s(x2,by=as.numeric(fac==1))+s(x2,by=as.numeric(fac==2))+ s(x2,by=as.numeric(fac==3))+s(x0)-1,data=dat) ## ... the `-1' is because the intercept is confounded with the ## *uncentred* smooths here. plot(b1,pages=1) summary(b1) ## repeat forcing all s(x2) terms to have the same smoothing param ## (not a very good idea for these data!) b2 <- gam(y ~ fac+s(x2,by=fac,id=1)+s(x0),data=dat) plot(b2,pages=1) summary(b2) rm(dat) ## An example of a simple random effects term implemented via ## penalization of the parametric part of the 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 ## now fit appropriate random effect model... PP <- list(X=list(rank=20,diag(20))) rm <- gam(y~ X+s(x0)+s(x1)+s(x2)+s(x3),data=dat,paraPen=PP) plot(rm,pages=1) ## Get estimated random effects standard deviation... sig.b <- sqrt(rm$sig2/rm$sp[1]);sig.b ## Simple comparison with lme, using Rail data. ## "ML" used as "REML" treatments of residual variance ## differ slightly... require(nlme) b0 <- lme(travel~1,data=Rail,~1|Rail,method="ML") Z <- model.matrix(~Rail-1,data=Rail, contrasts.arg=list(Rail="contr.treatment")) b <- gam(travel~Z,data=Rail,paraPen=list(Z=list(diag(6))),method="ML") b0 (b$reml.scale/b$sp)^.5 ## `gam' ML estimate of Rail sd b$reml.scale^.5 ## `gam' ML estimate of residual sd