-
-
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.
- Loading branch information
Showing
8 changed files
with
321 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#' 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 | ||
#' | ||
#' @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)) |> | ||
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,68 @@ | ||
#' 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 | ||
#' | ||
#' @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)) |> | ||
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
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,47 @@ | ||
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() | ||
) | ||
|
||
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,49 @@ | ||
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() | ||
) | ||
|
||
expect_equal( | ||
ard_stats_wilcox_test_onesample( | ||
cards::ADSL, | ||
variables = character(0) | ||
), | ||
dplyr::tibble() | ||
) | ||
}) | ||
|