| 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 | |
| 83 | library(AED); data(Ythan) |
| 84 | library(lattice) |
| 85 | Birds <- as.vector(as.matrix(Ythan[,2:8])) |
| 86 | X <- as.vector(as.matrix(Ythan[,9:15])) |
| 87 | YX14 <- c(Birds,X) |
| 88 | Year14 <- rep(Ythan$Year,14) |
| 89 | N <- length(Ythan$Year) |
| 90 | ID14<-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 | |
| 97 | xyplot(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 | |
| 112 | Birds7 <- as.vector(as.matrix(Ythan[,2:8])) |
| 113 | BirdNames<-c("Oystercatcher","Turnstone", |
| 114 | "Curlew","BartailedGodwit","Redshank","Knot", |
| 115 | "Dunlin") |
| 116 | ID7<-factor(rep(BirdNames, each = N), levels = BirdNames) |
| 117 | Year7<-rep(Ythan$Year,7) |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | Oyst.01 <- as.numeric(ID7=="Oystercatcher") |
| 123 | Turn.01 <- as.numeric(ID7=="Turnstone") |
| 124 | Curl.01 <- as.numeric(ID7=="Curlew") |
| 125 | Bart.01 <- as.numeric(ID7=="BartailedGodwit") |
| 126 | Reds.01 <- as.numeric(ID7=="Redshank") |
| 127 | Knot.01 <- as.numeric(ID7=="Knot") |
| 128 | Dunl.01 <- as.numeric(ID7=="Dunlin") |
| 129 | |
| 130 | #For older R versions (2.7.0 and before), use: |
| 131 | f7 <- 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: |
| 142 | f7 <- formula(Birds7 ~ ID7 + s(Year7, by = ID7, bs = "cr")) |
| 143 | |
| 144 | |
| 145 | |
| 146 | library(mgcv);library(nlme) |
| 147 | lmc<-lmeControl(niterEM=5000,msMaxIter=1000) |
| 148 | |
| 149 | M0<-gamm(f7, |
| 150 | control=lmc,method="REML", |
| 151 | weights=varIdent(form=~1|ID7)) |
| 152 | |
| 153 | AIC(M0$lme) |
| 154 | |
| 155 | |
| 156 | p1<-plot(M0$lme,resid(.,type="n")~fitted(.),abline=0,col=1) |
| 157 | p2<-plot(M0$lme,resid(.,type="n")~Year7,abline=0,col=1,xlab="Year") |
| 158 | p3<-plot(M0$lme,resid(.,type="n")~fitted(.)|ID7, |
| 159 | abline=0,col=1,par.strip.text=list(cex = 0.75)) |
| 160 | |
| 161 | |
| 162 | print(p1, position = c(0,0,1,1), split=c(1,1,2,2), more=TRUE) |
| 163 | print(p2, position = c(0,0,1,1), split=c(2,1,2,2), more=TRUE) |
| 164 | print(p3, position = c(0,0,2,1), split=c(1,2,2,2), more=FALSE) |
| 165 | |
| 166 | |
| 167 | plot(M0$lme,resid(.,type="n")~Year7|ID7,abline=0,col=1,xlab="Year") |
| 168 | ######################################################## |
| 169 | }}} |