¶
Comparing methods for imputing missing symptom onsets.
Line lists often lack an earlier event date for some cases, such as the date of symptom onset, infection or sampling. A later date, such as the report date, is usually known. These tools reconstruct the missing earlier date from the later one and allow for right truncation. The worked examples use symptom onsets reconstructed from report dates.
The project began by asking how biased one common approach is. That approach fills a missing onset with the report date minus a delay drawn from the pooled empirical onset-to-report delays. It is discussed in BDBV-Modeling-Hub issue #61. The project compares it with three other methods.
The source is on GitHub. Report problems on the issue tracker. To cite this work, see Citation and licence.
The code was generated by an LLM
Sam Abbott (@seabbs) has only checked the interactive page. The JavaScript module, the Python package and the tests have not been reviewed by a person. Treat the results as exploratory.
What is here¶
| Part | What it is |
|---|---|
| Interactive page | Simulates an epidemic and shows the bias of each method as you change the scenario. |
epidemic-dates.js |
The maths behind the page as a JavaScript module with no dependencies. |
epidemic_dates |
An independent stochastic simulation in Python, used to cross-check the page. |
Both languages can also impute a real line list.
The methods¶
| Key | Method |
|---|---|
backsampling |
Draw a delay from the pooled empirical delays of cases with both dates. |
mean |
Shift by the rounded mean empirical delay. |
fwd / forward |
Draw a delay from the forward delay distribution. |
gen / em |
EM back-projection, a partial deconvolution of the missing-onset cases. |
The JavaScript keys come first and the Python keys second where they differ. See Methods for the maths and Assumptions for what each result relies on.
Bias at a turn in growth¶
Back-sampling is close to unbiased in steady growth. It is biased where growth changes, and near the real-time cut-off. Retrospectively, the pooled delays of all cases are the forward delay, so the back-sampling draw and the forward draw give the same answer. In real time the back-sampling draw matches the truth during steady growth, then misplaces onsets after the turn. EM back-projection has no bias here.
D: growth at 0.06 per day turns to −0.04 per day at day 80 (dotted line).
The delay has mean 8 days and SD 8 days, and 40% of onsets are missing at random.
Left: retrospective, with every case reported.
Right: data cut at day 101, three weeks after the turn, with each method divided by \(F(T - s)\) and only complete weeks shown.
Rt uses a generation interval with mean 15 days and SD 9 days.
These are expected values, not random replicates.
The generative method is given the true delay, so it is a best case.
The Methods page shows why, for a single report day. The interactive page lets you change the scenario.
Quick start on your own data¶
The example line list example-linelist.csv has one row per case.
Its columns are case_id, onset_date and report_date, with onset_date empty where it is missing.
These snippets impute the missing onsets and print weekly onset counts for each method.
Run them from the repository root.
import csv
import datetime as dt
import numpy as np
import epidemic_dates as ed
np.set_printoptions(linewidth=100)
day0 = dt.date(2026, 5, 3) # earliest date in the file
def day(x):
return (dt.date.fromisoformat(x) - day0).days if x else np.nan
with open("docs/data/example-linelist.csv") as fh:
rows = list(csv.DictReader(fh))
onset = np.array([day(r["onset_date"]) for r in rows])
report = np.array([day(r["report_date"]) for r in rows])
res = ed.impute_linelist(onset, report, cutoff=int(report.max()))
week = np.arange(int(report.max()) + 1) // 7
for m in ["backsampling", "mean", "em"]:
print(m, np.bincount(week, weights=res["imputed"][m]).round(1))
const fs = require("node:fs");
const { imputeLinelist } = require("./epidemic-dates.js");
const t0 = Date.parse("2026-05-03"); // earliest date in the file
const day = (x) => (x ? (Date.parse(x) - t0) / 86400000 : null);
const rows = fs
.readFileSync("docs/data/example-linelist.csv", "utf8")
.trim()
.split(/\r?\n/)
.slice(1)
.map((line) => line.split(","));
const onset = rows.map((r) => day(r[1]));
const report = rows.map((r) => day(r[2]));
const res = imputeLinelist({ onset, report, cutoff: Math.max(...report) });
for (const m of ["backsampling", "mean", "gen"]) {
const weeks = [];
res.imputed[m].forEach((x, i) => {
const w = Math.floor(i / 7);
weeks[w] = (weeks[w] ?? 0) + x;
});
console.log(m, weeks.map((x) => x.toFixed(1)).join(" "));
}
library(epidemicdates)
ll <- read.csv("docs/data/example-linelist.csv")
onset <- as.Date(ifelse(ll$onset_date == "", NA_character_, ll$onset_date))
report <- as.Date(ll$report_date)
res <- impute_linelist(onset, report, cutoff = max(report))
for (m in c("backsampling", "mean", "em")) {
cat(m, round(weekly(res$imputed[[m]]), 1), "\n")
}
using EpidemicDates, Dates
rows = split.(readlines("docs/data/example-linelist.csv")[2:end], ",")
onset = [isempty(r[2]) ? missing : Date(r[2]) for r in rows]
report = [Date(r[3]) for r in rows]
res = impute_linelist(onset, report; cutoff = maximum(report))
for m in (:backsampling, :mean, :em)
println(m, " ", join(round.(weekly(res.imputed[m]); digits = 1), " "))
end
The worked example goes through each step and explains the output.