-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ard_stats_wilcox_test_onesample.R
73 lines (69 loc) · 2.56 KB
/
ard_stats_wilcox_test_onesample.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#' 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"))
#' 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")
# 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))
# return empty ARD if no variables selected ----------------------------------
if (is_empty(variables)) {
return(dplyr::tibble() |> cards::as_card())
}
cards::ard_continuous(
data = data,
variables = all_of(variables),
by = all_of(by),
statistic = all_of(variables) ~ list(wilcox_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 = "stats_wilcox_test_onesample",
) |>
cards::as_card() |>
cards::tidy_ard_column_order() |>
cards::tidy_ard_row_order()
}