# NOT RUN {
if(requireNamespace("EpiSoon")){
# Define an initial rt vector
rts <- c(rep(2, 20), (2 - 1:15 * 0.1), rep(0.5, 10))
rts
# Use the mean default generation interval for covid
# Generation time
generation_defs <- EpiNow2::gamma_dist_def(mean = generation_time$mean,
mean_sd = generation_time$mean_sd,
sd = generation_time$sd,
sd_sd = generation_time$sd_sd,
max_value = generation_time$max, samples = 1)
generate_pdf <- function(dist, max_value) {
## Define with 0 day padding
sample_fn <- function(n, ...) {
c(0, EpiNow2::dist_skel(n = n,
model = dist$model[[1]],
params = dist$params[[1]],
max_value = dist$max_value[[1]] - 1,
...))
}
dist_pdf <- sample_fn(0:(max_value - 1), dist = TRUE, cum = FALSE)
return(dist_pdf)}
generation_pdf <- generate_pdf(generation_defs, max_value = generation_defs$max)
# Sample a report delay as a lognormal
delay_def <- EpiNow2::lognorm_dist_def(mean = 5, mean_sd = 1,
sd = 3, sd_sd = 1, max_value = 30,
samples = 1, to_log = TRUE)
# Sample a incubation period (again using the default for covid)
incubation_def <- EpiNow2::lognorm_dist_def(mean = EpiNow2::covid_incubation_period[1, ]$mean,
mean_sd = EpiNow2::covid_incubation_period[1, ]$mean_sd,
sd = EpiNow2::covid_incubation_period[1, ]$sd,
sd_sd = EpiNow2::covid_incubation_period[1, ]$sd_sd,
max_value = 30, samples = 1)
# Simulate cases with a decrease in reporting at weekends
# and an increase on Monday
simulated_cases <- simulate_cases(rts, initial_cases = 100 ,
initial_date = as.Date("2020-03-01"),
generation_interval = generation_pdf,
delay_defs = list(incubation_def, delay_def),
reporting_effect = c(1.1, rep(1, 4), 0.95, 0.95))
print(simulated_cases)
# Simulate cases with a weekly reporting effect and
# stochastic noise in reporting (beyond the delay)
simulated_cases <- simulate_cases(rts, initial_cases = 100 ,
initial_date = as.Date("2020-03-01"),
generation_interval = generation_pdf,
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(simulated_cases)
}
# }
Run the code above in your browser using DataLab