Convolves cases by a PMF function. This function will soon be removed or replaced with a more robust stan implementation.
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
<data.frame>
of cases (in date order) with the following variables:date
andcases
.- 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 earliest 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 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
.
Examples
# \donttest{
cases <- example_confirmed
cases <- cases[, cases := as.integer(confirm)]
print(cases)
#> date confirm cases
#> <Date> <num> <int>
#> 1: 2020-02-22 14 14
#> 2: 2020-02-23 62 62
#> 3: 2020-02-24 53 53
#> 4: 2020-02-25 97 97
#> 5: 2020-02-26 93 93
#> ---
#> 126: 2020-06-26 296 296
#> 127: 2020-06-27 255 255
#> 128: 2020-06-28 175 175
#> 129: 2020-06-29 174 174
#> 130: 2020-06-30 126 126
# total cases
sum(cases$cases)
#> [1] 240581
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)
#> date cases
#> <Date> <num>
#> 1: 2020-02-17 1
#> 2: 2020-02-18 7
#> 3: 2020-02-19 4
#> 4: 2020-02-20 16
#> 5: 2020-02-21 38
#> ---
#> 131: 2020-06-26 222
#> 132: 2020-06-27 163
#> 133: 2020-06-28 132
#> 134: 2020-06-29 81
#> 135: 2020-06-30 44
# check that sum is equal to reported cases
total_onsets <- median(
purrr::map_dbl(
1:10,
~ sum(sample_approx_dist(
cases = cases,
dist_fn = delay_fn
)$cases)
)
)
total_onsets
#> [1] 240447.5
# 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"
)
# }