forked from epiforecasts/EpiNow2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
198 lines (133 loc) · 13.5 KB
/
README.Rmd
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
---
output: github_document
---
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/",
eval = TRUE
)
```
# EpiNow2: Estimate real-time case counts and time-varying epidemiological parameters
[![Lifecycle: maturing](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://www.tidyverse.org/lifecycle/#maturing) [![R-CMD-check](https://github.com/epiforecasts/EpiNow2/workflows/R-CMD-check/badge.svg)](https://github.com/epiforecasts/EpiNow2/actions) [![Codecov test coverage](https://codecov.io/gh/epiforecasts/EpiNow2/branch/master/graph/badge.svg)](https://codecov.io/gh/epiforecasts/EpiNow2?branch=master) [![metacran downloads](http://cranlogs.r-pkg.org/badges/grand-total/EpiNow2?color=ff69b4)](https://cran.r-project.org/package=EpiNow2)
[![MIT license](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/epiforecasts/EpiNow2/blob/master/LICENSE.md/) [![GitHub contributors](https://img.shields.io/github/contributors/epiforecasts/EpiNow2)](https://github.com/epiforecasts/EpiNow2/graphs/contributors) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-yellow.svg)](https://makeapullrequest.com/) [![GitHub commits](https://img.shields.io/github/commits-since/epiforecasts/EpiNow2/v1.3.2.svg?color=orange)](https://GitHub.com/epiforecasts/EpiNow2/commit/master/) [![DOI](https://zenodo.org/badge/272995211.svg)](https://zenodo.org/badge/latestdoi/272995211)
This package estimates the time-varying reproduction number, growth rate, and doubling time using a range of open-source tools ([Abbott et al.](https://doi.org/10.12688/wellcomeopenres.16006.1)), and current best practices ([Gostic et al.](https://doi.org/10.1371/journal.pcbi.1008409)). It aims to help users avoid some of the limitations of naive implementations in a framework that is informed by community feedback and is under active development.
It estimates the time-varying reproduction number on cases by date of infection (using a similar approach to that implemented in [`{EpiEstim}`](https://github.com/mrc-ide/EpiEstim)). Imputed infections are then mapped to observed data (for example cases by date of report) via a series of uncertain delay distributions (in the examples in the package documentation these are an incubation period and a reporting delay) and a reporting model that can include weekly periodicity.
Uncertainty is propagated from all inputs into the final parameter estimates, helping to mitigate spurious findings. This is handled internally. The time-varying reproduction estimates and the uncertain generation time also give time-varying estimates of the rate of growth.
The default model uses a non-stationary Gaussian process to estimate the time-varying reproduction number and then infer infections. Other options include:
* A stationary Gaussian process (faster to estimate but currently gives reduced performance for real time estimates).
* User specified breakpoints.
* A fixed reproduction number.
* As piecewise constant by combining a fixed reproduction number with breakpoints.
* As a random walk (by combining a fixed reproduction number with regularly spaced breakpoints (i.e weekly)).
* Inferring infections using back-calculation and then calculating the time-varying reproduction number.
The documentation for [`estimate_infections`](https://epiforecasts.io/EpiNow2/reference/estimate_infections.html) provides examples of the different options available.
Forecasting is also supported for the time-varying reproduction number, infections and reported cases. The time-varying reproduction number can be forecast forwards in time using an integration with the [`{EpiSoon}`](https://epiforecasts.io/EpiSoon/) package, and converted to a case forecast using the renewal equation. Alternatively, the time-varying reproduction number and cases can be forecast using a Gaussian process.
A simple example of using the package to estimate a national Rt for Covid-19 can be found [here](https://gist.github.com/seabbs/163d0f195892cde685c70473e1f5e867).
`EpiNow2` also supports adjustment for truncated data via `estimate_truncation()` and for estimating dependent observations (i.e deaths based on hospital admissions) using `estimate_secondary()`.
## Installation
Install the stable version of the package:
```{r, eval = FALSE}
install.packages("EpiNow2")
```
Install the stable development version of the package with:
```{r, eval = FALSE}
install.packages("EpiNow2", repos = "https://epiforecasts.r-universe.dev")
```
Install the unstable development version of the package with (few users should need to do this):
```{r, eval = FALSE}
remotes::install_github("epiforecasts/EpiNow2")
```
Windows users will need a working installation of Rtools in order to build the package from source. See [here](https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started#checking-the-c-toolchain) for a guide to installing Rtools for use with Stan (which is the statistical modelling platform used for the underlying model). For simple deployment/development a prebuilt docker image is also available (see documentation [here](https://github.com/epiforecasts/EpiNow2/wiki/Docker)).
## Quick start
`{EpiNow2}` is designed to be used with a single function call or to be
used in an ad-hoc fashion via individual function calls. The core functions of `{EpiNow2}` are the two single-call functions [`epinow()`](https://epiforecasts.io/EpiNow2/reference/epinow.html), [`regional_epinow()`](https://epiforecasts.io/EpiNow2/reference/regional_epinow.html), plus functions
[`estimate_infections()`](https://epiforecasts.io/EpiNow2/reference/estimate_infections.html), [`forecast_infections()`](https://epiforecasts.io/EpiNow2/reference/forecast_infections.html), [`estimate_secondary()`](https://epiforecasts.io/EpiNow2/reference/estimate_secondary.html) and [`estimate_truncation()`](https://epiforecasts.io/EpiNow2/reference/estimate_truncation.html). In the following section we give an overview of the simple use case for `epinow` and `regional_epinow`. [`estimate_infections()`](https://epiforecasts.io/EpiNow2/reference/estimate_infections.html) can be used on its own to infer the underlying infection case curve from reported cases and estimate Rt. Estimating the underlying infection case curve via back-calculation (and then calculating Rt) is substantially less computationally demanding than generating using default settings but may result in less reliable estimates of Rt. For more details on using each function see the [function documentation](https://epiforecasts.io/EpiNow2/reference/index.html).
The first step to using the package is to load it as follows.
```{r, message = FALSE}
library(EpiNow2)
```
### Reporting delays, incubation period and generation time
Distributions can either be fitted using package functionality or determined elsewhere and then defined with uncertainty for use in `{EpiNow2}`. When data is supplied a subsampled bootstrapped lognormal will be fit (to account for uncertainty in the observed data without being biased by changes in incidence). An arbitrary number of delay distributions are supported with the most common use case likely to be a incubation period followed by a reporting delay.
For example if data on the delay between onset and infection was available we could fit a distribution to it with appropriate uncertainty as follows (note this is a synthetic example),
```{r, eval = FALSE}
reporting_delay <- estimate_delay(rlnorm(1000, log(2), 1),
max_value = 15, bootstraps = 1)
```
If data was not available we could instead make an informed estimate of the likely delay (note this is a synthetic example and not applicable to real world use cases),
```{r}
reporting_delay <- list(
mean = convert_to_logmean(2, 1), mean_sd = 0.1,
sd = convert_to_logsd(2, 1), sd_sd = 0.1,
max = 10
)
```
Here we define the incubation period and generation time based on literature estimates for Covid-19 (see [here](https://github.com/epiforecasts/EpiNow2/tree/master/data-raw) for the code that generates these estimates). Note that these distributions may not be applicable for your use case.
```{r}
generation_time <- get_generation_time(disease = "SARS-CoV-2", source = "ganyani")
incubation_period <- get_incubation_period(disease = "SARS-CoV-2", source = "lauer")
```
### [epinow()](https://epiforecasts.io/EpiNow2/reference/epinow.html)
This function represents the core functionality of the package and includes results reporting, plotting and optional saving. It requires a data frame of cases by date of report and the distributions defined above. An additional forecasting module is supported via `EpiSoon` and companion packages (see documentation for an example).
Load example case data from `{EpiNow2}`.
```{r}
reported_cases <- example_confirmed[1:90]
head(reported_cases)
```
Estimate cases by date of infection, the time-varying reproduction number, the rate of growth and forecast these estimates into the future by 7 days. Summarise the posterior and return a summary table and plots for reporting purposes. If a `target_folder` is supplied results can be internally saved (with the option to also turn off explicit returning of results). *Note: For real use cases more samples and a longer warm up may be needed*. See fitting progress by setting `verbose = TRUE`.
```{r, message = FALSE, warning = FALSE}
estimates <- epinow(reported_cases = reported_cases,
generation_time = generation_time,
delays = delay_opts(incubation_period, reporting_delay),
rt = rt_opts(prior = list(mean = 2, sd = 0.2)),
stan = stan_opts(cores = 4))
names(estimates)
```
Both summary measures and posterior samples are returned for all parameters in an easily explored format which can be accessed using `summary`. The default is to return a summary table of estimates for key parameters at the latest date partially supported by data.
```{r}
knitr::kable(summary(estimates))
```
Summarised parameter estimates can also easily be returned, either filtered for a single parameter or for all parameters.
```{r}
head(summary(estimates, type = "parameters", params = "R"))
```
Reported cases are returned in a separate data frame in order to streamline the reporting of forecasts and for model evaluation.
```{r}
head(summary(estimates, output = "estimated_reported_cases"))
```
A range of plots are returned (with the single summary plot shown below). These plots can also be generated using the following `plot` method.
```{r, dpi = 330, fig.width = 12, fig.height = 12, message = FALSE, warning = FALSE}
plot(estimates)
```
### [regional_epinow()](https://epiforecasts.io/EpiNow2/reference/regional_epinow.html)
The `regional_epinow()` function runs the `epinow()` function across multiple regions in
an efficient manner.
Define cases in multiple regions delineated by the region variable.
```{r}
reported_cases <- data.table::rbindlist(list(
data.table::copy(reported_cases)[, region := "testland"],
reported_cases[, region := "realland"]))
head(reported_cases)
```
Calling `regional_epinow()` runs the `epinow()` on each region in turn (or in parallel depending on the settings used).
```{r, message = FALSE, warning = FALSE}
estimates <- regional_epinow(reported_cases = reported_cases,
generation_time = generation_time,
delays = delay_opts(incubation_period, reporting_delay),
rt = rt_opts(prior = list(mean = 2, sd = 0.2)),
stan = stan_opts(cores = 4))
```
Results from each region are stored in a `regional` list with across region summary measures and plots stored in a `summary` list. All results can be set to be internally saved by setting the `target_folder` and `summary_dir` arguments. Each region can be estimated in parallel using the `{future}` package (when in most scenarios `cores` should be set to 1). For routine use each MCMC chain can also be run in parallel (with `future` = TRUE) with a time out (`max_execution_time`) allowing for partial results to be returned if a subset of chains is running longer than expected. See the documentation for the `{future}` package for details on nested futures.
Summary measures that are returned include a table formatted for reporting (along with raw results for further processing). Futures updated will extend the S3 methods used above to smooth access to this output.
```{r}
knitr::kable(estimates$summary$summarised_results$table)
```
A range of plots are again returned (with the single summary plot shown below).
```{r, dpi = 330, fig.width = 12, fig.height = 12, message = FALSE, warning = FALSE}
estimates$summary$summary_plot
```
### Reporting templates
Rmarkdown templates are provided in the package (`templates`) for semi-automated reporting of estimates. These are currently undocumented but an example integration can be seen [here](https://github.com/epiforecasts/covid/blob/master/_posts/national/united-kingdom/united-kingdom.Rmd). If using these templates to report your results please highlight our [limitations](https://doi.org/10.12688/wellcomeopenres.16006.1) as these are key to understanding the results from `{EpiNow2}` .
## Contributing
File an issue [here](https://github.com/epiforecasts/EpiNow2/issues) if you have identified an issue with the package. Please note that due to operational constraints priority will be given to users informing government policy or offering methodological insights. We welcome all contributions, in particular those that improve the approach or the robustness of the code base. We also welcome additions and extensions to the underlying model either in the form of options or improvements.