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

feat: function to join the LPR2 registers #118

Merged
merged 26 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
3d83ef8
feat: create (draft) function to include hba1c criteria
lwjohnst86 May 16, 2024
d7093bb
feat: internal function to extract logic from algorithm data. Untested
lwjohnst86 May 16, 2024
e65b1b6
docs: add roxygen docs to logic getter
lwjohnst86 May 16, 2024
e45e1e2
docs: keep this function as internal only
lwjohnst86 May 16, 2024
b4c36da
refactor: keep only earliest two dates, might not work with some data…
lwjohnst86 May 16, 2024
ed7bb34
docs: regenerated the roxygen docs
lwjohnst86 May 16, 2024
a44448d
Merged origin/main into main
lwjohnst86 May 16, 2024
b477b0a
Merged origin/main into main
lwjohnst86 May 30, 2024
fb401be
docs: apply suggestions from review
lwjohnst86 May 31, 2024
dfb0b79
Merge branch 'main' of https://github.com/steno-aarhus/osdc
lwjohnst86 Jun 14, 2024
f25c183
Merge branch 'main' of https://github.com/steno-aarhus/osdc
lwjohnst86 Jun 14, 2024
727ef7c
feat: :sparkles: add the function to join the LPR2 registers
lwjohnst86 Jun 14, 2024
0d3c2cd
test: :white_check_mark: add a unit test for the `join_lpr2()` function
lwjohnst86 Jun 14, 2024
2482433
chore: regenerate roxygen docs
lwjohnst86 Jun 14, 2024
e70cc5c
Merged origin/main into feat/include-hba1c-criteria
lwjohnst86 Jun 14, 2024
8c532f9
Merge branch 'feat/include-hba1c-criteria' of https://github.com/sten…
lwjohnst86 Jun 14, 2024
2668ef2
Merge branch 'main' of https://github.com/steno-aarhus/osdc into feat…
lwjohnst86 Jun 19, 2024
e784741
Expanded tests a bit to more accurately replicate real life data
Jun 25, 2024
acadc6c
Merge branch 'main' into feat/join-lpr2
Aastedet Jun 25, 2024
6484724
Changed to inner join and added non-joinable rows to simulate bad dat…
Jun 25, 2024
4f8d296
Merge branch 'feat/join-lpr2' of https://github.com/steno-aarhus/osdc…
Jun 25, 2024
5f8e800
docs: lpr_adm should be first in argument list
lwjohnst86 Jun 25, 2024
8f73a6c
style: only reformatting
lwjohnst86 Jun 25, 2024
df9cc09
test: add helper script to set Arrow options for warnings
lwjohnst86 Jun 25, 2024
4b0f391
test: revise to test if other data sources also calculate correctly
lwjohnst86 Jun 25, 2024
2d61df6
Merged origin/main into feat/join-lpr2
lwjohnst86 Jun 25, 2024
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
20 changes: 20 additions & 0 deletions R/joins.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#' 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
#' 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"
)
}
24 changes: 24 additions & 0 deletions man/join_lpr2.Rd

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

3 changes: 3 additions & 0 deletions tests/testthat/helper-options.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# There is a warning when using Arrow about using pull. It says to set
# this option.
options(arrow.pull_as_vector = TRUE)
93 changes: 93 additions & 0 deletions tests/testthat/test-joins.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
actual_lpr_diag <- tibble::tibble(
recnum = rep(1:6, each = 2),
c_diag = 1:12,
c_diagtype = rep(c("A", "B"), 6)
)

actual_lpr_adm <- tibble::tibble(
pnr = rep(1:2, 3),
recnum = 2:7,
c_spec = 1:6,
d_inddto = c("20230101", "20220101", "20210101", "20200101", "20190101", "20180101"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is c_pattype missing here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Um, c_pattype isn't in the variable list.

)

expected_lpr2 <- tibble::tibble(
pnr = rep(1:2, length.out = 10, each = 2),
recnum = rep(2:6, length.out = 10, each = 2),
c_spec = rep(1:5, length.out = 10, each = 2),
d_inddto = rep(c("20230101", "20220101", "20210101", "20200101", "20190101"), each = 2),
c_diag = 3:12,
c_diagtype = rep(c("A", "B"), length.out = 10)
)

test_that("joining LPR2 correctly", {
actual <- join_lpr2(
actual_lpr_adm,
actual_lpr_diag
)

expect_equal(actual, expected_lpr2)
})

test_that("lpr_adm and lpr_diag must be in correct arg position", {
expect_error(join_lpr2(
actual_lpr_diag,
actual_lpr_adm
))
})

test_that("joining works for DuckDB Database", {
actual <- arrow::to_duckdb(actual_lpr_adm) |>
join_lpr2(arrow::to_duckdb(actual_lpr_diag))

actual_rows <- actual |>
dplyr::count() |>
dplyr::pull(n) |>
as.integer()

expect_contains(class(actual), "tbl_duckdb_connection")
expect_identical(colnames(actual), colnames(expected_lpr2))
expect_identical(actual_rows, nrow(expected_lpr2))
})

test_that("joining works for Arrow Tables (from Parquet)", {
actual <- arrow::as_arrow_table(actual_lpr_adm) |>
join_lpr2(arrow::as_arrow_table(actual_lpr_diag))

actual_rows <- actual |>
dplyr::count() |>
dplyr::pull(n) |>
as.integer()

expect_contains(class(actual), "arrow_dplyr_query")
expect_identical(names(actual), colnames(expected_lpr2))
expect_identical(actual_rows, nrow(expected_lpr2))
})

test_that("joining works for data.frame", {
actual <- as.data.frame(actual_lpr_adm) |>
join_lpr2(as.data.frame(actual_lpr_diag))

actual_rows <- actual |>
dplyr::count() |>
dplyr::pull(n) |>
as.integer()

expect_contains(class(actual), "data.frame")
expect_identical(names(actual), colnames(expected_lpr2))
expect_identical(actual_rows, nrow(expected_lpr2))
})

test_that("joining works for data.table", {
actual <- data.table::as.data.table(actual_lpr_adm) |>
join_lpr2(data.table::as.data.table(actual_lpr_diag))

actual_rows <- actual |>
dplyr::count() |>
dplyr::pull(n) |>
as.integer()

expect_contains(class(actual), "data.table")
expect_identical(colnames(actual), colnames(expected_lpr2))
expect_identical(actual_rows, nrow(expected_lpr2))
})
Loading