[R] Simulate from a GAM model

Gavin Simpson gavin.simpson at ucl.ac.uk
Tue Jul 1 23:41:32 CEST 2008


On Tue, 2008-07-01 at 17:12 +0000, Mark Lyman wrote:
> Does anybody have any suggestions on how I might simulate from fitted GAM 
> model? I am using the gam function in the mgcv package to fit a variable 
> coefficient model like the following from the examples. I would like simulate 
> based on the fitted model like the simulate function in the stats package does 
> for lm models.
> 
> library(mgcv)
> 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)

How about:

fit <- fitted(b)
sim.sd <- sqrt(deviance(b) / df.residual(b))
simu <- fit + rnorm(length(fit), mean = 0, sd = sim.sd)
simu

where simu contains a single simulation from the model.

G

> 
> 
> Thanks for any help you can give,
> Mark Lyman
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list