Skip to content

LK002 — same_encounter

Outcome codes from the index encounter itself.

default severity error
injector ehrlint.inject.inject_lk002

Why this matters

Codes are often entered at the end of an encounter, after the outcome is known. A diagnosis coded during the index visit may postdate the prediction moment even though its timestamp does not, because the timestamp is the visit's, not the moment of entry. Same-encounter outcome codes are therefore suspect regardless of their recorded time.

What it looks like in practice

A diagnosis coded at discharge from the index visit. Its timestamp is the visit date, so a naive time <= index_date filter keeps it -- but the code was entered once the outcome was already known.

The query it runs

WITH idx AS (
  SELECT subject_id, encounter_id AS index_encounter FROM events e
  JOIN index_dates i USING (subject_id) WHERE e.time = i.index_date
)
SELECT * FROM events e JOIN idx USING (subject_id)
WHERE e.code IN outcome_codes AND e.encounter_id = idx.index_encounter

Worked example

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

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

LK002 did not fire.
  No outcome code shares an encounter with the index event.
exit code 0

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

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

LK002 same_encounter  [error]
  4 subject(s) have an outcome code recorded in the same encounter as the index event. Coding often happens after the outcome is known.
  affected: 4 of 120 subject(s) (3.3%), 4 row(s)

  example rows:
    subject_id  time                      code    encounter_id  index_encounter
    S000050     2020-12-14 03:43:49.6697  E11.65  S000050-IDX   S000050-IDX    
    S000098     2018-12-05 13:20:19.4562  E10.10  S000098-IDX   S000098-IDX    
    S000109     2020-10-09 02:57:16.8241  E11.65  S000109-IDX   S000109-IDX    

exit code 1

Reproduce both:

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

What to do about it

Exclude the index encounter's codes from the feature window, or move the index date to the encounter's start and use only codes that predate it.

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.