Package 'EpiNow'

Title: Estimate Realtime Case Counts and Time-varying Epidemiological Parameters
Description: To identify changes in the reproduction number, rate of spread, and doubling time during the course of outbreaks whilst accounting for potential biases due to delays in case reporting.
Authors: Sam Abbott [aut, cre] , Joel Hellewell [aut] , James Munday [aut], Robin Thompson [aut], Sebastian Funk [aut]
Maintainer: Sam Abbott <[email protected]>
License: MIT + file LICENSE
Version: 0.3.0
Built: 2024-09-26 05:47:16 UTC
Source: https://github.com/epiforecasts/EpiNow

Help Index


Add Dates from Daata

Description

Pulls the last n dates from a vector

Usage

add_dates(dates, n)

Arguments

dates

Character vector of dates to pull from.

n

Number of dates required

Value

Character vector of dates of length N

Examples

dates <- rep(1:10)

add_dates(dates, 3)

Adjust Case Counts for Truncation

Description

Adjust Case Counts for Truncation

Usage

adjust_for_truncation(
  cases,
  cum_freq,
  dates,
  confidence_adjustment = NULL,
  samples,
  max_upscale
)

Arguments

cases

Numeric vector of cases

cum_freq

Numeric vector of cumulative frequencies

dates

Character vector of dates

confidence_adjustment

Numeric vector of frequencies used to adjust confidence

samples

Numeric, number of samples to take

max_upscale

Numeric, maximum upscaling of cases allowed at each time point

Value

A data.table adjusted for truncation


Adjust from Case Counts by Infection Date to Date of Report

Description

Stochastic mapping from cases by date of infection to date of report via date of onset. Essentially reversal of nowcast_pipeline.

Usage

adjust_infection_to_report(
  infections,
  delay_def,
  incubation_def,
  reporting_effect,
  reporting_model,
  type = "sample",
  return_onset = FALSE,
  truncate_future = TRUE
)

Arguments

infections

data.table containing a date variable and a numeric cases variable.

delay_def

A single row data.table that defines the delay distribution (model, parameters and maximum delay for each model). See lognorm_dist_def for an example of the structure.

incubation_def

A single row data.table that defines the incubation distribution (model, parameters and maximum delay for each model). See lognorm_dist_def for an example of the structure.

reporting_effect

A numeric vector of length 7 that allows the scaling of reported cases by the day on which they report (1 = Monday, 7 = Sunday). By default no scaling occurs.

reporting_model

A function that takes a single numeric vector as an argument and returns a single numeric vector. Can be used to apply stochastic reporting effects. See the examples for details.

type

Character string indicating the method to use to transfrom counts. Supports either "sample" which approximates sampling or "median" would shift by the median of the distribution.

return_onset

Logical, defaults to FALSE. Should cases by date of onset also be returned?

truncate_future

Logical, should cases be truncted if they occur after the first date reported in the data. Defaults to TRUE.

Value

A data.table containing a date variable (date of report) and a cases variable. If return_onset = TRUE there will be a third variable reference which indicates what the date variable refers to.

Examples

## Define example cases
cases <- data.table::as.data.table(EpiSoon::example_obs_cases) 

cases <- cases[, `:=`(confirm = as.integer(cases), import_status = "local")]

## Define a single report delay distribution
delay_def <- EpiNow::lognorm_dist_def(mean = 5, 
                                      mean_sd = 1,
                                      sd = 3,
                                      sd_sd = 1,
                                      max_value = 30,
                                      samples = 1,
                                      to_log = TRUE)
                                       
## Define a single incubation period
incubation_def <- EpiNow::lognorm_dist_def(mean = EpiNow::covid_incubation_period[1, ]$mean,
                                           mean_sd = EpiNow::covid_incubation_period[1, ]$mean_sd,
                                           sd = EpiNow::covid_incubation_period[1, ]$sd,
                                           sd_sd = EpiNow::covid_incubation_period[1, ]$sd_sd,
                                           max_value = 30, samples = 1)
                                           

## Perform a nowcast
nowcast <- nowcast_pipeline(reported_cases = cases, 
                            target_date = max(cases$date),
                            delay_defs = delay_def,
                            incubation_defs = incubation_def)
                            

infections <- nowcast[type %in% "infection_upscaled" & import_status %in% "local"]
infections <- infections[, `:=`(type = NULL, import_status = NULL)]


## Simple mapping
report <- adjust_infection_to_report(infections, delay_def, incubation_def)   

print(report)   

## Mapping with a weekly reporting effect
report_weekly <- adjust_infection_to_report(
                      infections, delay_def, incubation_def,
                      reporting_effect = c(1.1, rep(1, 4), 0.95, 0.95))          
                             
print(report_weekly) 

## Map using a deterministic median shift for both delays
report_median <- adjust_infection_to_report(infections, delay_def, 
                                           incubation_def, type = "median")      
                                           
                                                   
                                                           
## Map with a weekly reporting effect and stochastic reporting model
report_stochastic <- adjust_infection_to_report(
                      infections, delay_def, incubation_def,
                      reporting_effect = c(1.1, rep(1, 4), 0.95, 0.95),
                      reporting_model = function(n) {
                      out <- suppressWarnings(rnbinom(length(n), as.integer(n), 0.5))
                      out <- ifelse(is.na(out), 0, out)
                      })          
                             
print(report_stochastic)

Clean Nowcasts for a Supplied Date

Description

This function removes nowcasts in the format produced by EpiNow from a target directory for the date supplied.

Usage

clean_nowcasts(date = NULL, nowcast_dir = NULL)

Arguments

date

Date object. Defaults to todays date

nowcast_dir

Character string giving the filepath to the nowcast results directory.


Generate a country map for a single variable.

Description

This general purpose function can be used to generate a country map for a single variable. It has few defaults but the data supplied must contain a region_code variable for linking to mapping data. This function requires the installation of the rnaturalearth package.

Usage

country_map(
  data = NULL,
  country = NULL,
  variable = NULL,
  variable_label = NULL,
  trans = "identity",
  fill_labels = NULL,
  scale_fill = NULL,
  ...
)

Arguments

data

Dataframe containing variables to be mapped. Must contain a region_code variable.

country

Character string indicating the name of the country to be mapped.

variable

A character string indicating the variable to map data for. This must be supplied.

variable_label

A character string indicating the variable label to use. If not supplied then the underlying variable name is used.

trans

A character string specifying the transform to use on the specified metric. Defaults to no transform ("identity"). Other options include log scaling ("log") and log base 10 scaling ("log10"). For a complete list of options see ggplot2::continous_scale.

fill_labels

A function to use to allocate legend labels. An example (used below) is scales::percent, which can be used for percentage data.

scale_fill

Function to use for scaling the fill. Defaults to a custom ggplot2::scale_fill_manual

...

Additional arguments passed to the scale_fill function

Value

A ggplot2 object containing a country map.

Examples

country_map

Covid Generation Time Estimates

Description

Default Covid generation time estimates. See here for details: https://github.com/epiforecasts/EpiNow/blob/master/data-raw/ganyani-generation-time.R

Usage

covid_generation_times

Format

A data.table of mean and standard deviation estimates


Covid Generation Time Estimates - Summary

Description

Default Covid generation time estimates (summary). See here for details: https://github.com/epiforecasts/EpiNow/blob/master/data-raw/ganyani-generation-time.R

Usage

covid_generation_times_summary

Format

A data.table of summarising the distribution


Covid Incubation Period

Description

Default Covid incubation period estimates. See here for details: https://github.com/epiforecasts/EpiNow/blob/master/data-raw/incubation-period.R

Usage

covid_incubation_period

Format

A vector giviing the probability for each day


Covid Generation Serial Intervals

Description

Default Covid serial interval estimates. See here for details: https://github.com/epiforecasts/EpiNow/blob/master/data-raw/nishiura-serial-interval.R

Usage

covid_serial_intervals

Format

A vector giviing the probability for each day


Fit an integer adjusted exponential or gamma distribution

Description

Fit an integer adjusted exponential or gamma distribution

Usage

dist_fit(values = NULL, samples = NULL, dist = "exp")

Arguments

values

Numeric vector of values

samples

Numeric, number of samples to take

dist

Character string, which distribution to fit. Defaults to exponential ("exp") but gamma is also supported ("gamma").

Value

A stan fit of an interval censored distribution

Examples

dist_fit(rexp(1:100, 2), samples = 1000, dist = "exp")

Distribution Skeleton

Description

This function acts as a skeleton for a truncated distribution defined by model type, maximum value and model parameters. It is designed to be used with the output from get_dist.

Usage

dist_skel(n, dist = FALSE, cum = TRUE, model, params, max_value = 120)

Arguments

n

Numeric vector, number of samples to take (or days for the probability density).

dist

Logical, defaults to FALSE. Should the probability density be returned rather than a number of samples.

cum

Logical, defaults to TRUE. If dist = TRUE should the returned distribution be cumulative.

model

Character string, defining the model to be used. Supported options are exponential ("exp"), gamma ("gamma"), and log normal ("lognorm")

params

A list of parameters values (by name) required for each model. For the exponential model this is a rate parameter and for the gamma model this is alpha and beta.

max_value

Numeric, the maximum value to allow. Defaults to 120. Samples outside of this range are resampled.

Value

A vector of samples or a probability distribution.

Examples

## Exponential model

## Sample
dist_skel(10, model = "exp", params = list(rate = 1))

## Cumulative prob density
dist_skel(1:10, model = "exp", dist = TRUE, params = list(rate = 1))

## Probability density
dist_skel(1:10, model = "exp", dist = TRUE, 
          cum = FALSE, params = list(rate = 1))

## Gamma model

dist_skel(10, model = "gamma", params = list(alpha = 1, beta = 2))

## Cumulative prob density
dist_skel(0:10, model = "gamma", dist = TRUE,
          params = list(alpha = 1, beta = 2))

## Probability density
dist_skel(0:10, model = "gamma", dist = TRUE, 
          cum = FALSE, params = list(alpha = 2, beta = 2))

## Log normal model

dist_skel(10, model = "lognorm", params = list(mean = log(5), sd = log(2)))

## Cumulative prob density
dist_skel(0:10, model = "lognorm", dist = TRUE,
          params = list(mean = log(5), sd = log(2)))

## Probability density
dist_skel(0:10, model = "lognorm", dist = TRUE, cum = FALSE,
          params = list(mean = log(5), sd = log(2)))

Estimate time-varying measures and forecast

Description

Estimate time-varying measures and forecast

Usage

epi_measures_pipeline(
  nowcast = NULL,
  generation_times = NULL,
  min_est_date = NULL,
  gt_samples = 1,
  rt_samples = 5,
  rt_windows = 7,
  rate_window = 7,
  rt_prior = NULL,
  forecast_model = NULL,
  horizon = 0,
  verbose = TRUE
)

Arguments

nowcast

A nowcast as produced by nowcast_pipeline

generation_times

A matrix with columns representing samples and rows representing the probability of the generation timebeing on that day.

min_est_date

Date to begin estimation.

gt_samples

Numeric, the number of samples to take from the generaiton times supplied

rt_samples

Numeric, the number of samples to take from the estimated R distribution for each time point.

rt_windows

Numeric vector, windows over which to estimate time-varying R. The best performing window will be selected per serial interval sample by default (based on which window best forecasts current cases).

rate_window

Numeric, the window to use to estimate the rate of spread.

rt_prior

A list defining the reproduction number prior containing the mean (mean_prior) and standard deviation (std_prior)

forecast_model

An uninitialised bsts model passed to EpiSoon::forecast_rt to be used for forecasting future Rt values. An example of the required structure is: function(ss, y){bsts::AddSemilocalLinearTrend(ss, y = y)}.

horizon

Numeric, defaults to 0. The horizon over which to forecast Rts and cases.

verbose

Logical, defaults to TRUE. Should progress messages be shown.

Value

A list of data frames containing reproduction number estimates, case forecasts, rate of growth estimates both summarised and raw.

Examples

## Construct example distributions
## reporting delay dist
delay_dist <- EpiNow::lognorm_dist_def(mean = 3, 
                                      mean_sd = 1,
                                      sd = 3,
                                      sd_sd = 1,
                                      max_value = 30,
                                      samples = 1)

## incubation delay dist
incubation_dist <- delay_dist

## Uses example case vector from EpiSoon
cases <- data.table::setDT(EpiSoon::example_obs_cases)
cases <- cases[, `:=`(confirm = as.integer(cases), import_status = "local")]

## Basic nowcast
nowcast <- nowcast_pipeline(reported_cases = cases, 
                            target_date = max(cases$date),
                            delay_defs = delay_dist,
                            incubation_defs = incubation_dist)
 
## Estimate parameters                           
estimates <- epi_measures_pipeline(nowcast[type %in% "infection_upscaled"], 
                       generation_times = EpiNow::covid_generation_times,
                       rt_prior = list(mean_prior = 2.6, std_prior = 2))   
                       
estimates

Estimate the doubling time

Description

Estimate the doubling time

Usage

estimate_doubling_time(r)

Arguments

r

An estimate of the rate of change (r)

Value

A vector of numeric values

Examples

estimate_doubling_time(0.2)

Estimate r

Description

Estimate r

Usage

estimate_little_r(sample, min_time = NULL, max_time = NULL)

Arguments

sample

A datatable containing a numeric cases variable.

min_time

Numeric, minimum time to use to fit the model.

max_time

Numeric, maximum time to use to fit the model.

Value

A datatable containing an estimate of r, its standard deviation and a measure of the goodness of fit.

Examples

cases <- data.table::setDT(EpiSoon::example_obs_cases)[, 
                          cases := as.integer(cases)]

estimate_little_r(cases)

Estimate r in a set time window

Description

Estimate r in a set time window

Usage

estimate_r_in_window(
  onsets = NULL,
  min_time = NULL,
  max_time = NULL,
  bootstrap_samples = 1000
)

Arguments

onsets

A list of samples datasets nested within the dataset sampled from.

min_time

Numeric, the minimum time to fit the model to.

max_time

Numeric, the maximum time to fit the model to.

bootstrap_samples

Numeric, defaults to 1000. The number of samples to take when bootstrapping little r to account for model uncertainty.

Value

A list of 3 dataframes containing estimates for little r, doubling time and model goodness of fit.


Estimate the time varying R0 - using EpiEstim

Description

Estimate the time varying R0 - using EpiEstim

Usage

estimate_R0(
  cases = NULL,
  generation_times = NULL,
  rt_prior = NULL,
  windows = NULL,
  gt_samples = 100,
  rt_samples = 100,
  min_est_date = NULL,
  forecast_model = NULL,
  horizon = 0
)

Arguments

cases

A dataframe containing a list of local cases with the following variables: date, cases, and import_status

generation_times

A matrix with columns representing samples and rows representing the probability of the generation timebeing on that day.

rt_prior

A list defining the reproduction number prior containing the mean (mean_prior) and standard deviation (std_prior)

windows

Numeric vector, windows over which to estimate time-varying R. The best performing window will be selected per serial interval sample by default (based on which window best forecasts current cases).

gt_samples

Numeric, the number of samples to take from the generaiton times supplied

rt_samples

Numeric, the number of samples to take from the estimated R distribution for each time point.

min_est_date

Date to begin estimation.

forecast_model

An uninitialised bsts model passed to EpiSoon::forecast_rt to be used for forecasting future Rt values. An example of the required structure is: function(ss, y){bsts::AddSemilocalLinearTrend(ss, y = y)}.

horizon

Numeric, defaults to 0. The horizon over which to forecast Rts and cases.

Value

A list of data.table's containing the date and summarised R estimate and optionally a case forecast

Examples

## Nowcast Rts                  
estimates <- estimate_R0(cases = EpiSoon::example_obs_cases, 
                         generation_times = as.matrix(EpiNow::covid_generation_times[,2]), 
                         rt_prior = list(mean_prior = 2.6, std_prior = 2),
                         windows = c(1, 3, 7), rt_samples = 10, gt_samples = 1)
                         
                         
estimates$rts
 
## Not run:  
## Nowcast Rts, forecast Rts and the forecast cases
estimates <- estimate_R0(cases = EpiSoon::example_obs_cases, 
                         generation_times = as.matrix(EpiNow::covid_generation_times[,1]), 
                         rt_prior = list(mean_prior = 2.6, std_prior = 2),
                         windows = c(1, 3, 7), rt_samples = 10, gt_samples = 20,
                         min_est_date =  as.Date("2020-02-18"),
                         forecast_model = function(...){
                              EpiSoon::fable_model(model = fable::ETS(y ~ trend("A")), ...)
                              },
                         horizon = 14)
                                           
## Rt estimates and forecasts
estimates$rts



## Case forecasts
estimates$cases

## End(Not run)

Estimate time varying r

Description

Estimate time varying r

Usage

estimate_time_varying_r(onsets, window = 7)

Arguments

onsets

A list of samples datasets nested within the dataset sampled from.

window

integer value for window size in days (default = 7)

Value

A dataframe of r estimates over time summarisd across samples.


Generate a Gamma Distribution Definition Based on Parameter Estimates

Description

Generates a distribution definition when only parameter estimates are available for gamma distributed parameters. See rgamma for distribution information.

Usage

gamma_dist_def(
  shape,
  shape_sd,
  scale,
  scale_sd,
  mean,
  mean_sd,
  sd,
  sd_sd,
  max_value,
  samples
)

Arguments

shape

Numeric, shape parameter of the gamma distribution.

shape_sd

Numeric, standard deviation of the shape parameter.

scale

Numeric, scale parameter of the gamma distribution.

scale_sd

Numeric, standard deviation of the scale parameter.

max_value

Numeric, the maximum value to allow. Defaults to 120. Samples outside of this range are resampled.

samples

Numeric, number of sample distributions to generate.

Value

A data.table definining the distribution as used by dist_skel

Examples

## Using estimated shape and scale
def <- gamma_dist_def(shape = 5.807, shape_sd = 0.2,
               scale = 0.9, scale_sd = 0.05,
               max_value = 20, samples = 10)
               
print(def)

def$params[[1]]

## Using mean and sd
def <- gamma_dist_def(mean = 3, mean_sd = 0.5,
               sd = 3, sd_sd = 0.1,
               max_value = 20, samples = 10)
               
print(def)

def$params[[1]]

Generate a sample linelist from the observed linelist and sampled linelists

Description

Generate a sample linelist from the observed linelist and sampled linelists

Usage

generate_pseudo_linelist(
  count_linelist = NULL,
  observed_linelist = NULL,
  merge_actual_onsets = TRUE
)

Arguments

count_linelist

Dataframe with two variables: date_report and daily_linelist. As generated by linelist_from_case_counts.

observed_linelist

Dataframe with two variables: date_report and daily_observed_linelist. As generated by 'split_linelist_by_day“

merge_actual_onsets

Logical, defaults to TRUE. Should linelist onset dates be used where available?

Value

Dataframe with two variables: date_report and date_onset


Get a Parameters that Define a Discrete Distribution

Description

Get a Parameters that Define a Discrete Distribution

Usage

get_dist_def(
  values,
  verbose = FALSE,
  samples = 1,
  bootstraps = 1,
  bootstrap_samples = 250
)

Arguments

values

Numeric vector of integer values.

verbose

Logical, defaults to FALSE. Should progress messages be printed

samples

Numeric, number of samples to take overall from the bootstrapped posteriors.

bootstraps

Numeric, defaults to 1. The number of bootstrap samples (with replacement) of the delay distribution to take.

bootstrap_samples

Numeric, defaults to 100. The number of samples to take in each boostrap. When the sample size of the supplied delay distribution is less than 100 this is used instead.

Value

A data.table of distributions and the parameters that define them.

Author(s)

Sebastian Funk [email protected]

Examples

## Example with exponential and a small smaple
delays <- rexp(20, 1)

get_dist_def(delays, samples = 10, verbose = TRUE)


## Not run: 
## Example with gamma and a larger sample
delays <- rgamma(50, 4, 1)

out <- get_dist_def(delays, samples = 2, bootstraps = 2)

## Inspect
out

## Inspect one parameter
out$params[[1]]


## Load into skeleton and sample with truncation
EpiNow::dist_skel(10, model = out$model[[1]],
                  params = out$params[[1]],
                  max_value = out$max_value[[1]])

## End(Not run)

Combine total and imported case counts

Description

Combine total and imported case counts

Usage

get_local_import_case_counts(total_cases, linelist = NULL, cases_from = NULL)

Arguments

total_cases

Dataframe with following variables: date and cases.

linelist

Dataframe with at least the following variables: date_confirm, import_status

cases_from

A character string containing a date in the format "yyyy-mm-dd". Applies a filter to returned cases.

Value

A tibble containing cases by date locally and imported


Get Folders with Nowcast Results

Description

Get Folders with Nowcast Results

Usage

get_regions(results_dir)

Arguments

results_dir

A character string giving the directory in which results are stored (as produced by regional_rt_pipeline).

Value

A named character vector containing the results to plot.


Get Timeseries from EpiNow

Description

Get Timeseries from EpiNow

Usage

get_timeseries(results_dir = NULL, date = NULL, summarised = FALSE)

Arguments

results_dir

A character string indicating the folder containing the EpiNow results to extract.

date

A Character string (in the format "yyyy-mm-dd") indicating the date to extract data for. Defaults to "latest" which finds the latest results available.

summarised

Logical, defaults to FALSE. Should full or summarised results be returned.

Value

A list of reproduction number estimates and nowcast cases

Examples

## Not run: 
## Assuming epiforecasts/covid is one repo higher
## Summary results
get_timeseries("../covid/_posts/global/nowcast/results/", 
               summarised = TRUE)

## Simulations
get_timeseries("../covid/_posts/global/nowcast/results/")

## End(Not run)
## Code
get_timeseries

Generate a global map for a single variable.

Description

This general purpose function can be used to generate a global map for a single variable. It has few defaults but the data supplied must contain a country variable for linking to mapping data. This function requires the installation of the rnaturalearth package.

Usage

global_map(
  data = NULL,
  variable = NULL,
  variable_label = NULL,
  trans = "identity",
  fill_labels = NULL,
  scale_fill = NULL,
  ...
)

Arguments

data

Dataframe containing variables to be mapped. Must contain a country variable.

variable

A character string indicating the variable to map data for. This must be supplied.

variable_label

A character string indicating the variable label to use. If not supplied then the underlying variable name is used.

trans

A character string specifying the transform to use on the specified metric. Defaults to no transform ("identity"). Other options include log scaling ("log") and log base 10 scaling ("log10"). For a complete list of options see ggplot2::continous_scale.

fill_labels

A function to use to allocate legend labels. An example (used below) is scales::percent, which can be used for percentage data.

scale_fill

Function to use for scaling the fill. Defaults to a custom ggplot2::scale_fill_manual

...

Additional arguments passed to the scale_fill function

Value

A ggplot2 object containing a global map.

Examples

## Not run: 
df <- data.table::data.table(variable = "Increasing", country = "France") 

global_map(df, variable = "variable")

## End(Not run)

Convert Growth Rates to Reproduction numbers.

Description

See here for justification.

Usage

growth_to_R(r, gamma_mean, gamma_sd)

Arguments

r

Numeric, rate of growth estimates

gamma_mean

Numeric, mean of the gamma distribution

gamma_sd

Numeric, standard deviation of the gamma distribution

Value

Numeric vector of reproduction number estimates

Examples

growth_to_R(0.2, 4, 1)

Sample a linelist from case counts and a reporting delay distribution

Description

Sample a linelist from case counts and a reporting delay distribution

Usage

linelist_from_case_counts(cases = NULL)

Arguments

cases

Dataframe with two variables: confirm (numeric) and date_report (date).

Value

A linelist grouped by day as a data.table with two variables: date_report, and daily_observed_linelist


Load nowcast results

Description

Load nowcast results

Usage

load_nowcast_result(
  file = NULL,
  region = NULL,
  date = target_date,
  result_dir = results_dir
)

Arguments

file

Character string giving the result files name.

region

Character string giving the region of interest.

date

Target date (in the format ⁠"yyyy-mm-dd⁠).

result_dir

Character string giving the location of the target directory

Value

An R object read in from the targeted .rds file


Generate a Log Normal Distribution Definition Based on Parameter Estimates

Description

Generates a distribution definition when only parameter estimates are available for log normal distributed parameters. See rlnorm for distribution information.

Usage

lognorm_dist_def(mean, mean_sd, sd, sd_sd, max_value, samples, to_log = FALSE)

Arguments

mean

Numeric, log mean parameter of the gamma distribution.

mean_sd

Numeric, standard deviation of the log mean parameter.

sd

Numeric, log sd parameter of the gamma distribution.

sd_sd

Numeric, standard deviation of the log sd parameter.

max_value

Numeric, the maximum value to allow. Defaults to 120. Samples outside of this range are resampled.

samples

Numeric, number of sample distributions to generate.

to_log

Logical, should parameters be logged before use.

Value

A data.table definining the distribution as used by dist_skel

Examples

def <- lognorm_dist_def(mean = 1.621, mean_sd = 0.0640,
                        sd = 0.418, sd_sd = 0.0691,
                        max_value = 20, samples = 10)
               
print(def)

def$params[[1]]

def <- lognorm_dist_def(mean = 5, mean_sd = 1,
                        sd = 3, sd_sd = 1,
                        max_value = 20, samples = 10,
                        to_log = TRUE)
               
print(def)

def$params[[1]]

Format Credible Intervals

Description

Format Credible Intervals

Usage

make_conf(value, round_type = NULL, digits = 0)

Arguments

value

List of value to map into a string. Requires, point, lower, and upper.

round_type

Function, type of rounding to apply. Defaults to round.

digits

Numeric, defaults to 0. Amount of rounding to apply

Value

A character vector formatted for reporting

Examples

value <- list(list(point = 1, lower = 0, upper = 3))

make_conf(value, round_type = round, digits = 0)

Categorise the Probability of Change for Rt

Description

Categorises a numeric variable into "Increasing" (< 0.05), "Likely increasing" (<0.2), "Unsure" (< 0.8), "Likely decreasing" (< 0.95), "Decreasing" (<= 1)

Usage

map_prob_change(var)

Arguments

var

Numeric variable to be categorised

Value

A character variable.

Examples

var <- seq(0.01, 1, 0.01)

var
 
map_prob_change(var)

Impute Cases Date of Infection

Description

Impute Cases Date of Infection

Usage

nowcast_pipeline(
  reported_cases = NULL,
  linelist = NULL,
  target_date = NULL,
  earliest_allowed_onset = NULL,
  merge_actual_onsets = FALSE,
  approx_delay = FALSE,
  max_delay = 120,
  verbose = FALSE,
  delay_defs = NULL,
  incubation_defs = NULL,
  nowcast_lag = 8,
  max_upscale = 5,
  onset_modifier = NULL
)

Arguments

reported_cases

A dataframe of reported cases

linelist

A linelist of report dates and onset dates

target_date

Character string, in the form "2020-01-01". Date to cast.

earliest_allowed_onset

A character string in the form of a date ("2020-01-01") indiciating the earliest allowed onset.

merge_actual_onsets

Logical, defaults to TRUE. Should linelist onset dates be used where available?

approx_delay

Logical, defaults to FALSE. Should delay sampling be approximated using case counts. Not appropriate when case numbers are low. Useful for high cases counts as decouples run time and resource usage from case count.

max_delay

Numeric, maximum delay to allow. Defaults to 120 days

verbose

Logical, defaults to FALSE. Should internal nowcasting progress messages be returned.

delay_defs

A data.table that defines the delay distributions (model, parameters and maximum delay for each model). See get_delay_dist for an example of the structure.

incubation_defs

A data.table that defines the incubation distributions (model, parameters and maximum delay for each model). See get_delay_dist for an example of the structure.

nowcast_lag

Numeric, defaults to 4. The number of days by which to lag nowcasts. Helps reduce bias due to case upscaling.

max_upscale

Numeric, maximum upscaling of cases allowed at each time point. Defaults to 100 times the observed cases.

onset_modifier

data.frame containing a date variable and a function modifier variable. This is used to modify estimated cases by onset date. modifier must be a function that returns a proportion when called (enables inclusion of uncertainty) and takes the following arguments: n (samples to return) and status ("local" or "import").

Value

A dataframe of nowcast cases from each step of the nowcasting process (defined by type).

Examples

## Construct example distributions
## reporting delay dist
delay_dist <- suppressWarnings(
               EpiNow::get_dist_def(rexp(25, 1 / 10), 
                                    samples = 1, bootstraps = 1))
## incubation delay dist
incubation_dist <- delay_dist

## Uses example case vector from EpiSoon
cases <- data.table::setDT(EpiSoon::example_obs_cases)
cases <- cases[, `:=`(confirm = as.integer(cases), import_status = "local")]

## Basic nowcast
nowcast <- nowcast_pipeline(reported_cases = cases, 
                            target_date = max(cases$date),
                            delay_defs = delay_dist,
                            incubation_defs = incubation_dist)
                            
nowcast

Plot a Time Series with Confidence.

Description

Plot a Time Series with Confidence.

Usage

plot_confidence(
  data,
  outer_alpha = 0.1,
  inner_alpha = 0.2,
  plot_median = TRUE,
  legend = "none"
)

Arguments

data

Dataframe containing the follwoing variables: date, median, type, bottom, top, lower, upper, and confidence

outer_alpha

Numeric, outer alpha level.

inner_alpha

Numeric, inner alpha level.

plot_median

Logical, defaults to FALSE. Should the median be plotted.

legend

Character string defaults to "none". Should a legend be displayed.

Value

A ggplot2 object.


Add a Forecast to a Plot

Description

Add a Forecast to a Plot

Usage

plot_forecast(plot = NULL, forecast = NULL)

Arguments

plot

ggplot2 plot.

forecast

Dataframe containing a forecast with the following variables: bottom, top, lower, and upper.

Value

A ggplot2 plot


Plot a Grid of Plots

Description

Plot a Grid of Plots

Usage

plot_grid(
  regions = NULL,
  plot_object = "bigr_eff_plot.rds",
  results_dir = "results",
  target_date = NULL,
  ...
)

Arguments

regions

A character string containing the list of regions to extract results for (must all have results for the same target date).

plot_object

A character string indicating the plot object to use as the base for the grid.

results_dir

A character string indicating the location of the results directory to extract results from.

target_date

A character string indicating the target date to extract results for. All regions must have results for this date.

...

Additional arguments to pass to patchwork::plot_layout

Value

A ggplot2 object combining multiple plots


Plot Pipeline Results

Description

Plot Pipeline Results

Usage

plot_pipeline(
  target_date = NULL,
  target_folder = NULL,
  min_plot_date = NULL,
  report_forecast = FALSE
)

Arguments

target_date

Character string, in the form "2020-01-01". Date to cast.

target_folder

Character string, name of the folder in which to save the results.

min_plot_date

Character string, in the form "2020-01-01". Minimum date at which to start plotting estimates.

report_forecast

Logical, defaults to FALSE. Should the forecast be reported.

Value

A ggplot2 object


Plot a Summary of the Latest Results

Description

Plot a Summary of the Latest Results

Usage

plot_summary(summary_results, x_lab = "Region", log_cases = FALSE)

Arguments

summary_results

A datatable as returned by summarise_results (the data object).

x_lab

A character string giving the label for the x axis, defaults to region.

log_cases

Logical, should cases be shown on a logged scale. Defaults to FALSE

Value

A ggplot2 object


Extract a the Maximum Value of a Variable Based on a Filter

Description

Extract a the Maximum Value of a Variable Based on a Filter

Usage

pull_max_var(df, max_var = NULL, sel_var = NULL, type_selected = NULL)

Arguments

df

Datatable with the following variables: type and si_dist

max_var

Character string containing the variable name to pull out the maximum R estimate for.

sel_var

Character string indicating the variable to extract

type_selected

The nowcast type to extract.

Value

A character string containing the maximum variable

Examples

df <- data.table::data.table(type = c("nowcast", "other"),
                             var = c(1:10),
                             sel = "test")
                             
pull_max_var(df, max_var = "var", sel_var = "var", type_selected = "nowcast")

Convert Reproduction Numbers to Growth Rates

Description

See here for justification.

Usage

R_to_growth(R, gamma_mean, gamma_sd)

Arguments

R

Numeric, Reproduction number estimates

gamma_mean

Numeric, mean of the gamma distribution

gamma_sd

Numeric, standard deviation of the gamma distribution

Value

Numeric vector of reproduction number estimates

Examples

R_to_growth(2.18, 4, 1)

Draw with an offset from a negative binomial distribution

Description

Samples size (the number of trials) of a binomial distribution copied from https://github.com/sbfnk/bpmodels/blob/master/R/utils.r

Usage

rbinom_size(n, x, prob, max_upscale)

Arguments

n

Numeric, number of samples to draw

x

Numeric, offset.

prob

Numeric, probability of successful trial

max_upscale

Numeric, maximum upscaling of cases allowed at each time point

Examples

x <- c(0:10)
prob <- pgamma(1:11, 4, 2)

rbinom_size(length(x), x, prob, max_upscale = 10)

Regional Realtime Pipeline

Description

Runs a pipeline by region.

Usage

regional_rt_pipeline(
  cases = NULL,
  linelist = NULL,
  delay_defs = NULL,
  incubation_defs = NULL,
  target_folder = "results",
  target_date = NULL,
  merge_onsets = FALSE,
  case_limit = 40,
  onset_modifier = NULL,
  dt_threads = 1,
  verbose = FALSE,
  ...
)

Arguments

cases

A dataframe of cases (confirm) by date of confirmation (date), import status (import_status; ("imp)), and region (region).

linelist

A dataframe of of cases (by row) containing the following variables: import_status (values "local" and "imported"), date_onset, date_confirm, report_delay, and region. If a national linelist is not available a proxy linelist may be used but in this case merge_onsets should be set to FALSE.

delay_defs

A data.table that defines the delay distributions (model, parameters and maximum delay for each model). See get_delay_dist for an example of the structure.

incubation_defs

A data.table that defines the incubation distributions (model, parameters and maximum delay for each model). See get_delay_dist for an example of the structure.

target_folder

Character string indicating the folder into which to save results. Also used to extract previously generated results.

target_date

Character string, in the form "2020-01-01". Date to cast.

merge_onsets

Logical defaults to FALSE. Should available onset data be used. Typically if regional_delay is

case_limit

Numeric, the minimum number of cases in a region required for that region to be evaluated. Defaults to 10. set to FALSE this should also be FALSE

onset_modifier

data.frame containing a date variable and a function modifier variable. This is used to modify estimated cases by onset date. modifier must be a function that returns a proportion when called (enables inclusion of uncertainty) and takes the following arguments: n (samples to return) and status ("local" or "import").

dt_threads

Numeric, the number of data.table threads to use. Set internally to avoid issue when running in parallel. Defaults to 1 thread.

verbose

Logical, defaults to FALSE. Should progress messages be shown for each reigon?

...

Pass additional arguments to rt_pipeline

Examples

## Not run: 
## Save everything to a temporary directory 
## Change this to inspect locally
target_dir <- tempdir() 

## Construct example distributions
## reporting delay dist
delay_dist <- suppressWarnings(
               EpiNow::get_dist_def(rexp(25, 1/10), 
                                    samples = 5, bootstraps = 1))

## Uses example case vector from EpiSoon
cases <- data.table::setDT(EpiSoon::example_obs_cases)
cases <- cases[, `:=`(confirm = as.integer(cases), import_status = "local")][,
                  cases := NULL]

cases <- data.table::rbindlist(list(
  data.table::copy(cases)[, region := "testland"],
  cases[, region := "realland"]))
  
## Run basic nowcasting pipeline
regional_rt_pipeline(cases = cases,
            delay_defs = delay_dist,
            target_folder = target_dir)

## End(Not run)

Generate Regional Summary Output

Description

Generate Regional Summary Output

Usage

regional_summary(
  results_dir = NULL,
  summary_dir = NULL,
  target_date = NULL,
  region_scale = "Region",
  csv_region_label = "region",
  log_cases = FALSE
)

Arguments

results_dir

A character string indicating the location of the results directory to extract results from.

summary_dir

A character string giving the directory in which to store summary of results.

target_date

A character string giving the target date for which to extract results (in the format "yyyy-mm-dd").

region_scale

A character string indicating the name to give the regions being summarised.

csv_region_label

Character string indicating the label to assign to a region when saving to .csv.

log_cases

Logical, should cases be shown on a logged scale. Defaults to FALSE

Examples

## Not run: 

## Example asssumes that CovidGlobalNow (github.com/epiforecasts/covid-global) is  
## in the directory above the root.
regional_summary(results_dir = "../covid-global/national",
                 summary_dir = "../covid-global/national-summary",
                 target_date = "2020-03-19",
                 region_scale = "Country")


## End(Not run)

Report case counts by date of report

Description

Report case counts by date of report

Usage

report_cases(
  nowcast,
  case_forecast = NULL,
  delay_defs,
  incubation_defs,
  type = "median",
  reporting_effect
)

Arguments

nowcast

A dataframe as produced by nowcast_pipeline

case_forecast

A data.table of case forecasts as produced by epi_measures_pipeline If not supplied the default is not incoperate forecasts.

delay_defs

A data.table that defines the delay distributions (model, parameters and maximum delay for each model). See get_delay_dist for an example of the structure.

incubation_defs

A data.table that defines the incubation distributions (model, parameters and maximum delay for each model). See get_delay_dist for an example of the structure.

type

Character string indicating the method to use to transfrom counts. Supports either "sample" which approximates sampling or "median" would shift by the median of the distribution.

reporting_effect

A data.table giving the weekly reporting effect with the following variables: sample (must be the same as in nowcast), effect (numeric scaling factor for each weekday), day (numeric 1 - 7 (1 = Monday and 7 = Sunday)). If not supplied then no weekly reporting effect is assumed.

Value

A data.table containing the following variables sample, date and cases

Examples

## Define example cases
cases <- data.table::as.data.table(EpiSoon::example_obs_cases) 

cases <- cases[, `:=`(confirm = as.integer(cases), import_status = "local")]

## Define a single report delay distribution
delay_defs <- EpiNow::lognorm_dist_def(mean = 5, 
                                       mean_sd = 1,
                                       sd = 3,
                                       sd_sd = 1,
                                       max_value = 30,
                                       samples = 2)
                                       
## Define a single incubation period
incubation_defs <- EpiNow::lognorm_dist_def(mean = EpiNow::covid_incubation_period[1, ]$mean,
                                            mean_sd = EpiNow::covid_incubation_period[1, ]$mean_sd,
                                            sd = EpiNow::covid_incubation_period[1, ]$sd,
                                            sd_sd = EpiNow::covid_incubation_period[1, ]$sd_sd,
                                            max_value = 30, samples = 2)
                                           

## Perform a nowcast
nowcast <- nowcast_pipeline(reported_cases = cases, 
                            target_date = max(cases$date),
                            delay_defs = delay_defs,
                            incubation_defs = incubation_defs)
                            
                     
reported_cases <- report_cases(nowcast, delay_defs = delay_defs,
                               incubation_defs = incubation_defs)
                               
print(reported_cases)

Report Rate of Growth Estimates

Description

Report Rate of Growth Estimates

Usage

report_littler(target_folder)

Arguments

target_folder

Character string indicating the folder into which to save results. Also used to extract previously generated results.


Report Case Nowcast Estimates

Description

Returns a summarised nowcast as well as saving key information to the results folder.

Usage

report_nowcast(nowcast, cases, target, target_folder)

Arguments

nowcast

A dataframe as produced by nowcast_pipeline

cases

A dataframe of cases (in date order) with the following variables: date and cases.

target

Character string indicting the data type to use as the "nowcast".

target_folder

Character string indicating the folder into which to save results. Also used to extract previously generated results.


Report Effective Reproduction Number Estimates

Description

Report Effective Reproduction Number Estimates

Usage

report_reff(target_folder)

Arguments

target_folder

Character string indicating the folder into which to save results. Also used to extract previously generated results.


Provide Summary Statistics on an Rt Pipeline

Description

Provide Summary Statistics on an Rt Pipeline

Usage

report_summary(target_folder)

Arguments

target_folder

Character string indicating the folder into which to save results. Also used to extract previously generated results.


Real-time Pipeline

Description

Combine fitting a delay distribution, constructing a set of complete sampled linelists, nowcast cases by onset date, and estimate the time-varying effective reproduction number and rate of spread.

Usage

rt_pipeline(
  cases = NULL,
  linelist = NULL,
  delay_defs = NULL,
  incubation_defs = NULL,
  delay_cutoff_date = NULL,
  rt_samples = 5,
  rt_windows = 1:7,
  rate_window = 7,
  earliest_allowed_onset = NULL,
  merge_actual_onsets = TRUE,
  approx_delay = FALSE,
  approx_threshold = 10000,
  max_delay = 120,
  generation_times = NULL,
  rt_prior = NULL,
  nowcast_lag = 8,
  forecast_model = NULL,
  horizon = 0,
  report_forecast = FALSE,
  onset_modifier = NULL,
  min_forecast_cases = 200,
  target_folder = NULL,
  target_date = NULL,
  max_upscale = 5,
  dt_threads = 1,
  verbose = FALSE
)

Arguments

cases

A dataframe of cases (in date order) with the following variables: date and cases.

linelist

A dataframe of of cases (by row) containing the following variables: import_status (values "local" and "imported"), date_onset, date_confirm, report_delay.

delay_defs

A data.table that defines the delay distributions (model, parameters and maximum delay for each model). See get_delay_dist for an example of the structure.

incubation_defs

A data.table that defines the incubation distributions (model, parameters and maximum delay for each model). See get_delay_dist for an example of the structure.

delay_cutoff_date

Character string, in the form "2020-01-01". Cutoff date to use to estimate the delay distribution.

rt_samples

Numeric, the number of samples to take from the estimated R distribution for each time point.

rt_windows

Numeric vector, windows over which to estimate time-varying R. The best performing window will be selected per serial interval sample by default (based on which window best forecasts current cases).

rate_window

Numeric, the window to use to estimate the rate of spread.

earliest_allowed_onset

A character string in the form of a date ("2020-01-01") indiciating the earliest allowed onset.

merge_actual_onsets

Logical, defaults to TRUE. Should linelist onset dates be used where available?

approx_delay

Logical, defaults to FALSE. Should delay sampling be approximated using case counts. Not appropriate when case numbers are low. Useful for high cases counts as decouples run time and resource usage from case count.

approx_threshold

Numeric, defaults to 10,000. Threshold of cases below which explicit sampling of onsets always occurs.

max_delay

Numeric, maximum delay to allow. Defaults to 120 days

generation_times

A matrix with columns representing samples and rows representing the probability of the serial intervel being on that day. Defaults to EpiNow::covid_generation_times.

rt_prior

A list defining the reproduction number prior containing the mean (mean_prior) and standard deviation (std_prior)

nowcast_lag

Numeric, defaults to 4. The number of days by which to lag nowcasts. Helps reduce bias due to case upscaling.

forecast_model

An uninitialised bsts model passed to EpiSoon::forecast_rt to be used for forecasting future Rt values. An example of the required structure is: function(ss, y){bsts::AddSemilocalLinearTrend(ss, y = y)}.

horizon

Numeric, defaults to 0. The horizon over which to forecast Rts and cases.

report_forecast

Logical, defaults to FALSE. Should the forecast be reported.

onset_modifier

data.frame containing a date variable and a function modifier variable. This is used to modify estimated cases by onset date. modifier must be a function that returns a proportion when called (enables inclusion of uncertainty) and takes the following arguments: n (samples to return) and status ("local" or "import").

min_forecast_cases

Numeric, defaults to 200. The minimum number of cases required in the last 7 days of data in order for a forecast to be run. This prevents spurious forecasts based on highly uncertain Rt estimates.

target_folder

Character string indicating the folder into which to save results. Also used to extract previously generated results.

target_date

Character string, in the form "2020-01-01". Date to cast.

max_upscale

Numeric, maximum upscaling of cases allowed at each time point. Defaults to 100 times the observed cases.

dt_threads

Numeric, the number of data.table threads to use. Set internally to avoid issue when running in parallel. Defaults to 1 thread.

verbose

Logical, defaults to FALSE. Should internal nowcasting progress messages be returned.

Examples

## Not run: 
## Save everything to a temporary directory 
## Change this to inspect locally
target_dir <- tempdir()

## Construct example distributions
## reporting delay dist
delay_dist <- suppressWarnings(
               EpiNow::get_dist_def(rexp(25, 1/10), 
                                    samples = 5, bootstraps = 1))

## Uses example case vector from EpiSoon
cases <- data.table::setDT(EpiSoon::example_obs_cases)
cases <- cases[, `:=`(confirm = as.integer(cases), import_status = "local")][,
                  cases := NULL]

## Run basic nowcasting pipeline
rt_pipeline(cases = cases,
            delay_defs = delay_dist,
            target_date = max(cases$date),
            target_folder = target_dir)
            
            
## Run with forecasting and approximate delay sampling

## Requires additional packages:
library(EpiSoon)
library(forecastHybrid)

## Runs the estimation pipeline as before but this time with a 14 day Rt and case forecast
## Uses the {forecastHybrid} package to produce an unweighted
## ensemble using the last 3 weeks of data
rt_pipeline(cases = cases,
            delay_defs = delay_dist,
            target_date = max(cases$date),
            approx_delay = TRUE,
            target_folder = target_dir,
            horizon = 14, 
            report_forecast = TRUE
            nowcast_lag = 8,
            forecast_model = function(y, ...){EpiSoon::forecastHybrid_model(
            y = y[max(1, length(y) - 21):length(y)],
            model_params = list(models = "aefz", weights = "equal"),
            forecast_params = list(PI.combination = "mean"), ...)})


## End(Not run)

Approximate Sampling a Distribution using Counts

Description

Approximate Sampling a Distribution using Counts

Usage

sample_approx_dist(
  cases = NULL,
  dist_fn = NULL,
  max_value = 120,
  earliest_allowed_mapped = NULL,
  direction = "backwards",
  type = "sample",
  truncate_future = TRUE
)

Arguments

cases

A dataframe of cases (in date order) with the following variables: date and cases.

dist_fn

Function that takes two arguments with the first being numeric and the second being logical (and defined as dist). Should return the probability density or a sample from the defined distribution. See the examples for more.

max_value

Numeric, maximum value to allow. Defaults to 120 days

earliest_allowed_mapped

A character string representing a date ("2020-01-01"). Indicates the earlies allowed mapped value.

direction

Character string, defato "backwards". Direction in which to map cases. Supports either "backwards" or "forwards".

type

Character string indicating the method to use to transfrom counts. Supports either "sample" which approximates sampling or "median" would shift by the median of the distribution.

truncate_future

Logical, should cases be truncted if they occur after the first date reported in the data. Defaults to TRUE.

Value

A data.table of cases by date of onset

Examples

cases <- data.table::as.data.table(EpiSoon::example_obs_cases) 

cases <- cases[, cases := as.integer(cases)] 

## Reported case distribution
print(cases)

## Total cases
sum(cases$cases)

delay_fn <- function(n, dist, cum) {
              if(dist) {
                pgamma(n + 0.9999, 2, 1) - pgamma(n - 1e-5, 2, 1)
               }else{
                as.integer(rgamma(n, 2, 1))
               }
             }

onsets <- sample_approx_dist(cases = cases,
                             dist_fn = delay_fn)
   
## Estimated onset distribution
print(onsets)
  
## Check that sum is equal to reported cases
total_onsets <- median(
   purrr::map_dbl(1:1000, 
                  ~ sum(sample_approx_dist(cases = cases,
                  dist_fn = delay_fn)$cases))) 
                   
total_onsets
 
                   
## Map from onset cases to reported                  
reports <- sample_approx_dist(cases = cases,
                              dist_fn = delay_fn,
                              direction = "forwards")
                              
                              
## Map from onset cases to reported using a mean shift               
reports <- sample_approx_dist(cases = cases,
                              dist_fn = delay_fn,
                              direction = "forwards",
                              type = "median")

Sample Onset Dates for Cases missing them

Description

Sample Onset Dates for Cases missing them

Usage

sample_delay(linelist = NULL, delay_fn = NULL, earliest_allowed_onset = NULL)

Arguments

linelist

Dataframe with two variables: date_report and date_onset. As generated by generate_pseudo_linelist.

delay_fn

A sampling funtion that takes a single numeric argument and returns a vector of numeric samples this long.

earliest_allowed_onset

A character string in the form of a date ("2020-01-01") indiciating the earliest allowed onset.

Value

Dataframe with no missing data and two variables: date_report and date_onset


Simulate Cases by Date of Infection, Onset and Report

Description

Simulate Cases by Date of Infection, Onset and Report

Usage

simulate_cases(
  rts,
  initial_cases,
  initial_date,
  generation_interval,
  rdist = rpois,
  delay_def,
  incubation_def,
  reporting_effect,
  reporting_model,
  truncate_future = TRUE,
  type = "sample"
)

Arguments

rts

A dataframe of containing two variables rt and date with rt being numeric and date being a date.

initial_cases

Integer, initial number of cases.

initial_date

Date, (i.e as.Date("2020-02-01")). Starting date of the simulation.

generation_interval

Numeric vector describing the generation interval probability density

rdist

A function to be used to sample the number of cases. Must take two arguments with the first specfying the number of samples and the second the mean. Defaults to rpois if not supplied

delay_def

A single row data.table that defines the delay distribution (model, parameters and maximum delay for each model). See lognorm_dist_def for an example of the structure.

incubation_def

A single row data.table that defines the incubation distribution (model, parameters and maximum delay for each model). See lognorm_dist_def for an example of the structure.

reporting_effect

A numeric vector of length 7 that allows the scaling of reported cases by the day on which they report (1 = Monday, 7 = Sunday). By default no scaling occurs.

reporting_model

A function that takes a single numeric vector as an argument and returns a single numeric vector. Can be used to apply stochastic reporting effects. See the examples for details.

truncate_future

Logical, should cases be truncted if they occur after the first date reported in the data. Defaults to TRUE.

type

Character string indicating the method to use to transfrom counts. Supports either "sample" which approximates sampling or "median" would shift by the median of the distribution.

Value

A dataframe containing three variables: date, cases and reference.

Examples

## Define an initial rt vector 
rts <- c(rep(2, 20), (2 - 1:15 * 0.1), rep(0.5, 10))
rts
## Use the mean default generation interval for covid
generation_interval <- rowMeans(EpiNow::covid_generation_times)

## Sample a report delay as a lognormal
delay_def <- EpiNow::lognorm_dist_def(mean = 5, mean_sd = 1,
                                      sd = 3, sd_sd = 1, max_value = 30,
                                      samples = 1, to_log = TRUE)
                                      

## Sample a incubation period (again using the default for covid)
incubation_def <- EpiNow::lognorm_dist_def(mean = EpiNow::covid_incubation_period[1, ]$mean,
                                          mean_sd = EpiNow::covid_incubation_period[1, ]$mean_sd,
                                          sd = EpiNow::covid_incubation_period[1, ]$sd,
                                          sd_sd = EpiNow::covid_incubation_period[1, ]$sd_sd,
                                          max_value = 30, samples = 1)

## Simulate cases with a decrease in reporting at weekends and an increase on Monday                                     
simulated_cases <- simulate_cases(rts, initial_cases = 100 , initial_date = as.Date("2020-03-01"),
                    generation_interval = generation_interval, delay_def = delay_def, 
                   incubation_def = incubation_def, 
                   reporting_effect = c(1.1, rep(1, 4), 0.95, 0.95))
                   
print(simulated_cases)



## Simulate cases with a weekly reporting effect and stochastic noise in reporting (beyond the delay)                                  
simulated_cases <- simulate_cases(rts, initial_cases = 100 , initial_date = as.Date("2020-03-01"),
                    generation_interval = generation_interval, delay_def = delay_def, 
                   incubation_def = incubation_def, 
                   reporting_effect = c(1.1, rep(1, 4), 0.95, 0.95),
                   reporting_model = function(n) {
                      out <- suppressWarnings(rnbinom(length(n), as.integer(n), 0.5))
                      out <- ifelse(is.na(out), 0, out)
                      })
                   
print(simulated_cases)

Convert a linelist into a nested 'data.table“ of linelists by day

Description

Convert a linelist into a nested 'data.table“ of linelists by day

Usage

split_linelist_by_day(linelist = NULL)

Arguments

linelist

Dataframe with the following variables date_onset_symptoms and date_confirmation

Value

A nested data.table with a linelist per day (daily_observed_linelist) variable containing date_onset and date_report and a date_report variable


Summarise a nowcast

Description

Summarise a nowcast

Usage

summarise_cast(nowcast)

Arguments

nowcast

A dataframe as produced by nowcast_pipeline

Value

A summarised dataframe


Summarise Realtime Results

Description

Summarise Realtime Results

Usage

summarise_results(
  regions = NULL,
  results_dir = "results",
  target_date = NULL,
  region_scale = "Region"
)

Arguments

regions

A character string containing the list of regions to extract results for (must all have results for the same target date).

results_dir

A character string indicating the location of the results directory to extract results from.

target_date

A character string indicating the target date to extract results for. All regions must have results for this date.

region_scale

A character string indicating the name to give the regions being summarised.

Value

A list of summary data


Summarise rt and cases as a csv

Description

Summarise rt and cases as a csv

Usage

summarise_to_csv(
  results_dir = NULL,
  summary_dir = NULL,
  type = "country",
  date = NULL
)

Arguments

results_dir

Character string indicating the directory from which to extract results

summary_dir

Character string the directory into which to save results

type

Character string, the region identifier to apply

date

A Character string (in the format "yyyy-mm-dd") indicating the date to extract data for. Defaults to "latest" which finds the latest results available.


Custom Map Theme

Description

Custom Map Theme

Usage

theme_map(
  map = NULL,
  continuous = FALSE,
  variable_label = NULL,
  trans = "identity",
  fill_labels = NULL,
  scale_fill = NULL,
  breaks = NULL,
  ...
)

Arguments

map

ggplot2 map object

continuous

Logical defaults to FALSE. Is the fill variable continuous.

variable_label

A character string indicating the variable label to use. If not supplied then the underlying variable name is used.

trans

A character string specifying the transform to use on the specified metric. Defaults to no transform ("identity"). Other options include log scaling ("log") and log base 10 scaling ("log10"). For a complete list of options see ggplot2::continous_scale.

fill_labels

A function to use to allocate legend labels. An example (used below) is scales::percent, which can be used for percentage data.

scale_fill

Function to use for scaling the fill. Defaults to a custom ggplot2::scale_fill_manual

breaks

Breaks to use in legend. Defaults to ggplot2::waiver.

...

Additional arguments passed to the scale_fill function

Value

A ggplot2 object

Examples

## Code 
theme_map