Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions R/charlson.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#' @param iddf A `data.frame` of unique IDs
#' @param cmrb A `data.frame` containing at least `id.vars` and
#' `condition` columns; i.e., the 'comorbidity' `data.frame`.
#' @param primarydx.var Character (scalar) with the name of the column in
#' `cmrb` denoting if the condition was flaged as a primary diagnostic or not.
#' @param method Character scalar; name of the Charlson variant to assess
#'
#' @return A `data.frame` with `id.vars`, per-condition 0/1
Expand All @@ -15,8 +17,12 @@
#' @family internal comorbidity functions
#' @noRd
#' @keywords internal
.charlson <- function(id.vars, iddf, cmrb, method) {
ccc <- unique(mdcr_select(cmrb, cols = c(id.vars, "condition")))
.charlson <- function(id.vars, iddf, cmrb, primarydx.var, method) {
ccc <- unique(mdcr_select(cmrb, cols = c(id.vars, "condition", primarydx.var)))

# omit primary dx
idx <- which(ccc[[primarydx.var]] == 0L)
ccc <- mdcr_subset(ccc, i = idx)

# get the method weights and conditions
conditions <- mdcr_subset(..mdcr_internal_charlson_index_scores..,
Expand Down
28 changes: 16 additions & 12 deletions R/comorbidities.R
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ comorbidities.data.frame <- function(data,
}
}

if (startsWith(method, "elixhauser") & !is.null(primarydx.var)) {
if ((startsWith(method, "elixhauser") | startsWith(method, "charlson")) & !is.null(primarydx.var)) {
is_a_column(primarydx.var, names(data))
pn <- primarydx.var %in% ..protected_names..
if (pn) {
Expand All @@ -268,6 +268,9 @@ comorbidities.data.frame <- function(data,
)
)
}
} else if (startsWith(method, "pccc") & (!is.null(primarydx.var) | !is.null(primarydx))) {
warning("primarydx.var and primarydx are ignored when method = '%s'", method)
primarydx.var <- primarydx <- NULL
}

flag.method <-
Expand Down Expand Up @@ -447,26 +450,22 @@ comorbidities.data.frame <- function(data,
is_a_column(poa.var, nms)
}

if (startsWith(method, "elixhauser")) {
if (startsWith(method, "elixhauser") | startsWith(method, "charlson")) {
if (is.null(primarydx.var)) {
if (!is.null(primarydx)) {
stopifnot(inherits(primarydx, "numeric") | inherits(primarydx, "integer"))
stopifnot(length(primarydx) == 1L)
primarydx <- as.integer(primarydx)
stopifnot(primarydx %in% c(0L, 1L))
} else {
if (grepl("^elixhauser", method)) {
warning("Assuming all codes provided are secondary diagnostic codes. Define `primarydx.var` or `primarydx` if this assumption is incorrect.", call. = FALSE)
}
warning("Assuming all codes provided are secondary diagnostic codes. Define `primarydx.var` or `primarydx` if this assumption is incorrect.", call. = FALSE)
primarydx <- 0L
}

primarydx.var <- build_name("..medicalcoder_primarydx..", nms)

if (grepl("^elixhauser", method)) {
on_full <- mdcr_set(on_full, j = primarydx.var, value = rep(primarydx, nrow(on_full)))
on_comp <- mdcr_set(on_comp, j = primarydx.var, value = rep(primarydx, nrow(on_comp)))
}
on_full <- mdcr_set(on_full, j = primarydx.var, value = rep(primarydx, nrow(on_full)))
on_comp <- mdcr_set(on_comp, j = primarydx.var, value = rep(primarydx, nrow(on_comp)))

} else {
if (!is.null(primarydx)) {
Expand Down Expand Up @@ -556,7 +555,12 @@ comorbidities.data.frame <- function(data,

cmrb <- do.call(rbind, foc)

cmrb[[poa.var]][cmrb[[encid]] > cmrb[["first_occurrance"]]] <- 1L
# set poa to 1 and primarydx to 0 for prior conditions
idx <- cmrb[[encid]] > cmrb[["first_occurrance"]]
cmrb[[poa.var]][idx] <- 1L
if (!is.null(primarydx.var)) {
cmrb[[primarydx.var]][cmrb[[encid]] > cmrb[["first_occurrance"]]] <- 0L
}
cmrb <- mdcr_set(cmrb, j = "first_occurrance", value = NULL)

cmrb <- unique(cmrb)
Expand All @@ -577,7 +581,7 @@ comorbidities.data.frame <- function(data,
} else if (startsWith(method, "pccc_v3")) {
ccc <- .pccc_v3(id.vars = id.vars, iddf = iddf, cmrb = cmrb, subconditions = subconditions)
} else if (startsWith(method, "charlson")) {
ccc <- .charlson(id.vars = id.vars, iddf = iddf, cmrb = cmrb, method)
ccc <- .charlson(id.vars = id.vars, iddf = iddf, cmrb = cmrb, primarydx.var = primarydx.var, method = method)
if (!is.null(age.var)) {
ages <- unique(mdcr_select(data, cols = c(id.vars, age.var)))
ages[["age_score"]] <- as.integer(cut(ages[[age.var]], breaks = c(-Inf, 50, 60, 70, 80, Inf), right = FALSE)) - 1L
Expand All @@ -587,7 +591,7 @@ comorbidities.data.frame <- function(data,
ccc[["age_score"]] <- rep(NA_integer_, nrow(ccc))
}
} else if (startsWith(method, "elixhauser")) {
ccc <- .elixhauser(id.vars = id.vars, iddf = iddf, cmrb = cmrb, poa.var = poa.var, primarydx.var = primarydx.var, method)
ccc <- .elixhauser(id.vars = id.vars, iddf = iddf, cmrb = cmrb, poa.var = poa.var, primarydx.var = primarydx.var, method = method)
} else {
stop(sprintf("method '%s' has not yet been implemented", method))
}
Expand Down
6 changes: 3 additions & 3 deletions man-roxygen/params-comorbidities.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@
#'
#' @param primarydx.var Character scalar naming the column in `data` that
#' indicates whether `data[[icd.codes]]` are primary diagnostic codes (`1L`)
#' or not (`0L`). Primary diagnosis is used only for Elixhauser comorbidities
#' and is ignored when the method is PCCC or Charlson. `primarydx.var` takes
#' precedence over `primarydx` if both are provided.
#' or not (`0L`). Primary diagnosis is used only for Elixhauser and Charlson
#' comorbidities and is ignored when the method is a PCCC variant.
#' `primarydx.var` takes precedence over `primarydx` if both are provided.
#'
#' @param primarydx An integer value of `0` or `1`. If `0`,
#' treat all codes as non-primary diagnoses; if `1`, treat all codes as
Expand Down
6 changes: 3 additions & 3 deletions man/comorbidities.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions tests/test-comorbidities.R
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,21 @@ stopifnot(
inherits(out4, "error")
)

################################################################################
# when a primarydx.var was passed to comorbidities when not needed an error was
# thrown. https://github.com/dewittpe/medicalcoder/issues/16
#
# This has been corrected to be a warning - ignore primarydx.var unless
# elixhauser_ahrq2022 or newer
comorbidities(
data = mdcr,
id.var = "patid",
#primarydx.var = "full_code",
method = "charlson_quan2005",
icd.codes = "code",
poa = 1
)

################################################################################
# End of File #
################################################################################
1 change: 1 addition & 0 deletions vignettes/charlson.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ mdcr_results <-
dx.var = "dx",
flag.method = "current",
poa = 1,
primarydx = 0,
method = "charlson_quan2005"
)
```
Expand Down
50 changes: 25 additions & 25 deletions vignettes/comorbidities.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ subset(
record <-
structure(
list(
patid = c("A", "A", "A", "A", "A", "A", "A"),
patid = c("A", "A", "A", "A", "A", "A", "A"),
encid = c(1L, 2L, 3L, 4L, 5L, 5L, 6L),
code = c(NA, "C78.4", "I50.40", NA, "C78.4", "I50.40", NA),
poa = c(NA, 0L, 1L, NA, 1L, 0L, NA)),
Expand All @@ -236,12 +236,11 @@ args <-
icd.codes = "code",
id.vars = c("patid", "encid"),
icdv = 10L,
dx = 1,
primarydx = 0L
dx = 1
)
args_current_poa0 <- c(args, poa = 0L, flag.method = "current")
args_current_poa1 <- c(args, poa = 1L, flag.method = "current")
args_current_poav <- c(args, poa.var = "poa", flag.method = "current")
args_current_poa0 <- c(args, poa = 0L, flag.method = "current")
args_current_poa1 <- c(args, poa = 1L, flag.method = "current")
args_current_poav <- c(args, poa.var = "poa", flag.method = "current")
args_cumulative_poa0 <- c(args, poa = 0L, flag.method = "cumulative")
args_cumulative_poa1 <- c(args, poa = 1L, flag.method = "cumulative")
args_cumulative_poav <- c(args, poa.var = "poa", flag.method = "cumulative")
Expand All @@ -256,29 +255,29 @@ rtn <-
do.call(cbind,
list(
left_cols,
do.call(comorbidities, c(args_current_poa0, method = "pccc_v3.0"))[, .(CVD = cvd_dxpr_or_tech, CANCER = malignancy_dxpr_or_tech)],
do.call(comorbidities, c(args_current_poa0, method = "charlson_quan2011"))[, .(CVD = chf, CANCER = mst)],
do.call(comorbidities, c(args_current_poa0, method = "elixhauser_ahrq2025"))[, .(CVD = HF, CANCER = CANCER_METS)],
do.call(comorbidities, c(args_current_poa1, method = "pccc_v3.0"))[, .(CVD = cvd_dxpr_or_tech, CANCER = malignancy_dxpr_or_tech)],
do.call(comorbidities, c(args_current_poa1, method = "charlson_quan2011"))[, .(CVD = chf, CANCER = mst)],
do.call(comorbidities, c(args_current_poa1, method = "elixhauser_ahrq2025"))[, .(CVD = HF, CANCER = CANCER_METS)],
do.call(comorbidities, c(args_current_poav, method = "pccc_v3.0"))[, .(CVD = cvd_dxpr_or_tech, CANCER = malignancy_dxpr_or_tech)],
do.call(comorbidities, c(args_current_poav, method = "charlson_quan2011"))[, .(CVD = chf, CANCER = mst)],
do.call(comorbidities, c(args_current_poav, method = "elixhauser_ahrq2025"))[, .(CVD = HF, CANCER = CANCER_METS)]
do.call(comorbidities, c(args_current_poa0, method = "pccc_v3.0"))[, .(CVD = cvd_dxpr_or_tech, CANCER = malignancy_dxpr_or_tech)],
do.call(comorbidities, c(args_current_poa0, primarydx = 0L, method = "charlson_quan2011"))[, .(CVD = chf, CANCER = mst)],
do.call(comorbidities, c(args_current_poa0, primarydx = 0L, method = "elixhauser_ahrq2025"))[, .(CVD = HF, CANCER = CANCER_METS)],
do.call(comorbidities, c(args_current_poa1, method = "pccc_v3.0"))[, .(CVD = cvd_dxpr_or_tech, CANCER = malignancy_dxpr_or_tech)],
do.call(comorbidities, c(args_current_poa1, primarydx = 0L, method = "charlson_quan2011"))[, .(CVD = chf, CANCER = mst)],
do.call(comorbidities, c(args_current_poa1, primarydx = 0L, method = "elixhauser_ahrq2025"))[, .(CVD = HF, CANCER = CANCER_METS)],
do.call(comorbidities, c(args_current_poav, method = "pccc_v3.0"))[, .(CVD = cvd_dxpr_or_tech, CANCER = malignancy_dxpr_or_tech)],
do.call(comorbidities, c(args_current_poav, primarydx = 0L, method = "charlson_quan2011"))[, .(CVD = chf, CANCER = mst)],
do.call(comorbidities, c(args_current_poav, primarydx = 0L, method = "elixhauser_ahrq2025"))[, .(CVD = HF, CANCER = CANCER_METS)]
))
,
do.call(cbind,
list(
left_cols,
do.call(comorbidities, c(args_cumulative_poa0, method = "pccc_v3.0"))[, .(CVD = cvd_dxpr_or_tech, CANCER = malignancy_dxpr_or_tech)],
do.call(comorbidities, c(args_cumulative_poa0, method = "charlson_quan2011"))[, .(CVD = chf, CANCER = mst)],
do.call(comorbidities, c(args_cumulative_poa0, method = "elixhauser_ahrq2025"))[, .(CVD = HF, CANCER = CANCER_METS)],
do.call(comorbidities, c(args_cumulative_poa1, method = "pccc_v3.0"))[, .(CVD = cvd_dxpr_or_tech, CANCER = malignancy_dxpr_or_tech)],
do.call(comorbidities, c(args_cumulative_poa1, method = "charlson_quan2011"))[, .(CVD = chf, CANCER = mst)],
do.call(comorbidities, c(args_cumulative_poa1, method = "elixhauser_ahrq2025"))[, .(CVD = HF, CANCER = CANCER_METS)],
do.call(comorbidities, c(args_cumulative_poav, method = "pccc_v3.0"))[, .(CVD = cvd_dxpr_or_tech, CANCER = malignancy_dxpr_or_tech)],
do.call(comorbidities, c(args_cumulative_poav, method = "charlson_quan2011"))[, .(CVD = chf, CANCER = mst)],
do.call(comorbidities, c(args_cumulative_poav, method = "elixhauser_ahrq2025"))[, .(CVD = HF, CANCER = CANCER_METS)]
do.call(comorbidities, c(args_cumulative_poa0, method = "pccc_v3.0"))[, .(CVD = cvd_dxpr_or_tech, CANCER = malignancy_dxpr_or_tech)],
do.call(comorbidities, c(args_cumulative_poa0, primarydx = 0L, method = "charlson_quan2011"))[, .(CVD = chf, CANCER = mst)],
do.call(comorbidities, c(args_cumulative_poa0, primarydx = 0L, method = "elixhauser_ahrq2025"))[, .(CVD = HF, CANCER = CANCER_METS)],
do.call(comorbidities, c(args_cumulative_poa1, method = "pccc_v3.0"))[, .(CVD = cvd_dxpr_or_tech, CANCER = malignancy_dxpr_or_tech)],
do.call(comorbidities, c(args_cumulative_poa1, primarydx = 0L, method = "charlson_quan2011"))[, .(CVD = chf, CANCER = mst)],
do.call(comorbidities, c(args_cumulative_poa1, primarydx = 0L, method = "elixhauser_ahrq2025"))[, .(CVD = HF, CANCER = CANCER_METS)],
do.call(comorbidities, c(args_cumulative_poav, method = "pccc_v3.0"))[, .(CVD = cvd_dxpr_or_tech, CANCER = malignancy_dxpr_or_tech)],
do.call(comorbidities, c(args_cumulative_poav, primarydx = 0L, method = "charlson_quan2011"))[, .(CVD = chf, CANCER = mst)],
do.call(comorbidities, c(args_cumulative_poav, primarydx = 0L, method = "elixhauser_ahrq2025"))[, .(CVD = HF, CANCER = CANCER_METS)]
))
)
```
Expand Down Expand Up @@ -366,7 +365,8 @@ cmdf_mdcr <-
dx.var = "dx",
method = "charlson_cdmf2019",
flag.method = "current",
poa = 1)
primarydx = 0L,
poa = 1L)
data.table::setDT(cmdf_mdcr)

cmdf_mdcr[, .N, keyby = .(hiv, aids)]
Expand Down