Skip to content
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
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
79 changes: 79 additions & 0 deletions R/create-lpr2.R
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",
Copy link
Collaborator

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?

# < 8 and between 9-30
stringr::str_detect(x, "(0[0-8]|9|[1-3][0-9])") ~ "other medical"
Copy link
Collaborator

Choose a reason for hiding this comment

The 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")
}
23 changes: 0 additions & 23 deletions R/joins.R
Original file line number Diff line number Diff line change
@@ -1,26 +1,3 @@
#' Join together 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{
#' register_data$lpr_adm |>
#' join_lpr2(register_data$lpr_diag)
#' }
join_lpr2 <- function(lpr_adm, lpr_diag) {
verify_required_variables(lpr_adm, "lpr_adm")
verify_required_variables(lpr_diag, "lpr_diag")
dplyr::inner_join(
column_names_to_lower(lpr_adm),
column_names_to_lower(lpr_diag),
by = "recnum"
)
}

#' Join together the LPR3 (`diagnoser` and `kontakter`) registers.
#'
#' @param diagnoser The diagnosis register.
Expand Down
Loading