-
Notifications
You must be signed in to change notification settings - Fork 31
/
truncated.R
64 lines (59 loc) · 1.49 KB
/
truncated.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
library("EpiNow2")
#' Apply truncation to a data set
#'
#' @param index Index from which to truncate
#' @param data Data set
#' @param dist Truncation distribution
#' @importFrom stats dgamma dlnorm
#'
#' @return A truncated data set
#' @keywords internal
apply_truncation <- function(index, data, dist) {
set.seed(index)
if (get_distribution(dist) == "lognormal") {
dfunc <- dlnorm
} else {
dfunc <- dgamma
}
cmf <- cumsum(
dfunc(
seq_len(max(dist) + 1),
rnorm(1,
get_parameters(get_parameters(dist)$meanlog)$mean,
get_parameters(get_parameters(dist)$meanlog)$sd
),
rnorm(1,
get_parameters(get_parameters(dist)$sdlog)$mean,
get_parameters(get_parameters(dist)$sdlog)$sd
)
)
)
cmf <- cmf / cmf[max(dist) + 1]
cmf <- rev(cmf)[-1]
trunc_data <- data.table::copy(data)[1:(.N - index)]
trunc_data[
(.N - length(cmf) + 1):.N, confirm := as.integer(confirm * cmf)
]
return(trunc_data)
}
# get example case counts
reported_cases <- example_confirmed[1:60]
# define example truncation distribution (note not integer adjusted)
trunc <- LogNormal(
meanlog = Normal(0.9, 0.1),
sdlog = Normal(0.6, 0.1),
max = 10
)
# Use the make_truncated_data() function to generate example data for
# an example using estimate_truncation()
example_truncated <- purrr::map(
seq(20, 0, -5),
apply_truncation,
data = reported_cases,
dist = trunc
)
usethis::use_data(
example_truncated,
overwrite = TRUE,
compress = "xz"
)