-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathREADME.Rmd
More file actions
358 lines (309 loc) · 11.4 KB
/
README.Rmd
File metadata and controls
358 lines (309 loc) · 11.4 KB
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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
options(width = 76)
knitr::opts_chunk$set(
fig.path = "man/figures/README-",
digits = 3,
comment = "#>",
collapse = TRUE,
cache = TRUE,
dev.args = list(bg = "transparent"),
dpi = 300,
cache.lazy = FALSE,
out.width = "90%",
fig.align = "center",
fig.width = 9,
fig.height = 6
)
ggplot2::theme_set(ggplot2::theme_bw())
options(
dplyr.print_min = 6,
dplyr.print_max = 6,
pillar.max_footer_lines = 2,
pillar.min_chars = 15,
stringr.view_n = 6,
pillar.bold = TRUE,
width = 77
)
```
```{r pkgs, include=FALSE, echo=FALSE}
library(epipredict)
library(epidatr)
library(data.table)
library(dplyr)
library(tidyr)
library(ggplot2)
library(magrittr)
library(purrr)
library(scales)
```
```{r coloration, include=FALSE, echo=FALSE}
base <- "#002676"
primary <- "#941120"
secondary <- "#f9c80e"
tertiary <- "#177245"
fourth_colour <- "#A393BF"
fifth_colour <- "#2e8edd"
colvec <- c(
base = base, primary = primary, secondary = secondary,
tertiary = tertiary, fourth_colour = fourth_colour,
fifth_colour = fifth_colour
)
library(epiprocess)
suppressMessages(library(tidyverse))
theme_update(legend.position = "bottom", legend.title = element_blank())
delphi_pal <- function(n) {
if (n > 6L) warning("Not enough colors in this palette!")
unname(colvec)[1:n]
}
scale_fill_delphi <- function(..., aesthetics = "fill") {
discrete_scale(aesthetics = aesthetics, palette = delphi_pal, ...)
}
scale_color_delphi <- function(..., aesthetics = "color") {
discrete_scale(aesthetics = aesthetics, palette = delphi_pal, ...)
}
scale_colour_delphi <- scale_color_delphi
```
# Epipredict
<!-- badges: start -->
[](https://github.com/cmu-delphi/epipredict/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->
[`{epipredict}`](https://cmu-delphi.github.io/epipredict/) is a framework for building transformation and forecasting pipelines for epidemiological and other panel time-series datasets.
In addition to tools for building forecasting pipelines, it contains a number of “canned” forecasters meant to run with little modification as an easy way to get started forecasting.
It is designed to work well with
[`{epiprocess}`](https://cmu-delphi.github.io/epiprocess/), a utility for time series handling and geographic processing in an epidemiological context.
Both of the packages are meant to work well with the panel data provided by
[`{epidatr}`](https://cmu-delphi.github.io/epidatr/).
Pre-compiled example datasets are also available in
[`{epidatasets}`](https://cmu-delphi.github.io/epidatasets/).
If you are looking for detail beyond the package documentation, see our
[forecasting book](https://cmu-delphi.github.io/delphi-tooling-book/).
## Installation
Unless you’re planning on contributing to package development, we suggest using the stable version.
To install, run:
```r
# Stable version
pak::pkg_install("cmu-delphi/epipredict@main")
# Dev version
pak::pkg_install("cmu-delphi/epipredict@dev")
```
The documentation for the stable version is at
<https://cmu-delphi.github.io/epipredict>, while the development version is at
<https://cmu-delphi.github.io/epipredict/dev>.
## Motivating example
<details>
<summary> Required packages </summary>
```{r install, run = FALSE}
library(epipredict)
library(epidatr)
library(epiprocess)
library(dplyr)
library(ggplot2)
```
</details>
To demonstrate using [`{epipredict}`](https://cmu-delphi.github.io/epipredict/) for forecasting, suppose we want to
predict COVID-19 deaths per 100k people for each of a subset of states
```{r subset_geos}
used_locations <- c("ca", "ma", "ny", "tx")
```
on
```{r fc_date}
forecast_date <- as.Date("2021-08-01")
```
We will be using a subset of
[Johns Hopkins Center for Systems Science and Engineering deaths data](https://cmu-delphi.github.io/delphi-epidata/api/covidcast-signals/jhu-csse.html).
Below the fold, we pull the dataset from the epidata API and clean it.
<details>
<summary> Creating the dataset using `{epidatr}` and `{epiprocess}` </summary>
This section is intended to demonstrate some of the ubiquitous cleaning operations needed to be able to forecast.
A subset of the dataset prepared here is also included ready-to-go in [`{epipredict}`](https://cmu-delphi.github.io/epipredict/) as `covid_case_death_rates`.
First we pull both `jhu-csse` cases and deaths data from the
[Delphi API](https://cmu-delphi.github.io/delphi-epidata/api/covidcast.html) using the
[`{epidatr}`](https://cmu-delphi.github.io/epidatr/) package:
```{r case_death, warning = FALSE, eval = TRUE}
cases <- pub_covidcast(
source = "jhu-csse",
signals = "confirmed_7dav_incidence_prop",
time_type = "day",
geo_type = "state",
time_values = epirange(20200601, 20211231),
geo_values = "*"
) |>
select(geo_value, time_value, case_rate = value)
deaths <- pub_covidcast(
source = "jhu-csse",
signals = "deaths_7dav_incidence_prop",
time_type = "day",
geo_type = "state",
time_values = epirange(20200601, 20211231),
geo_values = "*"
) |>
select(geo_value, time_value, death_rate = value)
cases_deaths <-
full_join(cases, deaths, by = c("time_value", "geo_value")) |>
filter(geo_value %in% used_locations) |>
as_epi_df(as_of = as.Date("2022-01-01"))
```
Since visualizing the results on every geography is somewhat overwhelming,
we’ll only train on a subset of locations.
```{r date, warning = FALSE}
# plotting the data as it was downloaded
cases_deaths |>
autoplot(
case_rate,
death_rate,
.color_by = "none"
) +
facet_grid(
rows = vars(.response_name),
cols = vars(geo_value),
scale = "free"
) +
scale_x_date(date_breaks = "3 months", date_labels = "%Y %b") +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
```
As with the typical dataset, we will need to do some cleaning to
make it actually usable; we’ll use some utilities from
[`{epiprocess}`](https://cmu-delphi.github.io/epiprocess/) for this.
Specifically we'll trim outliers, especially negative values:
```{r outlier}
cases_deaths <-
cases_deaths |>
group_by(geo_value) |>
mutate(
outlr_death_rate = detect_outlr_rm(
time_value, death_rate,
detect_negatives = TRUE
),
outlr_case_rate = detect_outlr_rm(
time_value, case_rate,
detect_negatives = TRUE
)
) |>
unnest(cols = starts_with("outlr"), names_sep = "_") |>
ungroup() |>
mutate(
death_rate = outlr_death_rate_replacement,
case_rate = outlr_case_rate_replacement
) |>
select(geo_value, time_value, case_rate, death_rate)
```
</details>
After downloading and cleaning deaths per capita, as well as cases per 100k people, we can plot
a subset of the states, marking the desired forecast date with a vertical line:
<details>
<summary> Plot </summary>
```{r plot_locs}
forecast_date_label <-
tibble(
geo_value = rep(used_locations, 2),
.response_name = c(rep("case_rate", 4), rep("death_rate", 4)),
dates = rep(forecast_date - 7 * 2, 2 * length(used_locations)),
heights = c(rep(150, 4), rep(0.75, 4))
)
processed_data_plot <-
cases_deaths |>
filter(geo_value %in% used_locations) |>
autoplot(
case_rate,
death_rate,
.color_by = "none"
) +
facet_grid(
rows = vars(.response_name),
cols = vars(geo_value),
scale = "free"
) +
geom_vline(aes(xintercept = forecast_date)) +
geom_text(
data = forecast_date_label,
aes(x = dates, label = "forecast\ndate", y = heights),
size = 3, hjust = "right"
) +
scale_x_date(date_breaks = "3 months", date_labels = "%Y %b") +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
```
</details>
```{r show-processed-data, warning=FALSE, echo=FALSE}
processed_data_plot
```
To make a forecast, we will use a simple “canned” auto-regressive forecaster to
predict the death rate four weeks into the future using lagged[^3] deaths and
cases.
[^3]: lagged by 3 in this context meaning using the value from 3 days ago.
```{r make-forecasts, warning=FALSE}
four_week_ahead <- arx_forecaster(
cases_deaths |> filter(time_value <= forecast_date),
outcome = "death_rate",
predictors = c("case_rate", "death_rate"),
args_list = arx_args_list(
lags = list(c(0, 1, 2, 3, 7, 14), c(0, 7, 14)),
ahead = 4 * 7,
quantile_levels = c(0.1, 0.25, 0.5, 0.75, 0.9)
)
)
four_week_ahead
```
In our model setup, we are using as predictors the case rate lagged 0-3
days, one week, and two weeks, and the death rate lagged 0-2 weeks.
The result `four_week_ahead` is both a fitted model object which could be used
any time in the future to create different forecasts, and a set of predicted
values (and prediction intervals) for each location 28 days after the forecast
date.
Plotting the prediction intervals on the true values for our location subset[^2]:
[^2]: Alternatively, you could call `autoplot(four_week_ahead, observed_response =
cases_deaths)` to get the full collection of forecasts. This is too busy for
the space we have for plotting here.
<details>
<summary> Plot </summary>
```{r plotting_forecast, warning=FALSE}
epiworkflow <- four_week_ahead$epi_workflow
restricted_predictions <-
four_week_ahead$predictions |>
rename(time_value = target_date, value = .pred) |>
mutate(.response_name = "death_rate")
forecast_plot <-
four_week_ahead |>
autoplot(observed_response = cases_deaths) +
geom_vline(aes(xintercept = forecast_date)) +
geom_text(
data = forecast_date_label %>% filter(.response_name == "death_rate"),
aes(x = dates, label = "forecast\ndate", y = heights),
size = 3, hjust = "right"
) +
scale_x_date(date_breaks = "3 months", date_labels = "%y %b") +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
```
</details>
```{r show-single-forecast, warning=FALSE, echo=FALSE}
forecast_plot
```
And as a tibble of quantile level-value pairs:
```{r pivot_wider}
four_week_ahead$predictions |>
select(-.pred) |>
pivot_quantiles_longer(.pred_distn) |>
select(geo_value, forecast_date, target_date, quantile = .pred_distn_quantile_level, value = .pred_distn_value)
```
The orange dot gives the point prediction, while the blue intervals give the
25-75%, the 10-90%, and 2.5-97.5% inter-quantile ranges[^4].
For this particular day and these locations, the forecasts are relatively
accurate, with the true data being at least within the 10-90% interval.
A couple of things to note:
1. `epipredict` methods are primarily direct forecasters; this means we don't need to
predict 1, 2,..., 27 days ahead to then predict 28 days ahead.
2. All of our existing engines are geo-pooled, meaning the training data is
shared across geographies. This has the advantage of increasing the amount of
available training data, with the restriction that the data needs to be on
comparable scales, such as rates.
## Getting Help
If you encounter a bug or have a feature request, feel free to file an [issue on
our GitHub page](https://github.com/cmu-delphi/epipredict/issues).
For other questions, feel free to reach out to the authors, either via this
[contact form](https://docs.google.com/forms/d/e/1FAIpQLScqgT1fKZr5VWBfsaSp-DNaN03aV6EoZU4YljIzHJ1Wl_zmtg/viewform),
email, or the InsightNet Slack.
[^4]: Note that these are not the same quantiles that we fit when creating
`four_week_ahead`. They are extrapolated from those quantiles using `extrapolate_quantiles()` (which assumes an exponential decay in the tails).