Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
ddsjoberg committed May 23, 2024
1 parent c99e8d7 commit 1be06f4
Show file tree
Hide file tree
Showing 8 changed files with 321 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ ard_moodtest() -> ard_stats_mood_test()
- `ard_survey_svyranktest()` for weighted/survey rank tests using `survey::svyranktest()`. (#71)
- `ard_car_vif()` for calculating the variance inflation factor using `car::vif()`. (#10)
- `ard_emmeans_mean_difference()` for calculating the least-squares mean differences using the {emmeans} package. (#34)
- `ard_stats_wilcox_test_onesample()` for calculating one-sample results.
- `ard_stats_t_test_onesample()` for calculating one-sample results.

* Updated functions `ard_stats_t_test()`, `ard_stats_paired_t_test()`, `ard_stats_wilcox_test()`, `ard_stats_paired_wilcox_test()`, `ard_stats_chisq_test()`, `ard_stats_fisher_test()`, `ard_stats_kruskal_test()`, `ard_stats_mcnemar_test()`, and `ard_stats_mood_test()` to accept multiple variables at once. Independent tests are calculated for each variable. The `variable` argument is renamed to `variables`. (#77)

Expand Down
67 changes: 67 additions & 0 deletions R/ard_stats_t_test_onesample.R
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()
}
68 changes: 68 additions & 0 deletions R/ard_stats_wilcox_test_onesample.R
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()
}
2 changes: 2 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ reference:
- ard_stats_oneway_test
- ard_stats_prop_test
- ard_stats_t_test
- ard_stats_t_test_onesample
- ard_stats_wilcox_test
- ard_stats_wilcox_test_onesample

- subtitle: "{aod} package"
- contents:
Expand Down
43 changes: 43 additions & 0 deletions man/ard_stats_t_test_onesample.Rd

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

43 changes: 43 additions & 0 deletions man/ard_stats_wilcox_test_onesample.Rd

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

47 changes: 47 additions & 0 deletions tests/testthat/test-ard_stats_t_test_onesample.R
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()
)
})

49 changes: 49 additions & 0 deletions tests/testthat/test-ard_stats_wilcox_test_onesample.R
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()
)
})

0 comments on commit 1be06f4

Please sign in to comment.