-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcuckoo_analyses_habitatuse_modeloutput_MS_20141027.R
332 lines (239 loc) · 13.9 KB
/
cuckoo_analyses_habitatuse_modeloutput_MS_20141027.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
##########################################################
#
# CUCKOO ANALYSES: MODEL OUTPUT - AIC OUTPUT AND GRAPHS OF TOP MODELS
#
# Samantha Franks
# 16 Sep 2014 (based on initial code from cuckoo_analyses_models_20140805.R)
#
##########################################################
######## NOTES ON ANALYSIS ########
# Model output derived from RData files generated from cuckoo_analyses_habitatuse_models_20140915.R
# Source file to output AIC table and table of parameter estimates
######################
#
#### FUNCTIONS ####
#
######################
### Ben Bolker's function for calculating CIs on predictions from an merMod object and plotting the results from his RPubs GLMM worked examples
# http://rpubs.com/bbolker/glmmchapter
# by specifying re.form=NA we're saying that we want the population-level prediction, i.e. setting the random effects to zero and getting a prediction for an average (or unknown) group
# Computing confidence intervals on the predicted values is relatively easy if we're willing to completely ignore the random effects, and the uncertainty of the random effects
########################################### alpha=0.16 is approximately equal to 84% CIs ######################################
easyPredCI <- function(model,newdata,alpha=0.16) {
## baseline prediction, on the linear predictor (logit) scale:
pred0 <- predict(model,re.form=NA,newdata=newdata)
## fixed-effects model matrix for new data
X <- model.matrix(formula(model,fixed.only=TRUE)[-2],
newdata)
beta <- fixef(model) ## fixed-effects coefficients
V <- vcov(model) ## variance-covariance matrix of beta
pred.se <- sqrt(diag(X %*% V %*% t(X))) ## std errors of predictions
## inverse-link (logistic) function: could also use plogis()
linkinv <- model@resp$family$linkinv
## construct 95% Normal CIs on the link scale and
## transform back to the response (probability) scale:
crit <- -qnorm(alpha/2)
linkinv(cbind(lwr=pred0-crit*pred.se,
upr=pred0+crit*pred.se))
}
### FUNCTION TO LOAD REQUIRED PACKAGES
habitatusepackages <- function() {
library(plyr)
library(reshape)
library(lme4)
library(arm)
library(ggplot2)
library(grid)
library(scales)
}
### Capture warnings and errors from models
myTryCatch <- function(expr) {
warn <- err <- NULL
value <- withCallingHandlers(
tryCatch(expr, error=function(e) {
err <<- e
NULL
}), warning=function(w) {
warn <<- w
invokeRestart("muffleWarning")
})
list(value=value, warning=warn, error=err)
}
#########################################################################################
#########################################################################################
#########################################################################################
# loads habitatusepackages (as a function so it can be saved for use later)
habitatusepackages()
Mac <- FALSE
###----------------------------------------------###
#### SET WORKING DIRECTORIES ####
###----------------------------------------------###
if (!Mac) parentwd <- c("C:/Users/samf/Documents/Git/cuckoos")
if (Mac) parentwd <- c("/Volumes/SAM250GB/BTO PC Documents/Git/cuckoos")
datawd <- paste(parentwd, "/data", sep="")
outputwd <- paste(parentwd, "/output/autumn stopover GLMM results", sep="")
workspacewd <- paste(parentwd, "/workspaces/rerun workspaces", sep="")
setwd(workspacewd)
n <- length(list.files())
for (a in 1:n) {
setwd(workspacewd)
load(list.files()[a])
####################################################
#
#### GENERATE AIC OUTPUT & MODEL SUMMARIES ####
#
####################################################
# m <- SEmodels
# route <- "SE"
# randomradius <- 200
# analysistype <- "FM"
setwd(paste(parentwd, "/scripts/", sep=""))
source("source code cuckoo model selection.R")
### CLEAN WORKSPACE OF LARGE OBJECTS, leave packages and certain variables
# loaded model objects are very large and slow R down
# clear workspace of all objects EXCEPT: libraries, Mac, parentwd, datawd, outputwd, workspacewd, n, and a
# # rm(list=(ls()[-which(ls()=="Mac" | ls()=="parentwd" | ls()=="datawd" | ls()=="outputwd" | ls()=="workspacewd" | ls()=="n" | ls()=="a" | ls()=="analysistype" | ls()=="dat" | ls()=="route" | ls()=="randomradius"]))
keepvars <- c("Mac", "parentwd", "datawd", "outputwd", "workspacewd", "n", "a", "analysistype", "dat", "route", "randomradius", "topmodel")
# removes everything but functions and variables to keep; function habitatusepackages will reload required packages
allvars.nofunctions <- setdiff(ls(), lsf.str())
rm(list=setdiff(allvars.nofunctions, keepvars))
######################
#
#### PLOTS ####
#
######################
####==== HABITAT & PROTECTED AREA INTERACTION PLOT ====####
### DATA
newdat <- data.frame(habitat=levels(dat$habitat), PA=rep(levels(dat$PA), 5), elevation=mean(dat$elevation), spei.Mar=mean(dat$spei.Mar), spei.Aug=mean(dat$spei.Aug))
pred <- predict(topmodel, newdata=newdat, type="response", re.form=NA)
pred.CI <- data.frame(easyPredCI(topmodel, newdat))
plotdat <- data.frame(newdat, pred, pred.CI)
### PLOT SPECIFICATIONS
plottheme <- theme(
axis.title.x = element_text(face="bold", size=16, vjust=0.1),
axis.text.x = element_text(size=14, colour="black"),
axis.title.y = element_text(face="bold", size=16, vjust=0.9),
axis.text.y = element_text(size=14, vjust=0.5, colour="black"),
plot.title = element_text(size=16, face="bold", vjust=0.5),
legend.text=element_text(size=14),
legend.title=element_text(size=14),
legend.key=element_rect(fill="white"),
legend.key.width=unit(2,"cm"),
panel.background=element_rect(colour="black", fill="white"),
axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
)
if (analysistype=="FM") analysislabel <- "OVERALL"
if (analysistype=="DL") analysislabel <- "DESIGNATION LEVEL"
if (analysistype=="SS") analysislabel <- "SHORT STOPOVERS"
if (analysistype=="LS") analysislabel <- "LONG STOPOVERS"
if (analysistype=="FS") analysislabel <- "FINAL STOPOVERS"
if (analysistype=="DW") analysislabel <- "DESIGNATION LEVEL, LUMPING No and National for SW"
if (analysistype=="FR") analysislabel <- "FRANCE"
if (analysistype=="DE") analysislabel <- "GERMANY & LOW COUNTRIES"
if (analysistype=="IT") analysislabel <- "ITALY"
if (analysistype=="SP") analysislabel <- "SPAIN"
plotlabels <- labs(x="Habitat", y="Predicted Probability", title=paste("Predicted probability of presence at mean elevation and climate values", route, randomradius, "km", analysislabel, sep=" "))
xscale <- scale_x_discrete("Habitat", labels = c("scrub.grassland"="scrub/grassland", "wetland.water"="wetland/water"))
#habitatPA.yscale <- scale_y_continuous(limits=c(0,max(plotdat$upr) + 0.2))
### PLOT
p0 <- ggplot(plotdat, aes(x=habitat,y=pred, shape=PA, linetype=PA, group=PA, ymin=lwr, ymax=upr))
p0 + geom_point(size=4) + geom_line() + geom_errorbar(width=0.1) + plottheme + plotlabels + xscale
### SAVE PLOT
setwd(outputwd)
ggsave(paste(analysistype, route, randomradius, "km top model habitat PA interaction plot.jpg", sep=" "), height=9.4, width=14, units="in")
####==== HABITAT PLOT (holding all else at their means) ====####
plotdat2 <- aggregate(plotdat[,c("pred","lwr","upr")], by=list(habitat=plotdat$habitat, elevation=plotdat$elevation, spei.Mar=plotdat$spei.Mar, spei.Aug=plotdat$spei.Aug), mean)
plotdat2 <- rename(plotdat2, c("x"="pred"))
p1 <- ggplot(plotdat2, aes(x=habitat, y=pred, ymin=lwr, ymax=upr, group=1))
p1 + geom_point(size=4) + geom_line() + geom_errorbar(width=0.1) + plottheme + plotlabels + xscale
### SAVE PLOT
setwd(outputwd)
ggsave(paste(analysistype, route, randomradius, "km top model mean habitat plot.jpg", sep=" "), height=9.4, width=14, units="in")
####==== ELEVATION PLOT ====####
### DATA for each habitat x PA combination
elevationseq <- seq(min(dat$elevation), max(dat$elevation), 0.01)
newdat <- data.frame(elevation=elevationseq, habitat=rep(levels(dat$habitat), each=length(elevationseq)), PA=rep(levels(dat$PA), each=length(elevationseq)*length(levels(dat$habitat))) , spei.Mar=mean(dat$spei.Mar), spei.Aug=mean(dat$spei.Aug))
pred <- predict(topmodel, newdata=newdat, type="response", re.form=NA)
pred.CI <- data.frame(easyPredCI(topmodel, newdat))
plotdat <- data.frame(newdat, pred, pred.CI)
### PLOT SPECIFICATIONS
elevationplotlabels <- labs(x="Elevation", y="Predicted Probability", title=paste("Predicted probability of presence vs elevation", route, randomradius, "km", analysislabel, sep=" "))
elevation.yscale <- scale_y_continuous(limits=c(0,max(plotdat$pred) + 0.1))
### PLOT
p0 <- ggplot(plotdat, aes(x=elevation, y=pred, colour=habitat, linetype=PA, ymin=lwr, ymax=upr))
# without confidence bands
p0 + geom_line(size=1.5) + plottheme + elevationplotlabels + elevation.yscale + scale_color_manual(values=c("#FFCC33", "#009900", "#FF6600", "#FF0000", "#0000FF"))
# # with confidence bands
# p0 + geom_line(size=1.5) + geom_ribbon(aes(fill=habitat, linetype=NA), alpha=0.1) + plottheme + elevationplotlabels + elevation.yscale _ scale_color_manual(values=c("#FFCC33", "#009900", "#FF6600", "#FF0000", "#0000FF")) + scale_fill_manual(values=c("#FFCC33", "#009900", "#FF6600", "#FF0000", "#0000FF"))
### SAVE PLOT
setwd(outputwd)
ggsave(paste(analysistype, route, randomradius, "km top model elevation habitat + PA plot.jpg", sep=" "), height=9.4, width=14, units="in")
#-----------------------
### DATA for averaging for each habitat type across PA level
plotdat2 <- aggregate(plotdat$pred, by=list(elevation=plotdat$elevation, habitat=plotdat$habitat), mean)
plotdat2 <- rename(plotdat2, c("x"="pred"))
### PLOT
p1 <- ggplot(plotdat2, aes(x=elevation, y=pred, colour=habitat))
p1 + geom_line(size=1.5) + plottheme + elevationplotlabels + elevation.yscale + scale_color_manual(values=c("#FFCC33", "#009900", "#FF6600", "#FF0000", "#0000FF"))
### SAVE PLOT
setwd(outputwd)
ggsave(paste(analysistype, route, randomradius, "km top model elevation mean habitat plot.jpg", sep=" "), height=9.4, width=14, units="in")
####==== SUMMER CLIMATE PLOT ====####
### DATA for each habitat x PA combination
climateseq <- seq(min(dat$spei.Aug), max(dat$spei.Aug), 0.01)
newdat <- data.frame(spei.Aug=climateseq, habitat=rep(levels(dat$habitat), each=length(climateseq)), PA=rep(levels(dat$PA), each=length(climateseq)*length(levels(dat$habitat))) , elevation=mean(dat$elevation), spei.Mar=mean(dat$spei.Mar))
pred <- predict(topmodel, newdata=newdat, type="response", re.form=NA)
pred.CI <- data.frame(easyPredCI(topmodel, newdat))
plotdat <- data.frame(newdat, pred, pred.CI)
### PLOT SPECIFICATIONS
climateplotlabels <- labs(x="Climate", y="Predicted Probability", title=paste("Predicted probability of presence vs growing season climate", route, randomradius, "km", analysislabel, sep=" "))
climate.yscale <- scale_y_continuous(limits=c(0,max(plotdat$pred) + 0.1))
### PLOT
p0 <- ggplot(plotdat, aes(x=spei.Aug, y=pred, colour=habitat, linetype=PA, ymin=lwr, ymax=upr))
# without confidence bands
p0 + geom_line(size=1.5) + plottheme + climateplotlabels + climate.yscale + scale_color_manual(values=c("#FFCC33", "#009900", "#FF6600", "#FF0000", "#0000FF"))
### SAVE PLOT
setwd(outputwd)
ggsave(paste(analysistype, route, randomradius, "km top model spring climate habitat + PA plot.jpg", sep=" "), height=9.4, width=14, units="in")
#-----------------------
### DATA for averaging for each habitat type across PA level
plotdat2 <- aggregate(plotdat$pred, by=list(spei.Aug=plotdat$spei.Aug, habitat=plotdat$habitat), mean)
plotdat2 <- rename(plotdat2, c("x"="pred"))
### PLOT
p1 <- ggplot(plotdat2, aes(x=spei.Aug, y=pred, colour=habitat))
p1 + geom_line(size=1.5) + plottheme + climateplotlabels + climate.yscale + scale_color_manual(values=c("#FFCC33", "#009900", "#FF6600", "#FF0000", "#0000FF"))
### SAVE PLOT
setwd(outputwd)
ggsave(paste(analysistype, route, randomradius, "km top model spring climate mean habitat plot.jpg", sep=" "), height=9.4, width=14, units="in")
####==== WINTER CLIMATE PLOT ====####
climateseq <- seq(min(dat$spei.Mar), max(dat$spei.Mar), 0.01)
newdat <- data.frame(spei.Mar=climateseq, habitat=rep(levels(dat$habitat), each=length(climateseq)), PA=rep(levels(dat$PA), each=length(climateseq)*length(levels(dat$habitat))) , elevation=mean(dat$elevation), spei.Aug=mean(dat$spei.Aug))
pred <- predict(topmodel, newdata=newdat, type="response", re.form=NA)
pred.CI <- data.frame(easyPredCI(topmodel, newdat))
plotdat <- data.frame(newdat, pred, pred.CI)
### PLOT SPECIFICATIONS
climateplotlabels <- labs(x="Climate", y="Predicted Probability", title=paste("Predicted probability of presence vs winter climate", route, randomradius, "km", analysislabel, sep=" "))
climate.yscale <- scale_y_continuous(limits=c(0,max(plotdat$pred) + 0.1))
### PLOT
p0 <- ggplot(plotdat, aes(x=spei.Mar, y=pred, colour=habitat, linetype=PA, ymin=lwr, ymax=upr))
# without confidence bands
p0 + geom_line(size=1.5) + plottheme + climateplotlabels + climate.yscale + scale_color_manual(values=c("#FFCC33", "#009900", "#FF6600", "#FF0000", "#0000FF"))
### SAVE PLOT
setwd(outputwd)
ggsave(paste(analysistype, route, randomradius, "km top model winter climate habitat + PA plot.jpg", sep=" "), height=9.4, width=14, units="in")
#-----------------------
### DATA for averaging for each habitat type across PA level
plotdat2 <- aggregate(plotdat$pred, by=list(spei.Mar=plotdat$spei.Mar, habitat=plotdat$habitat), mean)
plotdat2 <- rename(plotdat2, c("x"="pred"))
### PLOT
p1 <- ggplot(plotdat2, aes(x=spei.Mar, y=pred, colour=habitat))
p1 + geom_line(size=1.5) + plottheme + climateplotlabels + climate.yscale + scale_color_manual(values=c("#FFCC33", "#009900", "#FF6600", "#FF0000", "#0000FF"))
### SAVE PLOT
setwd(outputwd)
ggsave(paste(analysistype, route, randomradius, "km top model winter climate mean habitat plot.jpg", sep=" "), height=9.4, width=14, units="in")
########################################################################
########################################################################
########################################################################
}