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 anddate
must be in date format.- generation_time
A call to
gt_opts()
(or its aliasgeneration_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 ofdelay_opts()
and the examples below for details.- truncation
A call to
trunc_opts()
defining the truncation of the observed data. Defaults totrunc_opts()
, i.e. no truncation. See theestimate_truncation()
help file for an approach to estimating this from data where thedist
list element returned byestimate_truncation()
is used as thetruncation
argument here, thereby propagating the uncertainty in the estimate.- rt
A list of options as generated by
rt_opts()
defining Rt estimation. Defaults tort_opts()
. To generate new infections using the non-mechanistic model instead of the renewal equation model, usert = NULL
. The non-mechanistic model internally uses the settingrt = 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 tobackcalc_opts()
.- gp
A list of options as generated by
gp_opts()
to define the Gaussian process. Defaults togp_opts()
. Set toNULL
to disable the Gaussian process.- obs
A list of options as generated by
obs_opts()
defining the observation model. Defaults toobs_opts()
.- stan
A list of stan options as generated by
stan_opts()
. Defaults tostan_opts()
. Can be used to overridedata
,init
, andverbose
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
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 forsetup_default_logging()
and thesetup_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 otherwiseFALSE
. Should verbose debug progress messages be printed. Corresponds to the "DEBUG" level fromfutile.logger
. Seesetup_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/RtmpaOwoK5/regional-epinow/2020-04-01.log.
#> Logging threshold set at INFO for the name logger
#> Writing EpiNow2.epinow logs to the console and:
#> /tmp/RtmpaOwoK5/epinow/2020-04-01.log.
#> WARN [2024-10-31 14:54:56] epinow: There were 13 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 [2024-10-31 14:54:56] epinow: Examine the pairs() plot to diagnose sampling problems
#> -
# summary of the latest estimates
summary(out)
#> measure estimate
#> <char> <char>
#> 1: New infections per day 3024 (1329 -- 6849)
#> 2: Expected change in daily reports Likely decreasing
#> 3: Effective reproduction no. 0.76 (0.51 -- 1.1)
#> 4: Rate of growth -0.079 (-0.18 -- 0.028)
#> 5: Doubling/halving time (days) -8.8 (25 -- -3.8)
# 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.0903531
#> 2: 2020-02-23 R <NA> estimate 2.0778218
#> 3: 2020-02-24 R <NA> estimate 2.0645933
#> 4: 2020-02-25 R <NA> estimate 2.0464644
#> 5: 2020-02-26 R <NA> estimate 2.0247321
#> 6: 2020-02-27 R <NA> estimate 2.0020291
#> 7: 2020-02-28 R <NA> estimate 1.9731440
#> 8: 2020-02-29 R <NA> estimate 1.9413709
#> 9: 2020-03-01 R <NA> estimate 1.9059528
#> 10: 2020-03-02 R <NA> estimate 1.8669116
#> 11: 2020-03-03 R <NA> estimate 1.8259431
#> 12: 2020-03-04 R <NA> estimate 1.7817565
#> 13: 2020-03-05 R <NA> estimate 1.7371170
#> 14: 2020-03-06 R <NA> estimate 1.6914813
#> 15: 2020-03-07 R <NA> estimate 1.6440057
#> 16: 2020-03-08 R <NA> estimate 1.5958288
#> 17: 2020-03-09 R <NA> estimate 1.5455224
#> 18: 2020-03-10 R <NA> estimate 1.4954989
#> 19: 2020-03-11 R <NA> estimate 1.4454526
#> 20: 2020-03-12 R <NA> estimate 1.3971662
#> 21: 2020-03-13 R <NA> estimate 1.3483203
#> 22: 2020-03-14 R <NA> estimate 1.3006112
#> 23: 2020-03-15 R <NA> estimate 1.2534373
#> 24: 2020-03-16 R <NA> estimate 1.2083612
#> 25: 2020-03-17 R <NA> estimate 1.1646893
#> 26: 2020-03-18 R <NA> estimate 1.1233735
#> 27: 2020-03-19 R <NA> estimate based on partial data 1.0836861
#> 28: 2020-03-20 R <NA> estimate based on partial data 1.0456948
#> 29: 2020-03-21 R <NA> estimate based on partial data 1.0091645
#> 30: 2020-03-22 R <NA> estimate based on partial data 0.9759447
#> 31: 2020-03-23 R <NA> estimate based on partial data 0.9440739
#> 32: 2020-03-24 R <NA> estimate based on partial data 0.9154586
#> 33: 2020-03-25 R <NA> estimate based on partial data 0.8899583
#> 34: 2020-03-26 R <NA> estimate based on partial data 0.8656916
#> 35: 2020-03-27 R <NA> estimate based on partial data 0.8419914
#> 36: 2020-03-28 R <NA> estimate based on partial data 0.8214383
#> 37: 2020-03-29 R <NA> estimate based on partial data 0.8033121
#> 38: 2020-03-30 R <NA> estimate based on partial data 0.7864876
#> 39: 2020-03-31 R <NA> estimate based on partial data 0.7724677
#> 40: 2020-04-01 R <NA> estimate based on partial data 0.7584018
#> 41: 2020-04-02 R <NA> forecast 0.7584018
#> 42: 2020-04-03 R <NA> forecast 0.7584018
#> 43: 2020-04-04 R <NA> forecast 0.7584018
#> 44: 2020-04-05 R <NA> forecast 0.7584018
#> 45: 2020-04-06 R <NA> forecast 0.7584018
#> 46: 2020-04-07 R <NA> forecast 0.7584018
#> 47: 2020-04-08 R <NA> forecast 0.7584018
#> 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.0929328 0.09417213 1.9343629 2.0336085 2.0664804 2.1150391 2.1548051
#> 2: 2.0806540 0.08448360 1.9417182 2.0247932 2.0572015 2.1007592 2.1383366
#> 3: 2.0658209 0.07909953 1.9332908 2.0134130 2.0446001 2.0845879 2.1199076
#> 4: 2.0480616 0.07718386 1.9223547 1.9970552 2.0274291 2.0666025 2.0982556
#> 5: 2.0270691 0.07731179 1.8999795 1.9757177 2.0058596 2.0453975 2.0767688
#> 6: 2.0026304 0.07807338 1.8774536 1.9501946 1.9803722 2.0203869 2.0546596
#> 7: 1.9746485 0.07849715 1.8503352 1.9212660 1.9521236 1.9922497 2.0237833
#> 8: 1.9431524 0.07814991 1.8217776 1.8903371 1.9202436 1.9590074 1.9912198
#> 9: 1.9082918 0.07702941 1.7905566 1.8557770 1.8861745 1.9244930 1.9539578
#> 10: 1.8703189 0.07537009 1.7557309 1.8196607 1.8495736 1.8848032 1.9160470
#> 11: 1.8295602 0.07344353 1.7147694 1.7801755 1.8087900 1.8436796 1.8742006
#> 12: 1.7863872 0.07142205 1.6754299 1.7374070 1.7657719 1.7996703 1.8318836
#> 13: 1.7411918 0.06934749 1.6325432 1.6940194 1.7209812 1.7551286 1.7854857
#> 14: 1.6943671 0.06718959 1.5899928 1.6479263 1.6751398 1.7090477 1.7383210
#> 15: 1.6462957 0.06492984 1.5444979 1.6017180 1.6276630 1.6602218 1.6889913
#> 16: 1.5973413 0.06260828 1.5014998 1.5529376 1.5790982 1.6100963 1.6388733
#> 17: 1.5478436 0.06031252 1.4563118 1.5044578 1.5300259 1.5594969 1.5875532
#> 18: 1.4981163 0.05813177 1.4090802 1.4579602 1.4814366 1.5085314 1.5354013
#> 19: 1.4484495 0.05611678 1.3619935 1.4097825 1.4324109 1.4596116 1.4846578
#> 20: 1.3991139 0.05427513 1.3147707 1.3628723 1.3834549 1.4108449 1.4332396
#> 21: 1.3503675 0.05260237 1.2664065 1.3159004 1.3357998 1.3612266 1.3818077
#> 22: 1.3024601 0.05112431 1.2185486 1.2696829 1.2885752 1.3134240 1.3335140
#> 23: 1.2556346 0.04992367 1.1745474 1.2241214 1.2427942 1.2658444 1.2867498
#> 24: 1.2101257 0.04914373 1.1303728 1.1794718 1.1981782 1.2202725 1.2413739
#> 25: 1.1661560 0.04898187 1.0857082 1.1353789 1.1530791 1.1771361 1.1970976
#> 26: 1.1239319 0.04968587 1.0438969 1.0916994 1.1105713 1.1353120 1.1558632
#> 27: 1.0836408 0.05154257 1.0019661 1.0493986 1.0717223 1.0968456 1.1162099
#> 28: 1.0454484 0.05483147 0.9583717 1.0092611 1.0319346 1.0601290 1.0805336
#> 29: 1.0094965 0.05974248 0.9109624 0.9695244 0.9947340 1.0252858 1.0488419
#> 30: 0.9758996 0.06630775 0.8656227 0.9303778 0.9587464 0.9936095 1.0201805
#> 31: 0.9447419 0.07440007 0.8244230 0.8936471 0.9245812 0.9636794 0.9963454
#> 32: 0.9160759 0.08379285 0.7810661 0.8557149 0.8944874 0.9373257 0.9739604
#> 33: 0.8899223 0.09423481 0.7380143 0.8229759 0.8647226 0.9125556 0.9542871
#> 34: 0.8662725 0.10550188 0.7000876 0.7929164 0.8364706 0.8927899 0.9389258
#> 35: 0.8450920 0.11741720 0.6633243 0.7641016 0.8105981 0.8735547 0.9243368
#> 36: 0.8263233 0.12984700 0.6267762 0.7362100 0.7881523 0.8567144 0.9108659
#> 37: 0.8098888 0.14268422 0.5909536 0.7140567 0.7666839 0.8401643 0.8987439
#> 38: 0.7956917 0.15582933 0.5613866 0.6924649 0.7494604 0.8255614 0.8858116
#> 39: 0.7836174 0.16917372 0.5335399 0.6725893 0.7328584 0.8113178 0.8791793
#> 40: 0.7735350 0.18258816 0.5088966 0.6545128 0.7162446 0.8002412 0.8732707
#> 41: 0.7735350 0.18258816 0.5088966 0.6545128 0.7162446 0.8002412 0.8732707
#> 42: 0.7735350 0.18258816 0.5088966 0.6545128 0.7162446 0.8002412 0.8732707
#> 43: 0.7735350 0.18258816 0.5088966 0.6545128 0.7162446 0.8002412 0.8732707
#> 44: 0.7735350 0.18258816 0.5088966 0.6545128 0.7162446 0.8002412 0.8732707
#> 45: 0.7735350 0.18258816 0.5088966 0.6545128 0.7162446 0.8002412 0.8732707
#> 46: 0.7735350 0.18258816 0.5088966 0.6545128 0.7162446 0.8002412 0.8732707
#> 47: 0.7735350 0.18258816 0.5088966 0.6545128 0.7162446 0.8002412 0.8732707
#> mean sd lower_90 lower_50 lower_20 upper_20 upper_50
#> upper_90
#> <num>
#> 1: 2.246805
#> 2: 2.216490
#> 3: 2.196592
#> 4: 2.174074
#> 5: 2.153805
#> 6: 2.132937
#> 7: 2.106421
#> 8: 2.077721
#> 9: 2.041547
#> 10: 2.002014
#> 11: 1.954385
#> 12: 1.909849
#> 13: 1.859341
#> 14: 1.809090
#> 15: 1.756388
#> 16: 1.703923
#> 17: 1.650932
#> 18: 1.597582
#> 19: 1.545688
#> 20: 1.490117
#> 21: 1.438286
#> 22: 1.389705
#> 23: 1.340247
#> 24: 1.291546
#> 25: 1.246231
#> 26: 1.206156
#> 27: 1.164974
#> 28: 1.133626
#> 29: 1.108109
#> 30: 1.083996
#> 31: 1.064481
#> 32: 1.052337
#> 33: 1.048211
#> 34: 1.043346
#> 35: 1.038913
#> 36: 1.037089
#> 37: 1.042757
#> 38: 1.047331
#> 39: 1.058790
#> 40: 1.070925
#> 41: 1.070925
#> 42: 1.070925
#> 43: 1.070925
#> 44: 1.070925
#> 45: 1.070925
#> 46: 1.070925
#> 47: 1.070925
#> upper_90
options(old_opts)
# }