Skip to contents

[Stable] This function wraps the functionality of estimate_infections() in order to estimate Rt and cases by date of infection and forecast these infections into the future. In addition to the functionality of estimate_infections() it produces additional summary output useful for reporting results and interpreting them as well as error catching and reporting, making it particularly useful for production use e.g. running at set intervals on a dedicated server.

Usage

epinow(
  data,
  generation_time = gt_opts(),
  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),
  filter_leading_zeros = TRUE,
  zero_threshold = Inf,
  return_output = is.null(target_folder),
  output = c("samples", "plots", "latest", "fit", "timing"),
  plot_args = list(),
  target_folder = NULL,
  target_date,
  logs = tempdir(),
  id = "epinow",
  verbose = interactive(),
  reported_cases
)

Arguments

data

A <data.frame> of confirmed cases (confirm) by date (date). confirm must be numeric and date must be in date format.

generation_time

A call to gt_opts() (or its alias generation_time_opts()) defining the generation time distribution used. For backwards compatibility a list of summary parameters can also be passed.

delays

A call to delay_opts() defining delay distributions and options. See the documentation of delay_opts() and the examples below for details.

truncation

A call to trunc_opts() defining the truncation of the observed data. Defaults to trunc_opts(), i.e. no truncation. See the estimate_truncation() help file for an approach to estimating this from data where the dist list element returned by estimate_truncation() is used as the truncation argument here, thereby propagating the uncertainty in the estimate.

rt

A list of options as generated by rt_opts() defining Rt estimation. Defaults to rt_opts(). To generate new infections using the non-mechanistic model instead of the renewal equation model, use rt = NULL. The non-mechanistic model internally uses the setting rt = rt_opts(use_rt = FALSE, future = "project", gp_on = "R0").

backcalc

A list of options as generated by backcalc_opts() to define the back calculation. Defaults to backcalc_opts().

gp

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.

obs

A list of options as generated by obs_opts() defining the observation model. Defaults to obs_opts().

stan

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.

horizon

Numeric, defaults to 7. Number of days into the future to forecast.

CrIs

Numeric vector of credible intervals to calculate.

filter_leading_zeros

Logical, defaults to TRUE. Should zeros at the start of the time series be filtered out.

zero_threshold

[Experimental] Numeric defaults to Inf. Indicates if detected zero cases are meaningful by using a threshold number of cases based on the 7-day average. If the average is above this threshold then the zero is replaced using fill.

return_output

Logical, defaults to FALSE. Should output be returned, this automatically updates to TRUE if no directory for saving is specified.

output

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.

plot_args

A list of optional arguments passed to plot.epinow().

target_folder

Character string specifying where to save results (will create if not present).

target_date

Date, defaults to maximum found in the data if not specified.

logs

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.

id

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.

verbose

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.

reported_cases

Deprecated; use data instead.

Value

A list of output from estimate_infections with additional elements summarising results and reporting errors if they have occurred.

Examples

# \donttest{
# set number of cores to use
old_opts <- options()
options(mc.cores = ifelse(interactive(), 4, 1))

# set an example generation time. In practice this should use an estimate
# from the literature or be estimated from data
generation_time <- Gamma(
  shape = Normal(1.3, 0.3),
  rate = Normal(0.37, 0.09),
  max = 14
)
# set an example incubation period. In practice this should use an estimate
# from the literature or be estimated from data
incubation_period <- LogNormal(
   meanlog = Normal(1.6, 0.06),
   sdlog = Normal(0.4, 0.07),
   max = 14
)
# set an example reporting delay. In practice this should use an estimate
# from the literature or be estimated from data
reporting_delay <- LogNormal(mean = 2, sd = 1, max = 10)

# example case data
reported_cases <- example_confirmed[1:40]

# estimate Rt and nowcast/forecast cases by date of infection
out <- epinow(
  data = reported_cases,
  generation_time = gt_opts(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 name logger
#> Writing EpiNow2 logs to the console and:
#> /tmp/RtmpL3prgX/regional-epinow/2020-04-01.log.
#> Logging threshold set at INFO for the name logger
#> Writing EpiNow2.epinow logs to the console and:
#> /tmp/RtmpL3prgX/epinow/2020-04-01.log.
# summary of the latest estimates
summary(out)
#>                             measure                 estimate
#>                              <char>                   <char>
#> 1:           New infections per day      3678 (1946 -- 7000)
#> 2: Expected change in daily reports        Likely decreasing
#> 3:       Effective reproduction no.         0.81 (0.64 -- 1)
#> 4:                   Rate of growth -0.061 (-0.13 -- 0.0085)
#> 5:     Doubling/halving time (days)         -11 (82 -- -5.5)
# plot estimates
plot(out)


# summary of R estimates
summary(out, type = "parameters", params = "R")
#>           date variable  strat                           type    median
#>         <Date>   <char> <char>                         <char>     <num>
#>  1: 2020-02-22        R   <NA>                       estimate 2.0961528
#>  2: 2020-02-23        R   <NA>                       estimate 2.0784009
#>  3: 2020-02-24        R   <NA>                       estimate 2.0560667
#>  4: 2020-02-25        R   <NA>                       estimate 2.0328691
#>  5: 2020-02-26        R   <NA>                       estimate 2.0060065
#>  6: 2020-02-27        R   <NA>                       estimate 1.9775868
#>  7: 2020-02-28        R   <NA>                       estimate 1.9449921
#>  8: 2020-02-29        R   <NA>                       estimate 1.9101370
#>  9: 2020-03-01        R   <NA>                       estimate 1.8752457
#> 10: 2020-03-02        R   <NA>                       estimate 1.8365357
#> 11: 2020-03-03        R   <NA>                       estimate 1.7955638
#> 12: 2020-03-04        R   <NA>                       estimate 1.7537567
#> 13: 2020-03-05        R   <NA>                       estimate 1.7100535
#> 14: 2020-03-06        R   <NA>                       estimate 1.6641687
#> 15: 2020-03-07        R   <NA>                       estimate 1.6183799
#> 16: 2020-03-08        R   <NA>                       estimate 1.5720295
#> 17: 2020-03-09        R   <NA>                       estimate 1.5254742
#> 18: 2020-03-10        R   <NA>                       estimate 1.4790464
#> 19: 2020-03-11        R   <NA>                       estimate 1.4343553
#> 20: 2020-03-12        R   <NA>                       estimate 1.3891437
#> 21: 2020-03-13        R   <NA>                       estimate 1.3446095
#> 22: 2020-03-14        R   <NA>                       estimate 1.3010570
#> 23: 2020-03-15        R   <NA>                       estimate 1.2585376
#> 24: 2020-03-16        R   <NA>                       estimate 1.2163765
#> 25: 2020-03-17        R   <NA>                       estimate 1.1769649
#> 26: 2020-03-18        R   <NA>                       estimate 1.1390398
#> 27: 2020-03-19        R   <NA> estimate based on partial data 1.1030151
#> 28: 2020-03-20        R   <NA> estimate based on partial data 1.0697206
#> 29: 2020-03-21        R   <NA> estimate based on partial data 1.0386599
#> 30: 2020-03-22        R   <NA> estimate based on partial data 1.0088289
#> 31: 2020-03-23        R   <NA> estimate based on partial data 0.9805172
#> 32: 2020-03-24        R   <NA> estimate based on partial data 0.9551878
#> 33: 2020-03-25        R   <NA> estimate based on partial data 0.9313054
#> 34: 2020-03-26        R   <NA> estimate based on partial data 0.9106050
#> 35: 2020-03-27        R   <NA> estimate based on partial data 0.8909962
#> 36: 2020-03-28        R   <NA> estimate based on partial data 0.8723336
#> 37: 2020-03-29        R   <NA> estimate based on partial data 0.8556821
#> 38: 2020-03-30        R   <NA> estimate based on partial data 0.8400602
#> 39: 2020-03-31        R   <NA> estimate based on partial data 0.8268199
#> 40: 2020-04-01        R   <NA> estimate based on partial data 0.8149562
#> 41: 2020-04-02        R   <NA>                       forecast 0.8149562
#> 42: 2020-04-03        R   <NA>                       forecast 0.8149562
#> 43: 2020-04-04        R   <NA>                       forecast 0.8149562
#> 44: 2020-04-05        R   <NA>                       forecast 0.8149562
#> 45: 2020-04-06        R   <NA>                       forecast 0.8149562
#> 46: 2020-04-07        R   <NA>                       forecast 0.8149562
#> 47: 2020-04-08        R   <NA>                       forecast 0.8149562
#>           date variable  strat                           type    median
#>          mean         sd  lower_90  lower_50  lower_20  upper_20  upper_50
#>         <num>      <num>     <num>     <num>     <num>     <num>     <num>
#>  1: 2.0984136 0.09120705 1.9531490 2.0348844 2.0714276 2.1202822 2.1595908
#>  2: 2.0797371 0.08314031 1.9515084 2.0206968 2.0547153 2.0988448 2.1362720
#>  3: 2.0586145 0.07668964 1.9378250 2.0038691 2.0344478 2.0766022 2.1107782
#>  4: 2.0349390 0.07188964 1.9211529 1.9848744 2.0123360 2.0518252 2.0830664
#>  5: 2.0086372 0.06856948 1.8992366 1.9607000 1.9885034 2.0232936 2.0537947
#>  6: 1.9796808 0.06639087 1.8760978 1.9328609 1.9598945 1.9939131 2.0240467
#>  7: 1.9480951 0.06494978 1.8481485 1.9035105 1.9300565 1.9619482 1.9906187
#>  8: 1.9139637 0.06388570 1.8174144 1.8703621 1.8957743 1.9283053 1.9540149
#>  9: 1.8774282 0.06294183 1.7816213 1.8339077 1.8582975 1.8919248 1.9168259
#> 10: 1.8386825 0.06196401 1.7424261 1.7955933 1.8206945 1.8518792 1.8766606
#> 11: 1.7979640 0.06086526 1.7023570 1.7554138 1.7802340 1.8107504 1.8346436
#> 12: 1.7555423 0.05959131 1.6614356 1.7145927 1.7375429 1.7670961 1.7916016
#> 13: 1.7117080 0.05810784 1.6205818 1.6716847 1.6943555 1.7231407 1.7479475
#> 14: 1.6667634 0.05640761 1.5781309 1.6280448 1.6494282 1.6773668 1.7019127
#> 15: 1.6210129 0.05452120 1.5362343 1.5831814 1.6044173 1.6319692 1.6553340
#> 16: 1.5747568 0.05251606 1.4936743 1.5389562 1.5591009 1.5845185 1.6088472
#> 17: 1.5282857 0.05048111 1.4500909 1.4939009 1.5134106 1.5391225 1.5613576
#> 18: 1.4818771 0.04850735 1.4060811 1.4478845 1.4672482 1.4928311 1.5126173
#> 19: 1.4357939 0.04667919 1.3640110 1.4036880 1.4218605 1.4468931 1.4641133
#> 20: 1.3902846 0.04508292 1.3196882 1.3585535 1.3779935 1.4009939 1.4192931
#> 21: 1.3455837 0.04382407 1.2782555 1.3155131 1.3346063 1.3550349 1.3721929
#> 22: 1.3019124 0.04303609 1.2349519 1.2736265 1.2903122 1.3110347 1.3279794
#> 23: 1.2594770 0.04286653 1.1911856 1.2314428 1.2479362 1.2678363 1.2860785
#> 24: 1.2184677 0.04344339 1.1499172 1.1908614 1.2073853 1.2268516 1.2460964
#> 25: 1.1790556 0.04484122 1.1078927 1.1493756 1.1669961 1.1882306 1.2065892
#> 26: 1.1413906 0.04706865 1.0671432 1.1101586 1.1285812 1.1509751 1.1708468
#> 27: 1.1055982 0.05008097 1.0271619 1.0725997 1.0915301 1.1167153 1.1373107
#> 28: 1.0717789 0.05380283 0.9856943 1.0357974 1.0561546 1.0838361 1.1070003
#> 29: 1.0400057 0.05814518 0.9473718 1.0014832 1.0233090 1.0532154 1.0783539
#> 30: 1.0103245 0.06301262 0.9102676 0.9682738 0.9918319 1.0243803 1.0529516
#> 31: 0.9827536 0.06830682 0.8738380 0.9377054 0.9639932 0.9979206 1.0290992
#> 32: 0.9572857 0.07393142 0.8390893 0.9080562 0.9365586 0.9730374 1.0076324
#> 33: 0.9338905 0.07979868 0.8052652 0.8803054 0.9118116 0.9523282 0.9885022
#> 34: 0.9125187 0.08583469 0.7731234 0.8551092 0.8890278 0.9314935 0.9708427
#> 35: 0.8931058 0.09197976 0.7452681 0.8311366 0.8676542 0.9126394 0.9561694
#> 36: 0.8755761 0.09818357 0.7191387 0.8089465 0.8482201 0.8961205 0.9410450
#> 37: 0.8598457 0.10439747 0.6950884 0.7882255 0.8303392 0.8822624 0.9282275
#> 38: 0.8458255 0.11056748 0.6743229 0.7695734 0.8143981 0.8694502 0.9186997
#> 39: 0.8334235 0.11663105 0.6531880 0.7518572 0.7992025 0.8584600 0.9107801
#> 40: 0.8225475 0.12251788 0.6351069 0.7366787 0.7867786 0.8485373 0.9025992
#> 41: 0.8225475 0.12251788 0.6351069 0.7366787 0.7867786 0.8485373 0.9025992
#> 42: 0.8225475 0.12251788 0.6351069 0.7366787 0.7867786 0.8485373 0.9025992
#> 43: 0.8225475 0.12251788 0.6351069 0.7366787 0.7867786 0.8485373 0.9025992
#> 44: 0.8225475 0.12251788 0.6351069 0.7366787 0.7867786 0.8485373 0.9025992
#> 45: 0.8225475 0.12251788 0.6351069 0.7366787 0.7867786 0.8485373 0.9025992
#> 46: 0.8225475 0.12251788 0.6351069 0.7366787 0.7867786 0.8485373 0.9025992
#> 47: 0.8225475 0.12251788 0.6351069 0.7366787 0.7867786 0.8485373 0.9025992
#>          mean         sd  lower_90  lower_50  lower_20  upper_20  upper_50
#>     upper_90
#>        <num>
#>  1: 2.250310
#>  2: 2.217515
#>  3: 2.185068
#>  4: 2.153505
#>  5: 2.122252
#>  6: 2.092663
#>  7: 2.061320
#>  8: 2.024567
#>  9: 1.987086
#> 10: 1.945849
#> 11: 1.904171
#> 12: 1.860122
#> 13: 1.812620
#> 14: 1.765906
#> 15: 1.717500
#> 16: 1.665433
#> 17: 1.615839
#> 18: 1.567529
#> 19: 1.516464
#> 20: 1.467540
#> 21: 1.419800
#> 22: 1.374690
#> 23: 1.332384
#> 24: 1.293545
#> 25: 1.257466
#> 26: 1.221707
#> 27: 1.189511
#> 28: 1.162588
#> 29: 1.137969
#> 30: 1.114952
#> 31: 1.096874
#> 32: 1.082976
#> 33: 1.068149
#> 34: 1.056455
#> 35: 1.046712
#> 36: 1.037961
#> 37: 1.033343
#> 38: 1.032793
#> 39: 1.031415
#> 40: 1.030629
#> 41: 1.030629
#> 42: 1.030629
#> 43: 1.030629
#> 44: 1.030629
#> 45: 1.030629
#> 46: 1.030629
#> 47: 1.030629
#>     upper_90

options(old_opts)
# }