
Approximate Sampling a Distribution using Counts
sample_approx_dist(
cases = NULL,
dist_fn = NULL,
max_value = 120,
earliest_allowed_mapped = NULL,
direction = "backwards",
type = "sample",
truncate_future = TRUE
)
A dataframe of cases (in date order) with the following variables:
date
and cases
.
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.
Numeric, maximum value to allow. Defaults to 120 days
A character string representing a date ("2020-01-01"). Indicates the earliest allowed mapped value.
Character string, defato "backwards". Direction in which to map cases. Supports either "backwards" or "forwards".
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.
Logical, should cases be truncated if they occur after the first date reported in the data.
Defaults to TRUE
.
A data.table
of cases by date of onset
# NOT RUN {
cases <- EpiNow2::example_confirmed
cases <- cases[, cases := as.integer(confirm)]
## 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:100,
~ 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")
# }
Run the code above in your browser using DataLab