-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4_PredictSpsSpeed.R
More file actions
526 lines (425 loc) · 20.2 KB
/
Copy path4_PredictSpsSpeed.R
File metadata and controls
526 lines (425 loc) · 20.2 KB
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
#? *********************************************************************************
#? ---------------------------- 4_PredictSpsSpeed.R ----------------------------
#? *********************************************************************************
# Code to get species specific speed estimates
#! Input ----------------------------------------------
# - data/res/mod_gu.rds : model one results
# - data/final.rds : tibble with bird and green-up information, plus species traits
# detach packages and clear workspace
if(!require(freshr)){install.packages('freshr')}
freshr::freshr()
#! Load packages ---------------------------------------
library(conflicted)
library(tidyverse)
conflicts_prefer(dplyr::select)
conflicts_prefer(dplyr::filter)
# conflicts_prefer(scales::alpha)
#! Make functions --------------------------------------
colanmes <- colnames
lenght <- length
`%!in%` <- Negate(`%in%`)
#! Import data -----------------------------------------
## file paths
MODGU_RES_PATH <- "data/res/mod_gu.rds"
FINAL_TIB_PATH <- "data/final.rds"
## read files
mod_gu <- readRDS(file = MODGU_RES_PATH)
final <- readRDS(file = FINAL_TIB_PATH)
# Predict how much a species speed will change with 1 sd of green-up date or speed
final2 <- final %>%
mutate(species = as.factor(species),
cell = as.factor(cell),
#mig_cell = abs(mig_cell - 1),
mig_cell = as.factor(mig_cell),
sps_cell = as.factor(glue("{species}_{cell}"))
)
## Green-up date -------------------------------------------------------------------------------------------
### average species: Contopus_virens -----------------------------------------------------------------------
#### early years -------------------------------------------------------------------------------------------
new_data_early <- data.frame(AnomDGr = -10,
AnomVGr = 0,
mig_cell = c(TRUE,FALSE),
species = "Contopus_virens",
year = 1,
cell_lat2 = 1,
sps_cell = 1,
cell = 1)
pred_early <- predict(mod_gu,
newdata = new_data_early,
se.fit = TRUE, iterms.type=2, re.form=NA,
exclude = list("s(year)","s(cell_lat2)","s(sps_cell)"))
pred_early_tab <- as.data.frame(matrix(ncol = 3,
data = c(pred_early$fit,
pred_early$fit + pred_early$se.fit,
pred_early$fit - pred_early$se.fit),
byrow = F))
colnames(pred_early_tab) <- c("mean", "up","low")
(pred_early_tabX <- pred_early_tab %>%
mutate(mean = exp(mean),
up = exp(up),
low = exp(low),
mig_cell = c(TRUE,FALSE)))
#### average year -------------------------------------------------------------------------------------------
new_data_ave <- data.frame(AnomDGr = 0,
AnomVGr = 0,
mig_cell = c(TRUE,FALSE),
species = "Contopus_virens",
year = 1,
cell_lat2 = 1,
sps_cell = 1)
pred_ave <- predict(mod_gu,
newdata = new_data_ave,
se.fit = TRUE, iterms.type=2, re.form=NA,
exclude = list("s(year)","s(cell_lat2)","s(sps_cell)"))
pred_ave_tab <- as.data.frame(matrix(ncol = 3,
data = c(pred_ave$fit,
pred_ave$fit + pred_ave$se.fit,
pred_ave$fit - pred_ave$se.fit),
byrow = F))
colnames(pred_ave_tab) <- c("mean", "up","low")
(pred_ave_tabX <- pred_ave_tab %>%
mutate(mean = exp(mean),
up = exp(up),
low = exp(low),
mig_cell = c(TRUE,FALSE)))
pred_ave_tabX$mean %>% mean()
#### late year -------------------------------------------------------------------------------------------
new_data_late <- data.frame(AnomDGr = 5,
AnomVGr = 0,
mig_cell = c(TRUE,FALSE),
species = "Contopus_virens",
year = 1,
cell_lat2 = 1,
sps_cell = 1,
cell = 1,
mig_cell = TRUE)
pred_late <- predict(mod_gu,
newdata = new_data_late,
se.fit = TRUE, iterms.type=2, re.form=NA,
exclude = list("s(year)","s(cell_lat2)","s(sps_cell)"))
pred_late_tab <- as.data.frame(matrix(ncol = 3,
data = c(pred_late$fit,
pred_late$fit + pred_late$se.fit,
pred_late$fit - pred_late$se.fit),
byrow = F))
colnames(pred_late_tab) <- c("mean", "up","low")
(pred_late_tabX <- pred_late_tab %>%
mutate(mean = exp(mean),
up = exp(up),
low = exp(low),
mig_cell = c(TRUE,FALSE)))
pred_late_tabX
pred_ave_tabX
pred_early_tabX
### slow species: Tachycineta_bicolor ----------------------------------------------------------------------
#### early years -------------------------------------------------------------------------------------------
new_data_early <- data.frame(AnomDGr = -10,
AnomVGr = 0,
mig_cell = c(TRUE,FALSE),
species = "Tachycineta_bicolor",
year = 1,
cell_lat2 = 1,
sps_cell = 1,
cell = 1)
pred_early <- predict(mod_gu,
newdata = new_data_early,
se.fit = TRUE, iterms.type=2, re.form=NA,
exclude = list("s(year)","s(cell_lat2)","s(sps_cell)"))
pred_early_tab <- as.data.frame(matrix(ncol = 3,
data = c(pred_early$fit,
pred_early$fit + pred_early$se.fit,
pred_early$fit - pred_early$se.fit),
byrow = F))
colnames(pred_early_tab) <- c("mean", "up","low")
(pred_early_tabX <- pred_early_tab %>%
mutate(mean = exp(mean),
up = exp(up),
low = exp(low),
mig_cell = c(TRUE,FALSE)))
#### average year -------------------------------------------------------------------------------------------
new_data_ave <- data.frame(AnomDGr = 0,
AnomVGr = 0,
mig_cell = c(TRUE,FALSE),
species = "Tachycineta_bicolor",
year = 1,
cell_lat2 = 1,
sps_cell = 1)
pred_ave <- predict(mod_gu,
newdata = new_data_ave,
se.fit = TRUE, iterms.type=2, re.form=NA,
exclude = list("s(year)","s(cell_lat2)","s(sps_cell)"))
pred_ave_tab <- as.data.frame(matrix(ncol = 3,
data = c(pred_ave$fit,
pred_ave$fit + pred_ave$se.fit,
pred_ave$fit - pred_ave$se.fit),
byrow = F))
colnames(pred_ave_tab) <- c("mean", "up","low")
(pred_ave_tabX <- pred_ave_tab %>%
mutate(mean = exp(mean),
up = exp(up),
low = exp(low),
mig_cell = c(TRUE,FALSE)))
pred_ave_tabX$mean %>% mean()
#### late year -------------------------------------------------------------------------------------------
new_data_late <- data.frame(AnomDGr = 5,
AnomVGr = 0,
mig_cell = c(TRUE,FALSE),
species = "Tachycineta_bicolor",
year = 1,
cell_lat2 = 1,
sps_cell = 1,
cell = 1,
mig_cell = TRUE)
pred_late <- predict(mod_gu,
newdata = new_data_late,
se.fit = TRUE, iterms.type=2, re.form=NA,
exclude = list("s(year)","s(cell_lat2)","s(sps_cell)"))
pred_late_tab <- as.data.frame(matrix(ncol = 3,
data = c(pred_late$fit,
pred_late$fit + pred_late$se.fit,
pred_late$fit - pred_late$se.fit),
byrow = F))
colnames(pred_late_tab) <- c("mean", "up","low")
(pred_late_tabX <- pred_late_tab %>%
mutate(mean = exp(mean),
up = exp(up),
low = exp(low),
mig_cell = c(TRUE,FALSE)))
pred_late_tabX
pred_ave_tabX
pred_early_tabX
### fast species: Empidonax_traillii ----------------------------------------------------------------------
#### early years -------------------------------------------------------------------------------------------
new_data_early <- data.frame(AnomDGr = -10,
AnomVGr = 0,
mig_cell = c(TRUE,FALSE),
species = "Empidonax_traillii",
year = 1,
cell_lat2 = 1,
sps_cell = 1,
cell = 1)
pred_early <- predict(mod_gu,
newdata = new_data_early,
se.fit = TRUE, iterms.type=2, re.form=NA,
exclude = list("s(year)","s(cell_lat2)","s(sps_cell)"))
pred_early_tab <- as.data.frame(matrix(ncol = 3,
data = c(pred_early$fit,
pred_early$fit + pred_early$se.fit,
pred_early$fit - pred_early$se.fit),
byrow = F))
colnames(pred_early_tab) <- c("mean", "up","low")
(pred_early_tabX <- pred_early_tab %>%
mutate(mean = exp(mean),
up = exp(up),
low = exp(low),
mig_cell = c(TRUE,FALSE)))
#### average year -------------------------------------------------------------------------------------------
new_data_ave <- data.frame(AnomDGr = 0,
AnomVGr = 0,
mig_cell = c(TRUE,FALSE),
species = "Empidonax_traillii",
year = 1,
cell_lat2 = 1,
sps_cell = 1)
pred_ave <- predict(mod_gu,
newdata = new_data_ave,
se.fit = TRUE, iterms.type=2, re.form=NA,
exclude = list("s(year)","s(cell_lat2)","s(sps_cell)"))
pred_ave_tab <- as.data.frame(matrix(ncol = 3,
data = c(pred_ave$fit,
pred_ave$fit + pred_ave$se.fit,
pred_ave$fit - pred_ave$se.fit),
byrow = F))
colnames(pred_ave_tab) <- c("mean", "up","low")
(pred_ave_tabX <- pred_ave_tab %>%
mutate(mean = exp(mean),
up = exp(up),
low = exp(low),
mig_cell = c(TRUE,FALSE)))
pred_ave_tabX$mean %>% mean()
#### late year -------------------------------------------------------------------------------------------
new_data_late <- data.frame(AnomDGr = 5,
AnomVGr = 0,
mig_cell = c(TRUE,FALSE),
species = "Empidonax_traillii",
year = 1,
cell_lat2 = 1,
sps_cell = 1,
cell = 1,
mig_cell = TRUE)
pred_late <- predict(mod_gu,
newdata = new_data_late,
se.fit = TRUE, iterms.type=2, re.form=NA,
exclude = list("s(year)","s(cell_lat2)","s(sps_cell)"))
pred_late_tab <- as.data.frame(matrix(ncol = 3,
data = c(pred_late$fit,
pred_late$fit + pred_late$se.fit,
pred_late$fit - pred_late$se.fit),
byrow = F))
colnames(pred_late_tab) <- c("mean", "up","low")
(pred_late_tabX <- pred_late_tab %>%
mutate(mean = exp(mean),
up = exp(up),
low = exp(low),
mig_cell = c(TRUE,FALSE)))
pred_late_tabX
pred_ave_tabX
pred_early_tabX
## Green-up date -------------------------------------------------------------------------------------------
### average species: Contopus_virens -----------------------------------------------------------------------
#### fast years -------------------------------------------------------------------------------------------
new_data_fast <- data.frame(AnomDGr = 0,
AnomVGr = 0.7,
mig_cell = c(TRUE,FALSE),
species = "Contopus_virens",
year = 1,
cell_lat2 = 1,
sps_cell = 1,
cell = 1)
pred_fast <- predict(mod_gu,
newdata = new_data_fast,
se.fit = TRUE, iterms.type=2, re.form=NA,
exclude = list("s(year)","s(cell_lat2)","s(sps_cell)"))
pred_fast_tab <- as.data.frame(matrix(ncol = 3,
data = c(pred_fast$fit,
pred_fast$fit + pred_fast$se.fit,
pred_fast$fit - pred_fast$se.fit),
byrow = F))
colnames(pred_fast_tab) <- c("mean", "up","low")
(pred_fast_tabX <- pred_fast_tab %>%
mutate(mean = exp(mean),
up = exp(up),
low = exp(low),
mig_cell = c(TRUE,FALSE)))
#### avesrage year -------------------------------------------------------------------------------------------
new_data_aves <- data.frame(AnomDGr = 0,
AnomVGr = 0,
mig_cell = c(TRUE,FALSE),
species = "Contopus_virens",
year = 1,
cell_lat2 = 1,
sps_cell = 1)
pred_aves <- predict(mod_gu,
newdata = new_data_aves,
se.fit = TRUE, iterms.type=2, re.form=NA,
exclude = list("s(year)","s(cell_lat2)","s(sps_cell)"))
pred_aves_tab <- as.data.frame(matrix(ncol = 3,
data = c(pred_aves$fit,
pred_aves$fit + pred_aves$se.fit,
pred_aves$fit - pred_aves$se.fit),
byrow = F))
colnames(pred_aves_tab) <- c("mean", "up","low")
(pred_aves_tabX <- pred_aves_tab %>%
mutate(mean = exp(mean),
up = exp(up),
low = exp(low),
mig_cell = c(TRUE,FALSE)))
pred_aves_tabX$mean %>% mean()
#### slow year -------------------------------------------------------------------------------------------
new_data_slow <- data.frame(AnomDGr = 0,
AnomVGr = -0.7,
mig_cell = c(TRUE,FALSE),
species = "Contopus_virens",
year = 1,
cell_lat2 = 1,
sps_cell = 1,
cell = 1,
mig_cell = TRUE)
pred_slow <- predict(mod_gu,
newdata = new_data_slow,
se.fit = TRUE, iterms.type=2, re.form=NA,
exclude = list("s(year)","s(cell_lat2)","s(sps_cell)"))
pred_slow_tab <- as.data.frame(matrix(ncol = 3,
data = c(pred_slow$fit,
pred_slow$fit + pred_slow$se.fit,
pred_slow$fit - pred_slow$se.fit),
byrow = F))
colnames(pred_slow_tab) <- c("mean", "up","low")
(pred_slow_tabX <- pred_slow_tab %>%
mutate(mean = exp(mean),
up = exp(up),
low = exp(low),
mig_cell = c(TRUE,FALSE)))
pred_slow_tabX
pred_aves_tabX
pred_fast_tabX
# Summary results -------------------------
# number of species
final2 %>% filter(!is.na(vArrMag)) %>% dplyr::select(species) %>%
distinct() %>% nrow()
# number of cells
final2 %>% filter(!is.na(vArrMag)) %>% dplyr::select(cell) %>%
distinct() %>% nrow()
# average number of cells per species
final2 %>% filter(!is.na(vArrMag)) %>% dplyr::select(species, cell) %>%
distinct() %>% dplyr::select(species) %>% table() %>% mean()
# sd of the number of cells per species
final2 %>% filter(!is.na(vArrMag)) %>% dplyr::select(species, cell) %>%
distinct() %>% dplyr::select(species) %>% table() %>% sd()
# min of the number of cells per species
final2 %>% filter(!is.na(vArrMag)) %>% dplyr::select(species, cell) %>%
distinct() %>% dplyr::select(species) %>% table() %>% min()
# max of the number of cells per species
final2 %>% filter(!is.na(vArrMag)) %>% dplyr::select(species, cell) %>%
distinct() %>% dplyr::select(species) %>% table() %>% max()
# average number of species per cells
final2 %>% filter(!is.na(vArrMag)) %>% dplyr::select(species, cell) %>%
distinct() %>% dplyr::select(cell) %>% table() %>% mean()
# sd of number of species per cells
final2 %>% filter(!is.na(vArrMag)) %>% dplyr::select(species, cell) %>%
distinct() %>% dplyr::select(cell) %>% table() %>% sd()
# min of number of species per cells
final2 %>% filter(!is.na(vArrMag)) %>% dplyr::select(species, cell) %>%
distinct() %>% dplyr::select(cell) %>% table() %>% min()
# max of number of species per cells
final2 %>% filter(!is.na(vArrMag)) %>% dplyr::select(species, cell) %>%
distinct() %>% dplyr::select(cell) %>% table() %>% max()
# speed: max, min, mean, median, sd
final2 %>% filter(!is.na(vArrMag)) %>% dplyr::select(vArrMag) %>% pull() %>% max()
final2 %>% filter(!is.na(vArrMag)) %>% dplyr::select(vArrMag) %>% pull() %>% min()
final2 %>% filter(!is.na(vArrMag)) %>% dplyr::select(vArrMag) %>% pull() %>% mean()
final2 %>% filter(!is.na(vArrMag)) %>% dplyr::select(vArrMag) %>% pull() %>% median()
final2 %>% filter(!is.na(vArrMag)) %>% dplyr::select(vArrMag) %>% pull() %>% sd()
# speed direction mean, sd, median
final2 %>% filter(!is.na(vArrMag)) %>% dplyr::select(vArrAng) %>% mutate(vArrAng = vArrAng + 180) %>% pull() %>% mean()
final2 %>% filter(!is.na(vArrMag)) %>% dplyr::select(vArrAng) %>% mutate(vArrAng = vArrAng + 180) %>% pull() %>% sd()
final2 %>% filter(!is.na(vArrMag)) %>% dplyr::select(vArrAng) %>% mutate(vArrAng = vArrAng + 180) %>% pull() %>% median()
# annual green-up averages
final2 %>%
dplyr::select(cell, AnomDGr, AnomVGr, year) %>%
distinct() %>%
group_by(year) %>%
summarise(annual_mean = mean(AnomDGr, na.rm = T)) %>%
dplyr::select(annual_mean) %>%
pull() %>%
sort()
# difference in speed (bird and green-up) between the first and second half of study area
lats <- finalG %>%
dplyr:: select(cell_lat2) %>%
pull()
min(lats)
max(lats)
med_lat <- (((max(lats) - min(lats))/2) + min(lats))
nort_b_spe <- final2 %>%
filter(cell_lat2>med_lat) %>%
select(vArrMag) %>%
pull() %>%
mean(na.rm = T)
sout_b_spe <- final2 %>%
filter(cell_lat2<=med_lat) %>%
select(vArrMag) %>%
pull() %>%
mean(na.rm = T)
nort_b_spe - sout_b_spe
nort_b_gup <- finalG %>%
filter(cell_lat2>med_lat) %>%
select(vGrMag) %>%
pull() %>%
mean(na.rm = T)
sout_b_gup <- finalG %>%
filter(cell_lat2<=med_lat) %>%
select(vGrMag) %>%
pull() %>%
mean(na.rm = T)
nort_b_gup - sout_b_gup
cat("\n\n DONE!!! \n\n\n")