Skip to content

Commit

Permalink
Merge pull request #9 from dylanrussellmd/dindo
Browse files Browse the repository at this point in the history
Merge Dindo into master
  • Loading branch information
dylanrussellmd authored Sep 3, 2020
2 parents 75617fc + bf27329 commit 388352e
Show file tree
Hide file tree
Showing 11 changed files with 250 additions and 47 deletions.
79 changes: 40 additions & 39 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
Package: nsqipr
Title: Interact with NSQIP data via R
Version: 0.0.0.9000
Authors@R:
person(given = "Dylan",
family = "Russell",
role = c("aut", "cre"),
email = "[email protected]",
comment = c(ORCID = "0000-0002-9543-9897"))
Description: Streamlines reading and writing clean NSQIP data.
License: CC0
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.0
Suggests:
knitr,
rmarkdown,
testthat,
spelling
VignetteBuilder: knitr
URL: https://github.com/dylanrussellmd/nsqipr
BugReports: https://github.com/dylanrussellmd/nsqipr/issues
Language: en-US
Imports:
stringr,
readr,
magrittr,
usethis,
dplyr,
tibble,
lubridate,
purrr,
tools,
utils,
filesstrings,
progress
Depends:
R (>= 3.5.0)
Package: nsqipr
Title: Interact with NSQIP data via R
Version: 0.0.0.9000
Authors@R:
person(given = "Dylan",
family = "Russell",
role = c("aut", "cre"),
email = "[email protected]",
comment = c(ORCID = "0000-0002-9543-9897"))
Description: Streamlines reading and writing clean NSQIP data.
License: CC0
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
Suggests:
knitr,
rmarkdown,
testthat,
spelling
VignetteBuilder: knitr
URL: https://github.com/dylanrussellmd/nsqipr
BugReports: https://github.com/dylanrussellmd/nsqipr/issues
Language: en-US
Imports:
stringr,
readr,
magrittr,
usethis,
dplyr,
tibble,
lubridate,
purrr,
tools,
utils,
filesstrings,
progress,
furniture
Depends:
R (>= 3.5.0)
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(dindo)
export(nsqip)
importFrom("utils","file_test")
importFrom(magrittr,"%>%")
Expand Down
12 changes: 12 additions & 0 deletions R/col_definitions.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ reason_cols <- c("readmsuspreason1", "readmunrelsusp1", "readmsuspreason2", "rea

redundant_cols <- c("race_new","readmission","unplanreadmission","reoperation", "aaa_andiam_unk", "col_steroid_unk","col_oral_antibiotic_unk","col_chemo_unk","col_margins_unk","col_ileus_unk","col_mech_bowel_prep_unk")

dindo_1 <- c("supinfec","wndinfd","dehis","renainsf")

dindo_2 <- c("orgspcssi","oupneumo","urninfec","pulembol","othbleed","othdvt","othsysep","othcdiff")

dindo_3 <- c("returnor")

dindo_4 <- c("reintub","failwean","oprenafl","cnscva","cdarrest","cdmi","othseshock","cnscoma","neurodef")

dindo_5 <- c("yrdeath")

acs_cols <- c("caseid","pufyear","sex","race","race_new","ethnicity_hispanic","prncptx","cpt","workrvu","inout","transt","age","admyr","admsyr","operyr","electsurg","dischdest","anesthes","anesthes_other","attend","surgspec","height","weight","diabetes","insulin","smoke","packs","etoh","dyspnea","when_dyspnea","dnr","fnstatus1","fnstatus2","ventilat","hxcopd","cpneumon","ascites","esovar","hxchf","hxmi","prvpci","prvpcs","hxangina","hypermed","hxpvd","restpain","renafail","dialysis","impsens","coma","hemi","hxtia","cva","cvano","tumorcns","para","quad","discancr","wndinf","steroid","wtloss","bleeddis","transfus","chemo","radio","prsepis","type_prsepis","pregnancy","proper30","dprna","dprbun","dprcreat","dpralbum","dprbili","dprsgot","dpralkph","dprwbc","dprhct","dprplate","dprptt","dprpt","dprinr","prsodm","prbun","prcreat","pralbum","prbili","prsgot","pralkph","prwbc","prhct","prplate","prptt","prinr","prpt","otherproc1","othercpt1","otherwrvu1","otherproc2","othercpt2","otherwrvu2","otherproc3","othercpt3","otherwrvu3","otherproc4","othercpt4","otherwrvu4","otherproc5","othercpt5","otherwrvu5","otherproc6","othercpt6","otherwrvu6","otherproc7","othercpt7","otherwrvu7","otherproc8","othercpt8","otherwrvu8","otherproc9","othercpt9","otherwrvu9","otherproc10","othercpt10","otherwrvu10","concurr1","concpt1","conwrvu1","concurr2","concpt2","conwrvu2","concurr3","concpt3","conwrvu3","concurr4","concpt4","conwrvu4","concurr5","concpt5","conwrvu5","concurr6","concpt6","conwrvu6","concurr7","concpt7","conwrvu7","concurr8","concpt8","conwrvu8","concurr9","concpt9","conwrvu9","concurr10","concpt10","conwrvu10","opnote","pgy","emergncy","wndclas","asaclas","airtra","mallamp","rbc","anesurg","surgane","dpatrm","anetime","optime","typeintoc","sdisdt","hdisdt","yrdeath","tothlos","admqtr","htooday","stooday","totslos","nsupinfec","supinfec","dsupinfec","sssipatos","nwndinfd","wndinfd","dwndinfd","dssipatos","norgspcssi","orgspcssi","dorgspcssi","ossipatos","ndehis","dehis","ddehis","noupneumo","oupneumo","doupneumo","pnapatos","nreintub","reintub","dreintub","npulembol","pulembol","dpulembol","nfailwean","failwean","dfailwean","ventpatos","nrenainsf","renainsf","drenainsf","noprenafl","oprenafl","doprenafl","nurninfec","urninfec","durninfec","utipatos","ncnscva","cnscva","dcnscva","ncnscoma","cnscoma","dcnscoma","nneurodef","neurodef","dneurodef","ncdarrest","cdarrest","dcdarrest","ncdmi","cdmi","dcdmi","nothbleed","othbleed","dothbleed","nothgrafl","othgrafl","dothgrafl","nothdvt","othdvt","dothdvt","nothsysep","othsysep","dothsysep","sepsispatos","nothseshock","othseshock","dothseshock","sepshockpatos","othcdiff","nothcdiff","dothcdiff","podiag","podiagtx","returnor","dsdtohd","dopertod","doptodis","mortprob","morbprob","stillinhosp","reoperation","reoperation1","retorpodays","reoporcpt1","retorrelated","reoporicd91","reoperation2","retor2podays","reopor2cpt1","retor2related","reopor2icd91","reoperation3","readmission","readmission1","readmpodays1","unplanreadmission","unplannedreadmission1","readmrelated1","readmsuspreason1","readmrelicd91","readmission2","readmpodays2","unplannedreadmission2","readmrelated2","readmsuspreason2","readmrelicd92","readmission3","readmpodays3","unplannedreadmission3","readmrelated3","readmsuspreason3","readmrelicd93","readmission4","readmpodays4","unplannedreadmission4","readmrelated4","readmsuspreason4","readmrelicd94","readmission5","readmpodays5","unplannedreadmission5","readmrelated5","readmsuspreason5","readmrelicd95","readmunrelsusp1","readmunrelicd91","readmunrelsusp2","readmunrelicd92","readmunrelsusp3","readmunrelicd93","readmunrelsusp4","readmunrelicd94","readmunrelsusp5","readmunrelicd95","podiag10","podiagtx10","reopor1icd101","reopor2icd101","readmrelicd101","readmunrelicd101","readmrelicd102","readmunrelicd102","readmrelicd103","readmunrelicd103","readmrelicd104","readmunrelicd104","readmrelicd105","readmunrelicd105","wound_closure","podiag_other","podiag_other10")

aaa_cols <- c("aaa_surgind", "aaa_andiam", "aaa_andiam_unk", "aaa_paas", "aaa_surgap", "aaa_pcl", "aaa_pae", "aaa_distext", "aaa_mima", "aaa_cp_renrevasc", "aaa_cp_viscrevasc", "aaa_cp_ler", "aaa_cp_are", "aaa_colitis", "aaa_dcolitis", "aaa_colitiis_treat", "aaa_lei", "aaa_dlei", "aaa_roa", "aaa_droa", "aaa_iculos")
Expand All @@ -22,4 +32,6 @@ aie_cols <- c("aie_proc", "aie_sympt", "aie_hrf_phys", "aie_hrf_anat", "aie_prem

col_cols <- c("col_steroid","col_steroid_unk","col_mech_bowel_prep","col_mech_bowel_prep_unk","col_oral_antibiotic","col_oral_antibiotic_unk","col_chemo","col_chemo_unk","col_indication","col_icd9_indication","col_emergent","col_icd9_emergent","col_approach","col_open_assist","col_unplanned_conversion","col_margins","col_margins_unk","col_malignancyt","col_malignancyn","col_malignancym","col_anastomotic","col_leak_treatment","col_ileus","col_ileus_unk","col_nodeseval","col_icd10_indication","col_icd10_emergent")

add_cols <- c("dindo")

col_order <- c(acs_cols, aaa_cols, aie_cols, col_cols)
9 changes: 1 addition & 8 deletions R/conv_standard.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,7 @@ set_up_df <- function(df, col_names) {
df %>%
dplyr::rename_with(., tolower) %>%
dplyr::mutate(dplyr::across(dplyr::everything(), tolower)) %>%
dplyr::mutate(dplyr::across(dplyr::everything(), dplyr::na_if, "unknown")) %>%
dplyr::mutate(dplyr::across(dplyr::everything(), dplyr::na_if, "null")) %>%
dplyr::mutate(dplyr::across(dplyr::everything(), dplyr::na_if, "n/a")) %>%
dplyr::mutate(dplyr::across(dplyr::everything(), dplyr::na_if, "not documented")) %>%
dplyr::mutate(dplyr::across(dplyr::everything(), dplyr::na_if, "none/not documented")) %>%
dplyr::mutate(dplyr::across(dplyr::everything(), dplyr::na_if, "not entered")) %>%
dplyr::mutate(dplyr::across(dplyr::everything(), dplyr::na_if, "-99")) %>%
dplyr::mutate(dplyr::across(dplyr::everything(), dplyr::na_if, -99)) %>%
dplyr::mutate(dplyr::across(dplyr::everything(), furniture::washer, "unknown", "null", "n/a", "not documented", "none/not documented", "not entered","-99", -99)) %>%
tibble::add_column(., !!!col_names[setdiff(names(col_names), names(.))])
}

Expand Down
82 changes: 82 additions & 0 deletions R/dindo.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#' Check if a boolean is TRUE and not NA
#'
#' This represent a key assumption about the Dindo classification sorting. If a complication is missing (NA), it is considered FALSE
#' for the purposes of the Dindo classification.
#'
#' @param bool a logical vector
#'
#' @return a logical vector
#'
#' @keywords internal
#'
checkTrue <- function(bool) {
(bool %in% TRUE) & !is.na(bool)
}

#' Check if any trues exist row-wise in a logical matrix.
#'
#' @param ... a list of logical vectors or a data frame
#'
#' @return a logical vector. TRUE if any trues, FALSE if no trues.
#'
#' @keywords internal
#'
checkAnyTrue <- function(...) {
apply(cbind(...), 2, checkTrue) %>%
apply(., 1, any)
}

#' Check if a patient has died within 30 days of the index procedure. Assumes that if a patient died, a date would be
#' recorded in `yrdeath`.
#'
#' @param col a vector of any type
#'
#' @return a logical vector. TRUE if not NA, FALSE if NA.
#'
#' @keywords internal
#'
isDead <- function(col) {
!is.na(col)
}

#' Check if any trues exist row-wise in a logical matrix.
#'
#' @param ... a list of vectors of any type or a data frame
#'
#' @return a logical vector. TRUE if any NA values in row, FALSE if no NA values in row.
#'
#' @keywords internal
#'
checkAnyDead <- function(...) {
apply(cbind(...), 2, isDead) %>%
apply(., 1, any)
}


#' Classifies a patient according to the Dindo-Clavien surgical complication grading scale.
#'
#' @param df a dataframe including relevant columns containing information on specific post-operative complications.
#'
#' @return a numeric vector representing the Dindo-Clavien classification.
#'
#' @export
#'
dindo <- function(df) {
e <- new.env()
dindo_list <- list(dindo_1, dindo_2, dindo_3, dindo_4, dindo_5)
dindo <- rep(0, nrow(df))

dindo_cat <- function(x, y, df) {
dindo_cols <- colnames(df)[which(colnames(df) %in% x)]
if(length(dindo_cols) > 0) {
if(y == 5) {
dindo[which(checkAnyDead(df[dindo_cols]))] <<- y
} else {
dindo[which(checkAnyTrue(df[dindo_cols]))] <<- y
}
}
}

purrr::imap(dindo_list, ~dindo_cat(.x, .y, df))
return(dindo)
}
18 changes: 18 additions & 0 deletions man/checkAnyDead.Rd

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

18 changes: 18 additions & 0 deletions man/checkAnyTrue.Rd

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

19 changes: 19 additions & 0 deletions man/checkTrue.Rd

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

17 changes: 17 additions & 0 deletions man/dindo.Rd

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

20 changes: 20 additions & 0 deletions man/isDead.Rd

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

22 changes: 22 additions & 0 deletions tests/testthat/test-dindo.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
testthat::test_that("checkTrue assigns TRUE correctly", {
testthat::expect_true(checkTrue(TRUE))
testthat::expect_false(checkTrue(NA))
testthat::expect_false(checkTrue(FALSE))
})

testthat::test_that("checkAnyTrue assigns TRUE correctly", {
testthat::expect_equal(checkAnyTrue(c(TRUE, FALSE, TRUE, FALSE), c(FALSE, FALSE, NA, NA )), c(TRUE, FALSE, TRUE, FALSE))
})

testthat::test_that("checkTrue and checkAnyTrue are equal if given a single vector", {
testthat::expect_equal(checkTrue(c(TRUE, TRUE, TRUE, FALSE)), checkAnyTrue(c(TRUE, TRUE, TRUE, FALSE)))
})

testthat::test_that("isDead assigns TRUE correctly", {
testthat::expect_true(isDead("dead"))
testthat::expect_false(isDead(NA))
})

testthat::test_that("checkAnyDead assigns TRUE correctly", {
testthat::expect_equal(checkAnyDead(c("dead", NA, NA), c(NA, "dead", NA)), c(TRUE, TRUE, FALSE))
})

0 comments on commit 388352e

Please sign in to comment.