This function wraps the functionality of
estimate_infections()
and forecast_infections()
in order
to estimate Rt and cases by date of infection, forecast into these infections into the future. It also contains
additional functionality to convert forecasts to date of report and produce summary output useful for reporting
results and interpreting them. See here for an
example of using epinow
to estimate Rt for Covid-19 in a country from the ECDC data source.
epinow(
reported_cases,
generation_time = NULL,
delays = delay_opts(),
truncation = trunc_opts(),
rt = rt_opts(),
backcalc = backcalc_opts(),
gp = gp_opts(),
obs = obs_opts(),
stan = stan_opts(),
horizon = 7,
CrIs = c(0.2, 0.5, 0.9),
zero_threshold = 50,
return_output = FALSE,
output = c("samples", "plots", "latest", "fit", "timing"),
plot_args = list(),
target_folder = NULL,
target_date,
forecast_args = NULL,
logs = tempdir(),
id = "epinow",
verbose = interactive()
)
A data frame of confirmed cases (confirm) by date (date). confirm must be integer and date must be in date format.
A list containing the mean, standard deviation of the mean (mean_sd), standard deviation (sd), standard deviation of the standard deviation and the maximum allowed value for the generation time (assuming a gamma distribution).
A call to delay_opts()
defining delay distributions and options. See the documentation of delay_opts()
and the examples below for details.
A list of options as generated by
trunc_opts()
defining the truncation of observed data. Defaults to trunc_opts()
. See estimate_truncation()
for an approach to estimating truncation from data.
A list of options as generated by rt_opts()
defining Rt estimation.
Defaults to rt_opts()
. Set to NULL
to switch to using back calculation
rather than generating infections using Rt.
A list of options as generated by backcalc_opts()
to define the
back calculation. Defaults to backcalc_opts()
.
A list of options as generated by gp_opts()
to define the
Gaussian process. Defaults to gp_opts()
.Set to NULL to disable the
Gaussian process.
A list of options as generated by obs_opts()
defining the
observation model. Defaults to obs_opts()
.
A list of stan options as generated by stan_opts()
. Defaults to stan_opts()
. Can be used to override
data
, init
, and verbose
settings if desired.
Numeric, defaults to 7. Number of days into the future to forecast.
Numeric vector of credible intervals to calculate.
Numeric defaults to 50. Indicates if detected zero
cases are meaningful by using a threshold of 50 cases on average over the last 7 days. If the average is
above this threshold then the zero is replaced with the backwards looking rolling average. If set to infinity
then no changes are made.
Logical, defaults to FALSE. Should output be returned, this automatically updates to TRUE if no directory for saving is specified.
A character vector of optional output to return. Supported options are samples ("samples"),
plots ("plots"), the run time ("timing"), copying the dated folder into a latest folder (if target_folder
is not null,
set using "latest"), and the stan fit ("fit"). The default is to return all options. This argument uses partial matching
so for example passing "sam" will lead to samples being reported.
A list of optional arguments passed to plot.epinow()
.
Character string specifying where to save results (will create if not present).
Date, defaults to maximum found in the data if not specified.
A list of arguments to pass to forecast_infections()
. Unless at a minimum a forecast_model
is passed
then forecast_infections
will be bypassed.
Character path indicating the target folder in which to store log
information. Defaults to the temporary directory if not specified. Default logging
can be disabled if logs
is set to NULL. If specifying a custom logging setup then
the code for setup_default_logging
and the setup_logging
function are a sensible
place to start.
A character string used to assign logging information on error. Used by regional_epinow
to assign errors to regions. Alter the default to run with error catching.
Logical, defaults to TRUE
when used interactively and otherwise FALSE
. Should verbose debug progress messages be printed. Corresponds to the "DEBUG" level from
futile.logger
. See setup_logging
for more detailed logging options.
A list of output from estimate_infections, forecast_infections, report_cases, and report_summary.
estimate_infections simulate_infections forecast_infections regional_epinow
# \donttest{
# set number of cores to use
options(mc.cores = ifelse(interactive(), 4, 1))
# construct example distributions
generation_time <- get_generation_time(disease = "SARS-CoV-2", source = "ganyani")
incubation_period <- get_incubation_period(disease = "SARS-CoV-2", source = "lauer")
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
)
# example case data
reported_cases <- example_confirmed[1:40]
# estimate Rt and nowcast/forecast cases by date of infection
out <- epinow(
reported_cases = reported_cases, generation_time = generation_time,
rt = rt_opts(prior = list(mean = 2, sd = 0.1)),
delays = delay_opts(incubation_period, reporting_delay)
)
#> Logging threshold set at INFO for the EpiNow2 logger
#> Writing EpiNow2 logs to the console and: /var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T//RtmpqbXERq/regional-epinow/2020-04-01.log
#> Logging threshold set at INFO for the EpiNow2.epinow logger
#> Writing EpiNow2.epinow logs to the console and: /var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T//RtmpqbXERq/epinow/2020-04-01.log
#> WARN [2022-03-28 02:00:21] epinow: There were 9 divergent transitions after warmup. See
#> https://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
#> to find out why this is a problem and how to eliminate them. -
#> WARN [2022-03-28 02:00:21] epinow: Examine the pairs() plot to diagnose sampling problems
#> -
# summary of the latest estimates
summary(out)
#> measure estimate
#> 1: New confirmed cases by infection date 3306 (1440 -- 6887)
#> 2: Expected change in daily cases Likely decreasing
#> 3: Effective reproduction no. 0.78 (0.51 -- 1.1)
#> 4: Rate of growth -0.064 (-0.15 -- 0.026)
#> 5: Doubling/halving time (days) -11 (27 -- -4.8)
# plot estimates
plot(out)
# summary of R estimates
summary(out, type = "parameters", params = "R")
#> date variable strat type median
#> 1: 2020-02-22 R <NA> estimate 2.0455266
#> 2: 2020-02-23 R <NA> estimate 2.0355453
#> 3: 2020-02-24 R <NA> estimate 2.0214885
#> 4: 2020-02-25 R <NA> estimate 2.0054198
#> 5: 2020-02-26 R <NA> estimate 1.9838752
#> 6: 2020-02-27 R <NA> estimate 1.9615209
#> 7: 2020-02-28 R <NA> estimate 1.9351708
#> 8: 2020-02-29 R <NA> estimate 1.9076376
#> 9: 2020-03-01 R <NA> estimate 1.8772431
#> 10: 2020-03-02 R <NA> estimate 1.8451138
#> 11: 2020-03-03 R <NA> estimate 1.8104175
#> 12: 2020-03-04 R <NA> estimate 1.7733977
#> 13: 2020-03-05 R <NA> estimate 1.7348261
#> 14: 2020-03-06 R <NA> estimate 1.6947691
#> 15: 2020-03-07 R <NA> estimate 1.6527716
#> 16: 2020-03-08 R <NA> estimate 1.6098026
#> 17: 2020-03-09 R <NA> estimate 1.5648714
#> 18: 2020-03-10 R <NA> estimate 1.5216427
#> 19: 2020-03-11 R <NA> estimate 1.4746486
#> 20: 2020-03-12 R <NA> estimate 1.4282353
#> 21: 2020-03-13 R <NA> estimate 1.3834100
#> 22: 2020-03-14 R <NA> estimate 1.3377224
#> 23: 2020-03-15 R <NA> estimate 1.2919152
#> 24: 2020-03-16 R <NA> estimate 1.2476613
#> 25: 2020-03-17 R <NA> estimate 1.2032030
#> 26: 2020-03-18 R <NA> estimate 1.1605419
#> 27: 2020-03-19 R <NA> estimate 1.1183136
#> 28: 2020-03-20 R <NA> estimate 1.0770799
#> 29: 2020-03-21 R <NA> estimate 1.0384909
#> 30: 2020-03-22 R <NA> estimate 1.0029222
#> 31: 2020-03-23 R <NA> estimate 0.9706271
#> 32: 2020-03-24 R <NA> estimate 0.9400338
#> 33: 2020-03-25 R <NA> estimate 0.9136200
#> 34: 2020-03-26 R <NA> estimate based on partial data 0.8890553
#> 35: 2020-03-27 R <NA> estimate based on partial data 0.8659194
#> 36: 2020-03-28 R <NA> estimate based on partial data 0.8444266
#> 37: 2020-03-29 R <NA> estimate based on partial data 0.8255214
#> 38: 2020-03-30 R <NA> estimate based on partial data 0.8094243
#> 39: 2020-03-31 R <NA> estimate based on partial data 0.7925643
#> 40: 2020-04-01 R <NA> estimate based on partial data 0.7777120
#> 41: 2020-04-02 R <NA> forecast 0.7777120
#> 42: 2020-04-03 R <NA> forecast 0.7777120
#> 43: 2020-04-04 R <NA> forecast 0.7777120
#> 44: 2020-04-05 R <NA> forecast 0.7777120
#> 45: 2020-04-06 R <NA> forecast 0.7777120
#> 46: 2020-04-07 R <NA> forecast 0.7777120
#> 47: 2020-04-08 R <NA> forecast 0.7777120
#> date variable strat type median
#> mean sd lower_90 lower_50 lower_20 upper_20 upper_50
#> 1: 2.0455081 0.08938793 1.8989682 1.9860599 2.0231243 2.0658775 2.1024315
#> 2: 2.0346971 0.07870145 1.9063444 1.9826255 2.0138765 2.0527459 2.0865478
#> 3: 2.0212981 0.07325304 1.9024814 1.9704138 2.0016775 2.0385781 2.0699673
#> 4: 2.0050454 0.07129002 1.8913614 1.9562726 1.9857142 2.0218250 2.0502993
#> 5: 1.9857814 0.07077894 1.8755058 1.9380719 1.9673493 2.0004562 2.0291205
#> 6: 1.9634623 0.07023395 1.8520543 1.9175658 1.9447347 1.9770162 2.0072618
#> 7: 1.9381524 0.06895785 1.8307828 1.8937723 1.9198111 1.9521721 1.9803007
#> 8: 1.9100057 0.06688796 1.8062524 1.8656361 1.8915813 1.9244339 1.9509444
#> 9: 1.8792397 0.06432641 1.7801058 1.8367712 1.8617508 1.8943230 1.9201523
#> 10: 1.8461052 0.06168372 1.7497523 1.8063934 1.8304702 1.8594938 1.8845183
#> 11: 1.8108595 0.05928865 1.7174613 1.7722521 1.7964303 1.8239814 1.8480435
#> 12: 1.7737464 0.05729303 1.6812943 1.7371320 1.7592298 1.7871567 1.8104217
#> 13: 1.7349849 0.05567793 1.6441883 1.6994867 1.7217454 1.7480213 1.7713357
#> 14: 1.6947662 0.05433431 1.6074930 1.6593476 1.6814286 1.7090377 1.7295277
#> 15: 1.6532577 0.05316265 1.5677654 1.6180787 1.6388859 1.6659887 1.6871466
#> 16: 1.6106092 0.05213538 1.5276979 1.5759394 1.5960637 1.6224585 1.6441902
#> 17: 1.5669625 0.05129289 1.4855074 1.5319229 1.5536171 1.5778543 1.5993860
#> 18: 1.5224608 0.05068163 1.4434787 1.4880346 1.5083944 1.5322188 1.5560617
#> 19: 1.4772592 0.05027741 1.3984332 1.4436562 1.4626783 1.4869443 1.5108114
#> 20: 1.4315349 0.04995081 1.3540547 1.3979202 1.4166854 1.4409890 1.4636956
#> 21: 1.3854963 0.04950691 1.3118759 1.3521137 1.3710639 1.3955904 1.4175270
#> 22: 1.3393888 0.04878282 1.2641839 1.3075763 1.3261622 1.3493834 1.3696159
#> 23: 1.2934952 0.04775185 1.2190449 1.2634665 1.2816614 1.3037478 1.3220519
#> 24: 1.2481295 0.04658534 1.1747867 1.2179590 1.2363751 1.2579368 1.2760174
#> 25: 1.2036257 0.04564862 1.1311462 1.1739599 1.1928643 1.2131923 1.2309911
#> 26: 1.1603214 0.04543202 1.0887948 1.1309790 1.1488347 1.1709682 1.1878948
#> 27: 1.1185419 0.04643173 1.0438191 1.0880412 1.1067444 1.1298056 1.1479163
#> 28: 1.0785853 0.04901571 1.0008195 1.0454776 1.0658466 1.0899569 1.1096778
#> 29: 1.0407109 0.05334152 0.9563889 1.0065352 1.0260580 1.0535766 1.0761076
#> 30: 1.0051319 0.05937388 0.9085380 0.9665387 0.9891281 1.0179003 1.0437118
#> 31: 0.9720122 0.06696954 0.8625184 0.9281447 0.9548114 0.9869390 1.0158584
#> 32: 0.9414656 0.07595683 0.8159382 0.8909856 0.9213723 0.9600133 0.9911177
#> 33: 0.9135577 0.08617492 0.7731083 0.8558576 0.8932010 0.9345412 0.9682053
#> 34: 0.8883086 0.09748045 0.7298347 0.8239822 0.8637367 0.9117637 0.9498084
#> 35: 0.8656968 0.10973986 0.6885585 0.7934494 0.8371959 0.8903410 0.9353298
#> 36: 0.8456630 0.12281870 0.6455064 0.7662474 0.8137627 0.8720010 0.9214392
#> 37: 0.8281150 0.13657377 0.6079125 0.7408094 0.7904100 0.8558006 0.9112980
#> 38: 0.8129337 0.15085022 0.5743131 0.7173821 0.7695432 0.8423461 0.9037523
#> 39: 0.7999797 0.16548363 0.5432116 0.6950539 0.7524348 0.8311363 0.8955191
#> 40: 0.7890993 0.18030298 0.5141473 0.6736171 0.7372986 0.8200038 0.8918222
#> 41: 0.7890993 0.18030298 0.5141473 0.6736171 0.7372986 0.8200038 0.8918222
#> 42: 0.7890993 0.18030298 0.5141473 0.6736171 0.7372986 0.8200038 0.8918222
#> 43: 0.7890993 0.18030298 0.5141473 0.6736171 0.7372986 0.8200038 0.8918222
#> 44: 0.7890993 0.18030298 0.5141473 0.6736171 0.7372986 0.8200038 0.8918222
#> 45: 0.7890993 0.18030298 0.5141473 0.6736171 0.7372986 0.8200038 0.8918222
#> 46: 0.7890993 0.18030298 0.5141473 0.6736171 0.7372986 0.8200038 0.8918222
#> 47: 0.7890993 0.18030298 0.5141473 0.6736171 0.7372986 0.8200038 0.8918222
#> mean sd lower_90 lower_50 lower_20 upper_20 upper_50
#> upper_90
#> 1: 2.192439
#> 2: 2.163992
#> 3: 2.142229
#> 4: 2.123604
#> 5: 2.106177
#> 6: 2.083462
#> 7: 2.052268
#> 8: 2.019773
#> 9: 1.986098
#> 10: 1.946689
#> 11: 1.906831
#> 12: 1.867799
#> 13: 1.826296
#> 14: 1.784018
#> 15: 1.741497
#> 16: 1.697301
#> 17: 1.653332
#> 18: 1.607500
#> 19: 1.560431
#> 20: 1.514345
#> 21: 1.468192
#> 22: 1.417664
#> 23: 1.369317
#> 24: 1.321560
#> 25: 1.278666
#> 26: 1.233951
#> 27: 1.197242
#> 28: 1.162355
#> 29: 1.129692
#> 30: 1.102418
#> 31: 1.083692
#> 32: 1.067328
#> 33: 1.053246
#> 34: 1.046388
#> 35: 1.041032
#> 36: 1.042677
#> 37: 1.048889
#> 38: 1.061284
#> 39: 1.079291
#> 40: 1.094595
#> 41: 1.094595
#> 42: 1.094595
#> 43: 1.094595
#> 44: 1.094595
#> 45: 1.094595
#> 46: 1.094595
#> 47: 1.094595
#> upper_90
# }