Skip to content

Commit

Permalink
Adding one-sample CI functions (#156)
Browse files Browse the repository at this point in the history
**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
ddsjoberg authored May 23, 2024
1 parent c99e8d7 commit e2e448c
Show file tree
Hide file tree
Showing 11 changed files with 339 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export(ard_stats_paired_t_test)
export(ard_stats_paired_wilcox_test)
export(ard_stats_prop_test)
export(ard_stats_t_test)
export(ard_stats_t_test_onesample)
export(ard_stats_wilcox_test)
export(ard_stats_wilcox_test_onesample)
export(ard_survey_svychisq)
export(ard_survey_svyranktest)
export(ard_survey_svyttest)
Expand Down
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
71 changes: 71 additions & 0 deletions R/ard_stats_t_test_onesample.R
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()
}
72 changes: 72 additions & 0 deletions R/ard_stats_wilcox_test_onesample.R
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()
}
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ editor_options:
wrap: 72
---

# cardx <a href="https://insightsengineering.github.io/cardx"><img src="man/figures/logo.png" alt="cardx website" align="right" height="138"/></a>
# cardx <a href="https://insightsengineering.github.io/cardx/"><img src="man/figures/logo.png" alt="cardx website" align="right" height="138"/></a>

[![R-CMD-check](https://github.com/insightsengineering/cardx/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/insightsengineering/cardx/actions/workflows/R-CMD-check.yaml)
[![Codecov test
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# cardx <a href="https://insightsengineering.github.io/cardx"><img src="man/figures/logo.png" alt="cardx website" align="right" height="138"/></a>
# cardx <a href="https://insightsengineering.github.io/cardx/"><img src="man/figures/logo.png" alt="cardx website" align="right" height="138"/></a>

[![R-CMD-check](https://github.com/insightsengineering/cardx/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/insightsengineering/cardx/actions/workflows/R-CMD-check.yaml)
[![Codecov test
Expand Down
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.

50 changes: 50 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,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()
)
})
52 changes: 52 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,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()
)
})

0 comments on commit e2e448c

Please sign in to comment.