Skip to content

Commit

Permalink
Updated ard_wilcoxtest() to allow multiple variables (#81)
Browse files Browse the repository at this point in the history
**What changes are proposed in this pull request?**
* Updated `ard_wilcoxtest()` to allow multiple variables

**Reference GitHub issue associated with pull request.** _e.g., 'closes
#<issue number>'_
closes #80 


--------------------------------------------------------------------------------

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()`
- [ ] 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.
- [x] 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".
  • Loading branch information
ddsjoberg authored Mar 12, 2024
1 parent 655f56b commit eb2b89f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 39 deletions.
91 changes: 56 additions & 35 deletions R/ard_wilcoxtest.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
#' a data frame. See below for details.
#' @param by ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
#' column name to compare by.
#' @param variable ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
#' column name to be compared.
#' @param variables ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
#' column names to be compared. Independent tests will be computed for
#' each variable.
#' @param id ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
#' column name of the subject or participant ID.
#' @param ... arguments passed to `wilcox.test(...)`
Expand Down Expand Up @@ -42,67 +43,87 @@ NULL

#' @rdname ard_wilcoxtest
#' @export
ard_wilcoxtest <- function(data, by, variable, ...) {
ard_wilcoxtest <- function(data, by, variables, ...) {
# check installed packages ---------------------------------------------------
cards::check_pkg_installed("broom", reference_pkg = "cardx")

# check/process inputs -------------------------------------------------------
check_not_missing(data)
check_not_missing(variable)
check_not_missing(variables)
check_not_missing(by)
check_data_frame(data)
data <- dplyr::ungroup(data)
cards::process_selectors(data, by = {{ by }}, variable = {{ variable }})
cards::process_selectors(data, by = {{ by }}, variables = {{ variables }})
check_scalar(by)
check_scalar(variable)

# if no variables selected, return empty tibble ------------------------------
if (is_empty(variables)) {
return(dplyr::tibble())
}

# build ARD ------------------------------------------------------------------
.format_wilcoxtest_results(
by = by,
variable = variable,
lst_tidy =
cards::eval_capture_conditions(
stats::wilcox.test(data[[variable]] ~ data[[by]], ...) |>
broom::tidy()
),
paired = FALSE,
...
)
lapply(
variables,
function(variable) {
.format_wilcoxtest_results(
by = by,
variable = variable,
lst_tidy =
cards::eval_capture_conditions(
stats::wilcox.test(data[[variable]] ~ data[[by]], ...) |>
broom::tidy()
),
paired = FALSE,
...
)
}
) |>
dplyr::bind_rows()
}

#' @rdname ard_wilcoxtest
#' @export
ard_paired_wilcoxtest <- function(data, by, variable, id, ...) {
ard_paired_wilcoxtest <- function(data, by, variables, id, ...) {
# check installed packages ---------------------------------------------------
cards::check_pkg_installed("broom", reference_pkg = "cardx")

# check/process inputs -------------------------------------------------------
check_not_missing(data)
check_not_missing(variable)
check_not_missing(variables)
check_not_missing(by)
check_not_missing(id)
check_data_frame(data)
data <- dplyr::ungroup(data)
cards::process_selectors(data, by = {{ by }}, variable = {{ variable }}, id = {{ id }})
cards::process_selectors(data, by = {{ by }}, variables = {{ variables }}, id = {{ id }})
check_scalar(by)
check_scalar(variable)
check_scalar(id)

# if no variables selected, return empty tibble ------------------------------
if (is_empty(variables)) {
return(dplyr::tibble())
}

# build ARD ------------------------------------------------------------------
.format_wilcoxtest_results(
by = by,
variable = variable,
lst_tidy =
cards::eval_capture_conditions({
# adding this reshape inside the eval, so if there is an error it's captured in the ARD object
data_wide <- .paired_data_pivot_wider(data, by = by, variable = variable, id = id)
# perform paired wilcox test
stats::wilcox.test(x = data_wide[["by1"]], y = data_wide[["by2"]], paired = TRUE, ...) |>
broom::tidy()
}),
paired = TRUE,
...
)
lapply(
variables,
function(variable) {
.format_wilcoxtest_results(
by = by,
variable = variable,
lst_tidy =
cards::eval_capture_conditions({
# adding this reshape inside the eval, so if there is an error it's captured in the ARD object
data_wide <- .paired_data_pivot_wider(data, by = by, variable = variable, id = id)
# perform paired wilcox test
stats::wilcox.test(x = data_wide[["by1"]], y = data_wide[["by2"]], paired = TRUE, ...) |>
broom::tidy()
}),
paired = TRUE,
...
)
}
) |>
dplyr::bind_rows()
}


Expand Down
9 changes: 5 additions & 4 deletions man/ard_wilcoxtest.Rd

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

13 changes: 13 additions & 0 deletions tests/testthat/test-ard_wilcoxtest.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ test_that("ard_wilcoxtest() works", {
getElement(1L),
"grouping factor must have exactly 2 levels"
)

# test that the function works with multiple variables as once
expect_equal(
dplyr::bind_rows(
ard_wilcoxtest,
cards::ADSL |>
dplyr::filter(ARM %in% c("Placebo", "Xanomeline High Dose")) |>
ard_wilcoxtest(by = ARM, variable = BMIBL, correct = FALSE, conf.int = TRUE)
),
cards::ADSL |>
dplyr::filter(ARM %in% c("Placebo", "Xanomeline High Dose")) |>
ard_wilcoxtest(by = ARM, variable = c(AGE, BMIBL), correct = FALSE, conf.int = TRUE)
)
})

test_that("ard_paired_wilcoxtest() works", {
Expand Down

0 comments on commit eb2b89f

Please sign in to comment.