-
Notifications
You must be signed in to change notification settings - Fork 0
/
PP-model-transfer.R
372 lines (280 loc) · 15.5 KB
/
PP-model-transfer.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
#Script to run model transfer analyses: PP case study###
#PACKAGES####
require(tscount)
require(portalr)
require(dplyr)
require(vctrs)
require(rsample)
require(lubridate)
require(portalcasting)
require(ggplot2)
require(tidymodels)
require(purrr)
require(yardstick)
require(Metrics)
require(tidyr)
require(ggpubr)
require(tidyverse)
require(overlapping)
source("model-transfer-functions.R")
#DATA MANIPULATION####
#rodent data
use_default_data_path("D:\\Dropbox (UFL)\\PhD-stuff\\Portal-Forecast-Swap")
rodent_data=summarize_rodent_data(
path = get_default_data_path(),
clean = TRUE,
level="Treatment",
type = "Rodents",
plots = "Longterm",
unknowns = FALSE,
shape = "crosstab",
time = "newmoon",
output = "abundance",
na_drop = FALSE,
zero_drop = FALSE,
min_traps = 1,
min_plots = 1,
effort = TRUE,
download_if_missing = TRUE,
quiet = FALSE
)
ppcont_dat=rodent_data%>%
select(newmoonnumber,treatment, PP)%>%
replace_na(list(treatment='control'))%>%
filter(treatment=="control")
ppexcl_dat=rodent_data%>%
select(newmoonnumber,treatment, PP)%>%
replace_na(list(treatment='exclosure'))%>%
filter(treatment=="exclosure")
#covariates data
covars=weather(level="newmoon", fill=TRUE, horizon=365, path=get_default_data_path())%>%
select(newmoonnumber,meantemp, mintemp, maxtemp, precipitation, warm_precip, cool_precip)%>%
mutate(meantemp_lag1=lag(meantemp,order_by=newmoonnumber))
ppcont_covs=right_join(covars,ppcont_dat)%>%
select(newmoonnumber, meantemp, meantemp_lag1,
warm_precip, cool_precip, PP)%>%rename("abundance"="PP")
ppexcl_covs=right_join(covars,ppexcl_dat)%>%
select(newmoonnumber, meantemp, meantemp_lag1,
warm_precip, cool_precip, PP)%>%rename("abundance"="PP")
#select data from Sept 2010- Dec 2019
ppcont_covs_dat=ppcont_covs%>% filter(!newmoonnumber<411, !newmoonnumber>526)
ppexcl_covs_dat=ppexcl_covs%>% filter(!newmoonnumber<411, !newmoonnumber>526)
#interpolate missing data
ppcont_covs_dat$abundance=round_na.interp(ppcont_covs_dat$abundance)
ppexcl_covs_dat$abundance=round_na.interp(ppexcl_covs_dat$abundance)
#rolling origin object for analysis####
n_moons_yr=12
n_yrs=5
n_moons_train=n_moons_yr*n_yrs
n_moons_test=n_moons_yr*1
PPcontrol_dat <-
rolling_origin(
data = ppcont_covs_dat, #all PP control data (2010-2019)
initial = n_moons_train, #samples used for modelling (training)
assess = n_moons_test, # number of samples used for each assessment resample (horizon)
cumulative = FALSE #length of analysis set is fixed;
)
PPexclosure_dat <-
rolling_origin(
data = ppexcl_covs_dat, #all PP exclosure data (2010-2019)
initial = n_moons_train, #samples used for modelling (training)
assess = n_moons_test, # number of samples used for each assessment resample (horizon)
cumulative = FALSE #length of analysis set is fixed
)
#DATA ANALYSES####
#Model fitting####
#fitting matched and mismatched models
#add column for model(same data and model)
PPcontrol_dat$model=map(PPcontrol_dat$splits, rolling_mod)
PPexclosure_dat$model=map(PPexclosure_dat$splits, rolling_mod)
#matched and mismatched model predictions
#add column for model predictions (same data and model)
PPcontrol_dat$preds_same=pmap(list(PPcontrol_dat$splits,PPcontrol_dat$model, PPcontrol_dat$model), get_preds)
PPexclosure_dat$preds_same=pmap(list(PPexclosure_dat$splits,PPexclosure_dat$model, PPexclosure_dat$model), get_preds)
#add column for model predictions from switched model
PPcontrol_dat$preds_switch=pmap(list(PPcontrol_dat$splits, PPexclosure_dat$model, PPcontrol_dat$model), get_preds)
PPexclosure_dat$preds_switch=pmap(list(PPexclosure_dat$splits, PPcontrol_dat$model, PPexclosure_dat$model), get_preds)
#matched and mismatched model evaluations
#add column for model evals from same model
PPcontrol_dat$evals_same=pmap(list(PPcontrol_dat$splits,PPcontrol_dat$preds_same), mod_evals_same)
PPexclosure_dat$evals_same=pmap(list(PPexclosure_dat$splits,PPexclosure_dat$preds_same), mod_evals_same)
#add column for model evals from switched model
PPcontrol_dat$evals_switch=pmap(list(PPcontrol_dat$splits,PPcontrol_dat$preds_switch),mod_evals_switch)
PPexclosure_dat$evals_switch=pmap(list(PPexclosure_dat$splits,PPexclosure_dat$preds_switch),mod_evals_switch)
#Parameter comparison####
ppcontrol_int_coefs=PPcontrol_dat$model%>%map(coef)%>%map_dbl(1)
ppexclosure_int_coefs=PPexclosure_dat$model%>%map(coef)%>%map_dbl(1)
ppcontrol_b1_coefs=PPcontrol_dat$model%>%map(coef)%>%map_dbl(2)
ppexclosure_b1_coefs=PPexclosure_dat$model%>%map(coef)%>%map_dbl(2)
ppcontrol_b12_coefs=PPcontrol_dat$model%>%map(coef)%>%map_dbl(3)
ppexclosure_b12_coefs=PPexclosure_dat$model%>%map(coef)%>%map_dbl(3)
ppcontrol_temp1_coefs=PPcontrol_dat$model%>%map(coef)%>%map_dbl(4)
ppexclosure_temp1_coefs=PPexclosure_dat$model%>%map(coef)%>%map_dbl(4)
ppcontrol_warmprec_coefs=PPcontrol_dat$model%>%map(coef)%>%map_dbl(5)
ppexclosure_warmprec_coefs=PPexclosure_dat$model%>%map(coef)%>%map_dbl(5)
ppcontrol_coolprec_coefs=PPcontrol_dat$model%>%map(coef)%>%map_dbl(6)
ppexclosure_coolprec_coefs=PPexclosure_dat$model%>%map(coef)%>%map_dbl(6)
warmprecpp=cbind(ppcontrol_warmprec_coefs, ppexclosure_warmprec_coefs)%>%as.data.frame%>%
rename("control"="ppcontrol_warmprec_coefs", "removal"="ppexclosure_warmprec_coefs")%>%
pivot_longer(cols=c(1:2),names_to="treatment", values_to = "warm_precip")
coolprecpp=cbind(ppcontrol_coolprec_coefs, ppexclosure_coolprec_coefs)%>%as.data.frame%>%
rename("control"="ppcontrol_coolprec_coefs", "removal"="ppexclosure_coolprec_coefs")%>%
pivot_longer(cols=c(1:2),names_to="treatment", values_to = "cool_precip")
tempspp=cbind(ppcontrol_temp1_coefs, ppexclosure_temp1_coefs)%>%as.data.frame%>%
rename("control"="ppcontrol_temp1_coefs", "removal"="ppexclosure_temp1_coefs")%>%
pivot_longer(cols=c(1:2),names_to="treatment", values_to = "temp")
intspp=cbind(ppcontrol_int_coefs, ppexclosure_int_coefs)%>%as.data.frame%>%
rename("control"="ppcontrol_int_coefs", "removal"="ppexclosure_int_coefs")%>%
pivot_longer(cols=c(1:2),names_to="treatment", values_to = "intercept")
b1pp=cbind(ppcontrol_b1_coefs, ppexclosure_b1_coefs)%>%as.data.frame%>%
rename("control"="ppcontrol_b1_coefs", "removal"="ppexclosure_b1_coefs")%>%
pivot_longer(cols=c(1:2),names_to="treatment", values_to = "beta1")
b12pp=cbind(ppcontrol_b12_coefs, ppexclosure_b12_coefs)%>%as.data.frame%>%
rename("control"="ppcontrol_b12_coefs", "removal"="ppexclosure_b12_coefs")%>%
pivot_longer(cols=c(1:2),names_to="treatment", values_to = "beta12")
coef_df_PP=as.data.frame(list(intspp, b1pp, b12pp, tempspp, warmprecpp, coolprecpp))%>%
select(treatment, intercept, beta1, beta12, temp,cool_precip, warm_precip)
ppint_cont=coef_df_PP%>%filter(treatment=="control")%>%select(intercept)%>%rename("control"="intercept")
ppint_excl=coef_df_PP%>%filter(treatment=="removal")%>%select(intercept)%>%rename("removal"="intercept")
ppar1_cont=coef_df_PP%>%filter(treatment=="control")%>%select(beta1)%>%rename("control"="beta1")
ppar1_excl=coef_df_PP%>%filter(treatment=="removal")%>%select(beta1)%>%rename("removal"="beta1")
ppar12_cont=coef_df_PP%>%filter(treatment=="control")%>%select(beta12)%>%rename("control"="beta12")
ppar12_excl=coef_df_PP%>%filter(treatment=="removal")%>%select(beta12)%>%rename("removal"="beta12")
pptemp_cont=coef_df_PP%>%filter(treatment=="control")%>%select(temp)%>%rename("control"="temp")
pptemp_excl=coef_df_PP%>%filter(treatment=="removal")%>%select(temp)%>%rename("removal"="temp")
ppcprec_cont=coef_df_PP%>%filter(treatment=="control")%>%select(cool_precip)%>%rename("control"="cool_precip")
ppcprec_excl=coef_df_PP%>%filter(treatment=="removal")%>%select(cool_precip)%>%rename("removal"="cool_precip")
ppwprec_cont=coef_df_PP%>%filter(treatment=="control")%>%select(warm_precip)%>%rename("control"="warm_precip")
ppwprec_excl=coef_df_PP%>%filter(treatment=="removal")%>%select(warm_precip)%>%rename("removal"="warm_precip")
ppo1=cbind(ppint_cont, ppint_excl)
ppo2=cbind(ppar1_cont, ppar1_excl)
ppo3=cbind(ppar12_cont, ppar12_excl)
ppo4=cbind(pptemp_cont, pptemp_excl)
ppo5=cbind(ppcprec_cont, ppcprec_excl)
ppo6=cbind(ppwprec_cont, ppwprec_excl)
##degree of overlap####
overlap(ppo1, plot=T)
overlap(ppo2, plot=T)
overlap(ppo3, plot=T)
overlap(ppo4, plot=T)
overlap(ppo5, plot=T)
overlap(ppo6, plot=T)
##directional shift####
ppo1=ppo1%>%mutate(B_diff=control-removal)
ppo2=ppo2%>%mutate(B_diff=control-removal)
ppo3=ppo3%>%mutate(B_diff=control-removal)
ppo4=ppo4%>%mutate(B_diff=control-removal)
ppo5=ppo5%>%mutate(B_diff=control-removal)
ppo6=ppo6%>%mutate(B_diff=control-removal)
length(which(ppo1$B_diff>0))/45
length(which(ppo2$B_diff>0))/45
length(which(ppo3$B_diff>0))/45
length(which(ppo4$B_diff>0))/45
length(which(ppo5$B_diff>0))/45
length(which(ppo6$B_diff>0))/45
#Evaluate model transferability####
###forecasts####
#control-control
ppcont_preds_same=pmap(list(PPcontrol_dat$splits,PPcontrol_dat$preds_same), get_dat_same)
#control dat-exclosure mod
ppcont_preds_switch=pmap(list(PPcontrol_dat$splits,PPcontrol_dat$preds_switch), get_dat_switch)
#exclosure
ppexcl_preds_same=pmap(list(PPexclosure_dat$splits,PPexclosure_dat$preds_same), get_dat_same)
#exclosure dat-controlmod
ppexcl_preds_switch=pmap(list(PPexclosure_dat$splits,PPexclosure_dat$preds_switch), get_dat_switch)
m=rep(seq(1:45), each=12) #no.of splits
code1="same"
code2="switched"
PPpreds_cont_same=do.call(rbind.data.frame, ppcont_preds_same)
PPpreds_cont_same=cbind(PPpreds_cont_same, m, code1)
PPpreds_cont_switch=do.call(rbind.data.frame, ppcont_preds_switch)
PPpreds_cont_switch=cbind(PPpreds_cont_switch, m, code2)
PPpreds_excl_same=do.call(rbind.data.frame, ppexcl_preds_same)
PPpreds_excl_same=cbind(PPpreds_excl_same, m, code1)
PPpreds_excl_switch=do.call(rbind.data.frame, ppexcl_preds_switch)
PPpreds_excl_switch=cbind(PPpreds_excl_switch, m, code2)
###RMSE####
ppcont_evals1_diff=pmap(list(PPcontrol_dat$splits,PPcontrol_dat$evals_same, PPcontrol_dat$evals_switch ,PPcontrol_dat$id), get_evals1_diff)
ppevals1_cont_diff=do.call(rbind.data.frame, ppcont_evals1_diff)%>%mutate(plot="control")
ppexcl_evals1_diff=pmap(list(PPexclosure_dat$splits,PPexclosure_dat$evals_same, PPexclosure_dat$evals_switch ,PPexclosure_dat$id), get_evals1_diff)
ppevals1_excl_diff=do.call(rbind.data.frame, ppexcl_evals1_diff)%>%mutate(plot="removal")
#h=1
ppevals1=rbind(ppevals1_cont_diff, ppevals1_excl_diff)
ppevals1$newmoon=as.integer(ppevals1$newmoon)
ppevals1$h=as.integer(ppevals1$h)
ppevals1$score_same=as.numeric(ppevals1$score_same)
ppevals1$score_switch=as.numeric(ppevals1$score_switch)
ppevals1$score_diff=as.numeric(ppevals1$score_diff)
#h=6
ppcont_evals6_diff=pmap(list(PPcontrol_dat$splits,PPcontrol_dat$evals_same, PPcontrol_dat$evals_switch ,PPcontrol_dat$id), get_evals6_diff)
ppevals6_cont_diff=do.call(rbind.data.frame, ppcont_evals6_diff)%>%mutate(plot="control")
ppexcl_evals6_diff=pmap(list(PPexclosure_dat$splits,PPexclosure_dat$evals_same, PPexclosure_dat$evals_switch ,PPexclosure_dat$id), get_evals6_diff)
ppevals6_excl_diff=do.call(rbind.data.frame, ppexcl_evals6_diff)%>%mutate(plot="removal")
ppevals6=rbind(ppevals6_cont_diff, ppevals6_excl_diff)
ppevals6$newmoon=as.integer(ppevals6$newmoon)
ppevals6$h=as.integer(ppevals6$h)
ppevals6$score_same=as.numeric(ppevals6$score_same)
ppevals6$score_switch=as.numeric(ppevals6$score_switch)
ppevals6$score_diff=as.numeric(ppevals6$score_diff)
#h=12
ppcont_evals12_diff=pmap(list(PPcontrol_dat$splits,PPcontrol_dat$evals_same, PPcontrol_dat$evals_switch ,PPcontrol_dat$id), get_evals12_diff)
ppevals12_cont_diff=do.call(rbind.data.frame, ppcont_evals12_diff)%>%mutate(plot="control")
ppexcl_evals12_diff=pmap(list(PPexclosure_dat$splits,PPexclosure_dat$evals_same, PPexclosure_dat$evals_switch ,PPexclosure_dat$id), get_evals12_diff)
ppevals12_excl_diff=do.call(rbind.data.frame, ppexcl_evals12_diff)%>%mutate(plot="removal")
ppevals12=rbind(ppevals12_cont_diff, ppevals12_excl_diff)
ppevals12$newmoon=as.integer(ppevals12$newmoon)
ppevals12$h=as.integer(ppevals12$h)
ppevals12$score_same=as.numeric(ppevals12$score_same)
ppevals12$score_switch=as.numeric(ppevals12$score_switch)
ppevals12$score_diff=as.numeric(ppevals12$score_diff)
###Brier scores####
#control
#combine predictions on same and switched models for control data
pp_preds_control=left_join(PPpreds_cont_same, PPpreds_cont_switch, by=c("moon", "holdout", "m"))
#calculate brier score for same models
pp_brier_cont1=scoring(pred=pp_preds_control$preds_same, response=pp_preds_control$holdout,distr="nbinom", distrcoefs=2, individual=TRUE,
cutoff=1000)%>%select(quadratic)
#combine same model predictions and brier score dataframes
pp_brier_cont_same=cbind(pp_preds_control, pp_brier_cont1)%>%rename(quadratic_same=quadratic)
#calculate brier score for switched models
pp_brier_cont2=scoring(pred=pp_preds_control$preds_switch, response=pp_preds_control$holdout,distr="nbinom", distrcoefs=2, individual=TRUE,
cutoff=1000)%>%select(quadratic)
#combine switched model predictions and brier score dataframes
pp_brier_cont_switch=cbind(pp_preds_control, pp_brier_cont2)%>%rename(quadratic_switch=quadratic)
#calculate brier score differences
pp_brier_control=left_join(pp_brier_cont_same, pp_brier_cont_switch)%>%mutate(treatment="control",brier_diff=quadratic_same-quadratic_switch)
#select relevant rows with horizons 1,6 12
pp_brier_control1=pp_brier_control%>%group_by(m)%>%slice(which(row_number()%in%c(1,6,12)))
#subdivide into each horizon for easier plotting
ppb1=pp_brier_control1%>%group_by(m)%>%slice_head(n=1)%>%mutate(horizon="1")
ppb6=pp_brier_control1%>%group_by(m)%>%slice(which(row_number()==2))%>%mutate(horizon="6")
ppb12=pp_brier_control1%>%group_by(m)%>%slice(which(row_number()==3))%>%mutate(horizon="12")
#combine all 3 horizons
ppb=rbind(ppb1,ppb6,ppb12)
#exclosure###
#combine predictions on same and switched models for exclosure data
pp_preds_exclosure=left_join(PPpreds_excl_same, PPpreds_excl_switch, by=c("moon", "holdout", "m"))
#calculate brier score for same models
pp_brier_excl1=scoring(pred=pp_preds_exclosure$preds_same, response=pp_preds_exclosure$holdout,distr="nbinom", distrcoefs=2, individual=TRUE,
cutoff=1000)%>%select(quadratic)
#combine same model predictions and brier score dataframes
pp_brier_excl_same=cbind(pp_preds_exclosure, pp_brier_excl1)%>%rename(quadratic_same=quadratic)
#calculate brier score for switched models
pp_brier_excl2=scoring(pred=pp_preds_exclosure$preds_switch, response=pp_preds_exclosure$holdout,distr="nbinom", distrcoefs=2, individual=TRUE,
cutoff=1000)%>%select(quadratic)
#combine switched model predictions and brier score dataframes
pp_brier_excl_switch=cbind(pp_preds_exclosure, pp_brier_excl2)%>%rename(quadratic_switch=quadratic)
#calculate brier score differences
pp_brier_exclosure=left_join(pp_brier_excl_same, pp_brier_excl_switch)%>%mutate(treatment="removal",brier_diff=quadratic_same-quadratic_switch)
#select relevant rows with horizons 1,6 12
pp_brier_exclosure1=pp_brier_exclosure%>%group_by(m)%>%slice(which(row_number()%in%c(1,6,12)))
#subdivide into each horizon for easier plotting
ppbx1=pp_brier_exclosure1%>%group_by(m)%>%slice_head(n=1)%>%mutate(horizon="1")
ppbx6=pp_brier_exclosure1%>%group_by(m)%>%slice(which(row_number()==2))%>%mutate(horizon="6")
ppbx12=pp_brier_exclosure1%>%group_by(m)%>%slice(which(row_number()==3))%>%mutate(horizon="12")
#combine all 3 horizons
ppbx=rbind(ppbx1,ppbx6, ppbx12)
#combine control and exclosure and filter out per horizon for plotting
pp_briers1=rbind(ppb,ppbx)%>%filter(horizon==1)
pp_briers6=rbind(ppb,ppbx)%>%filter(horizon==6)
pp_briers12=rbind(ppb,ppbx)%>%filter(horizon==12)