Skip to content

Commit

Permalink
update ard_moodtest function to accept multiple variables
Browse files Browse the repository at this point in the history
  • Loading branch information
ayogasekaram committed Mar 14, 2024
1 parent eb36605 commit 98e44b1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
45 changes: 28 additions & 17 deletions R/ard_moodtest.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,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 name to be compared. Independent tests will
#' be run for each variable.
#' @param ... arguments passed to `mood.test(...)`
#'
#' @return ARD data frame
Expand All @@ -23,32 +24,42 @@
#'
#' @examplesIf cards::is_pkg_installed("broom", reference_pkg = "cardx")
#' cards::ADSL |>
#' ard_moodtest(by = "SEX", variable = "AGE")
ard_moodtest <- function(data, by, variable, ...) {
#' ard_moodtest(by = "SEX", variables = "AGE")
ard_moodtest <- 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_moodtest_results(
by = by,
variable = variable,
lst_tidy =
cards::eval_capture_conditions(
stats::mood.test(data[[variable]] ~ data[[by]], ...) |>
broom::tidy()
),
...
)
lapply(
variables,
function(variable) {
.format_moodtest_results(
by = by,
variable = variable,
lst_tidy =
cards::eval_capture_conditions(
stats::mood.test(data[[variable]] ~ data[[by]], ...) |>
broom::tidy()
),
...
)
}
) |>
dplyr::bind_rows()
}
#' Convert mood test results to ARD
#'
Expand Down
9 changes: 5 additions & 4 deletions man/ard_moodtest.Rd

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

10 changes: 10 additions & 0 deletions tests/testthat/test-ard_moodtest.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,14 @@ test_that("ard_moodtest() works", {
ard_moodtest(by = SEX, variable = AGE) |>
as.data.frame()
)

expect_equal(
dplyr::bind_rows(
ard_moodtest,
cards::ADSL |>
ard_moodtest(by = SEX, variable = BMIBL)
),
cards::ADSL |>
ard_moodtest(by = SEX, variable = c(AGE, BMIBL))
)
})

0 comments on commit 98e44b1

Please sign in to comment.