-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
**What changes are proposed in this pull request?** * Style this entry in a way that can be copied directly into `NEWS.md`. (#<issue number>, @<username>) Provide more detail here as needed. **Reference GitHub issue associated with pull request.** _e.g., 'closes #<issue number>'_ closes #273 -------------------------------------------------------------------------------- Pre-review Checklist (if item does not apply, mark is as complete) - [ ] **All** GitHub Action workflows pass with a ✅ - [ ] PR branch has pulled the most recent updates from master branch: `usethis::pr_merge_main()` - [ ] If a bug was fixed, a unit test was added. - [ ] Code coverage is suitable for any new functions/features (generally, 100% coverage for new code): `devtools::test_coverage()` - [ ] Request a reviewer Reviewer Checklist (if item does not apply, mark is as complete) - [ ] If a bug was fixed, a unit test was added. - [ ] Run `pkgdown::build_site()`. Check the R console for errors, and review the rendered website. - [ ] Code coverage is suitable for any new functions/features: `devtools::test_coverage()` When the branch is ready to be merged: - [ ] Update `NEWS.md` with the changes from this pull request under the heading "`# cards (development version)`". If there is an issue associated with the pull request, reference it in parentheses at the end update (see `NEWS.md` for examples). - [ ] **All** GitHub Action workflows pass with a ✅ - [ ] Approve Pull Request - [ ] Merge the PR. Please use "Squash and merge" or "Rebase and merge". --------- Signed-off-by: Daniel Sjoberg <[email protected]>
- Loading branch information
Showing
9 changed files
with
333 additions
and
16 deletions.
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
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,88 @@ | ||
#' Stratified ARD | ||
#' | ||
#' @description | ||
#' `r lifecycle::badge('experimental')`\cr | ||
#' General function for calculating ARD results within subgroups. | ||
#' | ||
#' While the examples below show use with other functions from the cards package, | ||
#' this function would primarily be used with the statistical functions in the | ||
#' cardx functions. | ||
#' | ||
#' @param .data (`data.frame`)\cr | ||
#' a data frame | ||
#' @param .by,.strata ([`tidy-select`][dplyr::dplyr_tidy_select])\cr | ||
#' columns to tabulate by/stratify by for calculation. | ||
#' Arguments are similar, but with an important distinction: | ||
#' | ||
#' `.by`: results are tabulated by **all combinations** of the columns specified, | ||
#' including unobserved combinations and unobserved factor levels. | ||
#' | ||
#' `.strata`: results are tabulated by **all _observed_ combinations** of the | ||
#' columns specified. | ||
#' | ||
#' These argument *should not* include any columns that appear in the `.f` argument. | ||
#' @param .f (`function`, `formula`)\cr | ||
#' a function or a formula that can be coerced to a function with | ||
#' `rlang::as_function()` (similar to `purrr::map(.f)`) | ||
#' @param ... Additional arguments passed on to the `.f` function. | ||
#' | ||
#' @return an ARD data frame of class 'card' | ||
#' @export | ||
#' | ||
#' @examples | ||
#' ard_strata( | ||
#' ADSL, | ||
#' by = ARM, | ||
#' .f = ~ ard_continuous(.x, variables = AGE) | ||
#' ) | ||
ard_strata <- function(.data, .by = NULL, .strata = NULL, .f, ...) { | ||
set_cli_abort_call() | ||
|
||
# check inputs --------------------------------------------------------------- | ||
check_not_missing(.data) | ||
check_not_missing(.f) | ||
check_data_frame(.data) | ||
|
||
# process inputs ------------------------------------------------------------- | ||
.f <- rlang::as_function(x = .f, call = get_cli_abort_call()) | ||
process_selectors(.data, .by = {{ .by }}, .strata = {{ .strata }}) | ||
|
||
# nest the data frame -------------------------------------------------------- | ||
df_nested_data <- nest_for_ard(.data, by = .by, strata = .strata) | ||
|
||
# run fn on nested data frames ----------------------------------------------- | ||
df_nested_data <- df_nested_data |> | ||
dplyr::mutate(ard = lapply(.data$data, .f, ...)) |> | ||
dplyr::select(-"data") | ||
|
||
# rename grouping variables -------------------------------------------------- | ||
# get the number grouping columns in the calculations | ||
max_group_n <- | ||
map( | ||
df_nested_data$ard, | ||
~ dplyr::select(.x, all_ard_groups("names")) |> names() | ||
) |> | ||
unlist() |> | ||
unique() |> | ||
sort() |> | ||
str_remove(pattern = "^group") |> | ||
as.integer() %>% | ||
# if no grouping variables are present, this will return `-Inf` | ||
{suppressWarnings(max(..1 = .))} # styler: off | ||
|
||
if (!is.infinite(max_group_n) && !is_empty(c(.by, .strata))) { | ||
new_group_colnames <- | ||
c( | ||
paste0("group", seq_along(c(.by, .strata)) + max_group_n), | ||
paste0("group", seq_along(c(.by, .strata)) + max_group_n, "_level") | ||
) |> | ||
sort() | ||
names(df_nested_data)[seq_along(new_group_colnames)] <- new_group_colnames | ||
} | ||
|
||
# unnest ard data frame and return final table ------------------------------- | ||
df_nested_data |> | ||
tidyr::unnest(cols = all_of("ard")) |> | ||
as_card() |> | ||
tidy_ard_column_order() | ||
} |
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
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,46 @@ | ||
# ard_strata() works | ||
|
||
Code | ||
ard_strata(ADSL, .by = ARM, .f = ~ ard_continuous(.x, variables = AGE)) | ||
Message | ||
{cards} data frame: 24 x 10 | ||
Output | ||
group1 group1_level variable stat_name stat_label stat | ||
1 ARM Placebo AGE N N 86 | ||
2 ARM Placebo AGE mean Mean 75.209 | ||
3 ARM Placebo AGE sd SD 8.59 | ||
4 ARM Placebo AGE median Median 76 | ||
5 ARM Placebo AGE p25 Q1 69 | ||
6 ARM Placebo AGE p75 Q3 82 | ||
7 ARM Placebo AGE min Min 52 | ||
8 ARM Placebo AGE max Max 89 | ||
9 ARM Xanomeli… AGE N N 84 | ||
10 ARM Xanomeli… AGE mean Mean 74.381 | ||
Message | ||
i 14 more rows | ||
i Use `print(n = ...)` to see more rows | ||
i 4 more variables: context, fmt_fn, warning, error | ||
|
||
--- | ||
|
||
Code | ||
ard_strata(ADSL, .strata = ARM, .f = ~ ard_continuous(.x, variables = AGE, by = AGEGR1)) | ||
Message | ||
{cards} data frame: 72 x 12 | ||
Output | ||
group1 group1_level group2 group2_level variable stat_name stat_label stat | ||
1 AGEGR1 65-80 ARM Placebo AGE N N 42 | ||
2 AGEGR1 65-80 ARM Placebo AGE mean Mean 73.595 | ||
3 AGEGR1 65-80 ARM Placebo AGE sd SD 4.173 | ||
4 AGEGR1 65-80 ARM Placebo AGE median Median 74 | ||
5 AGEGR1 65-80 ARM Placebo AGE p25 Q1 70 | ||
6 AGEGR1 65-80 ARM Placebo AGE p75 Q3 77 | ||
7 AGEGR1 65-80 ARM Placebo AGE min Min 65 | ||
8 AGEGR1 65-80 ARM Placebo AGE max Max 80 | ||
9 AGEGR1 <65 ARM Placebo AGE N N 14 | ||
10 AGEGR1 <65 ARM Placebo AGE mean Mean 61.143 | ||
Message | ||
i 62 more rows | ||
i Use `print(n = ...)` to see more rows | ||
i 4 more variables: context, fmt_fn, warning, error | ||
|
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,37 @@ | ||
test_that("ard_strata() works", { | ||
expect_snapshot( | ||
ard_strata( | ||
ADSL, | ||
.by = ARM, | ||
.f = ~ ard_continuous(.x, variables = AGE) | ||
) | ||
) | ||
|
||
expect_snapshot( | ||
ard_strata( | ||
ADSL, | ||
.strata = ARM, | ||
.f = ~ ard_continuous(.x, variables = AGE, by = AGEGR1) | ||
) | ||
) | ||
|
||
expect_equal( | ||
ard_strata(ADSL, .by = ARM, .f = ~ ard_continuous(.x, by = c(SEX, AGEGR1), variables = AGE)) |> | ||
tidy_ard_column_order() |> | ||
tidy_ard_row_order(), | ||
ard_continuous(ADSL, by = c(SEX, AGEGR1, ARM), variables = AGE) |> | ||
tidy_ard_row_order() | ||
) | ||
}) | ||
|
||
test_that("ard_strata(by,strata) when both empty", { | ||
expect_equal( | ||
ard_strata(ADSL, .f = ~ ard_continuous(.x, variables = AGE)), | ||
ard_continuous(ADSL, variables = AGE) | ||
) | ||
|
||
expect_equal( | ||
ard_strata(ADSL, .f = ~ ard_continuous(.x, by = ARM, variables = AGE)), | ||
ard_continuous(ADSL, by = ARM, variables = AGE) | ||
) | ||
}) |