Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ard_chisqtest() #22

Merged
merged 8 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export("%>%")
export(all_of)
export(any_of)
export(ard_chisqtest)
export(ard_paired_ttest)
export(ard_paired_wilcoxtest)
export(ard_ttest)
Expand Down
57 changes: 57 additions & 0 deletions R/ard_chisqtest.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#' ARD Chi-squared Test
#'
#' @description
#' Analysis results data for Pearson's Chi-squared Test.
#' Calculated with `chisq.test(x = data[[variable]], y = data[[by]], ...)`
#'
#'
#' @param data (`data.frame`)\cr
#' a data frame.
#' @param by,variable ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
#' column names to compare
#' @param ... additional arguments passed to `fisher.test(...)`
#'
#' @return ARD data frame
#' @export
#'
#' @examples
#' cards::ADSL |>
#' ard_chisqtest(by = "ARM", variable = "AGEGR1")
ard_chisqtest <- function(data, by, variable, ...) {
# check installed packages ---------------------------------------------------
cards::check_pkg_installed("broom.helpers", reference_pkg = "cards")

# check/process inputs -------------------------------------------------------
check_not_missing(data)
check_not_missing(variable)
check_not_missing(by)
check_class_data_frame(x = data)
cards::process_selectors(data, by = {{ by }}, variable = {{ variable }})
check_scalar(by)
check_scalar(variable)

# build ARD ------------------------------------------------------------------
cards::tidy_as_ard(
lst_tidy =
cards::eval_capture_conditions(
stats::chisq.test(x = data[[variable]], y = data[[by]], ...) |>
broom::tidy()
),
tidy_result_names = c("statistic", "p.value", "parameter", "method"),
fun_args_to_record =
c("correct", "p", "rescale.p", "simulate.p.value", "B"),
formals = formals(stats::chisq.test),
passed_args = dots_list(...),
lst_ard_columns = list(group1 = by, variable = variable, context = "chisqtest")
) |>
dplyr::mutate(
.after = "stat_name",
stat_label =
dplyr::case_when(
.data$stat_name %in% "statistic" ~ "X-squared Statistic",
.data$stat_name %in% "p.value" ~ "p-value",
.data$stat_name %in% "parameter" ~ "Degrees of Freedom",
TRUE ~ .data$stat_name,
)
)
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ reference:
- subtitle: "Inference"
- contents:
- ard_ttest
- ard_chisqtest
- ard_wilcoxtest
28 changes: 28 additions & 0 deletions man/ard_chisqtest.Rd

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

17 changes: 17 additions & 0 deletions tests/testthat/_snaps/ard_chisqtest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# shuffle_ard fills missing group levels if the group is meaningful

Code
as.data.frame(cards::shuffle_ard(cards::bind_ard(ard_chisqtest(data = adsl_sub,
by = "ARM", variable = "AGEGR1"), ard_chisqtest(data = adsl_sub, by = "SEX",
variable = "AGEGR1"))))
Output
ARM SEX variable context stat_name statistic
1 Overall ARM <NA> AGEGR1 chisqtest statistic 5.079442e+00
2 Overall ARM <NA> AGEGR1 chisqtest p.value 7.888842e-02
3 Overall ARM <NA> AGEGR1 chisqtest parameter 2.000000e+00
4 Overall ARM <NA> AGEGR1 chisqtest B 2.000000e+03
5 <NA> Overall SEX AGEGR1 chisqtest statistic 1.039442e+00
6 <NA> Overall SEX AGEGR1 chisqtest p.value 5.946864e-01
7 <NA> Overall SEX AGEGR1 chisqtest parameter 2.000000e+00
8 <NA> Overall SEX AGEGR1 chisqtest B 2.000000e+03

39 changes: 39 additions & 0 deletions tests/testthat/test-ard_chisqtest.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
test_that("ard_chisqtest() works", {
expect_error(
ard_chisqtest <-
cards::ADSL |>
ard_chisqtest(by = ARM, variable = AGEGR1),
NA
)

expect_equal(
ard_chisqtest |>
cards::get_ard_statistics(stat_name %in% c("statistic", "p.value")),
with(cards::ADSL, chisq.test(AGEGR1, ARM)) |>
broom::tidy() |>
dplyr::select(statistic, p.value) |>
unclass(),
ignore_attr = TRUE
)
})

test_that("shuffle_ard fills missing group levels if the group is meaningful", {
adsl_sub <- cards::ADSL |> dplyr::filter(ARM %in% unique(ARM)[1:2])

expect_snapshot(
cards::bind_ard(
ard_chisqtest(
data = adsl_sub,
by = "ARM",
variable = "AGEGR1"
),
ard_chisqtest(
data = adsl_sub,
by = "SEX",
variable = "AGEGR1"
)
) |>
cards::shuffle_ard() |>
as.data.frame()
)
})