JANSEN, Ivy
2010-Apr-14 08:03 UTC
[R] GAMM : how to use a smoother for some levels of a variable, and a linear effect for other levels?
Hi, I was reading the book on "Mixed Effects Models and Extensions in Ecology with R" by Zuur et al. In Section 6.2, an example is discussed where a gamm-model is fitted, with a smoother for time, which differs for each value of ID (4 different bird species). In earlier versions of R, the following code was used BM2<-gamm(Birds~Rain+ID+ s(Time,by=as.numeric(ID=="Stilt.Oahu"))+ s(Time,by=as.numeric(ID=="Stilt.Maui"))+ s(Time,by=as.numeric(ID=="Coot.Oahu"))+ s(Time,by=as.numeric(ID=="Coot.Maui")), correlation=corAR1(form=~Time |ID ), weights=varIdent(form=~1|ID)) However, in the current version of R, this does not work anymore, and should be changed into BM2<-gamm(Birds~Rain+ID+ s(Time,by=ID), correlation=corAR1(form=~Time |ID ), weights=varIdent(form=~1|ID)) It turns out that 2 of the 4 smoothers have estimated degrees of freedom of 1, so a linear effect would be sufficient. Now my question is how I need to change the code in order to have a time smoother for ID=Coot.Oahu and ID=Coot.Maui, and a linear time effect for ID=Stilt.Oahu and ID=Stilt.Maui. With the "old" R-code, this seems trivial, but I don't have any idea how to do it in the newest R-version (interactions with a dummy variable do not work in gamm). Thanks, Ivy Druk dit bericht a.u.b. niet onnodig af. Please do not print this message unnecessarily. Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document. The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.
Gavin Simpson
2010-Apr-14 19:33 UTC
[R] GAMM : how to use a smoother for some levels of a variable, and a linear effect for other levels?
On Wed, 2010-04-14 at 10:03 +0200, JANSEN, Ivy wrote:> Hi, > > I was reading the book on "Mixed Effects Models and Extensions in > Ecology with R" by Zuur et al. > In Section 6.2, an example is discussed where a gamm-model is fitted, > with a smoother for time, which differs for each value of ID (4 > different bird species). In earlier versions of R, the following code > was used > > BM2<-gamm(Birds~Rain+ID+ > s(Time,by=as.numeric(ID=="Stilt.Oahu"))+ > s(Time,by=as.numeric(ID=="Stilt.Maui"))+ > s(Time,by=as.numeric(ID=="Coot.Oahu"))+ > s(Time,by=as.numeric(ID=="Coot.Maui")), > correlation=corAR1(form=~Time |ID ), > weights=varIdent(form=~1|ID)) > > However, in the current version of R, this does not work anymore, and > should be changed into > > BM2<-gamm(Birds~Rain+ID+ > s(Time,by=ID), > correlation=corAR1(form=~Time |ID ), > weights=varIdent(form=~1|ID)) > > It turns out that 2 of the 4 smoothers have estimated degrees of freedom > of 1, so a linear effect would be sufficient. > Now my question is how I need to change the code in order to have a time > smoother for ID=Coot.Oahu and ID=Coot.Maui, and a linear time effect for > ID=Stilt.Oahu and ID=Stilt.Maui. With the "old" R-code, this seems > trivial, but I don't have any idea how to do it in the newest R-version > (interactions with a dummy variable do not work in gamm).Isn't a smooth that uses 1 df == to a linear function? So doesn't the current model already do what you want? You don't need to refit it with two smooths and two linear parametric terms as the two models should be effectively equivalent anyway. Or have I misunderstood your question? HTH G> > Thanks, > Ivy > > Druk dit bericht a.u.b. niet onnodig af. > Please do not print this message unnecessarily. > > Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer > en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is > door een geldig ondertekend document. The views expressed in this message > and any annex are purely those of the writer and may not be regarded as stating > an official position of INBO, as long as the message is not confirmed by a duly > signed document. > > ______________________________________________ > 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.-- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Simon Wood
2010-Apr-16 14:42 UTC
[R] GAMM : how to use a smoother for some levels of a variable, and a linear effect for other levels?
Both versions of this should have worked, but you are right that the first version didn't when used with `gamm', I've fixed this for mgcv 1.6-2 (`mgcv:gam' was ok). Thanks for this. best, Simon On Wednesday 14 April 2010 09:03, JANSEN, Ivy wrote:> Hi, > > I was reading the book on "Mixed Effects Models and Extensions in > Ecology with R" by Zuur et al. > In Section 6.2, an example is discussed where a gamm-model is fitted, > with a smoother for time, which differs for each value of ID (4 > different bird species). In earlier versions of R, the following code > was used > > BM2<-gamm(Birds~Rain+ID+ > s(Time,by=as.numeric(ID=="Stilt.Oahu"))+ > s(Time,by=as.numeric(ID=="Stilt.Maui"))+ > s(Time,by=as.numeric(ID=="Coot.Oahu"))+ > s(Time,by=as.numeric(ID=="Coot.Maui")), > correlation=corAR1(form=~Time |ID ), > weights=varIdent(form=~1|ID)) > > However, in the current version of R, this does not work anymore, and > should be changed into > > BM2<-gamm(Birds~Rain+ID+ > s(Time,by=ID), > correlation=corAR1(form=~Time |ID ), > weights=varIdent(form=~1|ID)) > > It turns out that 2 of the 4 smoothers have estimated degrees of freedom > of 1, so a linear effect would be sufficient. > Now my question is how I need to change the code in order to have a time > smoother for ID=Coot.Oahu and ID=Coot.Maui, and a linear time effect for > ID=Stilt.Oahu and ID=Stilt.Maui. With the "old" R-code, this seems > trivial, but I don't have any idea how to do it in the newest R-version > (interactions with a dummy variable do not work in gamm). > > Thanks, > Ivy > > Druk dit bericht a.u.b. niet onnodig af. > Please do not print this message unnecessarily. > > Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver > weer en binden het INBO onder geen enkel beding, zolang dit bericht niet > bevestigd is door een geldig ondertekend document. The views expressed in > this message and any annex are purely those of the writer and may not be > regarded as stating an official position of INBO, as long as the message is > not confirmed by a duly signed document. > > ______________________________________________ > 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.--> Simon Wood, Mathematical Sciences, University of Bath, Bath, BA2 7AY UK > +44 1225 386603 www.maths.bath.ac.uk/~sw283