[R] MGCV: overlay fitted (marginal) curves over a plot of the original data
Christoph Scherber
Christoph.Scherber at agr.uni-goettingen.de
Tue Jul 16 10:52:50 CEST 2013
Dear R users,
I´ve stumbled over a problem that can be easily seen from the R code below:
- When I use plot.gam() on a fitted model object, I get a nice and well-looking smooth curve for all
terms in the model.
- However, when I use predict(model) for a given predictor, with values of all other predictors set
to their means, the resulting curve doesn´t fit well at all.
Is there a way to "overlay" the curve produced by plot.gam() over a plot of the original data?
Here´s some reproducible code with mgcv version 1.7-22 on R3.0.1 (Windows 7):
##
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:1,x1=mean(dat$x1),x2=mean(dat$x2),x3=mean(dat$x3))
lines(0:1,predict(b,mydata,type="response"))
##
Best wishes,
Christoph
--
PD Dr Christoph Scherber
Georg-August University Goettingen
Department of Crop Science
Agroecology
Grisebachstrasse 6
D-37077 Goettingen
Germany
phone 0049 (0)551 39 8807
fax 0049 (0)551 39 8806
http://www.gwdg.de/~cscherb1
More information about the R-help
mailing list