-
Notifications
You must be signed in to change notification settings - Fork 1
/
manuscript_figures.R
620 lines (527 loc) · 23.5 KB
/
manuscript_figures.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
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
#######################################################################
### Figures and values used (and maybe not used) for the manuscript ###
#######################################################################
######
### NOTE ###
######
### This script is meant to provide some code for exploring the results, it is not
### perfectly cleaned (more of a dump then other scripts because none of the code
### is *required* for fitting and interpreting results)
### That is, not meant to be run with soruce() *will break* or ctrl+A run.
######
######
### NOTE ###
######
### For example, recreating the Figure 1 boxplot requires run_spat_temp_aggregated to
### be set to TRUE, which increases computation time substantially
######
library(extrafont); library(scales); library(tikzDevice)
########
## Dataset info
########
## check number of communities in each data set
nrow(effort_metadata_com)
nrow(
effort_metadata_com %>% filter(num_lists <= 5)
)
## check species coverage
samp_data_with_meta <- left_join(samp_data_com, effort_metadata_com[, c(1,2,3,6)], by = c("county", "month", "year"))
n_distinct(samp_data_with_meta[["phylo_name"]])
samp_data_with_meta <- samp_data_with_meta %>%
filter(!is.na(num_lists)) %>%
filter(num_lists >= 80)
spec_cover <- samp_data_with_meta %>%
count(phylo_name)
spec_cover <- spec_cover %>%
mutate(prop_com = n / max(n))
## proportion of communities in which a few specific species are found
spec_cover %>% filter(phylo_name == "Cardinalis_cardinalis")
spec_cover %>% filter(phylo_name == "Aphelocoma_ultramarina")
spec_cover %>% filter(phylo_name == "Cyanocitta_cristata")
spec_cover %>% filter(phylo_name == "Zenaida_macroura")
quantile(spec_cover[["prop_com"]], c(0.05, 0.50, 0.95))
length(which(spec_cover[["n"]] < 26))
########
## Crow bite pref
########
host_comp_summary_c <- host_comp_summary %>%
filter(species == "Corvus_brachyrhynchos") %>%
filter(outcome == "biting_pref")
quantile(host_comp_summary_c[1, -c(1:5)], c(0.05, 0.50, 0.95))
########
## Histogram of R0
########
head(comm_comp_summary_p2)
ggplot(comm_comp_summary_p2_well_observed, aes(med_comp)) +
geom_histogram(aes(y=..count..), bins = 100) +
geom_vline(xintercept = 1, linetype = "dashed", col = "black", lwd = 1) +
# stat_density(geom = "line") +
geom_hline(yintercept = 0, colour = "white", size = 1) +
xlab("Community R0") +
ylab("Number of Communities") +
facet_wrap(~month)
ggplot(comm_comp_summary_p2, aes(med_comp)) +
geom_histogram(aes(y=..count..), bins = 100) +
# geom_vline(xintercept = median(comm_comp_summary_p2[["med_comp"]])
# , linetype = "dotted", col = "grey", lwd = 1) +
# geom_vline(xintercept = median(comm_comp_summary_p2_well_observed[["med_comp"]])
# , linetype = "dotted", col = "blue", lwd = 1) +
geom_histogram(data = comm_comp_summary_p2_well_observed, aes(y=..count..)
, colour = "blue", bins = 100, fill = "blue") +
geom_vline(xintercept = 1, linetype = "dashed", col = "black", lwd = 1) +
# stat_density(geom = "line") +
geom_hline(yintercept = 0, colour = "white", size = 1) +
xlab("Community R0") +
ylab("Number of Communities") +
facet_wrap(~month)
########
## Estimates of R0 by space ***prior to fitting the spatial GAM model***
## Plot both the results from the full model and the model with temperature aggregated over space
## Main text present the model with the reduced dataset and all uncertainty
## Supplement present both full dataset with all uncertainty and reduced dataset with no uncertainty
########
comm_comp_summary_p2_well_observed_m_dh_s <- comm_comp_summary_p2_well_observed_m_dh %>%
group_by(month, REGIONS) %>%
summarize(R0 = median(med_comp))
## Lines for each ecoregion that are a bit hard to look at
ggplot(comm_comp_summary_p2_well_observed_m_dh_s[
comm_comp_summary_p2_well_observed_m_dh_s$REGIONS != "COASTAL SAND PLAIN" &
comm_comp_summary_p2_well_observed_m_dh_s$REGIONS != "LLANO UPLIFT", ]
, aes(as.numeric(month), R0)) +
geom_line(aes(colour = REGIONS), lwd = 1) +
geom_point(data = comm_comp_summary_p2_well_observed_m_dh_s[
comm_comp_summary_p2_well_observed_m_dh_s$REGIONS == "COASTAL SAND PLAIN" |
comm_comp_summary_p2_well_observed_m_dh_s$REGIONS == "LLANO UPLIFT", ]
, aes(as.numeric(month), R0, colour = REGIONS), lwd = 3) +
scale_x_continuous(breaks = c(2, 5, 8, 10, 12), labels = c("Feb", "May", "Aug", "Oct", "Dec")) +
geom_vline(xintercept = 10, linetype = "dotted", lwd = 0.5) +
xlab("Month") + ylab(expression('Median R'[0]))
######
## Make a data frame with full results saved as XXX_s1 and
## one of the results from the aggregated dataset as XXX_s2
######
comm_comp_summary_p2_well_observed_m_dh_s2 <- comm_comp_summary_p2_well_observed_m_dh
comm_comp_summary_p2_well_observed_m_dh_s2 <- transform(comm_comp_summary_p2_well_observed_m_dh_s2, month = as.factor(month))
comm_comp_summary_p2_well_observed_m_dh_s1 <- transform(comm_comp_summary_p2_well_observed_m_dh_s1, model = "Full")
comm_comp_summary_p2_well_observed_m_dh_s2 <- transform(comm_comp_summary_p2_well_observed_m_dh_s2, model = "Reduced")
comm_comp_summary_p2_well_observed_m_dh_sb <- rbind(
comm_comp_summary_p2_well_observed_m_dh_s1
, comm_comp_summary_p2_well_observed_m_dh_s2)
## Boxplot that is much easier to look at
ggplot(comm_comp_summary_p2_well_observed_m_dh_sb, aes(month, med_comp, colour = model)) +
geom_boxplot(lwd = 0.6) +
scale_colour_manual(
values = c("steelblue4", "tomato4")
, name = "Model"
, labels = c("Full", "Aggregated")) +
scale_x_discrete(breaks = c(2, 5, 8, 10, 12), labels = c("Feb", "May", "Aug", "Oct", "Dec")) +
xlab("Month") + ylab(expression('Median R'[0]))
## Different boxplot for the supplement that are different colors
ggplot(comm_comp_summary_p2_well_observed_m_dh_sb, aes(month, med_comp, colour = model)) +
geom_boxplot(lwd = 0.6) +
scale_colour_manual(
values = c("purple4", "springgreen4")
, name = "Model"
, labels = c("Full eBird
dataset", "Reduced eBird
dataset")) +
scale_x_discrete(breaks = c(2, 5, 8, 10, 12), labels = c("Feb", "May", "Aug", "Oct", "Dec")) +
xlab("Month") + ylab(expression('Median R'[0]))
comm_comp_summary_p2_well_observed_m_dh_s %>% filter(month == 11) %>%
group_by(CNTY_NM) %>% summarize(R0 = median(med_comp))
comm_comp_summary_p2_well_observed_m_dh_s2 %>%
group_by(month) %>% summarize(R0 = median(med_comp))
########
## Range of R0, cv etc. !!! (old stuff from before temperature was added) !!!
########
quantile(comm_comp_summary_p2_well_observed[["med_comp"]], c(0.05, 0.50, 0.95))
quantile(comm_comp_summary_p2_well_observed[["cv_comp"]], c(0.05, 0.50, 0.95))
length(which(comm_comp_summary_p2_well_observed[["med_comp"]] < 1.00)) / nrow(comm_comp_summary_p2_well_observed)
## save one of the data frames for comparison between uncertainty propagated and not
# saveRDS(comm_comp_summary_p2_well_observed, file = "comm_comp_summary_p2_well_observed_no_uncer.rds")
# comm_comp_summary_p2_well_observed_no_uncer <- readRDS("comm_comp_summary_p2_well_observed_no_uncer.rds")
# length(which(comm_comp_summary_p2_well_observed_no_uncer[["med_comp"]] > comm_comp_summary_p2_well_observed[["med_comp"]]))
quantile(comm_comp_summary_p2[["med_comp"]], c(0.05, 0.50, 0.95))
quantile(comm_comp_summary_p2[["cv_comp"]], c(0.05, 0.50, 0.95))
length(which(comm_comp_summary_p2[["med_comp"]] < 1.00)) / nrow(comm_comp_summary_p2)
which_at_mean <- which(comm_comp_summary_p2_well_observed[["cv_comp"]] >
(quantile(comm_comp_summary_p2_well_observed[["cv_comp"]], c(0.50)) - 0.0005) &
comm_comp_summary_p2_well_observed[["cv_comp"]] <
(quantile(comm_comp_summary_p2_well_observed[["cv_comp"]], c(0.50)) + 0.0005))
comm_comp_summary_p2_well_observed[which_at_mean[2], ]
check_CI <- comm_comp_summary %>% filter(county == "Collin", month == 1, year == 2017)
quantile(check_CI[1, -c(1, 3)], c(0.05, 0.50, 0.95))
length(which(check_CI[1, -c(1, 3)] < 1.00))
which_at_mean <- which(comm_comp_summary_p2[["cv_comp"]] >
(quantile(comm_comp_summary_p2[["cv_comp"]], c(0.50)) - 0.00005) &
comm_comp_summary_p2[["cv_comp"]] <
(quantile(comm_comp_summary_p2[["cv_comp"]], c(0.50)) + 0.00005))
comm_comp_summary_p2[which_at_mean[1], ]
check_CI <- comm_comp_summary %>% filter(county == "Brewster", month == 11, year == 2013)
quantile(check_CI[1, -c(1, 3)], c(0.05, 0.50, 0.95))
length(which(check_CI[1, -c(1, 3)] < 1.00))
########
## Diluters and Amplifiers
########
species_importance_dat_for_plot <- species_importance_p_r[
species_importance_p_r[["phylo_name"]] == "Zenaida_macroura" |
species_importance_p_r[["phylo_name"]] == "Zenaida_asiatica" |
species_importance_p_r[["phylo_name"]] == "Cardinalis_cardinalis" |
species_importance_p_r[["phylo_name"]] == "Aphelocoma_ultramarina" |
species_importance_p_r[["phylo_name"]] == "Cyanocitta_cristata" |
species_importance_p_r[["phylo_name"]] == "Carpodacus_mexicanus" |
species_importance_p_r[["phylo_name"]] == "Cyanocorax_yncas"
, ]
## with robins (used for a presentation)
species_importance_dat_for_plot <- species_importance_p_r[
species_importance_p_r[["phylo_name"]] == "Zenaida_macroura" |
species_importance_p_r[["phylo_name"]] == "Zenaida_asiatica" |
species_importance_p_r[["phylo_name"]] == "Cardinalis_cardinalis" |
species_importance_p_r[["phylo_name"]] == "Aphelocoma_ultramarina" |
species_importance_p_r[["phylo_name"]] == "Cyanocitta_cristata" |
species_importance_p_r[["phylo_name"]] == "Cyanocorax_yncas" |
species_importance_p_r[["phylo_name"]] == "Turdus_migratorius"
, ]
species_importance_dat_for_plot <- species_importance_dat_for_plot[order(species_importance_dat_for_plot[["med_dilut_effect"]]), ]
species_importance_dat_for_plot <- transform(species_importance_dat_for_plot
, phylo_name = factor(species_importance_dat_for_plot[["phylo_name"]],
levels = species_importance_dat_for_plot[["phylo_name"]])
, dilut_type = c(rep("Diluters", 2), rep("Amplifiers", 5)))
## with robins (used for a presentation)
species_importance_dat_for_plot <- transform(species_importance_dat_for_plot
, phylo_name = factor(species_importance_dat_for_plot[["phylo_name"]],
levels = species_importance_dat_for_plot[["phylo_name"]])
, dilut_type = c(rep("Diluters", 3), rep("Amplifiers", 4)))
ggplot(species_importance_dat_for_plot
, aes(med_dilut_effect, phylo_name, colour = n_com / max(n_com))) +
geom_errorbarh(aes(xmin = min_dilut_effect, xmax = max_dilut_effect), height = 0.2, lwd = 1) +
geom_point(lwd = 3) +
geom_vline(xintercept = 1, linetype = "dashed", col = "black", lwd = 0.5) +
xlab(expression(paste("Proportional Change in", italic(" R")[0], sep = " "))) +
# xlab(expression(math{R}))
# xlab(TeX("$\\ensuremath{\\mathcal R}$")) +
ylab("") +
#scale_colour_continuous(trans = "reverse", low = muted("red"), mid = "grey", high = muted("blue"), midpoint = 0.50) +
scale_colour_gradient2(low = muted("red"), mid = "grey", high = muted("blue"), midpoint = 0.50) +
guides(color = guide_legend("Proportion of Communities")) +
theme(legend.key.size = unit(.55, "cm")
, legend.position = c(0.2, 0.8)
, axis.text.x = element_text(size = 12)
, axis.text.y = element_text(size = 14)
, axis.title.x = element_text(size = 12)
, axis.title.y = element_text(size = 14)) +
scale_y_discrete(labels =
c(
expression(atop(
" Northern Cardinal",
paste("(", italic("Cardinalis cardinalis"), ")", sep = "")))
,
expression(atop(
" Transvolcanic jay",
paste("(", italic("Aphelocoma ultramarina"), ")", sep = "")))
,
expression(atop(
" Blue jay",
paste("(", italic("Cyanocitta cristata"), ")", sep = "")))
,
expression(atop(
" House finch",
paste("(", italic("Carpodacus mexicanus"), ")", sep = "")))
,
expression(atop(
" Green jay",
paste("(", italic("Cyanocorax yncas"), ")", sep = "")))
,
expression(atop(
"White-winged dove",
paste( " (", italic("Zenaida asiatica"), ")", sep = "")))
,
expression(atop(
" Mourning dove",
paste("(", italic("Zenaida macroura"), ")", sep = "")))))
ggsave("spec_imp.pdf", width = 8.5, height = 7)
## with robins (used for a presentation)
ggplot(species_importance_dat_for_plot
, aes(med_dilut_effect, phylo_name, colour = n_com / max(n_com))) +
geom_errorbarh(aes(xmin = min_dilut_effect, xmax = max_dilut_effect), height = 0.2, lwd = 1) +
geom_point(lwd = 3) +
geom_vline(xintercept = 1, linetype = "dashed", col = "black", lwd = 0.5) +
xlab(expression(paste("Proportional Change in", italic(" R")[0], sep = " "))) +
ylab("") +
scale_colour_gradient2(low = muted("red"), mid = "grey", high = muted("blue"), midpoint = 0.50) +
guides(color = guide_legend("Proportion of Communities")) +
theme(legend.key.size = unit(.55, "cm")
, legend.position = c(0.2, 0.8)
, axis.text.x = element_text(size = 12)
, axis.text.y = element_text(size = 14)
, axis.title.x = element_text(size = 12)
, axis.title.y = element_text(size = 14)) +
scale_y_discrete(labels =
c(
expression(atop(
" Northern Cardinal",
paste("(", italic("Cardinalis cardinalis"), ")", sep = "")))
,
expression(atop(
" Blue jay",
paste("(", italic("Cyanocitta cristata"), ")", sep = "")))
,
expression(atop(
" Transvolcanic jay",
paste("(", italic("Aphelocoma ultramarina"), ")", sep = "")))
,
expression(atop(
" Green jay",
paste("(", italic("Cyanocorax yncas"), ")", sep = "")))
,
expression(atop(
" American robin",
paste( " (", italic("Turdus migratorius"), ")", sep = "")))
,
expression(atop(
"White-winged dove",
paste( " (", italic("Zenaida asiatica"), ")", sep = "")))
,
expression(atop(
" Mourning dove",
paste("(", italic("Zenaida macroura"), ")", sep = "")))))
########
## Species Richness
########
ggplot(comm_comp_summary_p2_well_observed, aes(meannum_spec, med_comp)) +
geom_point(lwd = 1) +
geom_smooth(lwd = 1, method = "lm") +
xlab("Species Richness") +
ylab(expression(italic(" R")[0]))
########
## Species Abundance and Physiological Competence
########
ggplot(dilut_assump_summary, aes(abundance, mean_trans)) +
geom_point(lwd = 1) +
geom_smooth(lwd = 1, method = "lm") +
scale_x_log10() +
xlab("log(Species Abundance)") +
ylab("Species Physiological Competence")
#######
## Spatio Temporal Model
#######
summary(comm_comp_spatio_temporal_f[[2]])
summary(comm_comp_spatio_temporal_r[[2]])
concurvity(comm_comp_spatio_temporal_f[[2]])
concurvity(comm_comp_spatio_temporal_r[[2]])
layout(1); plot(comm_comp_spatio_temporal_f[[2]], scheme = 2, scale = 0)
gam.check(comm_comp_spatio_temporal_r[[2]])
gamres <- resid(comm_comp_spatio_temporal_r[["lme"]], type = "normalized")
better_check(comm_comp_spatio_temporal_r)
par(mfrow = c(2,2))
### predict R0 from the spatial model
## reduced data set
comm_comp_summary_p2_well_observed_m_dh <- transform(comm_comp_summary_p2_well_observed_m_dh
, pred_vals = predict(comm_comp_spatio_temporal_r[[2]]))
## complete data set
comm_comp_summary_p2_m_dh <- transform(comm_comp_summary_p2_m_dh
, pred_vals = predict(comm_comp_spatio_temporal_f[[2]]))
texas_R0_pred_vals <- comm_comp_summary_p2_well_observed_m_dh[, c(4, 5, 6, 10, 12, 14, 15)]
#####
## Each time month gets changed rerun from here
#####
temp_month <- 10
texas_R0_pred_vals_m_s <- texas_R0_pred_vals %>%
group_by(REGIONS, month) %>%
summarize(
'Median R0' = median(pred_vals, na.rm = TRUE)
, 'CV in R0 among communities' = sd(pred_vals, na.rm = TRUE) / mean(pred_vals, na.rm = TRUE))
#####
## ggplot of variation in R0 across regions over the seasons
#####
names(texas_R0_pred_vals_m_s)[3] <- "Median_R0"
ggplot(texas_R0_pred_vals_m_s[texas_R0_pred_vals_m_s$REGIONS != "COASTAL SAND PLAIN" & texas_R0_pred_vals_m_s$REGIONS != "LLANO UPLIFT", ]
, aes(month, Median_R0)) +
geom_line(aes(colour = REGIONS), lwd = 1) +
geom_point(data = texas_R0_pred_vals_m_s[texas_R0_pred_vals_m_s$REGIONS == "COASTAL SAND PLAIN" | texas_R0_pred_vals_m_s$REGIONS == "LLANO UPLIFT", ]
, aes(month, Median_R0, colour = REGIONS), lwd = 3) +
scale_x_continuous(breaks = c(2, 5, 8, 10, 12), labels = c("Feb", "May", "Aug", "Oct", "Dec")) +
geom_vline(xintercept = 10, linetype = "dotted", lwd = 0.5) +
xlab("Month") + ylab("Median R0")
## Also try to aggregate by county
texas_R0_pred_vals_m_s <- texas_R0_pred_vals %>%
group_by(CNTY_NM, month) %>%
summarize(
'Median R0' = median(pred_vals, na.rm = TRUE)
, 'CV in R0 among communities' = sd(pred_vals, na.rm = TRUE) / mean(pred_vals, na.rm = TRUE))
#####
## ggplot of variation in R0 across regions over the seasons
#####
names(texas_R0_pred_vals_m_s)[3] <- "Median_R0"
ggplot(texas_R0_pred_vals_m_s, aes(month, Median_R0)) +
geom_boxplot(aes(group = month), lwd = 1) +
scale_x_continuous(breaks = c(2, 5, 8, 10, 12), labels = c("Feb", "May", "Aug", "Oct", "Dec")) +
geom_vline(xintercept = 10, linetype = "dotted", lwd = 0.5) +
xlab("Month") + ylab("Median R0")
## Just pick a single month to plot
texas_R0_pred_vals_m_s <- texas_R0_pred_vals_m_s %>% filter(month == temp_month)
## Store a habitat layer for editing each time month is changed (so not to edit the parent data frame)
hab_layer1month <- hab_layer
hab_layer1month@data <- left_join(hab_layer1month@data, texas_R0_pred_vals_m_s, by = "REGIONS")
hab_layer1month@polygons[[14]] <- NULL
hab_layer1month@data <- hab_layer1month@data[-14, ]
#qtm(shp = hab_layer1month, fill = c("Median_R0"), fill.palette = "Blues", ncol = 2)
tm1 <- tm_shape(hab_layer1month) + tm_fill(col = "CV in R0 among communities", palette = "Greens") +
tm_borders(col = "black")
tm2 <- tm_shape(hab_layer1month) + tm_fill(col = "Median R0", palette = "Blues") +
tm_borders(col = "black")
tmap_arrange(tm2, tm1)
#######
## Spatio Temporal Model predictions
#######
summary(comm_comp_spatio_temporal_r[[2]])
summary(comm_comp_spatio_temporal_f[[2]])
as.data.frame(
comm_comp_summary_p2_m_dh %>%
# filter(meannum_spec == 305) %>%
# filter(year == 2017) %>%
# filter(county == "Randall") %>%
# filter(county == "Cameron") %>%
# filter(county == "Jefferson") %>%
group_by(month) %>%
# group_by(REGIONS) %>%
summarise(median(pred_vals)))
as.data.frame(
comm_comp_summary_p2_well_observed_m_dh %>%
# filter(meannum_spec == 305) %>%
# filter(year == 2000) %>%
# filter(county == "Randall") %>%
# filter(county == "Cameron") %>%
# filter(county == "Jefferson") %>%
# group_by(REGIONS) %>%
group_by(month) %>%
# group_by(year) %>%
summarise(median(pred_vals)))
#######
## Calculate MSE relative to the estimates from the full model
## Take the full model as truth, and check how close the aggregated data models get to the
## predictions from the full model
#######
## First need to make sure all of the data frames are sorted in the correct way, so that the
## correct rows match
comm_comp_summary_p2_well_observed_full <- comm_comp_summary_p2_well_observed_full[
with(comm_comp_summary_p2_well_observed_full, order(county, month, year)), ]
comm_comp_summary_p2_well_observed_ts <- comm_comp_summary_p2_well_observed_ts[
with(comm_comp_summary_p2_well_observed_ts, order(county, month, year)), ]
comm_comp_summary_p2_well_observed_tt <- comm_comp_summary_p2_well_observed_tt[
with(comm_comp_summary_p2_well_observed_tt, order(county, month, year)), ]
#####
## Try some sort of mean absolute difference over the whole time period, or restricted to the
## months that we care about?
#####
MSE_calc <- data.frame(
model = c("FULL", "TS", "TT", "BCS", "BCT", "EMPTY")
, MSE = c(
## full model to itself
0
, mean(abs(comm_comp_summary_p2_well_observed_full[["med_comp"]] - comm_comp_summary_p2_well_observed_ts[["med_comp"]]))
, mean(abs(comm_comp_summary_p2_well_observed_full[["med_comp"]] - comm_comp_summary_p2_well_observed_tt[["med_comp"]]))
, mean(abs(comm_comp_summary_p2_well_observed_full[["med_comp"]] - comm_comp_summary_p2_well_observed_bcs[["med_comp"]]))
, mean(abs(comm_comp_summary_p2_well_observed_full[["med_comp"]] - comm_comp_summary_p2_well_observed_bct[["med_comp"]]))
## full model to a mean-only model
, mean(abs(comm_comp_summary_p2_well_observed_full[["med_comp"]] - comm_comp_summary_p2_well_observed_mean_model[["med_comp"]]))
)
)
MSE_calc <- MSE_calc[rev(order(MSE_calc$MSE)), ]
MSE_calc <- transform(MSE_calc, model = factor(model, levels = rev(MSE_calc$model)))
MSE_calc
#####
## ggplot this result
#####
ggplot(MSE_calc
, aes(model, MSE)) +
geom_bar(
# aes(fill = uncer),
stat = "identity", position = position_dodge()) +
# scale_fill_manual(values = c("steelblue4", "tomato4")) +
coord_flip() +
ylab("Mean Absolute Error") +
# guides(fill = guide_legend(title = "Model Uncertainty?")) +
scale_x_discrete(labels = c(
"Full Model"
, "Temporally Averaged
Bird Community"
, "Spatially Averaged
Bird Community"
, "Spatially Averaged
Temperature"
, "Mean Model"
, "Temporally Averaged
Temperature"
)) +
xlab("Model") +
theme(
panel.grid.major = element_line(colour = "black", size = 0.1)
, legend.key.size = unit(.55, "cm")
)
#####
## restricted to the months that we care about?
#####
submonths <- c(4, 5, 9, 10)
MSE_calc_nu <- data.frame(
model = c("FULL", "TS", "TT", "BCS", "BCT", "EMPTY")
, MSE = c(
## full model to itself
0
, mean(abs(
(comm_comp_summary_p2_well_observed_full %>% filter(month %in% submonths))[["med_comp"]] -
(comm_comp_summary_p2_well_observed_ts %>% filter(month %in% submonths))[["med_comp"]]
))
, mean(abs(
(comm_comp_summary_p2_well_observed_full %>% filter(month %in% submonths))[["med_comp"]] -
(comm_comp_summary_p2_well_observed_tt %>% filter(month %in% submonths))[["med_comp"]]
))
, mean(abs(
(comm_comp_summary_p2_well_observed_full %>% filter(month %in% submonths))[["med_comp"]] -
(comm_comp_summary_p2_well_observed_bcs %>% filter(month %in% submonths))[["med_comp"]]
))
, mean(abs(
(comm_comp_summary_p2_well_observed_full %>% filter(month %in% submonths))[["med_comp"]] -
(comm_comp_summary_p2_well_observed_bct %>% filter(month %in% submonths))[["med_comp"]]
))
## full model to a mean-only model
, mean(abs(
(comm_comp_summary_p2_well_observed_full %>% filter(month %in% submonths))[["med_comp"]] -
(comm_comp_summary_p2_well_observed_mean_model %>% filter(month %in% submonths))[["med_comp"]]
))
)
)
MSE_calc_nu <- MSE_calc_nu[rev(order(MSE_calc_nu$MSE)), ]
MSE_calc_nu <- transform(MSE_calc_nu, model = factor(model, levels = MSE_calc_nu$model))
MSE_calc
#####
## ggplot this result
#####
MSE_calc_nu <- transform(MSE_calc_nu, Uncertainty = "None")
MSE_calc <- transform(MSE_calc, Uncertainty = "All")
MSE_calc_f <- rbind(MSE_calc, MSE_calc_nu)
ggplot(MSE_calc_f
, aes(model, MSE)) +
# geom_bar(stat = "identity", position = position_dodge(), fill = "steelblue4") +
geom_bar(aes(fill = Uncertainty), stat = "identity", position = position_dodge()) +
scale_fill_manual(values = c("steelblue4", "tomato4")) +
coord_flip() +
ylab("Mean Absolute Error") +
# guides(fill = guide_legend(title = "Model Uncertainty?")) +
scale_x_discrete(labels = rev(c(
"Full Model"
, "Temporally Averaged
Bird Community"
, "Spatially Averaged
Bird Community"
, "Spatially Averaged
Temperature"
, "Temporally Averaged
Temperature"
, "Mean Model"
))) +
xlab("Model") +
theme(
panel.grid.major = element_line(colour = "black", size = 0.1)
, legend.key.size = unit(.55, "cm")
)