-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: ♻️ function to create LPR2 #152
Draft
lwjohnst86
wants to merge
1
commit into
main
Choose a base branch
from
refactor/updates-to-lpr2-creation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#' Create the LPR2 data needed for later purposes. | ||
#' | ||
#' Needs the LPR2 (`lpr_diag` and `lpr_adm`) registers. | ||
#' | ||
#' @param lpr_diag The diagnosis register. | ||
#' @param lpr_adm The admission register. | ||
#' | ||
#' @return The same class as the input, defaults to a [tibble::tibble()]. | ||
#' @keywords internal | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' create_lpr2( | ||
#' lpr_adm = register_data$lpr_adm, | ||
#' lpr_diag = register_data$lpr_diag | ||
#' ) | ||
#' } | ||
create_lpr2 <- function(lpr_adm, lpr_diag) { | ||
verify_required_variables(lpr_adm, "lpr_adm") | ||
verify_required_variables(lpr_diag, "lpr_diag") | ||
lpr_diag <- lpr_diag |> | ||
column_names_to_lower() |> | ||
keep_needed_diagnosis_code() |> | ||
keep_needed_diagnosis_type() | ||
|
||
join_lpr2( | ||
lpr_diag = lpr_diag, | ||
lpr_adm = column_names_to_lower(lpr_adm) | ||
) |> | ||
dplyr::mutate( | ||
is_primary = .data$c_diagtype == "A", | ||
# TODO: Include this in algorithm dataframe some how? | ||
is_t1d = stringr::str_detect(.data$c_diag, "^(249|DE10)"), | ||
is_t2d = stringr::str_detect(.data$c_diag, "^(250|DE11)"), | ||
department = get_department(.data$c_spec), | ||
date = yyyymmdd_to_iso(.data$d_inddto) | ||
) |> | ||
dplyr::select( | ||
"pnr", | ||
"date", | ||
"is_primary", | ||
"is_t1d", | ||
"is_t2d", | ||
"department" | ||
) | ||
} | ||
|
||
keep_needed_diagnosis_code <- function(data) { | ||
# TODO: Include this in algorithm dataframe some how? | ||
data |> | ||
dplyr::filter(stringr::str_detect(.data$c_diag, "^(DO|DZ3|DE1[0-4]|249|250)")) | ||
} | ||
|
||
keep_needed_diagnosis_type <- function(data) { | ||
# TODO: Include this in algorithm dataframe some how? | ||
data |> | ||
dplyr::filter(.data$c_diagtype %in% c("A", "B")) | ||
} | ||
|
||
join_lpr2 <- function(lpr_adm, lpr_diag) { | ||
dplyr::inner_join( | ||
lpr_adm, | ||
lpr_diag, | ||
by = "recnum" | ||
) | ||
} | ||
|
||
get_department <- function(x) { | ||
dplyr::case_when( | ||
# TODO: Include this in algorithm dataframe some how? | ||
x == "08" ~ "endocrinology", | ||
# < 8 and between 9-30 | ||
stringr::str_detect(x, "(0[0-8]|9|[1-3][0-9])") ~ "other medical" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also potential for data quirks here, of course. |
||
) | ||
} | ||
|
||
yyyymmdd_to_iso <- function(x) { | ||
date = lubridate::as_date(x, format = "%Y%m%d") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the real world data for some users may be weird if it's been converted from SAS format and changed to integer type in the process, so maybe we should make this piece of code robust to those sort of quirks? e.g. x == "08" | == 8?