-
-
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.
Adding one-sample CI functions (#156)
**What changes are proposed in this pull request?** - `ard_stats_wilcox_test_onesample()` for calculating one-sample results. - `ard_stats_t_test_onesample()` for calculating one-sample results. -------------------------------------------------------------------------------- Pre-review Checklist (if item does not apply, mark is as complete) - [x] **All** GitHub Action workflows pass with a ✅ - [x] PR branch has pulled the most recent updates from master branch: `usethis::pr_merge_main()` - [x] If a bug was fixed, a unit test was added. - [x] If a new `ard_*()` function was added, it passes the ARD structural checks from `cards::check_ard_structure()`. - [x] If a new `ard_*()` function was added, `set_cli_abort_call()` has been set. - [x] If a new `ard_*()` function was added and it depends on another package (such as, `broom`), `is_pkg_installed("broom", reference_pkg = "cardx")` has been set in the function call and the following added to the roxygen comments: `@examplesIf do.call(asNamespace("cardx")$is_pkg_installed, list(pkg = "broom"", reference_pkg = "cardx"))` - [x] Code coverage is suitable for any new functions/features (generally, 100% coverage for new code): `devtools::test_coverage()` Reviewer Checklist (if item does not apply, mark is as complete) - [x] If a bug was fixed, a unit test was added. - [x] Code coverage is suitable for any new functions/features: `devtools::test_coverage()` When the branch is ready to be merged: - [x] Update `NEWS.md` with the changes from this pull request under the heading "`# cardx (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). - [x] **All** GitHub Action workflows pass with a ✅ - [x] Approve Pull Request - [x] Merge the PR. Please use "Squash and merge" or "Rebase and merge".
- Loading branch information
Showing
11 changed files
with
339 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#' ARD one-sample t-test | ||
#' | ||
#' @description | ||
#' Analysis results data for one-sample t-tests. | ||
#' Result may be stratified by including the `by` argument. | ||
#' | ||
#' @param data (`data.frame`)\cr | ||
#' a data frame. See below for details. | ||
#' @param variables ([`tidy-select`][dplyr::dplyr_tidy_select])\cr | ||
#' column names to be analyzed. Independent t-tests will be computed for | ||
#' each variable. | ||
#' @param by ([`tidy-select`][dplyr::dplyr_tidy_select])\cr | ||
#' optional column name to stratify results by. | ||
#' @inheritParams ard_stats_t_test | ||
#' | ||
#' @return ARD data frame | ||
#' @export | ||
#' | ||
#' @examplesIf do.call(asNamespace("cardx")$is_pkg_installed, list(pkg = "broom", reference_pkg = "cardx")) | ||
#' cards::ADSL |> | ||
#' ard_stats_t_test_onesample(by = ARM, variables = AGE) | ||
ard_stats_t_test_onesample <- function(data, variables, by = dplyr::group_vars(data), conf.level = 0.95, ...) { | ||
set_cli_abort_call() | ||
|
||
# check installed packages --------------------------------------------------- | ||
check_pkg_installed("broom", reference_pkg = "cardx") | ||
|
||
# check/process inputs ------------------------------------------------------- | ||
check_not_missing(data) | ||
check_not_missing(variables) | ||
check_data_frame(data) | ||
data <- dplyr::ungroup(data) | ||
cards::process_selectors(data, by = {{ by }}, variables = {{ variables }}) | ||
check_scalar_range(conf.level, range = c(0, 1)) | ||
|
||
# if no variables selected, return empty tibble ------------------------------ | ||
if (is_empty(variables)) { | ||
return(dplyr::tibble()) | ||
} | ||
|
||
cards::ard_continuous( | ||
data = data, | ||
variables = all_of(variables), | ||
by = all_of(by), | ||
statistic = all_of(variables) ~ list(t_test_onesample = \(x) stats::t.test(x = x, conf.level = conf.level, ...) |> broom::tidy()) | ||
) |> | ||
cards::bind_ard( | ||
cards::ard_continuous( | ||
data = data, | ||
variables = all_of(variables), | ||
by = all_of(by), | ||
statistic = | ||
all_of(variables) ~ | ||
list(conf.level = \(x) { | ||
formals(asNamespace("stats")[["t.test.default"]])["mu"] |> | ||
utils::modifyList(list(conf.level = conf.level, ...)) | ||
}) | ||
) | ||
) |> | ||
dplyr::select(-"stat_label") |> | ||
dplyr::left_join( | ||
.df_ttest_stat_labels(by = NULL), | ||
by = "stat_name" | ||
) |> | ||
dplyr::mutate( | ||
stat_label = dplyr::coalesce(.data$stat_label, .data$stat_name), | ||
context = "ard_stats_t_test_onesample", | ||
) |> | ||
cards::tidy_ard_row_order() |> | ||
cards::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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#' ARD one-sample Wilcox Rank-sum | ||
#' | ||
#' @description | ||
#' Analysis results data for one-sample Wilcox Rank-sum. | ||
#' Result may be stratified by including the `by` argument. | ||
#' | ||
#' @param data (`data.frame`)\cr | ||
#' a data frame. See below for details. | ||
#' @param variables ([`tidy-select`][dplyr::dplyr_tidy_select])\cr | ||
#' column names to be analyzed. Independent Wilcox Rank-sum tests will be computed for | ||
#' each variable. | ||
#' @param by ([`tidy-select`][dplyr::dplyr_tidy_select])\cr | ||
#' optional column name to stratify results by. | ||
#' @inheritParams ard_stats_wilcox_test | ||
#' | ||
#' @return ARD data frame | ||
#' @export | ||
#' | ||
#' @examplesIf do.call(asNamespace("cardx")$is_pkg_installed, list(pkg = "broom", reference_pkg = "cardx")) | ||
#' cards::ADSL |> | ||
#' ard_stats_wilcox_test_onesample(by = ARM, variables = AGE) | ||
ard_stats_wilcox_test_onesample <- function(data, variables, by = dplyr::group_vars(data), conf.level = 0.95, ...) { | ||
set_cli_abort_call() | ||
|
||
# check installed packages --------------------------------------------------- | ||
check_pkg_installed("broom", reference_pkg = "cardx") | ||
|
||
# check/process inputs ------------------------------------------------------- | ||
check_not_missing(data) | ||
check_not_missing(variables) | ||
check_data_frame(data) | ||
data <- dplyr::ungroup(data) | ||
cards::process_selectors(data, by = {{ by }}, variables = {{ variables }}) | ||
check_scalar_range(conf.level, range = c(0, 1)) | ||
|
||
# if no variables selected, return empty tibble ------------------------------ | ||
if (is_empty(variables)) { | ||
return(dplyr::tibble()) | ||
} | ||
|
||
cards::ard_continuous( | ||
data = data, | ||
variables = all_of(variables), | ||
by = all_of(by), | ||
statistic = all_of(variables) ~ list(t_test_onesample = \(x) stats::wilcox.test(x = x, conf.level = conf.level, ...) |> broom::tidy()) | ||
) |> | ||
cards::bind_ard( | ||
cards::ard_continuous( | ||
data = data, | ||
variables = all_of(variables), | ||
by = all_of(by), | ||
statistic = | ||
all_of(variables) ~ | ||
list(conf.level = \(x) { | ||
formals(asNamespace("stats")[["wilcox.test.default"]])[c("mu", "exact", "conf.int", "tol.root", "digits.rank")] |> | ||
utils::modifyList(list(conf.level = conf.level, ...)) |> | ||
compact() | ||
}) | ||
) | ||
) |> | ||
dplyr::select(-"stat_label") |> | ||
dplyr::left_join( | ||
.df_ttest_stat_labels(by = NULL), | ||
by = "stat_name" | ||
) |> | ||
dplyr::mutate( | ||
stat_label = dplyr::coalesce(.data$stat_label, .data$stat_name), | ||
context = "ard_stats_wilcox_test_onesample", | ||
) |> | ||
cards::tidy_ard_row_order() |> | ||
cards::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
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.
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,50 @@ | ||
skip_if_not(is_pkg_installed("broom", reference_pkg = "cardx")) | ||
|
||
test_that("ard_stats_t_test_onesample() works", { | ||
# first calculate an object to test against | ||
expect_silent( | ||
ard1 <- ard_stats_t_test_onesample( | ||
cards::ADSL, | ||
variables = AGE, | ||
by = ARM, | ||
conf.level = 0.9, | ||
mu = 1 | ||
) | ||
) | ||
|
||
# first check arguments passed and returned correctly | ||
expect_equal( | ||
cards::get_ard_statistics( | ||
ard1, | ||
group1_level %in% "Placebo" | ||
)[c("mu", "conf.level")], | ||
list(mu = 1, conf.level = 0.9) | ||
) | ||
# check results are correct | ||
expect_equal( | ||
cards::get_ard_statistics( | ||
ard1, | ||
group1_level %in% "Placebo" | ||
)[c("estimate", "conf.low", "conf.high", "p.value")], | ||
t.test( | ||
cards::ADSL$AGE[cards::ADSL$ARM == "Placebo"], | ||
conf.level = 0.9, | ||
mu = 1 | ||
) |> | ||
broom::tidy() |> | ||
dplyr::select(c("estimate", "conf.low", "conf.high", "p.value")) |> | ||
as.list() | ||
) | ||
|
||
# test the structure is good | ||
expect_silent(cards::check_ard_structure(ard1)) | ||
|
||
# empty tibble returned with no variables | ||
expect_equal( | ||
ard_stats_t_test_onesample( | ||
cards::ADSL, | ||
variables = character(0) | ||
), | ||
dplyr::tibble() | ||
) | ||
}) |
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,52 @@ | ||
skip_if_not(is_pkg_installed("broom", reference_pkg = "cardx")) | ||
|
||
test_that("ard_stats_wilcox_test_onesample() works", { | ||
# first calculate an object to test against | ||
expect_silent( | ||
ard1 <- ard_stats_wilcox_test_onesample( | ||
cards::ADSL, | ||
variables = AGE, | ||
by = ARM, | ||
conf.level = 0.9, | ||
conf.int = TRUE, | ||
mu = 1 | ||
) | ||
) | ||
|
||
# first check arguments passed and returned correctly | ||
expect_equal( | ||
cards::get_ard_statistics( | ||
ard1, | ||
group1_level %in% "Placebo" | ||
)[c("mu", "conf.level")], | ||
list(mu = 1, conf.level = 0.9) | ||
) | ||
# check results are correct | ||
expect_equal( | ||
cards::get_ard_statistics( | ||
ard1, | ||
group1_level %in% "Placebo" | ||
)[c("estimate", "conf.low", "conf.high", "p.value")], | ||
wilcox.test( | ||
cards::ADSL$AGE[cards::ADSL$ARM == "Placebo"], | ||
conf.level = 0.9, | ||
mu = 1, | ||
conf.int = TRUE | ||
) |> | ||
broom::tidy() |> | ||
dplyr::select(c("estimate", "conf.low", "conf.high", "p.value")) |> | ||
as.list() | ||
) | ||
|
||
# test the structure is good | ||
expect_silent(cards::check_ard_structure(ard1)) | ||
|
||
# empty tibble returned with no variables | ||
expect_equal( | ||
ard_stats_wilcox_test_onesample( | ||
cards::ADSL, | ||
variables = character(0) | ||
), | ||
dplyr::tibble() | ||
) | ||
}) |