¶
Install epidemicdates from GitHub and impute a line list in a few lines of R.
The R package epidemicdates holds the core line-list tools.
It gives the same results as the Python package and the JavaScript module that drives the interactive page.
It does not include the simulation study.
- Source:
r/ - Every function: API reference
- The maths: Methods
- What it relies on: Assumptions
Warning
This package was generated by an LLM and has not been reviewed by a person. Sam Abbott has only checked the interactive page. Treat the results as exploratory.
Install¶
The package needs base R and stats only.
Quick start on your own data¶
This reads the example line list and imputes the missing onsets.
Run it from the repository root, or change the path.
impute_linelist() takes Date vectors and counts days from the earliest date.
Empty onset dates become NA.
library(epidemicdates)
linelist <- read.csv("docs/data/example-linelist.csv")
onset <- as.Date(linelist$onset_date)
report <- as.Date(linelist$report_date)
res <- impute_linelist(onset, report)
res$origin
res$cutoff
res$dropped
Day 0 is 3 May 2026 and the cut-off is the last report day, day 97. No cases were dropped.
Sum the daily output to weeks with weekly() and print it by method.
weeks <- data.frame(
week_from = res$origin + 7 * (seq_along(weekly(res$complete)) - 1),
complete = weekly(res$complete),
lapply(res$imputed, function(x) round(weekly(x), 1))
)
weeks
week_from complete backsampling mean em
1 2026-05-03 5 6.8 7 7.0
2 2026-05-10 12 14.9 15 15.4
3 2026-05-17 12 16.8 14 15.9
4 2026-05-24 20 27.9 31 26.5
5 2026-05-31 32 44.7 38 43.5
6 2026-06-07 62 81.9 84 81.3
7 2026-06-14 88 122.9 119 121.4
8 2026-06-21 151 200.2 197 203.2
9 2026-06-28 182 251.1 250 246.3
10 2026-07-05 256 353.7 351 346.2
11 2026-07-12 469 595.8 609 614.2
12 2026-07-19 549 697.1 708 723.5
13 2026-07-26 334 455.2 488 440.4
14 2026-08-02 119 174.0 132 159.2
These are the same numbers as in Python. The worked example explains each step.
Add a forward delay and correct for truncation¶
Pass a delay pmf as delay to add the forward draw, fwd.
EM then holds that delay fixed instead of estimating one.
delay_pmf() gives a double interval censored daily Gamma pmf.
Element d + 1 is the probability of a delay of d days.
pmf <- delay_pmf(mean = 8, sd = 8)
round(pmf[1:8], 3)
res <- impute_linelist(onset, report, delay = pmf)
names(res$imputed)
round(unlist(res$imputed_before_day0), 2)
[1] 0.060 0.111 0.098 0.086 0.076 0.067 0.059 0.052
[1] "backsampling" "mean" "em" "fwd"
backsampling mean fwd
1.09 1.00 1.84
imputed_before_day0 counts the expected missing-onset cases each method places before day 0.
They are not in imputed.
Recent onsets are still being reported at the cut-off.
correct_truncation() divides each day by the expected fraction reported, here using em_delay, the delay EM used.
As delay was given, that is pmf.
Days with less than 5% reported become NA.
nowcast <- correct_truncation(res$imputed$em, res$em_delay, res$cutoff)
round(tail(weekly(nowcast), 4), 1)
round(tail(weekly(res$imputed$em), 4), 1)
The correction assumes the delay is known. It does not carry uncertainty in the delay.
Changed behaviour
Before this version, em estimated its own delay even when delay was given.
It now holds the given delay fixed, as in the other languages.
Leave out delay for the EM estimate.
Delays that change over time¶
EM can use the delay in three ways:
- one static delay, estimated (the default);
- one delay per period of onset days, estimated, with
delay_period, each shrunk towards the one before it with strengthdelay_shrinkage; - a known delay, passed as
delay: one pmf, or a matrix with one row per onset day for a delay that changes over time, used as given.
correct_truncation() takes a matrix with one delay per onset day, so each day can use its own period's delay.
The nowcast with quantiles is in Python and JavaScript only.
This uses the second example line list, whose mean delay falls from 12 to 5 days, and the true delay of each onset day, known because the data are simulated. It prints the estimated mean delay of each 14-day period, then the point nowcast of the last four weeks with each of the three delays. This continues the quick start above.
rows2 <- read.csv("docs/data/example-linelist-changing-delays.csv")
onset2 <- as.Date(rows2$onset_date)
report2 <- as.Date(rows2$report_date)
true_rows <- read.csv("docs/data/example-changing-delays-true-delay.csv")
true_delay <- as.matrix(true_rows[, paste0("p", 0:60)])
static <- impute_linelist(onset2, report2)
period <- impute_linelist(onset2, report2, delay_period = 14)
plug <- impute_linelist(onset2, report2, delay = true_delay)
round(as.vector(period$em_delay_by_period %*% 0:60), 1)
in_period <- findInterval(seq(0, period$cutoff), period$delay_period_start)
nowcast <- function(fit, delay) {
x <- correct_truncation(fit$imputed$em, delay, fit$cutoff, min_cover = 0.01)
round(tail(weekly(x), 4), 1)
}
rbind(
static = nowcast(static, static$em_delay),
periods = nowcast(period, period$em_delay_by_period[in_period, ]),
`plug-in` = nowcast(plug, true_delay)
)
[1] 8.5 10.2 12.3 11.9 9.6 8.9 7.1 5.9 5.4
[,1] [,2] [,3] [,4]
static 487.5 458.0 421.2 444.1
periods 466.1 428.6 361.0 297.8
plug-in 460.7 420.6 356.5 320.4
The true mean delay falls from 12 days in the first periods to 5 in the last. The eventual true onsets in the last four weeks are 456, 414, 374 and 317. The static delay is too long for recent onsets, so its nowcast is up to 40% too high. Per-period delays bring the error down to 4% on average, and the true delay to 2%.
Delays that change over time in the worked example goes through this in all four languages, and the Methods page has the maths.
Main functions¶
impute_linelist(onset, report, cutoff = NULL, delay = NULL, max_delay = 60, n_iter = 150, delay_period = NULL, delay_shrinkage = 10) takes:
onset: onset day per case,NAwhere missing, as whole numbers or aDatevector;report: report day per case, never missing;cutoff: the last report day to use, by default the latest;delay: an optional known delay, one pmf or a matrix with one pmf per onset day, which addsfwdand whichemholds fixed;max_delay: the longest delay kept;n_iter: the number of EM iterations;delay_period: the length in days of periods that each get their own EM delay;delay_shrinkage: how strongly each period's delay is pulled towards the previous period's.
It returns a list.
imputed holds daily onset counts for backsampling, mean and em, and fwd if delay is given.
Element i is onset day i - 1.
complete holds the cases with a known onset and missing_by_report the missing-onset cases by report day.
empirical_delay is the pooled delay pmf and em_delay the delay EM used.
em_delay_by_period has one row per period and delay_period_start gives the first day of each period.
dropped counts cases that could not be used.
origin is the date of day 0 for Date input.
The methods are:
backsampling: \(P(s \mid t) = g(t - s)\), where \(g\) is the pooled empirical delay pmf of complete cases;mean: \(s = t - \operatorname{round}\big(\sum_d d \, g(d)\big)\), with halves rounded up;fwd: \(P(s \mid t) = f(t - s)\) for the given delay \(f\);em: EM back-projection, \(P(s \mid t) \propto \lambda(s) f_s(t - s)\), with the onset curve \(\lambda\) estimated jointly with a static delay, or one delay per period, allowing for right truncation; a given delay is held fixed.
All outputs are expected allocations, so they need not be whole numbers. All methods assume onsets are missing at random given the report day.
The other functions are:
delay_pmf(mean, sd, dmax = 60): the double interval censored daily Gamma pmf, \(P(d) = G(d + 1) - 2 G(d) + G(d - 1)\) with \(G(x) = x F(x; k, \theta) - k \theta F(x; k + 1, \theta)\), truncated atdmaxand renormalised;correct_truncation(counts, delay, cutoff): divides each onset day \(s\) by \(F_s(T - s)\), with days under 5% reported set toNA;delayis one pmf or one per day;weekly(daily, start = 0): sums daily counts to weeks.
Cross-language tests¶
The R tests compare with the reference outputs in tests/fixtures/cross-language.json, written by the Python package.
delay_pmf() matches to within \(10^{-10}\).
backsampling and mean match exactly, and em matches to within 2% on weeks with at least 50 cases.
The same holds with delay_period, and with a supplied delay matrix, where fwd also matches exactly.
See also¶
- Python guide for the simulation study.
- JavaScript guide for the same methods in the browser or Node.
- Methods for the maths.
- Assumptions for what the results rely on.