From cbabee3eee08874df34b7653c7042bdec0d8510e Mon Sep 17 00:00:00 2001 From: "Luke W. Johnston" Date: Thu, 19 Sep 2024 11:59:56 +0200 Subject: [PATCH 1/6] feat: wip, the skeleton of the core diabetes classification --- R/classify-diabetes.R | 119 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 R/classify-diabetes.R diff --git a/R/classify-diabetes.R b/R/classify-diabetes.R new file mode 100644 index 0000000..e41fe32 --- /dev/null +++ b/R/classify-diabetes.R @@ -0,0 +1,119 @@ +#' Classify diabetes status using Danish registers. +#' +#' @return The same object type as the input data, which would be a +#' [tibble::tibble()] type object. +#' @export +#' @seealso See the [vignette("function-flow", package = "osdc")] for a detailed +#' description of the internal implementation of this classification function. +#' +#' @examples +#' classify_diabetes( +#' kontakter = register_data$kontakter, +#' diagnoser = register_data$diagnoser, +#' lpr_diag = register_data$lpr_diag, +#' lpr_adm = register_data$lpr_adm, +#' sysi = register_data$sysi, +#' sssy = register_data$sssy, +#' lab_forsker = register_data$lab_forsker, +#' bef = register_data$bef, +#' lmdb = register_data$lmdb +#' ) +classify_diabetes <- function(kontakter, diagnoser, lpr_diag, lpr_adm, sysi, sssy, lab_forsker, bef, lmdb) { + # Verification step ----- + verify_required_variables(kontakter, "kontakter") + verify_required_variables(diagnoser, "diagnoser") + verify_required_variables(lpr_diag, "lpr_diag") + verify_required_variables(lpr_adm, "lpr_adm") + verify_required_variables(sysi, "sysi") + verify_required_variables(sssy, "sssy") + verify_required_variables(lab_forsker, "lab_forsker") + verify_required_variables(bef, "bef") + verify_required_variables(lmdb, "lmdb") + + # Initially processing ----- + lpr2 <- join_lpr2( + lpr_diag = lpr_diag, + lpr_adm = lpr_adm + ) + + lpr3 <- join_lpr3( + kontakter = kontakter, + diagnoser = diagnoser + ) + + # pregnancy_dates <- get_pregrancy_dates( + # lpr2 = lpr2, + # lpr3 = lpr3 + # ) + + # potential_pcos <- get_potential_pcos( + # bef = bef + # ) + + # wld_purchases <- get_wld_purchases( + # lmdb = lmdb + # ) + + # Inclusion steps ----- + # diabetes_diagnosis <- include_diabetes_diagnosis( + # lpr2 = lpr2, + # lpr3 = lpr3 + # ) + + # podiatrist_services <- include_podiatrist_services( + # sysi = sysi, + # sssy = sssy + # ) + + # gld_purchases <- include_gld_purchases( + # lmdb = lmdb + # ) + + hba1c <- include_hba1c(lab_forsker) + + # Exclusion steps ----- + # TODO: Consider the argument naming here of exclude functions. Is it `data` first or something else? + # exclude_pcos <- exclude_potential_pcos( + # data = gld_purchases, + # potential_pcos = potential_pcos + # ) + + # exclude_wld_purchases <- exclude_wld_purchases( + # data = exclude_pcos, + # wld_purchases = wld_purchases + # ) + + # exclude_pregnancy <- exclude_pregnancy( + # data = exclude_wld_purchases, + # pregnancy_dates = pregnancy_dates, + # hba1c = hba1c + # ) + + # Joining into an initial dataset ----- + # inclusions <- join_inclusions( + # diabetes_diagnosis = diabetes_diagnosis, + # podiatrist_services = podiatrist_services, + # pregnancy = exclude_pregnancy + # ) + + # inclusions |> + # get_diagnosis_dates() |> + # classify_subtypes() +} + +#' After inclusion and exclusion, classify the diabetes subtypes. +#' +#' @param data Joined data output from the inclusion and exclusion steps. +#' +#' @return The same object type as the input data, which would be a +#' [tibble::tibble()] type object. +#' @keywords internal +#' +classify_subtypes <- function(data) { + # data |> + # get_has_t1d_primary_diagnosis() |> + # get_only_insulin_purchases() |> + # get_majority_of_t1d_primary_diagnosis() |> + # get_insulin_purchases_within_180_days() |> + # get_insulin_is_two_thirds_of_gld_purchases() +} From d0604aa8a00329c825d876e23f03e86592923764 Mon Sep 17 00:00:00 2001 From: "Luke W. Johnston" Date: Thu, 19 Sep 2024 13:53:58 +0200 Subject: [PATCH 2/6] refactor: :recycle: exclusions as a pipe and merge processing into them --- R/classify-diabetes.R | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/R/classify-diabetes.R b/R/classify-diabetes.R index e41fe32..b91607e 100644 --- a/R/classify-diabetes.R +++ b/R/classify-diabetes.R @@ -46,14 +46,6 @@ classify_diabetes <- function(kontakter, diagnoser, lpr_diag, lpr_adm, sysi, sss # lpr3 = lpr3 # ) - # potential_pcos <- get_potential_pcos( - # bef = bef - # ) - - # wld_purchases <- get_wld_purchases( - # lmdb = lmdb - # ) - # Inclusion steps ----- # diabetes_diagnosis <- include_diabetes_diagnosis( # lpr2 = lpr2, @@ -72,22 +64,14 @@ classify_diabetes <- function(kontakter, diagnoser, lpr_diag, lpr_adm, sysi, sss hba1c <- include_hba1c(lab_forsker) # Exclusion steps ----- - # TODO: Consider the argument naming here of exclude functions. Is it `data` first or something else? - # exclude_pcos <- exclude_potential_pcos( - # data = gld_purchases, - # potential_pcos = potential_pcos - # ) - - # exclude_wld_purchases <- exclude_wld_purchases( - # data = exclude_pcos, - # wld_purchases = wld_purchases - # ) - - # exclude_pregnancy <- exclude_pregnancy( - # data = exclude_wld_purchases, - # pregnancy_dates = pregnancy_dates, - # hba1c = hba1c - # ) + exclusions <- gld_purchases |> + exclude_potential_pcos(bef = bef) |> + exclude_wld_purchases(lmdb = lmdb) |> + exclude_pregnancy( + # TODO: Need to think about arg naming here.. + hba1c = included_hba1c, + pregnancy_dates = pregnancy_dates + ) # Joining into an initial dataset ----- # inclusions <- join_inclusions( From af34be842f3e29221780e794fdf7e4e5c0611789 Mon Sep 17 00:00:00 2001 From: "Luke W. Johnston" Date: Thu, 19 Sep 2024 14:05:13 +0200 Subject: [PATCH 3/6] refactor: :recycle: follow the match of the other functions --- R/classify-diabetes.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/classify-diabetes.R b/R/classify-diabetes.R index b91607e..9ed58f0 100644 --- a/R/classify-diabetes.R +++ b/R/classify-diabetes.R @@ -61,7 +61,9 @@ classify_diabetes <- function(kontakter, diagnoser, lpr_diag, lpr_adm, sysi, sss # lmdb = lmdb # ) - hba1c <- include_hba1c(lab_forsker) + included_hba1c <- include_hba1c( + lab_forsker = lab_forsker + ) # Exclusion steps ----- exclusions <- gld_purchases |> From ba8126fa3e103d069714c246ab66ef77793d1915 Mon Sep 17 00:00:00 2001 From: "Luke W. Johnston" Date: Thu, 19 Sep 2024 14:05:35 +0200 Subject: [PATCH 4/6] refactor: :recycle: follow the arg pattern of other functions --- R/include-hba1c.R | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/R/include-hba1c.R b/R/include-hba1c.R index 3180e7b..c89e080 100644 --- a/R/include-hba1c.R +++ b/R/include-hba1c.R @@ -3,7 +3,7 @@ #' In the `lab_forsker` register, NPU27300 is HbA1c in the modern units (IFCC) #' while NPU03835 is HbA1c in old units (DCCT). #' -#' @param data The `lab_forsker` register. +#' @param lab_forsker The `lab_forsker` register. #' #' @return An object of the same input type, default as a [tibble::tibble()], #' with two columns: `pnr` and `included_hba1c`. @@ -13,12 +13,12 @@ #' \dontrun{ #' register_data$lab_forsker |> include_hba1c() #' } -include_hba1c <- function(data) { - verify_required_variables(data, "lab_forsker") +include_hba1c <- function(lab_forsker) { + verify_required_variables(lab_forsker, "lab_forsker") hba1c_criteria <- get_algorithm_logic("hba1c") |> # To convert the string into an R expression. rlang::parse_expr() - data |> + lab_forsker |> column_names_to_lower() |> # Use !! to inject the expression into filter. dplyr::filter(!!hba1c_criteria) |> From f956fbda2c08b00fb0f1aaf11409abb23a029fbc Mon Sep 17 00:00:00 2001 From: "Luke W. Johnston" Date: Thu, 19 Sep 2024 14:06:07 +0200 Subject: [PATCH 5/6] refactor: :recycle: function will take `...` arg --- R/classify-diabetes.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/classify-diabetes.R b/R/classify-diabetes.R index 9ed58f0..809545c 100644 --- a/R/classify-diabetes.R +++ b/R/classify-diabetes.R @@ -77,9 +77,9 @@ classify_diabetes <- function(kontakter, diagnoser, lpr_diag, lpr_adm, sysi, sss # Joining into an initial dataset ----- # inclusions <- join_inclusions( - # diabetes_diagnosis = diabetes_diagnosis, - # podiatrist_services = podiatrist_services, - # pregnancy = exclude_pregnancy + # included_diabetes_diagnosis, + # included_podiatrist_services, + # exclusions # ) # inclusions |> From 95f648112661f114b46f7b5ca948e4b57c2d697a Mon Sep 17 00:00:00 2001 From: "Luke W. Johnston" Date: Thu, 19 Sep 2024 14:06:26 +0200 Subject: [PATCH 6/6] refactor: :recycle: clarify this function is for T1D --- R/classify-diabetes.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/classify-diabetes.R b/R/classify-diabetes.R index 809545c..09239a7 100644 --- a/R/classify-diabetes.R +++ b/R/classify-diabetes.R @@ -84,10 +84,10 @@ classify_diabetes <- function(kontakter, diagnoser, lpr_diag, lpr_adm, sysi, sss # inclusions |> # get_diagnosis_dates() |> - # classify_subtypes() + # classify_t1d() } -#' After inclusion and exclusion, classify the diabetes subtypes. +#' After inclusion and exclusion, classify those with type 1 diabetes. #' #' @param data Joined data output from the inclusion and exclusion steps. #' @@ -95,7 +95,7 @@ classify_diabetes <- function(kontakter, diagnoser, lpr_diag, lpr_adm, sysi, sss #' [tibble::tibble()] type object. #' @keywords internal #' -classify_subtypes <- function(data) { +classify_t1d <- function(data) { # data |> # get_has_t1d_primary_diagnosis() |> # get_only_insulin_purchases() |>