This repository has been archived by the owner on Oct 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ind_tree_analysis_for_paper_061718.R
401 lines (320 loc) · 14.5 KB
/
ind_tree_analysis_for_paper_061718.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#### Analysis of field data
# Sam Flake, 12 Sep 2017
# load libraries
library(plyr)
library(boot)
library(lme4)
library(MuMIn)
library(car)
library(effects)
library(pscl)
library("pROC")
#Source some useful functions
source("pj_mortality_functions_090617.R")
## set up some options
set.seed(665224291)
options(na.action = na.fail)
pardefault <- par(no.readonly = T)
#read prepared data
all_tree_vars <- read.csv("./Clean data/all_tree_Vars_scaled_and_transformed.csv")
all_tree_vars_orig <- read.csv("./Clean data/all_tree_Vars_unscaled_untrans.csv")
#--------------------------------------------------------------------------------------------
# tree-level models
pimo_all_tree_vars <- all_tree_vars[all_tree_vars$Spp == "PIMO", ]
#global model with tree-level vars and abiotic vars
pimo_global <- lmer(formula = Delta_pdc ~ Neighbor_larger_log + ENN_dist_log + Diam_log + Height + BA_4m_log +
cwd_normal_cum + fdsi_anom +
cwd_peak_anom + tmean + ppt +
vpd_normal_annual_max + vpd_peak_anom + avg_summer_tmax +
peak_tmax + min_ann_ppt +
Pndjfm + Avg_depth + AWC +
(1|Cluster/site),
data = pimo_all_tree_vars,
na.action = na.fail)
#only use combos that aren't pairwise correlated
cormat <- abs(cor(all_tree_vars[all_tree_vars$Spp == "PIMO", c('Neighbor_larger_log', 'ENN_dist_log', 'Diam_log', 'Height', 'BA_4m_log',
'cwd_normal_cum', 'fdsi_anom',
'cwd_peak_anom' , 'tmean', 'ppt',
'vpd_normal_annual_max' , 'vpd_peak_anom' , 'avg_summer_tmax', 'peak_tmax',
'min_ann_ppt', 'Pndjfm' , 'Avg_depth' , 'AWC')])) <=.5
#use all tree-level vars
cormat[1:5,1:5] <- TRUE
#use only one side of matrix (avoid duplicated models)
cormat[!lower.tri(cormat)] <- NA
#find all combinations with a few given conditions:
# abiotic variables can change but stand structure variables are fixed;
# abiotic variables with correlations greater than .5 are not allowed;
# 9 maximum variables are allowed;
# progress is displayed in console
# this only takes a minute or two
pimo_dredge <- dredge(pimo_global,
fixed = c('Neighbor_larger_log', 'ENN_dist_log', 'Diam_log', 'Height', 'BA_4m_log'),
subset = cormat,
m.lim = c(5, 8),
trace = 2)
saveRDS(pimo_dredge, "./model output/pimo_dredge.RDS")
#get call for top model and evaluate it
best_pimo_call <- as.character(unlist(attr((pimo_dredge)[1], "model.calls")))
pimo_model_dredge <- eval(parse(text = best_pimo_call))
summary(pimo_model_dredge)
pimo_dredge[1:10] #top 10 models
write.csv(pimo_dredge[1:10], "./model output/pimo_mod_selection_table.csv")
# #----------------------------------------------
#
juos_all_tree_vars <- all_tree_vars[all_tree_vars$Spp == "JUOS", ]
#global model with tree-level vars and abiotic vars
juos_global <- lmer(formula = Delta_pdc ~ Neighbor_larger_log + ENN_dist_log + Diam_log + Height + BA_4m_log +
cwd_normal_cum + fdsi_anom +
cwd_peak_anom + tmean + ppt +
vpd_normal_annual_max + vpd_peak_anom + avg_summer_tmax +
peak_tmax + min_ann_ppt +
Pndjfm + Avg_depth + AWC +
(1|Cluster/site),
data = juos_all_tree_vars,
na.action = na.fail)
#only use combos that aren't pairwise correlated
cormat <- abs(cor(all_tree_vars[all_tree_vars$Spp == "JUOS", c('Neighbor_larger_log', 'ENN_dist_log', 'Height', 'Diam_log', 'BA_4m_log',
'cwd_normal_cum', 'fdsi_anom',
'cwd_peak_anom' , 'tmean', 'ppt',
'vpd_normal_annual_max' , 'vpd_peak_anom' , 'avg_summer_tmax', 'peak_tmax',
'min_ann_ppt', 'Pndjfm' , 'Avg_depth' , 'AWC')])) <=.5
#use all tree-level vars
cormat[1:5,1:5] <- TRUE
#use only one side of matrix (avoid duplicated models)
cormat[!lower.tri(cormat)] <- NA
#find all combinations
juos_dredge <- dredge(juos_global,
fixed = c('Neighbor_larger_log', 'ENN_dist_log', 'Height', 'Diam_log', 'BA_4m_log'),
subset = cormat,
m.lim = c(5, 8),
trace = 2)
saveRDS(juos_dredge, "./model output/juos_dredge.RDS")
#get call for top model and evaluate it
best_juos_call <- as.character(unlist(attr((juos_dredge)[1], "model.calls")))
juos_model_dredge <- eval(parse(text = best_juos_call))
write.csv(juos_dredge[1:10], "./model output/juos_mod_selection_table.csv")
#
# ###################################
# # PIMO mortality
# ###################################
pmort_global <- glmer(formula = Died ~ Neighbor_larger_log + ENN_dist_log + Diam_log + Height + BA_4m_log +
cwd_normal_cum + fdsi_anom +
cwd_peak_anom + tmean + ppt +
vpd_normal_annual_max + vpd_peak_anom + avg_summer_tmax +
peak_tmax + min_ann_ppt +
Pndjfm + Avg_depth + AWC +
(1|site), family = "binomial",
data = pimo_all_tree_vars, na.action = na.fail)
#only use combos that aren't pairwise correlated
cormat <- abs(cor(all_tree_vars[all_tree_vars$Spp == "PIMO", c('Neighbor_larger_log', 'ENN_dist_log', 'Diam_log', 'Height', 'BA_4m_log',
'cwd_normal_cum', 'fdsi_anom',
'cwd_peak_anom' , 'tmean', 'ppt',
'vpd_normal_annual_max' , 'vpd_peak_anom' , 'avg_summer_tmax', 'peak_tmax',
'min_ann_ppt', 'Pndjfm' , 'Avg_depth' , 'AWC')])) <=.5
#use all tree-level vars
cormat[1:5,1:5] <- TRUE
#use only one side of matrix (avoid duplicated models)
cormat[!lower.tri(cormat)] <- NA
# find all combinations with a few given conditions:
# abiotic variables can change but stand structure variables are fixed;
# abiotic variables with correlations greater than .5 are not allowed;
# 8 maximum variables are allowed;
# progress is displayed in console
# this only takes a half an hour or so
start.time <- Sys.time()
pmort_dredge <- dredge(pmort_global,
fixed = c('Neighbor_larger_log', 'ENN_dist_log', 'Diam_log', 'Height', 'BA_4m_log'),
subset = cormat,
m.lim = c(5, 8),
trace = 2)
end.time <- Sys.time()
time.taken_pmort_dredge <- end.time - start.time
time.taken_pmort_dredge #30 minutes
saveRDS(pmort_dredge, "./model output/pmort_dredge.RDS")
(pmort_dredge)[c(1:10)]
#get call for top model and evaluate it
best_pmort_call <- as.character(unlist(attr((pmort_dredge)[1], "model.calls")))
pmort_model_dredge <- eval(parse(text = best_pmort_call))
write.csv(pmort_dredge[1:10], "./model output/pmort_mod_selection_table.csv")
#############################################################################################
#Tree-level models
#############################################################################################
#PIMO dieback
#add interaction term to the PIMO dieback model
pimo_int_ba <- update(pimo_model_dredge, . ~ . + BA_4m_log:cwd_normal_cum)
summary(pimo_int_ba)
saveRDS(pimo_int_ba, "./model output/pimo_model.rds")
AICc(pimo_model_dredge) - AICc(pimo_int_ba) #improves AICc by 3.8
#nonparametric bootstrap cis
bs <- function(formula, data, indices) {
d <- data[indices,] # allows boot to select sample
fit <- lmer(formula, data=d)
return(fixef(fit))
}
bs(formula = pimo_int_ba@call,
data = all_tree_vars[all_tree_vars$Spp == "PIMO", ])
# bootstrapping with 5000 replications
#this takes a while (~6 hrs on an i7 3.4 Ghz processor. Would be faster with multithreading)
start.time <- Sys.time()
bootstrap_pimo_int <- boot(data=all_tree_vars[all_tree_vars$Spp == "PIMO", ],
statistic=bs,
R=5000,
formula= pimo_int_ba@call,
parallel = "no")
end.time <- Sys.time()
time.taken_pimo <- end.time - start.time
time.taken_pimo
saveRDS(bootstrap_pimo_int, file = paste0("./model output/bootstrap_pimo_int.rds"))
# view results
bootstrap_pimo <- bootstrap_pimo_int
summary(bootstrap_pimo)
plot(bootstrap_pimo, index=1) # intercept
plot(bootstrap_pimo, index=2) #
plot(bootstrap_pimo, index=3) #
# get 95% confidence intervals
ci_pimo <- cbind(attr(bootstrap_pimo_int$t0, "names"), summary(bootstrap_pimo_int))
ci_pimo$lo <- NA
ci_pimo$hi <- NA
for( i in 1:nrow(ci_pimo)){
ci_boot <- boot.ci(bootstrap_pimo_int, type = "perc", index = i)
#extract low and hi, add to ci_pimo df
ci_pimo$lo[i] <- ci_boot$perc[1,4]
ci_pimo$hi[i] <- ci_boot$perc[1,5]
ci_pimo$mean[i] <- mean(bootstrap_pimo_int$t[, i])
}
saveRDS(ci_pimo, file = "./model output/ci_pimo.rds")
write.csv(ci_pimo, "./model output/ci_pimo.csv")
# plot(allEffects(pimo_model))
# plot(Effect(focal.predictors = c("ENN_dist", "Neighbor_larger"), mod = pimo_model,
# ylim = c(-40, 0), partial.residuals = TRUE))
# plot(Effect(focal.predictors = c("BA_4m", "cwd_normal_cum"), mod = pimo_int_ba,
# ylim = c(-40, 0), partial.residuals = TRUE))
# coefplot2::coefplot2(pimo_model)
# AICc(pimo_model)
# vif.mer(pimo_model)
# r.squaredGLMM(pimo_int_ba)
#
# plot(resid(pimo_int_ba) ~ predict(pimo_int_ba))
# abline(h=0)
# hist(resid(pimo_int_ba))
# mse <- sum(resid(pimo_int_ba)^2)*(1 / length(resid(pimo_int_ba)))
#
#
# qqmath(ranef(pimo_model, postVar = TRUE))
#--------------------------------------------------------------
# add interaction to the selected model
juos_int_enn <- update(juos_model_dredge, . ~ . + ENN_dist_log:Neighbor_larger_log)
summary(juos_int_enn)
saveRDS(juos_int_enn, "./model output/juos_model.rds")
#nonparametric bootstrap cis
bs <- function(formula, data, indices) {
d <- data[indices,] # allows boot to select sample
fit <- lmer(formula, data=d)
return(fixef(fit))
}
bs(formula = juos_int_enn@call,
data = all_tree_vars[all_tree_vars$Spp == "JUOS", ])
# bootstrapping with 5000 replications
#this takes a while (~3.5 hrs on an i7 3.4 Ghz processor. Would be faster with multithreading)
start.time <- Sys.time()
bootstrap_juos <- boot(data=all_tree_vars[all_tree_vars$Spp == "JUOS", ],
statistic=bs,
R=5000,
formula=juos_int_enn@call)
end.time <- Sys.time()
time.taken_juos <- end.time - start.time
time.taken_juos
saveRDS(bootstrap_juos, file = "./model output/bootstrap_juos.rds")
# view results
# bootstrap_juos
# summary(bootstrap_juos)
# plot(bootstrap_juos, index=1) # intercept
# plot(bootstrap_juos, index=2) #
# plot(bootstrap_juos, index=3) #
# plot(bootstrap_juos, index=4) #
# plot(bootstrap_juos, index=5) #
# plot(bootstrap_juos, index=6) #
# plot(bootstrap_juos, index=7) #
# plot(bootstrap_juos, index=8) #
# plot(bootstrap_juos, index=9) #
# plot(bootstrap_juos, index=10) #
# get 95% confidence intervals
ci_juos <- cbind(attr(bootstrap_juos$t0, "names"), summary(bootstrap_juos))
ci_juos$lo <- NA
ci_juos$hi <- NA
for( i in 1:nrow(ci_juos)){
ci_boot <- boot.ci(bootstrap_juos, type = "perc", index = i)
#extract low and hi, add to ci_pimo df
ci_juos$lo[i] <- ci_boot$perc[1,4]
ci_juos$hi[i] <- ci_boot$perc[1,5]
ci_juos$mean[i] <- mean(bootstrap_juos$t[, i])
}
saveRDS(ci_juos, file = "./model output/ci_juos.rds")
write.csv(ci_juos, "./model output/ci_juos.csv")
# plot(allEffects(juos_model))
# plot(Effect(focal.predictors = c("ENN_dist", "Neighbor_larger"), mod = juos_model, partial.residuals = TRUE), ylim = c(-20, 0))
# coefplot2::coefplot2(juos_model)
# AICc(juos_model)
# vif.mer(juos_model)
# r.squaredGLMM(juos_int_enn)
#
# plot(resid(juos_int_enn) ~ predict(juos_int_enn))
# abline(h = 0)
# hist(resid(juos_int_enn))
# mse <- sum(resid(juos_int_enn)^2)*(1 / length(resid(juos_int_enn)))
#####################################
# Below here is PIMO mortality stuff
## PIMO mortality
#full model
p_mort <- pmort_model_dredge
summary(p_mort)
saveRDS(p_mort, "./model output/pimo_mort_model.rds")
## scale the gradient by the hessian to get relative gradient,
# see Bolker comments at https://github.com/lme4/lme4/issues/120
relgrad <- with(p_mort@optinfo$derivs,solve(Hessian,gradient))
max(abs(relgrad))
#looks good!
# bootstrapping with 5000 replications
#this takes a while (1.1 days on an i7 3.4 Ghz processor. Would be faster with multithreading)
bs <- function(formula, data, indices) {
d <- data[indices,] # allows boot to select sample
fit <- glmer(formula, data=d, nAGQ = 1, family = "binomial")
return(fixef(fit))
}
start.time <- Sys.time()
bootstrap_pmort <- boot(data=all_tree_vars[all_tree_vars$Spp == "PIMO", ],
statistic=bs,
R=5000,
formula = p_mort@call)
end.time <- Sys.time()
time.taken_pmort <- end.time - start.time
time.taken_pmort
saveRDS(bootstrap_pmort, file = "./model output/bootstrap_pmort.rds")
# view results
# # view results
# bootstrap_pmort
# summary(bootstrap_pmort)
# plot(bootstrap_pmort, index=1) # intercept x
# plot(bootstrap_pmort, index=2) #
# plot(bootstrap_pmort, index=3) #
# plot(bootstrap_pmort, index=4) #
# plot(bootstrap_pmort, index=5) #
# plot(bootstrap_pmort, index=6) #
# plot(bootstrap_pmort, index=7) #
# plot(bootstrap_pmort, index=8) #
# plot(bootstrap_pmort, index=9) #
# plot(bootstrap_pmort, index=10) #
# get 95% confidence intervals
ci_pmort <- cbind(attr(bootstrap_pmort$t0, "names"), summary(bootstrap_pmort))
ci_pmort$lo <- NA
ci_pmort$hi <- NA
for( i in 1:nrow(ci_pmort)){
ci_boot <- boot.ci(bootstrap_pmort, type = "perc", index = i)
#extract low and hi, add to ci_pimo df
ci_pmort$lo[i] <- ci_boot$perc[1,4]
ci_pmort$hi[i] <- ci_boot$perc[1,5]
ci_pmort$mean[i] <- mean(bootstrap_pmort$t[, i])
}
#saveRDS(ci_pmort, file = "./model output/ci_pmort.rds")
write.csv(ci_pmort, "./model output/ci_pmort.csv")