-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcasestudy1_kestrel.R
629 lines (548 loc) · 17.5 KB
/
casestudy1_kestrel.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
620
621
622
623
624
625
626
627
628
629
# Load packages
library(mvgam) # Fit, interrogate and forecast DGAMs
library(tidyverse) # Tidy and flexible data manipulation
library(ggplot2) # Flexible plotting
# Set up plotting environment
theme_set(theme_classic(base_size = 15,
base_family = 'serif'))
myhist = function(...){
geom_histogram(col = 'white',
fill = 'darkred', ...)
}
hist_theme = function(){
theme(axis.line.y = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y = element_blank())
}
par(family = "serif",
las = 0,
mar = c(4.2,
4.4,
2.2,
2.2),
mgp = c(2.2,
0.5,
0),
bty = "l",
cex.axis = 1.25,
cex.lab = 1.4,
cex.main = 1.5,
xaxs = 'r',
yaxs = 'r',
pch = 16)
# Load the annual American kestrel, Falco sparverius, abundance
# time series taken in British Columbia, Canada. These data have
# been collected annually, corrected for changes in observer
# coverage and detectability, and logged. They can be found in
# the MARSS package
load(url('https://github.com/atsa-es/MARSS/raw/master/data/kestrel.rda'))
head(kestrel)
# Arrange the data into a long-format data.frame
regions <- c("BC",
"Alb",
"Sask")
model_data <- do.call(rbind,
lapply(
seq_along(regions),
function(x){
data.frame(year = kestrel[, 1],
# Reverse the logging so that we deal
# directly with the detection-adjusted
# counts
adj_count = exp(kestrel[, 1 + x]),
region = regions[x])}
)
) %>%
# Add series and time indicators for mvgam modelling
dplyr::mutate(yearfac = as.factor(year),
region = as.factor(region),
series = as.factor(region),
time = year)
# Inspect modelling data structure
head(model_data)
dplyr::glimpse(model_data)
levels(model_data$series)
# Split the data into a training and a testing split
data_train <- model_data %>%
dplyr::filter(year <= 2001)
data_test <- model_data %>%
dplyr::filter(year > 2001)
# Plot all three time series together
plot_mvgam_series(data = data_train,
y = 'adj_count',
series = 'all') +
theme_bw(base_size = 15,
base_family = 'serif')
# Now plot features for just one series at a time
plot_mvgam_series(data = data_train,
newdata = data_test,
y = 'adj_count',
series = 1,
lines = FALSE) & # The & is used by patchwork
# to add themes to all sub-plots
theme_bw(base_size = 15,
base_family = 'serif')
plot_mvgam_series(data = data_train,
newdata = data_test,
y = 'adj_count',
series = 2) &
theme_bw(base_size = 15,
base_family = 'serif')
plot_mvgam_series(data = data_train,
newdata = data_test,
y = 'adj_count',
series = 3) &
theme_bw(base_size = 15,
base_family = 'serif')
# Look at the distribution of the outcome variable
ggplot(data_train,
aes(x = adj_count)) +
myhist() +
labs(x = 'Adjusted count', y = '') +
hist_theme()
summary(data_train$adj_count)
# Heavy-ish tail to the right; perhaps a Gamma distribution
# Inspect default priors for a simple model that only includes
# random intercepts for years, implying that all three series
# share the same (random) year effects
?mgcv::random.effects
?mgcv::gam.models
?mvgam::get_mvgam_priors
p <- get_mvgam_priors(formula = adj_count ~ series +
s(yearfac, bs = 're'),
data = data_train,
family = Gamma())
View(p)
# prior() from brms can be used within mvgam()
# to change default prior distributions
?brms::prior
# Fit the model
mod1 <- mvgam(
# Observation formula
formula = adj_count ~ series +
s(yearfac, bs = 're'),
# Updated prior distributions using brms::prior()
priors = c(prior(std_normal(), class = b),
prior(exponential(2), class = sigma_raw)),
# Training data in mvgam's long format
data = data_train,
# Testing data in mvgam's long format
newdata = data_test,
# Gamma observation model with shared shape parameter
family = Gamma(),
share_obs_params = TRUE,
# Ensure all messages are reported for transparency
silent = 1
)
# Look at the structure of the object
str(mod1, max.level = 1)
?mvgam::`mvgam-class`
methods(class = "mvgam")
# Look at the Stan code to better understand the model
stancode(mod1)
# Generate a methods skeleton with references
how_to_cite(mod1)
# Diagnostics
summary(mod1)
summary(mod1,
include_betas = FALSE,
smooth_test = FALSE)
mcmc_plot(mod1,
type = 'rhat_hist')
mcmc_plot(mod1,
variable = 'obs_params',
type = 'trace')
mcmc_plot(mod1,
variable = c('mean', 'sd'),
regex = TRUE,
type = 'trace')
pairs(mod1,
variable = c('mean', 'sd'),
regex = TRUE)
# Inspect estimated effects on the outcome scale ...
conditional_effects(mod1)
# ... and on the link scale
conditional_effects(mod1,
type = 'link')
# Many other types of predictions and contrasts can be
# made with marginaleffects
marginaleffects::avg_predictions(mod1, variable = 'series')
# Unconditional posterior predictive checks to
# look at model fit
pp_check(mod1,
type = "ecdf_overlay_grouped",
group = "series",
ndraws = 50)
pp_check(mod1,
type = "dens_overlay_grouped",
group = "series",
ndraws = 50)
# Conditional posterior predictive checks
hcs <- hindcast(mod1)
class(hcs)
?mvgam::`mvgam_forecast-class`
methods(class = "mvgam_forecast")
layout(matrix(1:4, nrow = 2, byrow = TRUE))
plot(hcs, series = 1)
plot(hcs, series = 2)
plot(hcs, series = 3)
layout(1)
# Residual checks
plot(mod1, type = 'residuals', series = 1)
plot(mod1, type = 'residuals', series = 2)
plot(mod1, type = 'residuals', series = 3)
# Inspect forecasts, which were already computed by the
# model for the test data
fcs <- forecast(mod1)
class(fcs)
layout(matrix(1:4, nrow = 2, byrow = TRUE))
plot(fcs, series = 1)
plot(fcs, series = 2)
plot(fcs, series = 3)
layout(1)
# Another way to look at forecasts for this example
plot_predictions(mod1, newdata = model_data,
by = c('yearfac', 'series', 'series'))
# As a quick aside, here is a model with splines of year
# for each region, which are partially pooled toward
# a shared spline of year
?mgcv::factor.smooth.interaction
mod1.2 <- mvgam(
# Observation formula containing region-level intercepts and
# hierarchical splines of year for each region
formula = adj_count ~ series +
# Shared smooth of year for all series
s(year, k = 30, bs = 'cr') +
# Deviation smooths for each series
s(year, series, k = 10, bs = 'sz'),
# Updated prior distributions using brms::prior()
priors = prior(std_normal(),
class = b),
# Training and testing data in mvgam's long format
data = data_train,
newdata = data_test,
# Gamma observation model
family = Gamma(),
share_obs_params = TRUE
)
# Draw the individual component smooths
gratia::draw(mod1.2)
# All in-sample, unconditional plots look excellent!
plot_predictions(mod1.2,
by = c('year', 'series', 'series'),
points = 0.5)
pp_check(mod1.2,
type = "dens_overlay_grouped",
group = "series",
ndraws = 50)
pp_check(mod1.2,
type = "pit_ecdf_grouped",
group = "series",
ndraws = 50)
# And this model is slightly favoured using loo in-sample
# model checks
loo_compare(mod1, mod1.2)
# But what about forecasts?!?
plot_predictions(mod1.2,
newdata = model_data,
by = c('year', 'series', 'series'),
type = 'response') +
geom_vline(xintercept = 2000,
linetype = 'dashed')
fcs <- forecast(mod1.2)
layout(matrix(1:4, nrow = 2, byrow = TRUE))
plot(fcs, series = 1)
plot(fcs, series = 2)
plot(fcs, series = 3)
layout(1)
# Expand to a State-Space model with more appropriate nonlinear
# temporal effects; here we use Gaussian Processes for the
# shared and deviation effects, which tend to extrapolate
# much better than splines do
?brms::gp
?mvgam::AR
?mvgam::mvgam_formulae
p <- get_mvgam_priors(formula = adj_count ~ series,
trend_formula = ~
gp(year,
k = 32) +
gp(year,
by = trend,
k = 20) - 1,
trend_model = AR(cor = TRUE),
data = model_data,
family = Gamma())
View(p)
# Fit the model
mod2 <- mvgam(
# Observation formula, only containing region-level intercepts
formula = adj_count ~ series,
# Process model formula, containing hierarchical GPs of time
trend_formula = ~
gp(year,
k = 32,
cov = 'exponential') +
gp(year,
by = trend,
k = 20,
cov = 'exponential') - 1,
# Additional autoregressive dynamics (using a correlated AR(1))
trend_model = AR(cor = TRUE),
# Updated prior distributions using brms::prior()
priors = c(prior(beta(3, 10),
class = sigma,
lb = 0,
ub = 1),
prior(std_normal(),
class = `alpha_gp_trend(year)`),
prior(std_normal(),
class = `alpha_gp_trend(year):trendtrend1`),
prior(std_normal(),
class = `alpha_gp_trend(year):trendtrend2`),
prior(std_normal(),
class = `alpha_gp_trend(year):trendtrend3`),
prior(normal(0.5, 0.2),
class = ar1),
prior(std_normal(),
class = b)),
# Training and testing data in mvgam's long format
data = data_train,
newdata = data_test,
# Gamma observation model with independent shape parameters
family = Gamma(),
share_obs_params = FALSE,
# Stan MCMC control for slower but more precise sampling
control = list(adapt_delta = 0.95)
)
# Inspect the Stan code
stancode(mod2)
how_to_cite(mod2)
# Diagnostics
summary(mod2,
include_betas = FALSE,
smooth_test = FALSE)
how_to_cite(mod2)
mcmc_plot(mod2,
type = 'rhat_hist')
mcmc_plot(mod2,
variable = c('sigma',
'ar1',
'shape'),
regex = TRUE,
type = 'trace')
# Unconditional posterior check
pp_check(mod2,
type = "dens_overlay_grouped",
group = "series",
ndraws = 50)
# Inferences and unconditional predictions
plot_predictions(mod2,
condition = c('year', 'series'),
type = 'link',
conf_level = 0.5)
plot_predictions(mod2,
condition = c('year', 'series', 'series'),
points = 0.5)
marginaleffects::avg_predictions(mod2,
variable = 'series')
# Rate of change, averaged over all regions
patchwork::wrap_plots(
plot_predictions(
mod2,
by = 'year',
newdata = datagrid(model = mod2,
year = seq(min(model_data$year),
max(model_data$year),
length.out = 75),
series = unique),
type = 'link'
) +
labs(y = 'Linear predictor',
x = ''),
plot_slopes(
mod2,
variables = 'year',
by = 'year',
newdata = datagrid(model = mod2,
year = seq(min(model_data$year),
max(model_data$year),
length.out = 75),
series = unique),
type = 'link'
) +
labs(y = 'Slope of linear predictor',
x = 'Year') +
geom_hline(yintercept = 0, linetype = 'dashed'),
nrow = 2
) +
patchwork::plot_annotation(
title = 'Average conditional and marginal effects'
)
# Inspect the AR1 variance-covariance parameters
Sigma_pars <- matrix(NA,
nrow = 3,
ncol = 3)
for(i in 1:3){
for(j in 1:3){
Sigma_pars[i, j] <- paste0('Sigma[', i, ',', j, ']')
}
}
mcmc_plot(mod2,
variable = as.vector(t(Sigma_pars)),
type = 'hist') +
geom_vline(xintercept = 0,
col = 'white',
linewidth = 2) +
geom_vline(xintercept = 0,
linewidth = 1)
# Compare models using in-sample fit metrics
loo_compare(mod1,
mod2)
# Look at forecasts from each model and compare
fcs1 <- forecast(mod1)
fcs2 <- forecast(mod2)
layout(matrix(1:2,
nrow = 2,
byrow = TRUE))
for(x in 1:3){
plot(fcs1,
series = x)
title('Random effects of year')
plot(fcs2,
series = x)
title('GPs of year with AR1 dynamics')
}
# Score forecasts
?mvgam::score
score(fcs1,
score = 'energy')
score(fcs1,
score = 'energy')$all_series$score
score(fcs2,
score = 'energy')$all_series$score
score(fcs1,
score = 'variogram')$all_series$score
score(fcs2,
score = 'variogram')$all_series$score
# Now a completely different model that uses a State-Space Vector
# Autoregression of order 1, with only the region-level intercepts
# as regression parameters
varmod <- mvgam(
# Observation formula, empty to only consider the Gamma
# observation process
formula = adj_count ~ -1,
# Process model formula that includes regional intercepts
trend_formula = ~ trend,
# A VAR(1) dynamic process with fully parameterized covariance
# matrix Sigma
trend_model = VAR(cor = TRUE),
# Modified prior distributions using brms::prior()
priors = c(prior(std_normal(),
class = Intercept_trend),
prior(std_normal(),
class = b),
prior(beta(3, 10),
class = sigma,
lb = 0,
ub = 1)),
# The time series data in 'long' format
data = data_train,
newdata = data_test,
# Gamma observation model with independent shape parameters
family = Gamma(),
share_obs_params = FALSE,
# Stan MCMC control for slower but more precise sampling
control = list(adapt_delta = 0.95)
)
summary(varmod)
how_to_cite(varmod)
mcmc_plot(varmod,
type = 'rhat_hist')
# Estimates of the autoregressive coefficients
A_pars <- matrix(NA,
nrow = 3,
ncol = 3)
for(i in 1:3){
for(j in 1:3){
A_pars[i, j] <- paste0('A[', i, ',', j, ']')
}
}
mcmc_plot(varmod,
variable = as.vector(t(A_pars)),
type = 'hist') +
geom_vline(xintercept = 0,
col = 'white',
linewidth = 2) +
geom_vline(xintercept = 0,
linewidth = 1)
# And of the variance-covariance parameters
Sigma_pars <- matrix(NA,
nrow = 3,
ncol = 3)
for(i in 1:3){
for(j in 1:3){
Sigma_pars[i, j] <- paste0('Sigma[', i, ',', j, ']')
}
}
mcmc_plot(varmod,
variable = as.vector(t(Sigma_pars)),
type = 'hist') +
geom_vline(xintercept = 0,
col = 'white',
linewidth = 2) +
geom_vline(xintercept = 0,
linewidth = 1)
# Impulse response functions
?mvgam::irf
irfs <- irf(varmod,
h = 12,
orthogonal = FALSE)
plot(irfs, series = 1)
plot(irfs, series = 2)
plot(irfs, series = 3)
# Forecast error variance decompositions
?mvgam::fevd
fevds <- fevd(varmod, h = 12)
plot(fevds) +
scale_fill_manual(values = c("#DCBCBC",
"#A25050",
"#5C0000")) +
labs(fill = 'Process')
?mvgam::stability
# In-sample comparisons don't suggest much difference with mod2
loo_compare(mod1,
mod2,
varmod)
# What about forecast comparisons?
fcsvar <- forecast(varmod)
layout(matrix(1:2,
nrow = 2,
byrow = TRUE))
for(x in 1:3){
plot(fcs2,
series = x)
title('GPs of year with AR1 dynamics')
plot(fcsvar,
series = x)
title('VAR1 dynamics')
}
# Compare energy scores
score(fcs2,
score = 'energy')$all_series$score
score(fcsvar,
score = 'energy')$all_series$score
# Compare variogram scores
score(fcs2,
score = 'variogram')$all_series$score
score(fcsvar,
score = 'variogram')$all_series$score
# What about an ensemble of these two?
ens <- ensemble(fcs2, fcsvar, ndraws = 2000)
sum(score(fcs2,
score = 'energy')$all_series$score)
sum(score(fcsvar,
score = 'energy')$all_series$score)
sum(score(ens,
score = 'energy')$all_series$score)
# Perhaps the VAR1 isn't capturing the nonlinear trends as well
# as the hierarchical GPs; but we could easily combine the two!