Skip to contents

Extracts laboratory and vital sign measurements with selectable windowing strategies. Supports LOINC consolidation groups (e.g. BUN, WBC, Creatinine) and aggregates numeric measurement values (value_as_number) during the index stay or lookback window.

Usage

extract_measurements(
  con,
  cohort_table = "cohort",
  cohort_id = NULL,
  loinc_map = NULL,
  strategy = "last_before_discharge",
  window = "stay",
  lookback_days = NULL,
  format = "df",
  outlier_bounds = NULL,
  worst_direction = NULL,
  measurement_table = "measurement"
)

Arguments

con

Active DuckDB DBI connection.

cohort_table

Cohort table or view name (default "cohort").

cohort_id

Optional integer cohort definition ID filter.

loinc_map

Named list mapping measurement feature name to LOINC codes or concept IDs. If NULL, defaults to the 14 consolidated acute labs and vitals.

strategy

Aggregation strategy: 'last_before_discharge', 'first_on_admission', 'mean', 'median', 'min', 'max', or 'worst' (per lab, the max or min according to worst_direction).

window

Window scope: 'stay', 'lookback', or 'all'.

lookback_days

Lookback window in days when window='lookback' (default 365).

format

Output format: 'df' or 'arrow'.

outlier_bounds

Optional named list feature -> c(low, high) of inclusive bounds (either may be NA). Readings outside them are discarded BEFORE the strategy is applied, so an impossible last value cannot mask a valid earlier one.

worst_direction

For strategy = "worst": named list/vector feature -> "max"|"min". Defaults to WORST_LAB_DIRECTION; a feature with no entry is an error.

measurement_table

Table/view to read (default "measurement"); pass the view made by harmonize_measurement_units() for unit-normalized values.

Value

A data.frame or Arrow Table.