[R] GAMM : how to use a smoother for some levels of a variable, and a linear effect for other levels?

Gavin Simpson gavin.simpson at ucl.ac.uk
Wed Apr 14 21:33:14 CEST 2010

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?



> 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

More information about the R-help mailing list