[R] mgcv package plot superimposing smoothers

Jannis bt_jannis at yahoo.de
Mon Dec 6 21:22:33 CET 2010

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)) +

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):



Jef Vlegels schrieb:
> 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
> ______________________________________________
> 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