diff --git a/R/callback-icu-mortality.R b/R/callback-icu-mortality.R index 0b6fc7b2..def8250f 100644 --- a/R/callback-icu-mortality.R +++ b/R/callback-icu-mortality.R @@ -61,3 +61,30 @@ miiv_death_icu <- function(x, env, ...){ mi_death_icu(x, transfers, icu_wards, ...) } +picdb_death_icu <- function(x, env, ...){ + #' Extract in-ICU mortality from PICdb + #' + #' We rely on a concept-dict config, which passes rows for each `icustay_id` + #' separately (e.g. the `icustays` table). As this table does not contain mortality + #' information we rely on a auxiliary column defined for all patients in PICdb in + #' the chosen table (e.g. `los`). + #' We extract the last ICU stay for each `hadm_id`. We reset the `los` column to be + #' FALSE for each `icustay_id` and set it to TRUE for the `icustay_id` of the last + #' ICU stay of each `hadm_id`, which is marked as deceased. + + icu_stays <- load_ts(env[["icustays"]], id_var = "hadm_id", index_var = "intime", interval = mins(1L)) + admissions <- load_id(env[["admissions"]], id_var = "hadm_id", cols = c("hospital_expire_flag")) + + # For each "hadm_id" we find the "icustay_id" of the last ICU stay based on the "intime" + last_icu_stay <- icu_stays[, .(icustay_id = icustay_id[.N]), by = hadm_id] + deceased_hadm_id <- admissions[ricu:::is_true(hospital_expire_flag), .(hadm_id)]$hadm_id + deceased_icustay_id <- icu_stays[hadm_id %in% deceased_hadm_id, .(icustay_id)]$icustay_id + + # For all `icustay_id` not in last_icu_stay, we set the value of the `hospital_expire_flag` variable to FALSE + # Patients only die in the last ICU stay + # `los` should be configured to be the target extraction column as a dummy column + # to put the value of the `hospital_expire_flag` variable + x[, los := FALSE] + x[icustay_id %in% deceased_icustay_id, los := TRUE] + x[!icustay_id %in% last_icu_stay$icustay_id, los := FALSE] +} diff --git a/R/data-load.R b/R/data-load.R index 113b32dc..34bae002 100644 --- a/R/data-load.R +++ b/R/data-load.R @@ -131,7 +131,7 @@ load_difftime.eicu_tbl <- function(x, rows, cols = colnames(x), warn_dots(...) - load_eiau(x, {{ rows }}, cols, id_hint, time_vars, min_as_mins) + load_eisi(x, {{ rows }}, cols, id_hint, time_vars, min_as_mins) } #' @rdname load_src @@ -153,7 +153,7 @@ load_difftime.aumc_tbl <- function(x, rows, cols = colnames(x), warn_dots(...) - load_eiau(x, {{ rows }}, cols, id_hint, time_vars, ms_as_mins) + load_au(x, {{ rows }}, cols, id_hint, time_vars) } #' @rdname load_src @@ -175,7 +175,7 @@ load_difftime.sic_tbl <- function(x, rows, cols = colnames(x), sec_as_mins <- function(x) min_as_mins(as.integer(x / 60)) warn_dots(...) - load_eiau(x, {{ rows }}, cols, id_hint, time_vars, sec_as_mins) + load_eisi(x, {{ rows }}, cols, id_hint, time_vars, sec_as_mins) } #' @rdname load_src @@ -248,7 +248,7 @@ load_mihi <- function(x, rows, cols, id_hint, time_vars) { as_id_tbl(dat, id_vars = id_col, by_ref = TRUE) } -load_eiau <- function(x, rows, cols, id_hint, time_vars, mins_fun) { +load_eisi <- function(x, rows, cols, id_hint, time_vars, mins_fun) { id_col <- resolve_id_hint(x, id_hint) @@ -270,6 +270,35 @@ load_eiau <- function(x, rows, cols, id_hint, time_vars, mins_fun) { as_id_tbl(dat, id_vars = id_col, by_ref = TRUE) } +load_au <- function(x, rows, cols, id_hint, time_vars) { + dt_round_min <- function(x, y) round_to(ms_as_mins(x - y)) + + id_col <- resolve_id_hint(x, id_hint) + + assert_that(is.string(id_col), id_col %in% colnames(x)) + + if (!id_col %in% cols) { + cols <- c(cols, id_col) + } + + time_vars <- intersect(time_vars, cols) + + dat <- load_src(x, {{ rows }}, cols) + + if (length(time_vars)) { + + dat <- merge(dat, id_origin(x, id_col, origin_name = "origin"), + by = id_col) + dat <- dat[, + c(time_vars) := lapply(.SD, dt_round_min, get("origin")), + .SDcols = time_vars + ] + dat <- dat[, c("origin") := NULL] + } + + as_id_tbl(dat, id_vars = id_col, by_ref = TRUE) +} + #' Load data as `id_tbl` or `ts_tbl` objects #' #' Building on functionality provided by [load_src()] and [load_difftime()], diff --git a/R/utils-ts.R b/R/utils-ts.R index 74afa62a..b9b1fa7d 100644 --- a/R/utils-ts.R +++ b/R/utils-ts.R @@ -643,11 +643,11 @@ padded_capped_diff <- function(x, final, max) { trunc_time <- function(x, min, max) { if (not_null(min)) { - replace(x, x < min, min) + x <- replace(x, x < min, min) } if (not_null(max)) { - replace(x, x > max, max) + x <- replace(x, x > max, max) } x diff --git a/inst/extdata/config/concept-dict/hematology.json b/inst/extdata/config/concept-dict/hematology.json index 73193b6c..41423baa 100644 --- a/inst/extdata/config/concept-dict/hematology.json +++ b/inst/extdata/config/concept-dict/hematology.json @@ -73,10 +73,14 @@ "picdb": [ { "table": "labevents", - "ids": [ - 5492 - ], + "ids": 5492, "sub_var": "itemid" + }, + { + "table": "labevents", + "ids": 5493, + "sub_var": "itemid", + "callback": "blood_cell_ratio" } ] } @@ -139,8 +143,7 @@ "table": "labevents", "sub_var": "itemid" } - ], - "picdb": [] + ] } }, "eos": { @@ -217,9 +220,7 @@ "picdb": [ { "table": "labevents", - "ids": [ - 5002 - ], + "ids": 5002, "sub_var": "itemid" } ] @@ -274,8 +275,7 @@ "table": "labevents", "sub_var": "itemid" } - ], - "picdb": [] + ] } }, "fgn": { @@ -350,7 +350,14 @@ "class": "sic_itm" } ], - "picdb": [] + "picdb": [ + { + "table": "labevents", + "ids": 5164, + "sub_var": "itemid", + "callback": "convert_unit(binary_op(`*`, 100), 'mg/dL', 'g/L')" + } + ] } }, "hba1c": { @@ -398,7 +405,13 @@ "class": "sic_itm" } ], - "picdb": [] + "picdb": [ + { + "table": "labevents", + "ids": [5628, 6070], + "sub_var": "itemid" + } + ] } }, "hct": { @@ -567,7 +580,8 @@ 5099, 5257 ], - "sub_var": "itemid" + "sub_var": "itemid", + "callback": "convert_unit(binary_op(`*`, 0.1), 'g/dL', 'g/L')" } ] } @@ -626,8 +640,7 @@ "table": "labevents", "sub_var": "itemid" } - ], - "picdb": [] + ] } }, "lymph": { @@ -710,7 +723,19 @@ "class": "sic_itm" } ], - "picdb": [] + "picdb": [ + { + "table": "labevents", + "ids": [5006, 5503, 5111, 5503], + "sub_var": "itemid" + }, + { + "table": "labevents", + "ids": 5110, + "sub_var": "itemid", + "callback": "blood_cell_ratio" + } + ] } }, "mch": { @@ -782,9 +807,7 @@ "picdb": [ { "table": "labevents", - "ids": [ - 5113 - ], + "ids": 5113, "sub_var": "itemid" } ] @@ -862,10 +885,9 @@ "picdb": [ { "table": "labevents", - "ids": [ - 5114 - ], - "sub_var": "itemid" + "ids": 5114, + "sub_var": "itemid", + "callback": "convert_unit(binary_op(`*`, 0.1), '%')" } ] } @@ -939,9 +961,7 @@ "picdb": [ { "table": "labevents", - "ids": [ - 5115 - ], + "ids": 5115, "sub_var": "itemid" } ] @@ -1033,9 +1053,7 @@ "picdb": [ { "table": "labevents", - "ids": [ - 5094 - ], + "ids": [5005, 5094, 5095, 5511], "sub_var": "itemid" } ] @@ -1118,9 +1136,7 @@ "picdb": [ { "table": "labevents", - "ids": [ - 5129 - ], + "ids": 5129, "sub_var": "itemid" } ] @@ -1186,11 +1202,9 @@ "picdb": [ { "table": "labevents", - "ids": [ - 5186, - 6890 - ], - "sub_var": "itemid" + "ids": [5186, 6890], + "sub_var": "itemid", + "callback": "convert_unit(identity_callback, 'sec')" } ] } @@ -1263,10 +1277,9 @@ "picdb": [ { "table": "labevents", - "ids": [ - 5161 - ], - "sub_var": "itemid" + "ids": 5161, + "sub_var": "itemid", + "callback": "convert_unit(identity_callback, 'sec')" } ] } @@ -1336,11 +1349,7 @@ "picdb": [ { "table": "labevents", - "ids": [ - 5379, - 5381, - 6515 - ], + "ids": 5132, "sub_var": "itemid" } ] @@ -1399,9 +1408,7 @@ "picdb": [ { "table": "labevents", - "ids": [ - 5136 - ], + "ids": 5136, "sub_var": "itemid" } ] @@ -1481,9 +1488,7 @@ "picdb": [ { "table": "labevents", - "ids": [ - 6516 - ], + "ids": 5141, "sub_var": "itemid" } ] diff --git a/inst/extdata/config/concept-dict/neurological.json b/inst/extdata/config/concept-dict/neurological.json index 22a714b6..2f1036ac 100644 --- a/inst/extdata/config/concept-dict/neurological.json +++ b/inst/extdata/config/concept-dict/neurological.json @@ -76,8 +76,7 @@ "table": "chartevents", "sub_var": "itemid" } - ], - "picdb": [] + ] } }, "gcs": { @@ -170,8 +169,7 @@ "table": "chartevents", "sub_var": "itemid" } - ], - "picdb": [] + ] } }, "rass": { @@ -232,8 +230,7 @@ "table": "chartevents", "sub_var": "itemid" } - ], - "picdb": [] + ] } }, "tgcs": { @@ -270,8 +267,7 @@ "table": "chartevents", "sub_var": "itemid" } - ], - "picdb": [] + ] } }, "vgcs": { @@ -343,8 +339,7 @@ "table": "chartevents", "sub_var": "itemid" } - ], - "picdb": [] + ] } } } \ No newline at end of file diff --git a/inst/extdata/config/concept-dict/outcome.json b/inst/extdata/config/concept-dict/outcome.json index 6316741b..01943d9f 100644 --- a/inst/extdata/config/concept-dict/outcome.json +++ b/inst/extdata/config/concept-dict/outcome.json @@ -141,6 +141,15 @@ "callback": "transform_fun(comp_na(`==`, 1L))", "class": "col_itm" } + ], + "picdb": [ + { + "table": "admissions", + "index_var": "deathtime", + "val_var": "hospital_expire_flag", + "callback": "transform_fun(comp_na(`==`, 1L))", + "class": "col_itm" + } ] } }, @@ -222,6 +231,15 @@ "callback": "sic_death", "class": "col_itm" } + ], + "picdb": [ + { + "table": "icustays", + "index_var": "intime", + "val_var": "los", + "callback": "picdb_death_icu", + "class": "col_itm" + } ] } }, @@ -297,7 +315,13 @@ "class": "fun_itm" } ], - "picdb": [] + "picdb": [ + { + "callback": "los_callback", + "win_type": "hadm", + "class": "fun_itm" + } + ] } }, "los_icu": { @@ -364,7 +388,13 @@ "class": "col_itm" } ], - "picdb": [] + "picdb": [ + { + "table": "icustays", + "val_var": "los", + "class": "col_itm" + } + ] } }, "mews": { diff --git a/inst/extdata/config/concept-dict/output.json b/inst/extdata/config/concept-dict/output.json index 12439354..31844d9a 100644 --- a/inst/extdata/config/concept-dict/output.json +++ b/inst/extdata/config/concept-dict/output.json @@ -157,7 +157,14 @@ { "table": "chartevents", "ids": [ - "1008" + 1008 + ], + "sub_var": "itemid" + }, + { + "table": "outputevents", + "ids": [ + 1034 ], "sub_var": "itemid" } @@ -221,6 +228,12 @@ "class": "fun_itm", "callback": "combine_callbacks(fwd_concept('urine'), urine_rate)" } + ], + "picdb": [ + { + "class": "fun_itm", + "callback": "combine_callbacks(fwd_concept('urine'), urine_rate)" + } ] } }, diff --git a/inst/extdata/config/concept-dict/respiratory.json b/inst/extdata/config/concept-dict/respiratory.json index 7efc8189..ae624acf 100644 --- a/inst/extdata/config/concept-dict/respiratory.json +++ b/inst/extdata/config/concept-dict/respiratory.json @@ -65,8 +65,7 @@ "target": "ts_tbl", "callback": "combine_callbacks(\n transform_fun(set_val(TRUE)),\n ts_to_win_tbl(mins(1L))\n )" } - ], - "picdb": [] + ] } }, "mech_vent": { @@ -115,8 +114,7 @@ "dur_var": "endtime", "callback": "apply_map(c(`225792` = 'invasive',\n `225794` = 'noninvasive'),\n var = 'sub_var')" } - ], - "picdb": [] + ] } }, "spo2": { @@ -202,6 +200,13 @@ "1006" ], "sub_var": "itemid" + }, + { + "table": "surgery_vital_signs", + "ids": [ + "SV17" + ], + "sub_var": "itemid" } ] } @@ -423,6 +428,13 @@ "1004" ], "sub_var": "itemid" + }, + { + "table": "surgery_vital_signs", + "ids": [ + "SV13" + ], + "sub_var": "itemid" } ] } @@ -550,8 +562,7 @@ "sub_var": "itemid", "callback": "transform_fun(set_val(TRUE))" } - ], - "picdb": [] + ] } }, "vent_ind": { @@ -774,8 +785,7 @@ "sub_var": "itemid", "callback": "transform_fun(set_val(TRUE))" } - ], - "picdb": [] + ] } } } \ No newline at end of file diff --git a/inst/extdata/config/concept-dict/vitals.json b/inst/extdata/config/concept-dict/vitals.json index 75190235..71773ee8 100644 --- a/inst/extdata/config/concept-dict/vitals.json +++ b/inst/extdata/config/concept-dict/vitals.json @@ -104,6 +104,11 @@ 1015 ], "sub_var": "itemid" + }, + { + "table": "surgery_vital_signs", + "ids": "SV4", + "sub_var": "itemid" } ] } @@ -182,6 +187,11 @@ } ], "picdb": [ + { + "table": "surgery_vital_signs", + "ids": "SV16", + "sub_var": "itemid" + } ] } }, @@ -268,6 +278,11 @@ 1003, 1002 ], "sub_var": "itemid" + }, + { + "table": "surgery_vital_signs", + "ids": ["SV1", "SV2"], + "sub_var": "itemid" } ] } @@ -379,6 +394,11 @@ } ], "picdb": [ + { + "table": "surgery_vital_signs", + "ids": "SV5", + "sub_var": "itemid" + } ] } }, @@ -487,6 +507,11 @@ 1016 ], "sub_var": "itemid" + }, + { + "table": "surgery_vital_signs", + "ids": "SV3", + "sub_var": "itemid" } ] } @@ -642,6 +667,11 @@ 5253 ], "sub_var": "itemid" + }, + { + "table": "surgery_vital_signs", + "ids": ["SV14", "SV15"], + "sub_var": "itemid" } ] } diff --git a/inst/extdata/config/data-sources/picdb.json b/inst/extdata/config/data-sources/picdb.json index adce0b71..038d6f50 100644 --- a/inst/extdata/config/data-sources/picdb.json +++ b/inst/extdata/config/data-sources/picdb.json @@ -798,7 +798,7 @@ "time_vars": [ "monitor_time" ], - "index_var": "oper_id", + "index_var": "monitor_time", "val_var": "value" }, "num_rows": 1944187,