Skip to content

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.

Four panels of weekly error by method in a scenario where growth turns sharply at day 80. Top row: relative error in weekly onsets. Bottom row: error in Rt. Left column: retrospective, all cases reported. Right column: real time, cut at day 101, with a truncation correction. The generative method is flat at zero throughout. Retrospectively the back-sampling and forward draws coincide, are up to 11% too high during growth and 9% too low in the turn week, with Rt errors down to −0.18. In real time back-sampling is closer during growth but is 11% too high in the last complete week. Four panels of weekly error by method in a scenario where growth turns sharply at day 80. Top row: relative error in weekly onsets. Bottom row: error in Rt. Left column: retrospective, all cases reported. Right column: real time, cut at day 101, with a truncation correction. The generative method is flat at zero throughout. Retrospectively the back-sampling and forward draws coincide, are up to 11% too high during growth and 9% too low in the turn week, with Rt errors down to −0.18. In real time back-sampling is closer during growth but is 11% too high in the last complete week.
Expected weekly error by method around a sharp turn in growth. Scenario 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))
backsampling [  6.8  14.9  16.8  27.9  44.7  81.9 122.9 200.2 251.1 353.7 595.8 697.1 455.2 174. ]
mean [  7.  15.  14.  31.  38.  84. 119. 197. 250. 351. 609. 708. 488. 132.]
em [  7.   15.4  15.9  26.5  43.5  81.3 121.4 203.2 246.3 346.2 614.2 723.5 440.4 159.2]
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(" "));
}
backsampling 6.8 14.9 16.8 27.9 44.7 81.9 122.9 200.2 251.1 353.7 595.8 697.1 455.2 174.0
mean 7.0 15.0 14.0 31.0 38.0 84.0 119.0 197.0 250.0 351.0 609.0 708.0 488.0 132.0
gen 7.0 15.4 15.9 26.5 43.5 81.3 121.4 203.2 246.3 346.2 614.2 723.5 440.4 159.2
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")
}
backsampling 6.8 14.9 16.8 27.9 44.7 81.9 122.9 200.2 251.1 353.7 595.8 697.1 455.2 174 
mean 7 15 14 31 38 84 119 197 250 351 609 708 488 132 
em 7 15.4 15.9 26.5 43.5 81.3 121.4 203.2 246.3 346.2 614.2 723.5 440.4 159.2 
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
backsampling 6.8 14.9 16.8 27.9 44.7 81.9 122.9 200.2 251.1 353.7 595.8 697.1 455.2 174.0
mean 7.0 15.0 14.0 31.0 38.0 84.0 119.0 197.0 250.0 351.0 609.0 708.0 488.0 132.0
em 7.0 15.4 15.9 26.5 43.5 81.3 121.4 203.2 246.3 346.2 614.2 723.5 440.4 159.2

The worked example goes through each step and explains the output.