-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into 37-ard-for-moods-test
Signed-off-by: Daniel Sjoberg <[email protected]>
- Loading branch information
Showing
9 changed files
with
242 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#' ARD McNemar's Test | ||
#' | ||
#' @description | ||
#' Analysis results data for McNemar's statistical test. | ||
#' | ||
#' @param data (`data.frame`)\cr | ||
#' 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 ... arguments passed to `stats::mcnemar.test(...)` | ||
#' | ||
#' @return ARD data frame | ||
#' @name ard_mcnemartest | ||
#' | ||
#' @details | ||
#' For the `ard_mcnemartest()` function, the data is expected to be one row per subject. | ||
#' The data is passed as `stats::mcnemar.test(x = data[[variable]], y = data[[by]], ...)`. | ||
#' Please use `table(x = data[[variable]], y = data[[by]])` to check the contingency table. | ||
#' | ||
#' @examples | ||
#' cards::ADSL |> | ||
#' ard_mcnemartest(by = "SEX", variable = "EFFFL") | ||
#' | ||
NULL | ||
|
||
#' @rdname ard_mcnemartest | ||
#' @export | ||
ard_mcnemartest <- function(data, by, variable, ...) { | ||
# 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(by) | ||
check_class_data_frame(x = data) | ||
data <- dplyr::ungroup(data) | ||
cards::process_selectors(data, by = {{ by }}, variable = {{ variable }}) | ||
check_scalar(by) | ||
check_scalar(variable) | ||
|
||
# build ARD ------------------------------------------------------------------ | ||
.format_mcnemartest_results( | ||
by = by, | ||
variable = variable, | ||
lst_tidy = | ||
cards::eval_capture_conditions( | ||
stats::mcnemar.test(x = data[[variable]], y = data[[by]], ...) |> | ||
broom::tidy() | ||
), | ||
... | ||
) | ||
} | ||
|
||
#' Convert McNemar's test to ARD | ||
#' | ||
#' @inheritParams cards::tidy_as_ard | ||
#' @inheritParams stats::mcnemar.test | ||
#' @param by (`string`)\cr by column name | ||
#' @param variable (`string`)\cr variable column name | ||
#' @param ... passed to `stats::mcnemar.test(...)` | ||
#' | ||
#' @return ARD data frame | ||
#' | ||
#' @examples | ||
#' cardx:::.format_mcnemartest_results( | ||
#' by = "ARM", | ||
#' variable = "AGE", | ||
#' lst_tidy = | ||
#' cards::eval_capture_conditions( | ||
#' stats::mcnemar.test(cards::ADSL[["SEX"]], cards::ADSL[["EFFFL"]]) |> | ||
#' broom::tidy() | ||
#' ) | ||
#' ) | ||
#' | ||
#' @keywords internal | ||
.format_mcnemartest_results <- function(by, variable, lst_tidy, ...) { | ||
# build ARD ------------------------------------------------------------------ | ||
ret <- | ||
cards::tidy_as_ard( | ||
lst_tidy = lst_tidy, | ||
tidy_result_names = c("statistic", "p.value", "method"), | ||
fun_args_to_record = c("correct"), | ||
formals = formals(asNamespace("stats")[["mcnemar.test"]]), | ||
passed_args = dots_list(...), | ||
lst_ard_columns = list(group1 = by, variable = variable, context = "mcnemartest") | ||
) | ||
|
||
# add the stat label --------------------------------------------------------- | ||
ret |> | ||
dplyr::left_join( | ||
.df_mcnemar_stat_labels(), | ||
by = "stat_name" | ||
) |> | ||
dplyr::mutate(stat_label = dplyr::coalesce(.data$stat_label, .data$stat_name)) |> | ||
cards::tidy_ard_column_order() | ||
} | ||
|
||
.df_mcnemar_stat_labels <- function() { | ||
dplyr::tribble( | ||
~stat_name, ~stat_label, | ||
"statistic", "X-squared Statistic", | ||
"parameter", "Degrees of Freedom", | ||
"p.value", "p-value", | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ Clopper | |
Codecov | ||
Hoffmann | ||
Jeffreys | ||
McNemar's | ||
Lifecycle | ||
Newcombe | ||
Su | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
test_that("ard_mcnemartest() works", { | ||
expect_error( | ||
ard_mcnemartest <- | ||
cards::ADSL |> | ||
ard_mcnemartest(by = SEX, variable = EFFFL), | ||
NA | ||
) | ||
|
||
expect_equal( | ||
ard_mcnemartest |> | ||
cards::get_ard_statistics(stat_name %in% c("statistic", "p.value", "parameter", "method")), | ||
stats::mcnemar.test(cards::ADSL[["SEX"]], cards::ADSL[["EFFFL"]], correct = TRUE) |> | ||
broom::tidy() |> | ||
unclass(), | ||
ignore_attr = TRUE | ||
) | ||
|
||
# errors are properly handled | ||
expect_equal( | ||
cards::ADSL |> | ||
ard_mcnemartest(by = ARM, variable = AGE, correct = FALSE) |> | ||
dplyr::pull(error) |> | ||
getElement(1L), | ||
"'x' and 'y' must have the same number of levels (minimum 2)" | ||
) | ||
|
||
# non-syntactic column names work too | ||
ADSL_tmp <- cards::ADSL |> | ||
dplyr::rename("if" = AGE, "_c d" = EFFFL) | ||
|
||
expect_equal( | ||
cards::ADSL |> | ||
dplyr::rename(`Planned Tx` = TRT01P, `Age Group` = AGEGR1) |> | ||
ard_mcnemartest(by = `Planned Tx`, variable = `Age Group`) |> | ||
cards::get_ard_statistics(), | ||
cards::ADSL |> | ||
ard_mcnemartest(by = TRT01P, variable = AGEGR1) |> | ||
cards::get_ard_statistics() | ||
) | ||
}) |