# NOT RUN {
# Get example case counts
reported_cases <- EpiNow2::example_confirmed[1:50]
# Add a dummy breakpoint (used only when optionally estimating breakpoints)
reported_cases <- reported_cases[, breakpoint := data.table::fifelse(date == as.Date("2020-03-16"),
1, 0)]
# Set up example generation time
generation_time <- list(mean = EpiNow2::covid_generation_times[1, ]$mean,
mean_sd = EpiNow2::covid_generation_times[1, ]$mean_sd,
sd = EpiNow2::covid_generation_times[1, ]$sd,
sd_sd = EpiNow2::covid_generation_times[1, ]$sd_sd,
max = 30)
# Set delays between infection and case report
# (any number of delays can be specifed here)
incubation_period <- list(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 = 30)
reporting_delay <- list(mean = log(5),
mean_sd = log(2),
sd = log(2),
sd_sd = log(1.5),
max = 30)
# Run model with default settings
def <- estimate_infections(reported_cases, family = "negbin",
generation_time = generation_time,
delays = list(incubation_period, reporting_delay),
samples = 1000, warmup = 200, cores = ifelse(interactive(), 4, 1),
chains = 4, estimate_rt = TRUE, verbose = FALSE, return_fit = TRUE)
# Plot output
plots <- report_plots(summarised_estimates = def$summarised,
reported = reported_cases)
plots$summary
# Run model with Rt fixed into the future
fixed_rt <- estimate_infections(reported_cases, family = "negbin",
generation_time = generation_time,
delays = list(incubation_period, reporting_delay),
samples = 1000, warmup = 200, cores = ifelse(interactive(), 4, 1),
chains = 4, estimate_rt = TRUE, fixed_future_rt = TRUE,
return_fit = TRUE)
# Plot output
plots <- report_plots(summarised_estimates = fixed_rt$summarised,
reported = reported_cases)
plots$summary
# Run the model with default settings on a later snapshot of
# data (use burn_in here to remove the first week of
# estimates that may be impacted by this most).
snapshot_cases <- EpiNow2::example_confirmed[80:130]
snapshot <- estimate_infections(snapshot_cases, family = "negbin",
generation_time = generation_time,
delays = list(incubation_period, reporting_delay),
samples = 1000, warmup = 400, cores = ifelse(interactive(), 4, 1),
chains = 4, estimate_rt = TRUE, verbose = FALSE, return_fit = TRUE,
burn_in = 7)
# Plot output
plots <- report_plots(summarised_estimates = snapshot$summarised,
reported = snapshot_cases)
plots$summary
## Run model with stationary Rt assumption (likely to provide biased real-time estimates)
stat <- estimate_infections(reported_cases, family = "negbin",
generation_time = generation_time,
delays = list(incubation_period, reporting_delay),
samples = 1000, warmup = 200, cores = ifelse(interactive(), 4, 1),
chains = 4, estimate_rt = TRUE, stationary = TRUE,
verbose = FALSE, return_fit = TRUE)
# Plot output
plots <- report_plots(summarised_estimates = stat$summarised,
reported = reported_cases)
plots$summary
# Run model with fixed Rt assumption
fixed <- estimate_infections(reported_cases, family = "negbin",
generation_time = generation_time,
delays = list(incubation_period, reporting_delay),
samples = 1000, warmup = 200, cores = ifelse(interactive(), 4, 1),
chains = 4, estimate_rt = TRUE, fixed = TRUE,
verbose = FALSE, return_fit = TRUE)
# Plot output
plots <- report_plots(summarised_estimates = fixed$summarised,
reported = reported_cases)
plots$summary
# Run model with no delays
no_delay <- estimate_infections(reported_cases, family = "negbin",
generation_time = generation_time,
samples = 1000, warmup = 200,
cores = ifelse(interactive(), 4, 1),
chains = 4, estimate_rt = TRUE,
verbose = FALSE, return_fit = TRUE)
# Plot output
plots <- report_plots(summarised_estimates = no_delay$summarised,
reported = reported_cases)
plots$summary
# Run model with breakpoints
bkp <- estimate_infections(reported_cases, family = "negbin",
generation_time = generation_time,
delays = list(incubation_period, reporting_delay),
samples = 1000, warmup = 200, cores = ifelse(interactive(), 4, 1),
chains = 4, estimate_rt = TRUE, estimate_breakpoints = TRUE,
verbose = FALSE, return_fit = TRUE)
# Plot output
plots <- report_plots(summarised_estimates = bkp$summarised,
reported = reported_cases)
plots$summary
# Run model with breakpoints but with constrained non-linear change over time
# This formulation may increase the apparent effect of the breakpoint but needs to be tested using
# model fit criteria (i.e LFO).
cbkp <- estimate_infections(reported_cases, family = "negbin",
generation_time = generation_time,
gp = list(basis_prop = 0.3, boundary_scale = 2,
lengthscale_mean = 20, lengthscale_sd = 1),
delays = list(incubation_period, reporting_delay),
samples = 1000, warmup = 200, cores = ifelse(interactive(), 4, 1),
chains = 4, estimate_rt = TRUE, estimate_breakpoints = TRUE,
verbose = FALSE, return_fit = TRUE)
# Plot output
plots <- report_plots(summarised_estimates = cbkp$summarised,
reported = reported_cases)
plots$summary
# Pull out breakpoint summary
cbkp$summarised[variable == "breakpoints"]
# Run model with breakpoints but otherwise static Rt
# This formulation may increase the apparent effect of the breakpoint but needs to be tested using
# model fit criteria (i.e LFO).
fbkp <- estimate_infections(reported_cases, family = "negbin",
generation_time = generation_time,
delays = list(incubation_period, reporting_delay),
samples = 1000, warmup = 200, cores = ifelse(interactive(), 4, 1),
chains = 4, estimate_breakpoints = TRUE, fixed = TRUE,
verbose = FALSE, return_fit = TRUE)
# Plot output
plots <- report_plots(summarised_estimates = fbkp$summarised,
reported = reported_cases)
plots$summary
# Pull out breakpoint summary
fbkp$summarised[variable == "breakpoints"]
# Run model without Rt estimation (just backcalculation)
backcalc <- estimate_infections(reported_cases, family = "negbin",
generation_time = generation_time,
delays = list(incubation_period, reporting_delay),
samples = 1000, warmup = 200, cores = ifelse(interactive(), 4, 1),
chains = 4, estimate_rt = FALSE, verbose = FALSE, return_fit = TRUE)
# plot just infections as report_plots does not support the backcalculation only model
plot_estimates(estimate = backcalc$summarised[variable == "infections"],
reported = reported_cases, ylab = "Cases")
# }
Run the code above in your browser using DataLab