Changes between Version 9 and Version 10 of GAM with interaction


Ignore:
Timestamp:
Apr 8, 2011 11:24:12 AM (14 years ago)
Author:
celine
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • GAM with interaction

    v9 v10  
    6161See in Mixed Effect Modes and Extensions in Ecology with R (2009), A. F.Zuur et al., Statistics for Biology and Health, Springer chapter 15 (page367-368)[[BR]] 
    6262See in http://www.highstat.com/book2.htm  for the code. [[BR]] 
     63 
     64{{{ 
     65#   Mixed Effects Models and Extensions in Ecology with R (2009) 
     66#    Zuur, Ieno, Walker, Saveliev and Smith.    Springer 
     67#    This file was produced by Alain Zuur (highstat@highstat.com) 
     68#    www.highstat.com 
     69 
     70#    This program is free software; you can redistribute it and/or modify 
     71#    it under the terms of the GNU General Public License as published by 
     72#    the Free Software Foundation; either version 2 of the License, or 
     73#    (at your option) any later version. 
     74# 
     75#    This program is distributed in the hope that it will be useful, 
     76#    but WITHOUT ANY WARRANTY; without even the implied warranty of 
     77#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     78#    GNU General Public License for more details. 
     79 
     80 
     81 
     82 
     83library(AED); data(Ythan) 
     84library(lattice) 
     85Birds <- as.vector(as.matrix(Ythan[,2:8])) 
     86X <- as.vector(as.matrix(Ythan[,9:15])) 
     87YX14 <- c(Birds,X) 
     88Year14 <- rep(Ythan$Year,14) 
     89N <- length(Ythan$Year) 
     90ID14<-factor(rep(names(Ythan[,2:15]),each = N), 
     91     levels=c("wheat", 
     92    "barley","oats","cattle","sheep","pigs", 
     93    "nitrate","Oystercatcher","Turnstone","Curlew", 
     94    "BartailedGodwit","Redshank","Knot","Dunlin")) 
     95 
     96 
     97xyplot(YX14~Year14|ID14,xlab="Time",ylab="Variable", 
     98   layout=c(4,4), 
     99   scales=list(alternating=T,x=list(relation = "same"), 
     100               y = list(relation = "free")), 
     101   panel=function(x,y){ 
     102     panel.xyplot(x,y,col=1) 
     103     panel.grid(h=-1,v=2) 
     104     panel.loess(x,y,col=1,span=0.5) 
     105     I2<-is.na(y) 
     106     panel.text(x[I2],min(y,na.rm=T),'|',cex=0.5)  }) 
     107 
     108 
     109##########  Section 15.3 Estimation of trends 
     110 
     111 
     112Birds7 <- as.vector(as.matrix(Ythan[,2:8])) 
     113BirdNames<-c("Oystercatcher","Turnstone", 
     114   "Curlew","BartailedGodwit","Redshank","Knot", 
     115   "Dunlin") 
     116ID7<-factor(rep(BirdNames, each = N), levels = BirdNames) 
     117Year7<-rep(Ythan$Year,7) 
     118 
     119 
     120 
     121 
     122Oyst.01 <- as.numeric(ID7=="Oystercatcher") 
     123Turn.01 <- as.numeric(ID7=="Turnstone") 
     124Curl.01 <- as.numeric(ID7=="Curlew") 
     125Bart.01 <- as.numeric(ID7=="BartailedGodwit") 
     126Reds.01 <- as.numeric(ID7=="Redshank") 
     127Knot.01 <- as.numeric(ID7=="Knot") 
     128Dunl.01 <- as.numeric(ID7=="Dunlin") 
     129 
     130#For older R versions (2.7.0 and before), use: 
     131f7 <- formula(Birds7 ~ ID7 + 
     132    s(Year7, by = Oyst.01, bs = "cr") + 
     133    s(Year7, by = Turn.01, bs = "cr") + 
     134    s(Year7, by = Curl.01, bs = "cr") + 
     135    s(Year7, by = Bart.01, bs = "cr") + 
     136    s(Year7, by = Reds.01, bs = "cr") + 
     137    s(Year7, by = Knot.01, bs = "cr") + 
     138    s(Year7, by = Dunl.01, bs = "cr")) 
     139     
     140     
     141#For later R versions, use: 
     142f7 <- formula(Birds7 ~ ID7 + s(Year7, by = ID7, bs = "cr")) 
     143 
     144 
     145 
     146library(mgcv);library(nlme) 
     147lmc<-lmeControl(niterEM=5000,msMaxIter=1000) 
     148 
     149M0<-gamm(f7, 
     150      control=lmc,method="REML", 
     151      weights=varIdent(form=~1|ID7)) 
     152 
     153AIC(M0$lme) 
     154 
     155 
     156p1<-plot(M0$lme,resid(.,type="n")~fitted(.),abline=0,col=1) 
     157p2<-plot(M0$lme,resid(.,type="n")~Year7,abline=0,col=1,xlab="Year") 
     158p3<-plot(M0$lme,resid(.,type="n")~fitted(.)|ID7, 
     159         abline=0,col=1,par.strip.text=list(cex = 0.75)) 
     160 
     161 
     162print(p1, position = c(0,0,1,1), split=c(1,1,2,2), more=TRUE) 
     163print(p2, position = c(0,0,1,1), split=c(2,1,2,2), more=TRUE) 
     164print(p3, position = c(0,0,2,1), split=c(1,2,2,2), more=FALSE) 
     165 
     166 
     167plot(M0$lme,resid(.,type="n")~Year7|ID7,abline=0,col=1,xlab="Year") 
     168######################################################## 
     169}}}