Skip to content

LK004 — immortal_time

Subjects who could not have had the outcome during follow-up.

default severity error
injector ehrlint.inject.inject_lk004

Why this matters

A subject labelled negative who left the data after one month is not a two-year negative -- they are censored. Counting them as negatives means the negative class is partly made of people who had no opportunity to have the outcome, which inflates every metric and is the classic immortal-time error.

What it looks like in practice

A negative label on a patient whose record stops three months after the index date in a two-year task. They were never observed long enough to have the outcome, so calling them a negative counts censoring as evidence.

The query it runs

WITH followup AS (
  SELECT subject_id, MAX(time) AS last_event_time FROM events
  WHERE time IS NOT NULL GROUP BY subject_id
)
SELECT * FROM index_dates i LEFT JOIN followup f USING (subject_id)
WHERE DATEDIFF('day', i.index_date, f.last_event_time) < 730.5
  AND subject_id NOT IN (outcome_subjects UNION competing_event_subjects)

Worked example

On the clean synthetic cohort, which is clean by construction:

$ ehrlint audit --data clean/ --task task.yaml --checks LK004

LK004 did not fire.
  Every negative subject is observed for the full 2y horizon.
exit code 0

With the matching leak planted by ehrlint.inject.inject_lk004:

$ ehrlint audit --data leaky/ --task task.yaml --checks LK004

LK004 immortal_time  [error]
  4 subject(s) are observed for less than the 2y horizon without having the outcome or a competing event. Labelling them negative counts people who had no opportunity to have the outcome.
  affected: 4 of 120 subject(s) (3.3%), 4 row(s)

  example rows:
    subject_id  index_date                last_event_time           observed_days  required_days
    S000006     2019-08-20 06:30:23.8090  2019-08-20 06:30:23.8090  0.0            730.5        
    S000060     2018-02-04 12:36:52.2483  2018-02-04 12:36:52.2483  0.0            730.5        
    S000065     2020-07-07 14:03:43.4688  2020-07-07 14:03:43.4688  0.0            730.5        

exit code 1

Reproduce both:

ehrlint synth --out clean/ --n-subjects 120 --seed 0
ehrlint synth --out leaky/ --n-subjects 120 --seed 0 --inject LK004
ehrlint audit --data leaky/ --task leaky/task.yaml \
  --labels leaky/labels.parquet --splits leaky/splits.json --checks LK004

What to do about it

Require the full horizon for negatives and drop the rest, or switch to a time-to-event formulation that handles censoring explicitly.

When it skips

A skip is not a pass. This check reports a skip, with the reason, when its inputs are not available — see reading a report.