Skip to content
Prev 392304 / 398502 Next

mgcv: relative risk from GAM with distributed lag

I doubt you want a 7 by 1000 grid for your persp plot. Here's an example 
of producing a persp plot using predict.gam and a custom grid. Since 
only the effects of x1 and x2 are being plotted it doesn't matter what 
x0 and x3 are set to (the model is additive after all). In your case 
only one smooth term is involved of course.

library(mgcv)
n <- 200
sig <- 2
dat <- gamSim(1,n=n,scale=sig)

b <- gam(y~s(x0)+s(I(x1^2))+s(x2)+offset(x3),data=dat)

m1 <- 20;m2 <- 30; n <- m1*m2
x1 <- seq(.2,.8,length=m1);x2 <- seq(.2,.8,length=m2) ## marginal values 
for evaluation grid
df <- data.frame(x0=rep(.5,n),x1=rep(x1,m2),x2=rep(x2,each=m1),x3=rep(0,n))
pf <- predict(b,newdata=df,type="terms")

persp(x1,x2,matrix(pf[,2]+pf[,3],m1,m2),theta=-130,col="blue",zlab="")
On 23/07/2022 14:54, jade.shodan at googlemail.com wrote: