Skip to contents

[Stable] Maps from cases by date of infection to date of report via date of onset.

Usage

adjust_infection_to_report(
  infections,
  delay_defs,
  reporting_model,
  reporting_effect,
  type = "sample",
  truncate_future = TRUE
)

Arguments

infections

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

delay_defs

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

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.

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.

type

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

truncate_future

Logical, should cases be truncated 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.

Author

Sam Abbott

Examples

# \donttest{
# define example cases
cases <- data.table::copy(example_confirmed)[, cases := as.integer(confirm)]

# define a single report delay distribution
delay_def <- 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 <- lognorm_dist_def(
  mean = incubation_periods[1, ]$mean,
  mean_sd = incubation_periods[1, ]$mean_sd,
  sd = incubation_periods[1, ]$sd,
  sd_sd = incubation_periods[1, ]$sd_sd,
  max_value = 30, samples = 1
)

# simple mapping
report <- adjust_infection_to_report(
 cases, delay_defs = list(incubation_def, delay_def)
)
print(report)
#>            date cases
#>   1: 2020-02-24     2
#>   2: 2020-02-25     4
#>   3: 2020-02-26    18
#>   4: 2020-02-27    20
#>   5: 2020-02-28    29
#>  ---                 
#> 124: 2020-06-26   244
#> 125: 2020-06-27   240
#> 126: 2020-06-28   252
#> 127: 2020-06-29   274
#> 128: 2020-06-30   267

# mapping with a weekly reporting effect
report_weekly <- adjust_infection_to_report(
  cases,
  delay_defs = list(incubation_def, delay_def),
  reporting_effect = c(1.1, rep(1, 4), 0.95, 0.95)
)
print(report_weekly)
#>            date cases
#>   1: 2020-02-24     1
#>   2: 2020-02-25     1
#>   3: 2020-02-26     9
#>   4: 2020-02-27    15
#>   5: 2020-02-28    26
#>  ---                 
#> 124: 2020-06-26   231
#> 125: 2020-06-27   209
#> 126: 2020-06-28   233
#> 127: 2020-06-29   234
#> 128: 2020-06-30   250

# map using a deterministic median shift for both delays
report_median <- adjust_infection_to_report(cases,
  delay_defs = list(incubation_def, delay_def),
  type = "median"
)
print(report_median)
#>            date confirm cases
#>   1: 2020-03-01      14    14
#>   2: 2020-03-02      62    62
#>   3: 2020-03-03      53    53
#>   4: 2020-03-04      97    97
#>   5: 2020-03-05      93    93
#>  ---                         
#> 126: 2020-07-04     296   296
#> 127: 2020-07-05     255   255
#> 128: 2020-07-06     175   175
#> 129: 2020-07-07     174   174
#> 130: 2020-07-08     126   126

# map with a weekly reporting effect and stochastic reporting model
report_stochastic <- adjust_infection_to_report(
  cases,
  delay_defs = list(incubation_def, delay_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-02-23     0
#>   2: 2020-02-24     0
#>   3: 2020-02-25     0
#>   4: 2020-02-26     0
#>   5: 2020-02-27    18
#>  ---                 
#> 125: 2020-06-26   229
#> 126: 2020-06-27   211
#> 127: 2020-06-28   247
#> 128: 2020-06-29   274
#> 129: 2020-06-30   259
# }