-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ard_survey_svychisq.R
76 lines (72 loc) · 2.89 KB
/
ard_survey_svychisq.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
74
75
76
#' ARD Survey Chi-Square Test
#'
#' @description
#' Analysis results data for survey Chi-Square test using [`survey::svychisq()`].
#' Only two-way comparisons are supported.
#'
#' @param data (`survey.design`)\cr
#' a survey design object often created with the \{survey\} package
#' @param by ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
#' column name to compare by.
#' @param variables ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
#' column names to be compared. Independent tests will be computed for
#' each variable.
#' @param statistic (`character`)\cr
#' statistic used to estimate Chisq p-value.
#' Default is the Rao-Scott second-order correction ("F"). See [`survey::svychisq`]
#' for available statistics options.
#' @param ... arguments passed to [`survey::svychisq()`].
#'
#' @return ARD data frame
#' @export
#'
#' @examplesIf do.call(asNamespace("cardx")$is_pkg_installed, list(pkg = c("survey", "broom")))
#' data(api, package = "survey")
#' dclus1 <- survey::svydesign(id = ~dnum, weights = ~pw, data = apiclus1, fpc = ~fpc)
#'
#' ard_survey_svychisq(dclus1, variables = sch.wide, by = comp.imp, statistic = "F")
ard_survey_svychisq <- function(data, by, variables, statistic = "F", ...) {
set_cli_abort_call()
# check installed packages ---------------------------------------------------
check_pkg_installed(c("survey", "broom"))
# check/process inputs -------------------------------------------------------
check_not_missing(data)
check_not_missing(variables)
check_not_missing(by)
check_class(data, cls = "survey.design")
cards::process_selectors(data[["variables"]], by = {{ by }}, variables = {{ variables }})
check_scalar(by)
# return empty ARD if no variables selected ----------------------------------
if (is_empty(variables)) {
return(dplyr::tibble() |> cards::as_card())
}
# build ARD ------------------------------------------------------------------
lapply(
variables,
function(variable) {
cards::tidy_as_ard(
lst_tidy =
cards::eval_capture_conditions(
survey::svychisq(reformulate2(termlabels = c(variable, by)), design = data, statistic = statistic, ...) |>
broom::tidy()
),
tidy_result_names = c("statistic", "p.value", "ndf", "ddf", "method"),
passed_args = dots_list(...),
lst_ard_columns = list(group1 = by, variable = variable, context = "survey_svychisq")
) |>
dplyr::mutate(
.after = "stat_name",
stat_label =
dplyr::case_when(
.data$stat_name %in% "statistic" ~ "Statistic",
.data$stat_name %in% "p.value" ~ "p-value",
.data$stat_name %in% "ndf" ~ "Nominator Degrees of Freedom",
.data$stat_name %in% "ddf" ~ "Denominator Degrees of Freedom",
TRUE ~ .data$stat_name,
)
)
}
) |>
dplyr::bind_rows() |>
cards::as_card()
}