Skip to content
Prev 326941 / 398502 Next

MGCV: overlay fitted (marginal) curves over a plot of the original data

Probably you didn't want to set x0=0:1? Here is some code, to do what you want.
(The CI shape is not identical to the plot(b) version as the uncertainty includes
the uncertainty in the other smooths and the intercept now.)

library(mgcv)
set.seed(2)
dat <- gamSim(1,n=400,dist="normal",scale=2)
b <- gam(y~s(x0)+s(x1)+s(x2)+s(x3),data=dat)
plot(b,select=1)

plot(y~x0,dat)
mydata=data.frame(x0=0:200/200,x1=mean(dat$x1),x2=mean(dat$x2),x3=mean(dat$x3))
pv <- predict(b,mydata,type="response",se=TRUE)
lines(mydata$x0,pv$fit)
lines(mydata$x0,pv$fit+2*pv$se.fit,lty=2)
lines(mydata$x0,pv$fit-2*pv$se.fit,lty=2)
On 16/07/13 09:52, Christoph Scherber wrote: