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

add ard_svychisq() function #83

Merged
merged 17 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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 DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Suggests:
parameters (>= 0.20.2),
smd (>= 0.6.6),
spelling,
survey (>= 4.1),
testthat (>= 3.2.0),
withr
Remotes:
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export(ard_proptest)
export(ard_regression)
export(ard_regression_basic)
export(ard_smd)
export(ard_svychisq)
export(ard_ttest)
export(ard_vif)
export(ard_wilcoxtest)
Expand Down
64 changes: 64 additions & 0 deletions R/ard_svychisq.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#' 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
#' @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 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 cards::is_pkg_installed(c("survey", "broom"), reference_pkg = "cardx")
#' data(api, package = "survey")
#' dclus2 <- survey::svydesign(id = ~dnum, weights = ~pw, data = apiclus1, fpc = ~fpc)
ayogasekaram marked this conversation as resolved.
Show resolved Hide resolved
#'
#' ard_svychisq(dclus2, variable = sch.wide, by = comp.imp, statistic = "F")
ard_svychisq <- function(data, by, variable, statistic = "F", ...) {
ayogasekaram marked this conversation as resolved.
Show resolved Hide resolved
ayogasekaram marked this conversation as resolved.
Show resolved Hide resolved
# check installed packages ---------------------------------------------------
cards::check_pkg_installed(c("survey", "broom"), reference_pkg = "cardx")

# check/process inputs -------------------------------------------------------
check_not_missing(data)
check_not_missing(variable)
check_not_missing(by)
check_class(data, cls = "survey.design")
cards::process_selectors(data[["variables"]], by = {{ by }}, variable = {{ variable }})
check_scalar(by)
check_scalar(variable)

# build ARD ------------------------------------------------------------------
cards::tidy_as_ard(
lst_tidy =
cards::eval_capture_conditions(
survey::svychisq(stats::reformulate(termlabels = paste(variable, by, sep = "+"), response = NULL), design = data, ...) |>
broom::tidy()
),
tidy_result_names = c("statistic", "p.value", "ndf", "ddf", "method"),
formals = formals(survey::svyttest),
ayogasekaram marked this conversation as resolved.
Show resolved Hide resolved
passed_args = dots_list(...),
lst_ard_columns = list(group1 = by, variable = variable, context = "svychisqtest")
ayogasekaram marked this conversation as resolved.
Show resolved Hide resolved
) |>
dplyr::mutate(
.after = "stat_name",
stat_label =
dplyr::case_when(
.data$stat_name %in% "statistic" ~ "X-squared Statistic",
ayogasekaram marked this conversation as resolved.
Show resolved Hide resolved
.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",
.data$stat_name %in% "method" ~ "Stat calculation method",
TRUE ~ .data$stat_name,
)
)
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ reference:
- ard_moodtest
- ard_mcnemartest
- ard_proptest
- ard_svychisq
- ard_ttest
- ard_wilcoxtest

Expand Down
2 changes: 2 additions & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ Jeffreys
Lifecycle
McNemar's
Newcombe
Rao
Su
VIF
XG
Xin
agresti
chisq
clopper
coull
funder
Expand Down
40 changes: 40 additions & 0 deletions man/ard_svychisq.Rd

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

26 changes: 26 additions & 0 deletions tests/testthat/test-ard_svychisq.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
skip_if_not(cards::is_pkg_installed(c("survey", "broom"), reference_pkg = "cardx"))

test_that("ard_svychisq() works", {
data(api, package = "survey")
dclus2 <- survey::svydesign(id = ~ dnum + snum, fpc = ~ fpc1 + fpc2, data = apiclus2)

expect_error(
ard_svychisq <-
ard_svychisq(
dclus2,
variable = sch.wide,
by = comp.imp,
statistic = "F"
),
NA
)

expect_equal(
cards::get_ard_statistics(
ard_svychisq,
stat_name %in% c("statistic", "p.value")
),
survey::svychisq(~ sch.wide + comp.imp, dclus2)[c("statistic", "p.value")],
ignore_attr = TRUE
)
})
Loading