-
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
feat: ✨ add include_podiatrist_services()
with tests
#182
Open
signekb
wants to merge
17
commits into
main
Choose a base branch
from
feat/include-podiatrist-services
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.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6eb2d35
docs: :memo: add podiatrist services logic
signekb de2cd32
build: :heavy_plus_sign: add lubridate
signekb dedd5e2
feat: :white_check_mark: add `yyww_to_date()` with tests
signekb 8bd888d
chore: :bento: run pipeline to include podiatrist services logic
signekb 1fb9dcb
feat: :white_check_mark: add `include_podiatrist_services()` with tests
signekb 495684c
docs: :memo: add and update function docs autogenerated by roxygen
signekb 7b0e2bc
docs: :pencil2: fix typo
signekb a78c218
chore: :bento: Re-generated roxygen2 documentation
signekb 9d188a6
test: :test_tube: add test for numeric year 2000
signekb ed910a9
fix: :bug: change paste0 to sprintf
signekb 4f52c23
feat: :sparkles: update based on #163
signekb 588e62d
test: :white_check_mark: add row with non-diabetes-specific podiatris…
signekb 128cdec
chore: :bento: Re-generated roxygen2 documentation
signekb d57a8c2
docs: pencil2: lower case comment
signekb d15ab93
refactor: :recycle: rename `yyww_to_date` to `yyww_to_yyyymmdd()` wit…
signekb 39efded
refactor: :truck: rename function and test file to `-yyyymmdd` instea…
signekb 4175fa2
chore: :bento: Re-generated roxygen2 documentation
signekb 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
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,57 @@ | ||
#' Include diabetes-specific podiatrist services. | ||
#' | ||
#' #' Uses the `sysi` or `sssy` registers as input to extract the dates of all | ||
#' diabetes-specific podiatrist services. Removes duplicate services on the | ||
#' same date. Only the two earliest dates per individual are kept. | ||
#' | ||
#' #' The output is passed to the `join_inclusions()` function for the final | ||
#' step of the inclusion process. | ||
#' | ||
#' @param sysi The SYSI register. | ||
#' @param sssy The SSSY register. | ||
#' | ||
#' @return The same type as the input data, default as a [tibble::tibble()], | ||
#' with two columns and up to two rows for each individual: | ||
#' | ||
#' - `pnr`: Identifier variable | ||
#' - `date`: The dates of the first and second diabetes-specific | ||
#' podiatrist record | ||
#' - `has_podiatrist_services`: A logical variable that acts as a helper | ||
#' indicator for use in later functions. | ||
#' | ||
#' @keywords internal | ||
#' @inherit algorithm seealso | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' include_podiatrist_services(register_data$sysi, register_data$sssy) | ||
#' } | ||
include_podiatrist_services <- function(sysi, sssy) { | ||
verify_required_variables(sysi, "sysi") | ||
verify_required_variables(sssy, "sssy") | ||
criteria <- get_algorithm_logic("podiatrist_services") |> | ||
# To convert the string into an R expression. | ||
rlang::parse_expr() | ||
|
||
column_names_to_lower(sysi) |> | ||
dplyr::full_join(column_names_to_lower(sssy), | ||
by = dplyr::join_by(pnr, barnmak, speciale, honuge) | ||
) |> | ||
# filter based algorithm logic | ||
dplyr::filter(!!criteria) |> | ||
# remove duplicates | ||
dplyr::distinct() |> | ||
# keep only the two columns we need and transform `honuge` to date | ||
dplyr::mutate( | ||
pnr = .data$pnr, | ||
date = yyww_to_yyyymmdd(.data$honuge), | ||
.keep = "none" | ||
) |> | ||
# FIXME: This might be computationally intensive. | ||
dplyr::group_by(.data$pnr) |> | ||
# keep earliest two dates per individual | ||
dplyr::filter(dplyr::row_number(.data$date) %in% 1:2) |> | ||
dplyr::ungroup() |> | ||
# create Boolean helper variable | ||
dplyr::mutate(has_podiatrist_services = TRUE) | ||
} |
Binary file not shown.
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,38 @@ | ||
#' Convert date format YYWW to YYYY-MM-DD | ||
#' | ||
#' Since the exact date isn't given in the input, this function will set the | ||
#' date to Monday of the week. | ||
#' | ||
#' As a precaution, a leading zero is added if it has been removed | ||
#' This can e.g., happen if the input was "0107" and has been converted to a | ||
#' numeric 107. | ||
#' | ||
#' @param yyww Character(s) of the format yyww | ||
#' | ||
#' @returns Date(s) in the format YYYY-MM-DD. | ||
#' | ||
#' @keywords internal | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' yyww_to_yyyymmdd("0102") | ||
#' yyww_to_yyyymmdd(c("0102", "0304")) | ||
#' } | ||
yyww_to_yyyymmdd <- function(yyww) { | ||
# ensure input is zero-padded to length 4 | ||
yyww <- sprintf("%04d", as.numeric(yyww)) | ||
|
||
year <- stringr::str_sub(yyww, 1, 2) | ||
week <- stringr::str_sub(yyww, 3, 4) | ||
|
||
# define helper variables | ||
first_day_of_iso_year <- lubridate::ymd(paste(year, "-01-04")) | ||
n_weekday_start_of_year <- lubridate::wday(first_day_of_iso_year, week_start = 1) | ||
|
||
# calculate date | ||
date <- first_day_of_iso_year | ||
lubridate::week(date) <- as.numeric(week) | ||
date <- date - n_weekday_start_of_year + 1 # adjust date to be Monday in that week | ||
|
||
return(date) | ||
} |
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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name,logic | ||
hba1c,(analysiscode == 'NPU27300' AND value >= 48) OR (analysiscode == 'NPU03835' AND value >= 6.5) | ||
gld,atc =~ '^A10' | ||
|
||
podiatrist_services,speciale =~ '^54' AND barnmak != 0 |
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,113 @@ | ||
sysi <- tibble::tribble( | ||
~pnr, ~barnmak, ~speciale, ~honuge, | ||
1000000000, 0, 54711, "1879", # removed since barnmark = 0 | ||
2000000000, 1, 54800, "9207", # kept but deduplicated | ||
2000000000, 1, 54800, "9207", # kept but deduplicated | ||
3000000000, 1, 54005, "0752", # kept bc it's the first date for this person | ||
3000000000, 1, 54005, "2430", # removed bc it's the third date for this person | ||
4000000000, 1, 55000, "0044" # removed since speciale doesn't start with 54 | ||
) | ||
|
||
sssy <- tibble::tribble( | ||
~pnr, ~barnmak, ~speciale, ~honuge, | ||
2000000000, 1, 54800, "9207", # kept but deduplicated | ||
3000000000, 0, 10000, "1801", # removed since barnmark = 0 | ||
3000000000, 1, 54005, "0830", # kept bc it's the second date for this person | ||
4000000000, 1, 76255, "1123", # removed since speciale doesn't start with 54 | ||
) | ||
|
||
expected <- tibble::tribble( | ||
~pnr, ~date, ~has_podiatrist_services, | ||
2000000000, lubridate::ymd("1992-02-10"),TRUE, | ||
3000000000, lubridate::ymd("2007-12-24"),TRUE, | ||
3000000000, lubridate::ymd("2008-07-21"),TRUE | ||
) | ||
|
||
|
||
test_that("sysi needs expected variables", { | ||
sysi <- sysi[-2] | ||
expect_error(include_podiatrist_services(sysi, sssy)) | ||
}) | ||
|
||
test_that("ssy needs expected variables", { | ||
sssy <- sssy[-2] | ||
expect_error(include_podiatrist_services(sysi, sssy)) | ||
}) | ||
|
||
|
||
test_that("those with inclusion are kept", { | ||
actual <- include_podiatrist_services(sysi, sssy) | ||
expect_equal(actual, expected) | ||
}) | ||
|
||
test_that("casing of input variables doesn't matter", { | ||
sysi <- sysi |> | ||
dplyr::rename_with(\(columns) toupper(columns)) | ||
sssy <- sssy |> | ||
dplyr::rename_with(\(columns) toupper(columns)) | ||
actual <- include_podiatrist_services(sysi, sssy) | ||
expect_equal(actual, expected) | ||
}) | ||
|
||
test_that("verification works for DuckDB Database", { | ||
skip_on_cran() | ||
skip_if_not_installed("duckplyr") | ||
|
||
sysi <- duckplyr::as_duckplyr_tibble(sysi) | ||
sssy <- duckplyr::as_duckplyr_tibble(sssy) | ||
actual <- include_podiatrist_services(sysi, sssy) | ||
|
||
actual_rows <- actual |> | ||
dplyr::count() |> | ||
dplyr::pull(n) | ||
|
||
expect_equal(actual_rows, nrow(expected)) | ||
expect_equal(colnames(actual), colnames(expected)) | ||
}) | ||
|
||
test_that("verification works for Arrow Tables (from Parquet)", { | ||
# FIXME: This test fails because of some issue with the criteria and colnames | ||
skip() | ||
skip_on_cran() | ||
skip_if_not_installed("arrow") | ||
|
||
sysi <- arrow::as_arrow_table(sysi) | ||
sssy <- arrow::as_arrow_table(sssy) | ||
actual <- include_podiatrist_services(sysi, sssy) | ||
|
||
actual_rows <- actual |> | ||
dplyr::count() |> | ||
dplyr::pull(n) | ||
|
||
expect_equal(actual_rows, nrow(expected)) | ||
# TODO: Arrow doesn't work with colname(), fix? | ||
expect_equal(names(actual), colnames(expected)) | ||
}) | ||
|
||
test_that("verification works for data.frame", { | ||
sysi <- as.data.frame(sysi) | ||
ssy <- as.data.frame(sssy) | ||
actual <- include_podiatrist_services(sysi, sssy) | ||
|
||
actual_rows <- actual |> | ||
dplyr::count() |> | ||
dplyr::pull(n) | ||
|
||
expect_equal(actual_rows, nrow(expected)) | ||
expect_equal(colnames(actual), colnames(expected)) | ||
}) | ||
|
||
test_that("verification works for data.table", { | ||
skip_on_cran() | ||
skip_if_not_installed("data.table") | ||
sysi <- data.table::as.data.table(sysi) | ||
sssy <- data.table::as.data.table(sssy) | ||
actual <- include_podiatrist_services(sysi, sssy) | ||
|
||
actual_rows <- actual |> | ||
dplyr::count() |> | ||
dplyr::pull(n) | ||
|
||
expect_equal(actual_rows, nrow(expected)) | ||
expect_equal(colnames(actual), colnames(expected)) | ||
}) |
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,28 @@ | ||
test_that("conversion works when 01-01 is Monday in week 1", { | ||
expect_equal(yyww_to_yyyymmdd("2439"), lubridate::ymd("2024-09-23")) | ||
}) | ||
|
||
test_that("conversion works when 01-01 is Friday in week 52 of the prior year", { | ||
expect_equal(yyww_to_yyyymmdd("9307"), lubridate::ymd("1993-02-15")) | ||
}) | ||
|
||
test_that("conversion works for week 53", { | ||
expect_equal(yyww_to_yyyymmdd("1853"), lubridate::ymd("2018-12-31")) | ||
}) | ||
|
||
test_that("conversion works for zero-padded year and week (i.e., numbers < 10)", { | ||
expect_equal(yyww_to_yyyymmdd("0107"), lubridate::ymd("2001-02-12")) | ||
}) | ||
|
||
test_that("conversion works for numeric one digit year", { | ||
# This could happen if the input was "0107" and has been converted to numeric | ||
expect_equal(yyww_to_yyyymmdd(107), lubridate::ymd("2001-02-12")) | ||
}) | ||
|
||
test_that("conversion works for numeric year 2000", { | ||
expect_equal(yyww_to_yyyymmdd(0007), lubridate::ymd("2000-02-14")) | ||
}) | ||
|
||
test_that("conversion works for multiple inputs", { | ||
expect_equal(yyww_to_yyyymmdd(c("0107", "2439")), lubridate::ymd("2001-02-12", "2024-09-23")) | ||
}) |
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.
After explicitly adding the columns to join by, I get a note from
devtools::check()
:I'm not sure how to fix this, since
join_by()
doesn't seem to allow the use of.data$