papertrail/tests/effectSize.test.ts
caelum0x 827794eba3 fix(stats): correct scientific-integrity bugs found in a biostatistics audit
A rigorous correctness audit of the deterministic statistical core (meta-analysis pooling
CONFIRMED correct) surfaced real defects; each fixed and pinned with worked-example tests:

- effectSize.reconcile(): no longer flags a claim that sits INSIDE the source's own 95% CI.
  The bare materiality-factor heuristic now only fires when NO usable CI was parsed; a present
  significant CI is the authoritative boundary. Fixes false 'overstated' verdicts on
  marginally-significant small-effect trials (statin/aspirin RR ~0.90-0.95) on the core path.
- trialSequential required information size: was 4x n_arm (risPerGroup=2x, risTotal=4x),
  now the correct 2x n_arm total — every TSA verdict was biased toward 'insufficient'.
- GRADE imprecision: a numerically wide ratio CI that decisively excludes the null (e.g.
  RR 0.30, 95% CI 0.15-0.60) is no longer downgraded for 'imprecision'; the wide-CI rule now
  requires a bound approaching the null.
- GRADE publication bias: automated Egger downgrade now gated behind k>=10 studies
  (Sterne 2011/Cochrane); below that a funnel test is uninterpretable.
- riskOfBias: a trial judged overall:high now contributes >=1 GRADE step (was 0 when the
  high judgement came via the pragmatic-escalation path) — removes an internal contradiction.
- Hedges' g variance: second-term denominator corrected to 2N (Borenstein Eq 4.24) from 2(N-2).
- Honesty: docstrings no longer overclaim the OBF boundary as 'Lan-DeMets' or Egger as
  'exact metafor regtest' — both are described as the approximations/classic forms they are.

Full suite green (1062 passed). Meta-analysis pooling, biostats RR/OR CIs, absolute effects/NNT,
and Egger math were all audited and CONFIRMED correct — unchanged.
2026-07-12 01:18:01 +03:00

146 lines
5.8 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { parseEffectSizes, reconcile } from "../lib/effectSize";
describe("parseEffectSizes", () => {
it("extracts an HR point estimate and CI from NEJM-style phrasing", () => {
const effects = parseEffectSizes("hazard ratio, 0.75; 95% CI, 0.64 to 0.89");
const hr = effects.find((e) => e.measure === "HR");
expect(hr).toBeDefined();
expect(hr!.point).toBe(0.75);
expect(hr!.ciLow).toBe(0.64);
expect(hr!.ciHigh).toBe(0.89);
expect(hr!.isPercent).toBe(false);
});
it("extracts an HR + CI from parenthetical hyphen-CI phrasing", () => {
const effects = parseEffectSizes("HR 0.75 (95% CI 0.64-0.89)");
const hr = effects.find((e) => e.measure === "HR");
expect(hr).toBeDefined();
expect(hr!.point).toBe(0.75);
expect(hr!.ciLow).toBe(0.64);
expect(hr!.ciHigh).toBe(0.89);
});
it("extracts a 27% relative risk reduction and keeps isPercent", () => {
const effects = parseEffectSizes("relative risk reduction of 27%");
const rrr = effects.find((e) => e.measure === "RRR");
expect(rrr).toBeDefined();
expect(rrr!.point).toBe(27);
expect(rrr!.isPercent).toBe(true);
});
it("extracts an odds ratio with a bare parenthetical CI", () => {
const effects = parseEffectSizes("odds ratio 1.8 (1.2-2.7)");
const or = effects.find((e) => e.measure === "OR");
expect(or).toBeDefined();
expect(or!.point).toBe(1.8);
expect(or!.ciLow).toBe(1.2);
expect(or!.ciHigh).toBe(2.7);
});
it("extracts an absolute effect in points", () => {
const effects = parseEffectSizes("reduced decline by 0.45 points versus placebo");
const abs = effects.find((e) => e.measure === "absolute");
expect(abs).toBeDefined();
expect(abs!.point).toBe(0.45);
expect(abs!.isPercent).toBe(false);
});
});
describe("reconcile", () => {
it("flags caveat_dropped when the source CI crosses the null", () => {
const r = reconcile(
"The drug significantly reduced cardiovascular events.",
"The primary endpoint showed HR 0.90 (95% CI 0.78 to 1.04)."
);
expect(r.verdict).toBe("caveat_dropped");
expect(r.sourceEffect!.measure).toBe("HR");
expect(r.rationale).toContain("1.04");
});
it("flags magnitude_overstated when the claim cuts risk in half vs an 18% RRR source", () => {
const r = reconcile(
"The drug cuts cardiovascular risk in half.",
"The trial showed a relative risk reduction of 18%."
);
expect(r.verdict).toBe("magnitude_overstated");
expect(r.claimedValue).toBe(50);
expect(r.sourceEffect!.measure).toBe("RRR");
});
it("returns consistent when the claim matches an RRR source", () => {
const r = reconcile(
"The treatment reduced events by about 25%.",
"The trial reported a relative risk reduction of 25%."
);
expect(r.verdict).toBe("consistent");
expect(r.claimedValue).toBe(25);
});
// Regression: a claim whose implied effect sits INSIDE the source's own 95% CI must NOT be
// flagged as overstated, even when it exceeds the source point by more than the materiality
// factor. The CI is the authoritative decision boundary; flagging a CI-compatible claim was a
// false positive on marginally-significant small-effect trials (statins/aspirin, RR ~0.90-0.95).
it("does NOT flag a claim that falls within the source's significant 95% CI", () => {
const r = reconcile(
"The drug reduced events by 8%.",
"The drug reduced events (risk ratio 0.95, 95% CI 0.90 to 0.998)."
);
expect(r.verdict).toBe("consistent");
});
// Counterpart: a claim implying an effect STRONGER than the CI lower bound is still flagged.
it("still flags a claim that exceeds the source's 95% CI lower bound", () => {
const r = reconcile(
"The drug reduced events by 50%.",
"The drug reduced events (risk ratio 0.95, 95% CI 0.90 to 0.998)."
);
expect(r.verdict).toBe("magnitude_overstated");
});
it("returns consistent when a ~25% claim matches an HR 0.75 source", () => {
const r = reconcile(
"The treatment reduced events by about 25%.",
"The primary endpoint was HR 0.75 (95% CI 0.64 to 0.89)."
);
expect(r.verdict).toBe("consistent");
expect(r.sourceEffect!.measure).toBe("HR");
});
it("cannot_reconcile when the source has no parseable numbers", () => {
const r = reconcile(
"The drug reduced events by 30%.",
"The trial showed a meaningful benefit on the primary endpoint."
);
expect(r.verdict).toBe("cannot_reconcile");
expect(r.sourceEffect).toBeNull();
});
it("cannot_reconcile when a relative % claim faces an absolute-points source", () => {
const r = reconcile(
"The drug reduced risk by 40%.",
"Lecanemab reduced decline on the CDR-SB by 0.45 points versus placebo."
);
expect(r.verdict).toBe("cannot_reconcile");
expect(r.sourceEffect!.measure).toBe("absolute");
});
// Regression: real SPRINT phrasing has connector words between "hazard ratio" and 0.75,
// and "hazard ratio of 0.75" in the claim — both were previously missed (cannot_reconcile).
it("is consistent for real SPRINT phrasing with words between label and estimate", () => {
const r = reconcile(
"reduced the primary composite cardiovascular outcome, with a hazard ratio of 0.75",
"hazard ratio with intensive treatment, 0.75; 95% CI, 0.64 to 0.89"
);
expect(r.verdict).toBe("consistent");
expect(r.sourceEffect!.point).toBe(0.75);
});
// Regression: the ratio parser must NOT truncate "27%" into a spurious ratio of 2/27.
it("does not misparse 'relative risk reduction of 27%' as a ratio measure", () => {
const effects = parseEffectSizes("relative risk reduction of 27%");
expect(effects.some((e) => ["HR", "RR", "OR"].includes(e.measure))).toBe(false);
expect(effects.find((e) => e.measure === "RRR")!.point).toBe(27);
});
});