The goal of assessEpidemicCurves is to model epidemic curves under heterogeneous disease reproduction, providing estimates for the time-varying reproduction number and assessing epidemic curves for evidence of superspreading. This package accompanies Meagher & Friel (2021)
You can install the development version of assessEpidemicCurves from GitHub with:
# install.packages("devtools")
devtools::install_github("jpmeagher/assessEpidemicCurves")
library(assessEpidemicCurves)
library(rstan)
library(loo)
library(ggplot2)
library(dplyr)
library(magrittr)
library(lubridate)
library(EpiEstim)
library(knitr)
library(reshape2)
library(HDInterval)
library(latex2exp)
library(matrixStats)
As a case study we include the epidemic curve of confirmed COVID-19 cases in the Republic of Ireland from March 1, 2020 to 28 February, 2021. Cases are ordered by epidemiological date. This is either the date of onset of symptoms, date of diagnosis, laboratory specimen collection date, laboratory received date, laboratory reported date or the notification date.

Analyses are based on a hierarchical model for the daily incidence count of COVID-19
where
is the epidemic curve seeded by
,
is the rate at which cases are imported,
is the generation interval pmf,
is the disease momentum up to day
,
is the case dispersion
parameter and
are the cohort reproduction numbers.
This model is underpinned by a branching process model for secondary infections such that
where
is the number of secondary infections arising from the
index case on day
with
an individual reproduction number
.
Within this model,
and
.
This links to the model for
via
Thus, the hierarchical model allows for heterogeneous disease
reproduction resulting in over dispersed distributions of secondary
infections, that is, superspreading may be a feature of the epidemic.
Note that
and so this model admits homogeneous disease reproduction as a special
case.
In order to fit this generative model to data, we fix import rates
,
the generation interval pmf
,
and case dispersion parameter
. We then propose a
Gaussian process prior for
such that
where amplitude
and length-scale
are specified a priori.
Alternatively, we can treat either or both of
and
as unknown parameters
with prior distributions defined by
Care should be taken when specifying these prior distributions to ensure
that they are sufficiently informative to ensure stability for the
sampling scheme. For example
and
provide weakly informative priors. Note that full posterior inference
over
scales
with
and so posterior inference for this quantity is particularly expensive.
The model described here can be computationally expensive to implement and so we restrict our analysis to a subset of the data. We examine reported COVID-19 cases from October 6 to November 30 2020, allowing the 5 days from October 1 to October 5 inclusive to seed the epidemic.
We assume that
for all
and that
is proportional to a discretised gamma distribution such that generation
intervals have a mean of 5 days, standard deviation of 2.5, and a
maximum of 21 days. The Gaussian process prior for
is specified by
and
.
We fit 3 models to the data.
assumes that
.
assumes homogeneous disease reproduction where
.
The third model,
,
fits Wallinga & Teunis’ branching process model using the
EpiEstim
package, where we take the 3-day moving average cohort reproductiion
number.
We smooth over day-of-the-week effects in the epidemic curve by taking
to be the 7-day
moving average of reported cases on day
and assume that the
import rate is 1, where applicable.
first_day <- dmy(01102020)
last_day <- dmy(30112020)
df <- covid_incidence_roi_epidemiological_date %>%
mutate(ma_count = stats::filter(count, rep(1/7, 7)) %>% round) %>%
filter(date >= first_day & date <= last_day)
D <- nrow(df)
# initialise latent variables that we expect to be far from 0
init_list <- lapply(
1:4, function(i) {
initialise_momentum(
epidemic_curve = df$ma_count
)
}
)
# fit heterogeneous disease reproduction (about 1 minute to fit)
M_k <- fit_Rt_lgp(
epidemic_curve = df$ma_count, seed_days = 5,
import_rate = rep(1, D),
generation_interval_mean = 5, generation_interval_sd = 2.5,
generation_interval_length = 21,
gp_amplitude = 1, ls_prior_mean = 17.5,
log_k_prior_mean = 0, log_k_prior_sd = 1,
cores = 4, refresh = 1000,
init = init_list,
control = list(adapt_delta = 0.95, max_treedepth = 15)
)
# fit homogeneous disease reproduction (about 5 minutes to fit)
M_inf <- fit_Rt_lgp(
epidemic_curve = df$ma_count, seed_days = 5,
import_rate = rep(1, D),
generation_interval_mean = 5, generation_interval_sd = 2.5,
generation_interval_length = 21,
gp_amplitude = 1, ls_prior_mean = 17.5,
log_k_prior_mean = Inf,
cores = 4, refresh = 1000,
control = list(adapt_delta = 0.95, max_treedepth = 15)
)
wt <- wallinga_teunis(
incid = df$ma_count,
method = "parametric_si",
config = list(
t_start = (5:(D-2)),
t_end = 7:D,
mean_si = 5,
std_si = 2.5,
n_sim = 100
)
)
M_k and M_inf are stanfit objects and so all the standard tests of
convergence and diagnostics apply. Similarly we can use the summary
and plot methods included with rstan. In particular, we are
interested in the posterior for
in
.
For the period we consider here, we estimate that
.
summary(M_k, pars = "k")$summary
#> mean se_mean sd 2.5% 25% 50%
#> k[1] 0.0417721 0.0005485901 0.01542042 0.01926222 0.03105091 0.03936085
#> 75% 97.5% n_eff Rhat
#> k[1] 0.04983501 0.07947632 790.1259 1.002067
plot(M_k, pars = "k")

Having fit these models to the epidemic curve we can explore the fitted
posterior for
.
Note that heterogeneous disease reproduction results in greater
uncertainty on estimates for
on each day. All
three estimates are in broad agreement. Estimates for
over the final
five days of the period are omitted as these estimates are not supported
by data.
R_k <- rstan::extract(M_k, "R") %>%
as.data.frame() %>%
colMeans()
ci_k <- rstan::extract(M_k, "R") %>%
as.data.frame() %>%
hdi()
R_inf <- rstan::extract(M_inf, "R") %>%
as.data.frame() %>%
colMeans()
ci_inf <- rstan::extract(M_inf, "R") %>%
as.data.frame() %>%
hdi()
y_scalar <- ci_k[, -(1:5)] %>%
max()
data.frame(
date = df$date,
a = R_k,
b = R_inf,
wt = c(rep(NA, 5), wt$R$`Mean(R)`, rep(NA, 1))
) %>%
filter(date <= last_day - 5) %>%
filter(date >= first_day + 5) %>%
reshape2::melt(id.vars = "date") %>%
ggplot() +
theme_classic(base_size = 10) +
theme(legend.position = c(0.875, 0.75)) +
geom_bar(
data = df,
aes(x = date, y = y_scalar * ma_count / max(ma_count)), stat = "identity",
alpha = 0.5
) +
geom_ribbon(
data = data.frame(
date = df$date[6:(D-1)],
hdi = cbind(
lower = wt$R$`Quantile.0.025(R)`,
upper = wt$R$`Quantile.0.975(R)`),
model = "wt"
) %>%
rbind(data.frame(date = df$date, hdi = t(ci_k), model = "a")) %>%
rbind(data.frame(date = df$date, hdi = t(ci_inf), model = "b")) %>%
filter(date <= last_day - 5) %>%
filter(date >= first_day + 5),
aes(x = date, ymin = hdi.lower, ymax = hdi.upper, fill = model),
alpha= 0.25
) +
geom_line(aes(x = date, y = value, color = variable)) +
scale_color_viridis_d(labels = c(bquote("M"[k]), bquote("M"[infinity]), "W&T")) +
scale_fill_viridis_d(labels = c(bquote("M"[k]), bquote("M"[infinity]), "W&T")) +
geom_hline(yintercept = 1, lty = 3) +
scale_y_continuous(
TeX("Reproduction Number $(R_t)$"),
sec.axis = sec_axis(
~ . * max(df$ma_count) / y_scalar,
name = TeX("7 Day Moving Average Incidence $(Y_t)$")
)
) +
labs(
x = "Epidemiological Date",
color = NULL,
fill = NULL
)

Pareteo-smoothed importance sampling (PSIS) for approximate
leave-one-out cross-validation (LOO-CV) provides an easy to implement
measure of model fit. Although time series data are not exchangeable and
violate the assumptions underpinning LOO-CV, PSIS LOO-CV offers an
efficient approach to model comparison. Applying this technique to
and
we find that
provises a better fit to the data.
loo_k <- loo(M_k)
#> Warning: Some Pareto k diagnostic values are too high. See help('pareto-k-diagnostic') for details.
loo_inf <- loo(M_inf)
#> Warning: Some Pareto k diagnostic values are too high. See help('pareto-k-diagnostic') for details.
| | elpd_diff | se_diff | elpd_loo | se_elpd_loo | p_loo | se_p_loo | looic | se_looic | |:-------|-----------:|---------:|----------:|--------------:|-------:|-----------:|-------:|----------:| | model1 | 0.00 | 0.00 | -243.16 | 3.99 | 6.91 | 2.23 | 486.32 | 7.98 | | model2 | -60.22 | 18.38 | -303.38 | 21.65 | 19.80 | 8.59 | 606.77 | 43.31 |
PSIS-LOO model comparison favours
(model1) over
(model2).
We could adopt a more rigorous Leave-Future-Out (LFO) Cross-Validation
(CV) approach to model comparison, estimating the posterior predictive
density for
under
where
and
is the set of model parameters and latent variables up to time
. This amounts to
one-step-ahead prediction (1-SAP) for the epidemic curve.
Given
drawn from
for
we have that
If
observations are required for reliable 1-SAP, then we assess the
performance of
using its LFO expected log predictive density (ELPD)
We refit
for each estimate of
assuming that
such that we only fit to
observations up to time
to obtain the estimate (this becomes
observations when
days seed the
epidemic).
For illustrative purposes, we present code for LFO-CV for the 15 days
from 10-24 December, 2020 with
and
where we compare a model where
to the case where
.
This comparison requires a few minutes to complete.
n_samples <- 4000
N0 <- 5
L <- 21 # lead in days to prediction
first_validation_day <- dmy(10122020)
last_validation_day <- dmy(25122020)
M <- as.numeric(last_validation_day - first_validation_day)
# Get 7-day moving average
df <- covid_incidence_roi_epidemiological_date %>%
mutate(ma_count = stats::filter(count, rep(1/7, 7)) %>% round)
# candidate models
candidate_k <- c(0.1, Inf)
posterior_predictive <- log_lik <- list()
for (j in seq_along(candidate_k)) {
i <- 1
day <- first_validation_day
tmp_posterior_predictive <- tmp_log_lik <- array(dim = c(n_samples, M))
# pb <- txtProgressBar(min = 0, max = M, style = 3)
while (day < last_validation_day) {
df_i <- df %>%
filter(date >= day - (L + N0) & date < day)
fit_i <- fit_Rt_lgp(
epidemic_curve = df_i$ma_count, seed_days = 5,
import_rate = rep(1, (L + N0)),
generation_interval_mean = 5,
generation_interval_sd = 2.5,
generation_interval_length = 21,
gp_amplitude = 1, ls_prior_mean = 17.5,
log_k_prior_mean = log(candidate_k[j]),
ahead = TRUE,
next_day_cases = df$ma_count[df$date == day],
next_day_import_rate = 1, refresh = 0
)
tmp_posterior_predictive[, i] <- unlist(rstan::extract(fit_i, "y_rep_ahead"))
tmp_log_lik[, i] <- unlist(rstan::extract(fit_i, "log_lik_ahead"))
day <- day + 1
# setTxtProgressBar(pb, i)
i <- i + 1
}
posterior_predictive[[j]] <- tmp_posterior_predictive
log_lik[[j]] <- tmp_log_lik
# close(pb)
}
pointwise_elpd <- sapply(
log_lik,
function(x) colLogSumExps(x) - log(n_samples)
)
elpd_lfo <- apply(pointwise_elpd, 2, sum)
se_elpd_lfo <- apply(
pointwise_elpd, 2,
function(x) sqrt(length(x) * var(x))
)
best_fit <- which.max(elpd_lfo)
diff_elpd <- sweep(pointwise_elpd, 1, pointwise_elpd[, best_fit]) %>%
apply(2, sum)
se_diff_elpd <- sweep(pointwise_elpd, 1, pointwise_elpd[, best_fit]) %>%
apply(2, function(x) sqrt(length(x) * var(x)))
| | elpd | se_elpd | elpd_diff | se_elpd_diff | |:-------|-------:|---------:|-----------:|---------------:| | model1 | -80.40 | 3.06 | 0.0 | 0.00 | | model2 | -82.19 | 3.54 | -1.8 | 2.29 |
LFO model comparison of
(model1) and
(model2).
Model comparison for the validation period provides some support for
over
,
indicating that heterogeneity is a feature of the COVID-19 epidemic in
the Republic of Ireland. This suggests that estimates for
by
provide more appropriate uncertainty quantification and should be
preferred to those of
.
The package also includes a simpler prior for
which fits data more efficiently. The histogram estimator for
is defined by
for the set of disjoint intervals
discretising time. The interval
defines the
bin for the estimator where
covers a bin width of
days,
then the histogram estimator for
is defined by
Assuming that
and
,
this model fits the epidemic curve from April 01, 2020 to February 14,
2021 in seconds.
first_day <- dmy(01042020)
last_day <- dmy(14022021)
df <- covid_incidence_roi_epidemiological_date %>%
mutate(ma_count = stats::filter(count, rep(1/7, 7)) %>% round) %>%
filter(date >= first_day & date <= last_day)
D <- nrow(df)
# fit homogeneous disease reproduction
H_inf <- fit_Rt_hist(
epidemic_curve = df$ma_count, seed_days = 5,
import_rate = rep(1, D),
generation_interval_mean = 5, generation_interval_sd = 2.5,
generation_interval_length = 21,
bin_width = 7,
log_k_prior_mean = Inf,
cores = 4, refresh = 1000
)

The LFO framework for model comparison can also be applied to this model
to objectively tune the bin width
. The
model also accommodates heterogeneous disease reproduction given some
fixed value for
.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.