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

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)
#> date cases #> 1: 2020-01-16 1 #> 2: 2020-01-17 1 #> 3: 2020-01-18 0 #> 4: 2020-01-19 0 #> 5: 2020-01-20 0 #> 6: 2020-01-21 0 #> 7: 2020-01-22 1 #> 8: 2020-01-23 0 #> 9: 2020-01-24 0 #> 10: 2020-01-25 3 #> 11: 2020-01-26 0 #> 12: 2020-01-27 1 #> 13: 2020-01-28 0 #> 14: 2020-01-29 0 #> 15: 2020-01-30 0 #> 16: 2020-01-31 0 #> 17: 2020-02-01 0 #> 18: 2020-02-02 0 #> 19: 2020-02-03 1 #> 20: 2020-02-04 1 #> 21: 2020-02-05 0 #> 22: 2020-02-06 0 #> 23: 2020-02-07 0 #> 24: 2020-02-08 4 #> 25: 2020-02-09 4 #> 26: 2020-02-10 6 #> 27: 2020-02-11 3 #> 28: 2020-02-12 1 #> 29: 2020-02-13 4 #> 30: 2020-02-14 7 #> 31: 2020-02-15 4 #> 32: 2020-02-16 14 #> 33: 2020-02-17 13 #> 34: 2020-02-18 15 #> 35: 2020-02-19 16 #> 36: 2020-02-20 20 #> 37: 2020-02-21 28 #> 38: 2020-02-22 21 #> 39: 2020-02-23 35 #> 40: 2020-02-24 36 #> 41: 2020-02-25 44 #> 42: 2020-02-26 41 #> 43: 2020-02-27 55 #> 44: 2020-02-28 62 #> 45: 2020-02-29 70 #> 46: 2020-03-01 63 #> 47: 2020-03-02 80 #> 48: 2020-03-03 97 #> 49: 2020-03-04 102 #> 50: 2020-03-05 98 #> 51: 2020-03-06 83 #> 52: 2020-03-07 146 #> 53: 2020-03-08 129 #> 54: 2020-03-09 150 #> 55: 2020-03-10 164 #> 56: 2020-03-11 177 #> 57: 2020-03-12 188 #> 58: 2020-03-13 221 #> 59: 2020-03-14 246 #> date cases
## 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)
#> date cases #> 1: 2020-01-16 1 #> 2: 2020-01-17 0 #> 3: 2020-01-18 0 #> 4: 2020-01-19 0 #> 5: 2020-01-20 0 #> 6: 2020-01-21 1 #> 7: 2020-01-22 0 #> 8: 2020-01-23 1 #> 9: 2020-01-24 1 #> 10: 2020-01-25 0 #> 11: 2020-01-26 0 #> 12: 2020-01-27 0 #> 13: 2020-01-28 0 #> 14: 2020-01-29 0 #> 15: 2020-01-30 1 #> 16: 2020-01-31 0 #> 17: 2020-02-01 0 #> 18: 2020-02-02 0 #> 19: 2020-02-03 1 #> 20: 2020-02-04 0 #> 21: 2020-02-05 2 #> 22: 2020-02-06 2 #> 23: 2020-02-07 2 #> 24: 2020-02-08 4 #> 25: 2020-02-09 2 #> 26: 2020-02-10 1 #> 27: 2020-02-11 6 #> 28: 2020-02-12 10 #> 29: 2020-02-13 8 #> 30: 2020-02-14 9 #> 31: 2020-02-15 1 #> 32: 2020-02-16 6 #> 33: 2020-02-17 13 #> 34: 2020-02-18 17 #> 35: 2020-02-19 25 #> 36: 2020-02-20 21 #> 37: 2020-02-21 28 #> 38: 2020-02-22 21 #> 39: 2020-02-23 32 #> 40: 2020-02-24 34 #> 41: 2020-02-25 40 #> 42: 2020-02-26 52 #> 43: 2020-02-27 49 #> 44: 2020-02-28 53 #> 45: 2020-02-29 70 #> 46: 2020-03-01 63 #> 47: 2020-03-02 61 #> 48: 2020-03-03 91 #> 49: 2020-03-04 87 #> 50: 2020-03-05 108 #> 51: 2020-03-06 106 #> 52: 2020-03-07 121 #> 53: 2020-03-08 131 #> 54: 2020-03-09 181 #> 55: 2020-03-10 144 #> 56: 2020-03-11 166 #> 57: 2020-03-12 184 #> 58: 2020-03-13 220 #> 59: 2020-03-14 228 #> date cases
## 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)
#> date cases #> 1: 2020-01-17 3 #> 2: 2020-01-18 0 #> 3: 2020-01-19 0 #> 4: 2020-01-20 2 #> 5: 2020-01-21 0 #> 6: 2020-01-22 0 #> 7: 2020-01-23 0 #> 8: 2020-01-24 0 #> 9: 2020-01-25 0 #> 10: 2020-01-26 0 #> 11: 2020-01-27 0 #> 12: 2020-01-28 0 #> 13: 2020-01-29 0 #> 14: 2020-01-30 0 #> 15: 2020-01-31 0 #> 16: 2020-02-01 0 #> 17: 2020-02-02 0 #> 18: 2020-02-03 0 #> 19: 2020-02-04 0 #> 20: 2020-02-05 3 #> 21: 2020-02-06 9 #> 22: 2020-02-07 0 #> 23: 2020-02-08 1 #> 24: 2020-02-09 0 #> 25: 2020-02-10 6 #> 26: 2020-02-11 5 #> 27: 2020-02-12 1 #> 28: 2020-02-13 5 #> 29: 2020-02-14 7 #> 30: 2020-02-15 4 #> 31: 2020-02-16 15 #> 32: 2020-02-17 16 #> 33: 2020-02-18 13 #> 34: 2020-02-19 20 #> 35: 2020-02-20 22 #> 36: 2020-02-21 19 #> 37: 2020-02-22 16 #> 38: 2020-02-23 29 #> 39: 2020-02-24 37 #> 40: 2020-02-25 38 #> 41: 2020-02-26 39 #> 42: 2020-02-27 40 #> 43: 2020-02-28 40 #> 44: 2020-02-29 62 #> 45: 2020-03-01 37 #> 46: 2020-03-02 89 #> 47: 2020-03-03 54 #> 48: 2020-03-04 102 #> 49: 2020-03-05 103 #> 50: 2020-03-06 132 #> 51: 2020-03-07 106 #> 52: 2020-03-08 131 #> 53: 2020-03-09 158 #> 54: 2020-03-10 188 #> 55: 2020-03-11 206 #> 56: 2020-03-12 139 #> 57: 2020-03-13 220 #> 58: 2020-03-14 248 #> date cases