Skip to content

Instantly share code, notes, and snippets.

@docsteveharris
Created June 16, 2017 15:45
Show Gist options
  • Save docsteveharris/caf4fe7814c68711ef8de65d84f532d4 to your computer and use it in GitHub Desktop.
Save docsteveharris/caf4fe7814c68711ef8de65d84f532d4 to your computer and use it in GitHub Desktop.
generic 1d variable definitions for cchic (depends on ccfun library)
# ==================================
# = Define generic study variables =
# ==================================
find_NHIC("shortName", "dead")
# Mortality
gen_mortality(wdt, "mort.icu", "NIHR_HIC_ICU_0097")
table(unique(wdt[,.(id,mort.icu)])$mort.icu)
gen_mortality(wdt, "mort.hosp", "NIHR_HIC_ICU_0095")
table(unique(wdt[,.(id,mort.hosp)])$mort.hosp)
# Sex
names(wdt)
lookfor(NIHR_HIC_ICU_0093, wdt)
wdt[tolower(NIHR_HIC_ICU_0093) == "m", `:=`(male, TRUE)]
wdt[tolower(NIHR_HIC_ICU_0093) == "f", `:=`(male, FALSE)]
table(wdt$male)
# Age
# - [ ] TODO(2016-11-30): fix, convert back to calc when AGE is replaced with DoB
# gen_age(wdt, sim=!cconfig$IDHS)
# wdt[, .(NIHR_HIC_ICU_0411, NIHR_HIC_ICU_0033)]
# gen_age(wdt, sim=FALSE)
wdt[, AGE:= as.double(AGE)]
summary(wdt$AGE)
# Unit LOS
find_NHIC("shortName","dt_icu_admit")
find_NHIC("shortName","dt_icu_dc")
library(lubridate)
wdt[, los.icu := ymd_hms(NIHR_HIC_ICU_0412)-ymd_hms(NIHR_HIC_ICU_0411)]
# summary(as.numeric(wdt$los.icu)/(60*24))
wdt[, los.icu.pre := as.Date(ymd_hms(NIHR_HIC_ICU_0412))-ymd(NIHR_HIC_ICU_0032)]
wdt[, los.icu.post := ymd(NIHR_HIC_ICU_0406)-as.Date(ymd_hms(NIHR_HIC_ICU_0412))]
# Hospital LOS
find_NHIC("shortName","dt_hosp_admit")
find_NHIC("shortName","dt_hosp_dc")
wdt[, los.hosp := ymd(NIHR_HIC_ICU_0406)-ymd(NIHR_HIC_ICU_0032)]
wdt[,.(NIHR_HIC_ICU_0032, NIHR_HIC_ICU_0406, los.hosp)]
# Report los.hosp in days
summary(as.numeric(wdt$los.hosp))
# Admission category
find_NHIC("shortName", "adm_type")
find_NHIC("shortName", "loca_in")
gen_admx.cat(wdt)
table(unique(wdt[,.(id,adm.cat)])$adm.cat)
# Diagnostic coding
find_NHIC("shortName", "diag_admx1")
wdt <- parse.icnarc.code(wdt, "NIHR_HIC_ICU_0399")
wdt[, lapply(.SD, head, 1), by=id, .SDcols=c("condition", "dc.2")][order(dc.2)]
table(unique(wdt[,.(id,dc.2)])$dc.2)
# ========================
# = Past medical history =
# ========================
find_NHIC("shortName", "pmhx")
find_NHIC("shortName", "pmhx_depend")
# Remove dependency prior to admission from PMHx
f <- function(y,v) grepl(v, y)
which_NHIC("shortName", "pmhx", f)
pmhx.cols <- setdiff(which_NHIC("shortName", "pmhx", f), c("NIHR_HIC_ICU_0055", "pmhx.sum"))
pmhx.cols
sapply(pmhx.cols, function(x) x %in% names(wdt))
gen_pmhx.sum(wdt, pmhx.cols)
table(unique(wdt[,.(id,pmhx.sum)])$pmhx.sum)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment