-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBBS_trends_models.R
254 lines (228 loc) · 9.81 KB
/
BBS_trends_models.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
#### Fit candidate phylogenetic and functional smoothing models
# to the full dataset ####
library(mgcv)
library(dplyr)
# Load data objects
load("./data/model_objects.rda")
# Source a few utility functions
source('Functions/utilities.R')
# Randomly drop 10% of species * region combinations for using as a
# validation set
all_sp_combos <- mod_data %>%
dplyr::select(sp_latin, strata_name) %>%
dplyr::distinct()
validation_combos <- all_sp_combos %>%
dplyr::slice_sample(prop = 0.1) %>%
dplyr::mutate(training = 'No')
mod_data <- validation_combos %>%
dplyr::right_join(mod_data) %>%
dplyr::mutate(training = ifelse(is.na(training), 'Yes', 'No'))
training_data <- mod_data %>%
dplyr::filter(training == 'Yes')
validation_data <- mod_data %>%
dplyr::filter(training == 'No')
rm(validation_combos, all_sp_combos)
# Save the training splits
save(training_data,
validation_data,
file = "./data/training_split.rds")
# Some useful details on MRF smooths:
# https://stats.stackexchange.com/questions/638522/gam-model-with-spatial-account-via-mrf
# Fit a phylogenetic trend model uses a Gaussian observation
# process. The model is a decomposition that includes terms capturing
# marginal spatial and spatiotemporal fields, as well as contributions
# from species' phylogenetic relationships. Higher-order interaction terms
# allow each species' spatiotemporal field to be informed by phylogeny
ptm <- proc.time()
mod <- bam(count_sc ~
## First order effects ##
# No need for a global intercept
0 +
# Account for variation in number of routes per region per year
n_records +
# Primary smooth of year
s(year, bs = 'cr', k = 10) +
## Second order effects ##
# Non-phylogenetic average spatiotemporal pattern
ti(year, strata_name,
bs = c('cr', 'mrf'),
xt = list(list(penalty = NULL),
list(nb = strat_penalty)),
k = c(10, 25)) +
# Phylogenetically-informed average temporal trends
ti(year, sp_latin_phy,
bs = c('cr', 'mrf'),
xt = list(list(penalty = NULL),
list(penalty = phylo_penalty)),
k = c(10, 25)) +
# Non-phylogenetic average temporal trends
ti(year, sp_latin,
bs = c('cr', 'mrf'),
xt = list(list(penalty = NULL),
list(penalty = sp_penalty)),
k = c(10, 25)) +
## Third order effects ##
# Phylogenetically-informed spatiotemporal effects
ti(year, strata_name, sp_latin_phy,
bs = c('cr', 'mrf', 'mrf'),
xt = list(list(penalty = NULL),
list(nb = strat_penalty),
list(penalty = phylo_penalty)),
k = c(10, 25, 25)) +
# Non-phylogenetic spatiotemporal effects
ti(year, strata_name, sp_latin,
bs = c('cr', 'mrf', 'mrf'),
xt = list(list(penalty = NULL),
list(nb = strat_penalty),
list(penalty = sp_penalty)),
k = c(10, 25, 25)),
family = gaussian(),
data = training_data,
method = 'fREML',
select = TRUE,
drop.unused.levels = FALSE,
discrete = TRUE,
# Adjust appropriately; I'm using a beefy i9 processor
# and this model takes ~90 - 120 minutes to complete
nthreads = 12)
runtime <- proc.time() - ptm
gc()
mod$runtime <- runtime
# Reduce model size and add draws of posterior coefficients
mod <- post_process(mod)
# Save the model object
dir.create('models')
saveRDS(mod, "./models/mod.rds")
# Now for two benchmark variants of model 1. Benchmark 1 ignores the phylogenetic
# components but still allows for nonlinear trends
ptm <- proc.time()
mod_bench1 <- bam(count_sc ~
0 +
n_records +
s(year, bs = 'cr', k = 10) +
ti(year, strata_name,
bs = c('cr', 'mrf'),
xt = list(list(penalty = NULL),
list(nb = strat_penalty)),
k = c(10, 25)) +
ti(year, sp_latin,
bs = c('cr', 'mrf'),
xt = list(list(penalty = NULL),
list(penalty = sp_penalty)),
k = c(10, 25)) +
ti(year, strata_name, sp_latin,
bs = c('cr', 'mrf', 'mrf'),
xt = list(list(penalty = NULL),
list(nb = strat_penalty),
list(penalty = sp_penalty)),
k = c(10, 25, 25)),
family = gaussian(),
data = training_data,
method = 'fREML',
select = TRUE,
drop.unused.levels = FALSE,
discrete = TRUE,
nthreads = 12)
runtime <- proc.time() - ptm
gc()
mod_bench1$runtime <- runtime
mod_bench1 <- post_process(mod_bench1)
saveRDS(mod_bench1, "./models/mod_bench1.rds")
# The second benchmark allows for phylogenetic and non-phylogenetic
# slopes but assumes the trend is linear
ptm <- proc.time()
mod_bench2 <- bam(count_sc ~
0 +
n_records +
# Feeding in large smoothing penalties will regularise
# smooths back to linear functions
s(year, bs = 'cr', k = 3, sp = .Machine$double.xmax) +
ti(year, strata_name,
bs = c('cr', 'mrf'),
xt = list(list(penalty = NULL),
list(nb = strat_penalty)),
k = c(3, 25),
sp = c(.Machine$double.xmax, -1)) +
ti(year, sp_latin_phy,
bs = c('cr', 'mrf'),
xt = list(list(penalty = NULL),
list(penalty = phylo_penalty)),
k = c(13, 25),
sp = c(.Machine$double.xmax, -1)) +
ti(year, sp_latin,
bs = c('cr', 'mrf'),
xt = list(list(penalty = NULL),
list(penalty = sp_penalty)),
k = c(3, 25),
sp = c(.Machine$double.xmax, -1)) +
ti(year, strata_name, sp_latin_phy,
bs = c('cr', 'mrf', 'mrf'),
xt = list(list(penalty = NULL),
list(nb = strat_penalty),
list(penalty = phylo_penalty)),
k = c(3, 25, 25),
sp = c(.Machine$double.xmax, -1, -1)) +
ti(year, strata_name, sp_latin,
bs = c('cr', 'mrf', 'mrf'),
xt = list(list(penalty = NULL),
list(nb = strat_penalty),
list(penalty = sp_penalty)),
k = c(3, 25, 25),
sp = c(.Machine$double.xmax, -1, -1)),
family = gaussian(),
data = training_data,
method = 'fREML',
drop.unused.levels = FALSE,
discrete = TRUE,
nthreads = 12)
runtime <- proc.time() - ptm
gc()
mod_bench2$runtime <- runtime
mod_bench2 <- post_process(mod_bench2)
saveRDS(mod_bench2, "./models/mod_bench2.rds")
# Now a second model that uses functional relationships in place
# of phylogenetic relationships
ptm <- proc.time()
mod2 <- bam(count_sc ~
0 +
n_records +
s(year, bs = 'cr', k = 10) +
ti(year, strata_name,
bs = c('cr', 'mrf'),
xt = list(list(penalty = NULL),
list(nb = strat_penalty)),
k = c(10, 25)) +
ti(year, sp_latin_func,
bs = c('cr', 'mrf'),
xt = list(list(penalty = NULL),
list(penalty = func_penalty)),
k = c(10, 25)) +
ti(year, sp_latin,
bs = c('cr', 'mrf'),
xt = list(list(penalty = NULL),
list(penalty = sp_penalty)),
k = c(10, 25)) +
ti(year, strata_name, sp_latin_func,
bs = c('cr', 'mrf', 'mrf'),
xt = list(list(penalty = NULL),
list(nb = strat_penalty),
list(penalty = func_penalty)),
k = c(10, 25, 25)) +
ti(year, strata_name, sp_latin,
bs = c('cr', 'mrf', 'mrf'),
xt = list(list(penalty = NULL),
list(nb = strat_penalty),
list(penalty = sp_penalty)),
k = c(12, 25, 25)),
family = gaussian(),
data = training_data,
method = 'fREML',
select = TRUE,
drop.unused.levels = FALSE,
discrete = TRUE,
nthreads = 12)
runtime <- proc.time() - ptm
gc()
mod2$runtime <- runtime
mod2 <- post_process(mod2)
saveRDS(mod2, "./models/mod2.rds")