-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBBS_trends_analysis.R
126 lines (112 loc) · 4.63 KB
/
BBS_trends_analysis.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
#### Post-processing ####
library(mgcv)
library(dplyr)
library(ggplot2)
library(marginaleffects)
# Source a few utility functions
source('Functions/utilities.R')
# Source the fitted models and data objects
load("./data/model_objects.rda")
mod <- readRDS("./models/mod.rds")
mod_bench1 <- readRDS("./models/mod_bench1.rds")
# Don't run full mgcv summaries; they are extremely computationally
# expensive for these models
summary(mod, re.test = FALSE)
summary(mod_bench1, re.test = FALSE)
AIC(mod); AIC(mod_bench1)
mod2 <- readRDS("./models/mod2.rds")
summary(mod2, re.test = FALSE)
AIC(mod2)
# Calculate phylogenetic contributions to species' nonlinear trend
# estimates
phylo_derivs <- compute_derivs(data = mod_data,
model = mod,
type = 'phylogenetic',
n_cores = 8)
plot_trait_conts(trait_derivs = phylo_derivs,
type = 'phylogenetic')
# Proportion of trends in which phylogenetic information contributes at
# least 50% to the estimated variance of the function compared to
# other information (i.e. spatial and non-phylogenetic variation)
phylo_derivs %>%
dplyr::summarise(prop = 100 * length(which(trait_conts >= 0.5)) /
dplyr::n())
# Repeat for the functional relatedness model
func_derivs <- compute_derivs(mod_data,
model = mod2,
type = 'functional',
n_cores = 8)
plot_trait_conts(trait_derivs = func_derivs,
type = 'functional')
func_derivs %>%
dplyr::summarise(prop = 100 * length(which(trait_conts >= 0.5)) /
dplyr::n())
# Plot some predictions against truth
plot_sp_trends(model = mod,
data = mod_data,
species = 'Hirundo_rustica',
type = 'response',
median_records = FALSE)
plot_sp_trends(model = mod,
data = mod_data,
species = levels(mod_data$sp_latin)[8],
type = 'response',
median_records = FALSE)
plot_sp_trends(model = mod,
data = mod_data,
species = levels(mod_data$sp_latin)[9],
type = 'response',
median_records = FALSE)
plot_sp_trends(model = mod,
data = mod_data,
species = levels(mod_data$sp_latin)[30],
type = 'response',
median_records = FALSE)
plot_sp_trends(model = mod,
data = mod_data,
species = levels(mod_data$sp_latin)[65],
type = 'response',
median_records = FALSE)
# Plot some expected trends by fixing the offsets and asking
# how the model would expect species' abundances to change over time
plot_sp_trends(model = mod,
data = mod_data,
species = 'Hirundo_rustica',
type = 'expected',
median_records = TRUE)
plot_sp_trends(model = mod,
data = mod_data,
species = levels(mod_data$sp_latin)[8],
type = 'expected',
median_records = TRUE)
plot_sp_trends(model = mod,
data = mod_data,
species = levels(mod_data$sp_latin)[9],
type = 'expected',
median_records = TRUE)
plot_sp_trends(model = mod,
data = mod_data,
species = levels(mod_data$sp_latin)[30],
type = 'expected',
median_records = TRUE)
plot_sp_trends(model = mod,
data = mod_data,
species = levels(mod_data$sp_latin)[2],
type = 'expected',
median_records = TRUE)
# Some attempt at an 'average' temporal trend for the last
# 25 years of data. Use only 65% of species
plot_av_trend(model = mod, mod_data = mod_data,
prop_species = 0.65)
# Will need to:
# 1. fit simpler models that ignore all relationship information, as
# well as phylogenetic/functional slopes (linear) models for comparisons
# 2. leave 10% of combos (strata x species) out and generate
# predictions; compute CRPS from each model; run several CV folds
# 3. assess variation in prediction accuracy across space and
# across the trees
# 4. visualise the proportional change in variance explained
# (as calculated above) on the tree to see if there are clusters
# of species that depend more heavily on phylogenetic relationships
# 5. assess support for nonlinearity of trends; determine when trends
# were accelerating / decelerating most rapidly