Skip to content

R API reference

Every exported function of the R package epidemicdates. This page is generated from the roxygen documentation by docs/r/make_reference.R. In R, see ?impute_linelist and so on.

Warning

This code was generated by an LLM. Sam Abbott has only checked the interactive page. Treat the results as exploratory.

epidemicdates-package

epidemicdates: impute missing symptom onsets in line lists

Some line lists fill a missing onset date with the report date minus a delay drawn from the pooled empirical onset-to-report delays of cases with both dates. This is back-sampling. epidemicdates applies back-sampling and three alternatives to a real line list. It is the R version of the core line-list tools in the Python package epidemic_dates and the JavaScript module epidemic-dates.js.

Warning

This code was generated by an LLM. Sam Abbott has only checked the interactive page. This package and its tests have not been reviewed by a person. Treat the results as exploratory.

Main functions

  • impute_linelist() imputes missing onsets by back-sampling, a mean shift, EM back-projection and, given a delay, a forward draw.
  • delay_pmf() gives a double interval censored daily Gamma delay pmf.
  • correct_truncation() corrects daily onset counts for right truncation.
  • weekly() sums daily counts to weeks.

Methods

Take a case with a missing onset, reported on day \(t\), with onset day \(s\) and delay \(d = t - s\). Let \(T\) be the data cut-off and \(g(d)\) the pooled empirical delay pmf of complete cases.

Back-sampling (backsampling) uses \(P(s \mid t) = g(t - s)\).

The mean shift (mean) uses

\[ s = t - \mathrm{round}\left(\sum_d d \, g(d)\right), \]

with halves rounded up.

The forward draw (fwd) uses \(P(s \mid t) = f(t - s)\) for a given forward delay \(f\).

EM back-projection (em) uses \(P(s \mid t) \propto \lambda(s) f(t - s)\), with the onset curve \(\lambda\) and a static delay \(f\) estimated jointly, allowing for right truncation at \(T\).

All methods assume onsets are missing at random given the report day.

References

Repository: https://github.com/seabbs/EpidemicDates

Interactive page: https://seabbs.github.io/EpidemicDates/interactive/

See also

Useful links:

Author

Maintainer: Sam Abbott sam.abbott@lshtm.ac.uk (ORCID) [copyright holder]

Authors:

  • Sam Abbott sam.abbott@lshtm.ac.uk (ORCID) [copyright holder]

impute_linelist

Impute missing onsets in a line list

Takes one onset day and one report day per case and imputes the missing onsets by each method. Cases with a known onset keep it. Outputs are expected allocations, not random draws.

Usage

impute_linelist(
  onset,
  report,
  cutoff = NULL,
  delay = NULL,
  max_delay = 60,
  n_iter = 150,
  delay_period = NULL,
  delay_shrinkage = 10
)

Arguments

onset
Onset day per case, NA where missing. Either whole numbers of days or a Date vector.
report
Report day per case, with no missing values. Either whole numbers of days (for example days since the first report) or a Date vector. If either onset or report is a Date, both are converted to days since the earliest date in either.
cutoff
Data cut-off day \(T\), a non-negative whole number or, for Date input, a Date. Defaults to the last report day.
delay
Optional forward delay pmf. Element d + 1 is the probability of a delay of d days, for example from delay_pmf(). It is normalised to sum to 1. Alternatively a matrix with one such pmf per onset day 0..cutoff in its rows, for a delay that changes over time. A given delay is used as is (a plug-in) by fwd and held fixed by em. If given, imputed also holds fwd. Cannot be combined with delay_period.
max_delay
Longest delay \(D\) kept, a non-negative whole number. Defaults to 60.
n_iter
Number of EM iterations, at least 1. Defaults to 150.
delay_period
Length in days of the periods of onset days that get their own EM delay, counted back from the cut-off. Defaults to NULL, one static delay.
delay_shrinkage
Strength, in expected cases, of the shrinkage of each period's delay towards the previous period's delay. Must be positive. Defaults to 10.

Details

Let \(T\) be cutoff, \(D\) be max_delay and \(g(d)\) the pooled empirical delay pmf of complete cases. A case with a missing onset reported on day \(t\) is placed on onset day \(s\) as follows.

  • backsampling spreads it over \(s = t - d\) with weight \(g(d)\), so \(P(s \mid t) = g(t - s)\). This is the expected allocation of drawing a delay from the pooled empirical delays.
  • mean moves it back by the mean empirical delay, rounded with halves up (as JavaScript's Math.round):

    \[ s = t - \lfloor \textstyle\sum_d d \, g(d) + 0.5 \rfloor. \]
  • fwd uses the given forward delay \(f\), so \(P(s \mid t) = f(t - s)\). It is only returned when delay is given. For a delay matrix, \(P(s \mid t) \propto f_s(t - s)\) over onset days \(0, \dots, t\).

  • em is EM back-projection. The E-step allocates missing cases by \(P(s \mid t) \propto \lambda(s) f_{k(s)}(t - s)\), where \(k(s)\) is the period of onset day \(s\). The M-step alternates three times between

    \[ \lambda(s) = \frac{n(s)}{F_{k(s)}\big(\min(T - s, D)\big)}, \qquad f(d) \propto \frac{n(d)}{\sum_{s \le T - d} \lambda(s)}, \]

    where \(n\) counts complete and allocated cases by onset day or by delay and \(F_k\) is the CDF of \(f_k\). By default there is one period and the delay \(f_0 = f\) is static, estimated jointly with \(\lambda\) from all cases, allowing for right truncation. With delay_period, onset days are split into periods of that many days counted back from the cut-off, and in order

    \[ f_k(d) \propto \frac{N_k(d) + \kappa h_k(d)}{E_k(d) + \kappa}, \]

    where \(N_k(d)\) counts cases with onset in period \(k\) and delay \(d\), \(E_k(d)\) sums \(\lambda(s)\) over days of period \(k\) up to \(T - d\), \(\kappa\) is delay_shrinkage, \(h_0 = f\) and \(h_k = f_{k-1}\). Each period is shrunk towards the one before, with a weight on its own data that grows with its expected number of cases. If delay is given, em holds it fixed (a plug-in) and only estimates \(\lambda\). No convergence check is made.

All methods assume onsets are missing at random given the report day. If missingness depends on the delay, all are biased.

Cases reported before day 0 or after cutoff are dropped. Complete cases with a negative onset, an onset after report or a delay over max_delay are dropped. They are not imputed. For backsampling, mean and fwd, imputed onsets before day 0 are not in imputed. The amount is in imputed_before_day0.

Value

A list. Daily vectors have length cutoff + 1 and element i is onset day i - 1. Delay pmfs have length max_delay + 1 and element d + 1 is a delay of d days.

  • complete: onset counts of cases with a known onset.
  • missing_by_report: counts of missing-onset cases by report day.
  • empirical_delay: pooled delay pmf of complete cases.
  • imputed: a list of daily onset counts (complete plus imputed) for backsampling, mean and em, and fwd if delay is given.
  • em_delay: the delay pmf used by EM: the static estimate, the pooled estimate with delay_period, or the given delay (its onset-weighted mean for a matrix).
  • em_delay_by_period: a matrix with the delay pmf of each period in its rows. One row without delay_period, and one row per onset day for a delay matrix.
  • delay_period_start: the first onset day of each period.
  • dropped: number of cases dropped.
  • imputed_before_day0: a list with the expected number of missing-onset cases that backsampling, mean and (if delay is given) fwd place before day 0.
  • cutoff: the cut-off used, in days.
  • origin: the Date of day 0 for Date input, otherwise NULL.

Warning

This code was generated by an LLM. Sam Abbott has only checked the interactive page.

Examples

onset <- c(0, 2, NA, 5, NA)
report <- c(3, 6, 7, 9, 12)
res <- impute_linelist(onset, report, cutoff = 12)
round(res$imputed$backsampling, 2)
res$imputed$mean
round(res$imputed$em, 2)

# Date input, with a forward delay
onset_date <- as.Date(c("2026-05-01", "2026-05-03", NA, "2026-05-06", NA))
report_date <- as.Date("2026-05-01") + report + 1
res <- impute_linelist(onset_date, report_date, delay = delay_pmf(4, 3))
res$origin
round(res$imputed$fwd, 2)

# a delay estimated for each 4-day period of onsets
res <- impute_linelist(onset, report, cutoff = 12, delay_period = 4)
res$delay_period_start
round(res$em_delay_by_period[, 1:6], 2)

See also

Other linelist: correct_truncation(), weekly()

correct_truncation

Correct daily onset counts for right truncation

Cases with a recent onset may not be reported by the cut-off. This divides the count on each onset day \(s\) by the expected fraction reported by the cut-off \(T\),

\[ \tilde{n}(s) = \frac{n(s)}{F(T - s)}, \]

where \(F\) is the CDF of the delay pmf. \(F(x) = 1\) beyond the longest delay and \(F(x) = 0\) for \(x < 0\). Days with \(F(T - s)\) below min_cover are set to NA, as the corrected count is too uncertain to use.

Usage

correct_truncation(
  counts,
  delay,
  cutoff = length(counts) - 1,
  min_cover = 0.05
)

Arguments

counts
Daily onset counts. Element i is onset day i - 1, as in the output of impute_linelist().
delay
Delay pmf, element d + 1 the probability of a delay of d days, for example from delay_pmf() or the em_delay output of impute_linelist(). It is normalised to sum to 1. Alternatively a matrix with one pmf per day of counts in its rows, for a delay that changes over time.
cutoff
Data cut-off day \(T\), a whole number. Defaults to the last day of counts.
min_cover
Smallest fraction reported to correct. Defaults to 0.05.

Details

The correction assumes the delay is known. It can be static, or change over time with one pmf per onset day, in which case \(F\) is the CDF of onset day \(s\)'s delay. It does not allow for uncertainty in the delay.

Value

A numeric vector the length of counts, with NA on days with less than min_cover reported.

Warning

This code was generated by an LLM. Sam Abbott has only checked the interactive page.

Examples

counts <- c(10, 12, 15, 14, 9, 5, 2)
delay <- delay_pmf(3, 2, dmax = 20)
round(correct_truncation(counts, delay), 1)

# on a line list, with the delay estimated by EM
res <- impute_linelist(
  onset = c(0, 1, NA, 3, NA, 4),
  report = c(3, 5, 6, 8, 9, 10)
)
round(correct_truncation(res$imputed$em, res$em_delay, res$cutoff), 2)

# with a delay for each onset day, from per-period estimates
res <- impute_linelist(
  onset = c(0, 1, NA, 3, NA, 4),
  report = c(3, 5, 6, 8, 9, 10),
  delay_period = 4
)
period <- findInterval(seq(0, res$cutoff), res$delay_period_start)
by_day <- res$em_delay_by_period[period, ]
round(correct_truncation(res$imputed$em, by_day, res$cutoff), 2)

See also

Other linelist: impute_linelist(), weekly()

delay_pmf

Double interval censored daily Gamma delay pmf

Computes the pmf of a daily delay whose continuous form is Gamma. The primary event (onset) is uniform within its day. The secondary event (report) is censored to its day. This is the primarycensored form.

Usage

delay_pmf(mean, sd, dmax = 60)

Arguments

mean
Mean \(\mu\) of the continuous Gamma delay, in days. Must be positive.
sd
Standard deviation \(\sigma\) of the continuous Gamma delay, in days. Must be positive.
dmax
Maximum delay \(D\), a non-negative whole number. Defaults to 60.

Details

The pmf is

\[ P(d) = G(d + 1) - 2 G(d) + G(d - 1), \]

with

\[ G(x) = \int_0^x F(u; k, \theta) \, du = x F(x; k, \theta) - k \theta F(x; k + 1, \theta), \]

where \(F(x; k, \theta)\) is the Gamma CDF with shape \(k = (\mu / \sigma)^2\) and scale \(\theta = \sigma^2 / \mu\), and \(G(x) = 0\) for \(x \le 0\). The pmf is truncated at dmax and renormalised.

This matches gamma_pmf() in the Python package epidemic_dates.

Value

A numeric vector of length dmax + 1. Element d + 1 is the probability of a delay of d days. It sums to 1.

Warning

This code was generated by an LLM. Sam Abbott has only checked the interactive page.

Examples

pmf <- delay_pmf(8, 8)
length(pmf)
sum(pmf)
# mean of the censored delay
sum(0:60 * pmf)
round(delay_pmf(4, 2, dmax = 10), 3)

weekly

Sum daily counts to weeks

Daily imputed counts are noisy, so sum them to weeks before comparing methods. Week 0 starts on day start. Days before start are left out. The last week may be partial. It then sums the days there are. A week with any NA day is NA.

Usage

weekly(daily, start = 0)

Arguments

daily
Daily counts. Element i is day i - 1.
start
First day of week 0, a non-negative whole number. Defaults to 0.

Value

A numeric vector of weekly totals, element w + 1 for week w (days start + 7w to start + 7w + 6).

Warning

This code was generated by an LLM. Sam Abbott has only checked the interactive page.

Examples

weekly(1:15)
weekly(1:15, start = 1)

res <- impute_linelist(
  onset = c(0, 2, NA, 5, NA, 9),
  report = c(3, 6, 7, 9, 12, 13)
)
vapply(res$imputed, weekly, numeric(2))

See also

Other linelist: correct_truncation(), impute_linelist()