Skip to contents

The EpiNow2 package contains functionality to run estimate_infections() in production mode, i.e. with full logging and saving all relevant outputs and plots to dedicated folders in the hard drive. This is done with the epinow() function, that takes the same options as estimate_infections() with some additional options that determine, for example, where output gets stored and what output exactly. The function can be a useful option when, e.g., running the model daily with updated data on a high-performance computing server to feed into a dashboard. For more detail on the various model options available, see the Examples vignette, for more on the general modelling approach the Workflow, and for theoretical background the Model definitions vignette

Running the model on a single region

To run the model in production mode for a single region, set the parameters up in the same way as for estimate_infections() (see the Workflow vignette). Here we use the example delay and generation time distributions that come with the package. This should be replaced with parameters relevant to the system that is being studied.

library("EpiNow2")
options(mc.cores = 4)
reported_cases <- example_confirmed[1:60]
reporting_delay <- LogNormal(mean = 2, sd = 1, max = 10)
delay <- example_incubation_period + reporting_delay
rt_prior <- list(mean = 2, sd = 0.1)

We can then run the epinow() function with the same arguments as estimate_infections().

res <- epinow(reported_cases,
  generation_time = generation_time_opts(example_generation_time),
  delays = delay_opts(delay),
  rt = rt_opts(prior = rt_prior),
  target_folder = "results"
)
#> Logging threshold set at INFO for the EpiNow2 logger
#> Writing EpiNow2 logs to the console and: /var/folders/n9/h_419gjj2mg3d208nplgvbg40000gp/T//RtmpCLEtE0/regional-epinow/2020-04-21.log
#> Logging threshold set at INFO for the EpiNow2.epinow logger
#> Writing EpiNow2.epinow logs to the console and: /var/folders/n9/h_419gjj2mg3d208nplgvbg40000gp/T//RtmpCLEtE0/epinow/2020-04-21.log
#> WARN [2024-03-06 10:52:09] 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 [2024-03-06 10:52:09] epinow: Examine the pairs() plot to diagnose sampling problems
#>  -
res$plots$R
#> NULL

The initial messages here indicate where log files can be found, and summarised results and plots are in the folder given by target_folder (here: results/).

Running the model simultaneously on multiple regions

The package also contains functionality to conduct inference contemporaneously (if separately) in production mode on multiple time series, e.g. to run the model on multiple regions. This is done with the regional_epinow() function.

Say, for example, we construct a dataset containing two regions, testland and realland (in this simple example both containing the same case data).

cases <- example_confirmed[1:60]
cases <- data.table::rbindlist(list(
  data.table::copy(cases)[, region := "testland"],
  cases[, region := "realland"]
 ))

To then run this on multiple regions using the default options above, we could use

region_rt <- regional_epinow(
  reported_cases = cases,
  generation_time = generation_time_opts(example_generation_time),
  delays = delay_opts(delay),
  rt = rt_opts(prior = rt_prior),
)
#> INFO [2024-03-06 10:52:13] Producing following optional outputs: regions, summary, samples, plots, latest
#> Logging threshold set at INFO for the EpiNow2 logger
#> Writing EpiNow2 logs to the console and: /var/folders/n9/h_419gjj2mg3d208nplgvbg40000gp/T//RtmpCLEtE0/regional-epinow/2020-04-21.log
#> Logging threshold set at INFO for the EpiNow2.epinow logger
#> Writing EpiNow2.epinow logs to: /var/folders/n9/h_419gjj2mg3d208nplgvbg40000gp/T//RtmpCLEtE0/epinow/2020-04-21.log
#> INFO [2024-03-06 10:52:13] Reporting estimates using data up to: 2020-04-21
#> INFO [2024-03-06 10:52:13] No target directory specified so returning output
#> INFO [2024-03-06 10:52:13] Producing estimates for: testland, realland
#> INFO [2024-03-06 10:52:13] Regions excluded: none
#> INFO [2024-03-06 10:52:48] Completed estimates for: testland
#> INFO [2024-03-06 10:53:17] Completed estimates for: realland
#> INFO [2024-03-06 10:53:17] Completed regional estimates
#> INFO [2024-03-06 10:53:17] Regions with estimates: 2
#> INFO [2024-03-06 10:53:17] Regions with runtime errors: 0
#> INFO [2024-03-06 10:53:17] Producing summary
#> INFO [2024-03-06 10:53:17] No summary directory specified so returning summary output
#> INFO [2024-03-06 10:53:17] No target directory specified so returning timings
## summary
region_rt$summary$summarised_results$table
#>      Region New confirmed cases by infection date
#>      <char>                                <char>
#> 1: realland                   2254 (1153 -- 4243)
#> 2: testland                   2261 (1059 -- 4347)
#>    Expected change in daily cases Effective reproduction no.
#>                            <fctr>                     <char>
#> 1:              Likely decreasing          0.87 (0.6 -- 1.2)
#> 2:              Likely decreasing          0.88 (0.6 -- 1.2)
#>            Rate of growth Doubling/halving time (days)
#>                    <char>                       <char>
#> 1: -0.028 (-0.1 -- 0.034)             -24 (20 -- -6.9)
#> 2: -0.027 (-0.1 -- 0.037)             -25 (19 -- -6.8)
## plot
region_rt$summary$plots$R
plot of chunk regional_epinow
plot of chunk regional_epinow

If instead, we wanted to use the Gaussian Process for testland and a weekly random walk for realland we could specify these separately using the opts_list() function from the package and modifyList() from R.

gp <- opts_list(gp_opts(), cases)
gp <- modifyList(gp, list(realland = NULL), keep.null = TRUE)
rt <- opts_list(rt_opts(), cases, realland = rt_opts(rw = 7))
region_separate_rt <- regional_epinow(
  reported_cases = cases,
  generation_time = generation_time_opts(example_generation_time),
  delays = delay_opts(delay),
  rt = rt, gp = gp,
)
#> INFO [2024-03-06 10:53:17] Producing following optional outputs: regions, summary, samples, plots, latest
#> Logging threshold set at INFO for the EpiNow2 logger
#> Writing EpiNow2 logs to the console and: /var/folders/n9/h_419gjj2mg3d208nplgvbg40000gp/T//RtmpCLEtE0/regional-epinow/2020-04-21.log
#> Logging threshold set at INFO for the EpiNow2.epinow logger
#> Writing EpiNow2.epinow logs to: /var/folders/n9/h_419gjj2mg3d208nplgvbg40000gp/T//RtmpCLEtE0/epinow/2020-04-21.log
#> INFO [2024-03-06 10:53:17] Reporting estimates using data up to: 2020-04-21
#> INFO [2024-03-06 10:53:17] No target directory specified so returning output
#> INFO [2024-03-06 10:53:17] Producing estimates for: testland, realland
#> INFO [2024-03-06 10:53:17] Regions excluded: none
#> INFO [2024-03-06 10:53:50] Completed estimates for: testland
#> INFO [2024-03-06 10:53:59] Completed estimates for: realland
#> INFO [2024-03-06 10:53:59] Completed regional estimates
#> INFO [2024-03-06 10:53:59] Regions with estimates: 2
#> INFO [2024-03-06 10:53:59] Regions with runtime errors: 0
#> INFO [2024-03-06 10:53:59] Producing summary
#> INFO [2024-03-06 10:53:59] No summary directory specified so returning summary output
#> INFO [2024-03-06 10:54:00] No target directory specified so returning timings
## summary
region_separate_rt$summary$summarised_results$table
#>      Region New confirmed cases by infection date
#>      <char>                                <char>
#> 1: realland                   2122 (1144 -- 3976)
#> 2: testland                    2177 (992 -- 4216)
#>    Expected change in daily cases Effective reproduction no.
#>                            <fctr>                     <char>
#> 1:              Likely decreasing         0.85 (0.61 -- 1.2)
#> 2:              Likely decreasing         0.86 (0.54 -- 1.2)
#>              Rate of growth Doubling/halving time (days)
#>                      <char>                       <char>
#> 1: -0.033 (-0.094 -- 0.031)             -21 (22 -- -7.3)
#> 2:  -0.031 (-0.11 -- 0.034)             -22 (20 -- -6.1)
## plot
region_separate_rt$summary$plots$R
plot of chunk regional_epinow_multiple
plot of chunk regional_epinow_multiple