Skip to content

mgcv package plot superimposing smoothers

2 messages · Jef Vlegels, Jannis

#
Dear R help list,

I'm fitting a 'variable coefficient model' in the MGCV package and I want to
plot the different smoothers I get for each factor level in one graph. 

So, I do something like this to fit the gam:
Mtest <- gam(outcome ~ s(age, by=as.numeric(gender==0)) +
s(age,by=as.numeric(gender==1))+factor(Gender))

Then I can plot the smoother for gender=0:
plot(Mtest,select=1)
And for gender=1:
plot(Mtest,select=2)

But the second plot superimposes the first one. Normally I use 'lines' to
plot just the line from the second plot in the first plot, but this doesn't
seem to work with the plot.gam function. 
Does someone have a solution to plot these two smoothers in one graph, each
with a different color/line type?

Thanks in advance,
Jef Vlegels
3 days later
#
Hi Jef,


I would extract the information about the smoothers and plot them using 
standard plotting routines. After

Mtest <- gam(outcome ~ s(age, by=as.numeric(gender==0)) +
s(age,by=as.numeric(gender==1))+factor(Gender))

run something similar to:

smoothers <- predict(s,type='terms')

and you get the values for the smoothers saved to "smoothers" (See 
?gam.predict). You may haave to tweak around a bit as I could not test 
the function. Another solution may be (also untested):

plot(Mtest,select=1)
par(new=TRUE)
plot(Mtest,select=2)


HTH
Jannis

Jef Vlegels schrieb: