Skip to content

Commit a76c5c0

Browse files
add ard_svychisq() function (#83)
**What changes are proposed in this pull request?** add ard_svychisq function #72 @ayogasekaram Provide more detail here as needed. closes #72 -------------------------------------------------------------------------------- Pre-review Checklist (if item does not apply, mark is as complete) - [x] **All** GitHub Action workflows pass with a ✅ - [x] PR branch has pulled the most recent updates from master branch: `usethis::pr_merge_main()` - [x] If a bug was fixed, a unit test was added. - [x] Code coverage is suitable for any new functions/features (generally, 100% coverage for new code): `devtools::test_coverage()` - [x] Request a reviewer Reviewer Checklist (if item does not apply, mark is as complete) - [x] If a bug was fixed, a unit test was added. - [x] Run `pkgdown::build_site()`. Check the R console for errors, and review the rendered website. - [x] Code coverage is suitable for any new functions/features: `devtools::test_coverage()` When the branch is ready to be merged: - [x] Update `NEWS.md` with the changes from this pull request under the heading "`# cards (development version)`". If there is an issue associated with the pull request, reference it in parentheses at the end update (see `NEWS.md` for examples). - [x] **All** GitHub Action workflows pass with a ✅ - [x] Approve Pull Request - [x] Merge the PR. Please use "Squash and merge" or "Rebase and merge". --------- Co-authored-by: Daniel Sjoberg <danield.sjoberg@gmail.com>
1 parent df07e0f commit a76c5c0

File tree

11 files changed

+191
-7
lines changed

11 files changed

+191
-7
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Suggests:
2929
parameters (>= 0.20.2),
3030
smd (>= 0.6.6),
3131
spelling,
32+
survey (>= 4.1),
3233
testthat (>= 3.2.0),
3334
withr
3435
Remotes:

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export(ard_proptest)
2121
export(ard_regression)
2222
export(ard_regression_basic)
2323
export(ard_smd)
24+
export(ard_svychisq)
2425
export(ard_ttest)
2526
export(ard_vif)
2627
export(ard_wilcoxtest)

R/ard_svychisq.R

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#' ARD Survey Chi-Square Test
2+
#'
3+
#' @description
4+
#' Analysis results data for survey Chi-Square test using [`survey::svychisq()`].
5+
#' Only two-way comparisons are supported.
6+
#'
7+
#' @param data (`survey.design`)\cr
8+
#' a survey design object often created with the {survey} package
9+
#' @param by ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
10+
#' column name to compare by.
11+
#' @param variables ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
12+
#' column names to be compared. Independent tests will be computed for
13+
#' each variable.
14+
#' @param statistic (`character`)\cr
15+
#' statistic used to estimate Chisq p-value.
16+
#' Default is the Rao-Scott second-order correction ("F"). See [`survey::svychisq`]
17+
#' for available statistics options.
18+
#' @param ... arguments passed to [`survey::svychisq()`].
19+
#'
20+
#' @return ARD data frame
21+
#' @export
22+
#'
23+
#' @examplesIf cards::is_pkg_installed(c("survey", "broom"), reference_pkg = "cardx")
24+
#' data(api, package = "survey")
25+
#' dclus1 <- survey::svydesign(id = ~dnum, weights = ~pw, data = apiclus1, fpc = ~fpc)
26+
#'
27+
#' ard_svychisq(dclus1, variables = sch.wide, by = comp.imp, statistic = "F")
28+
ard_svychisq <- function(data, by, variables, statistic = "F", ...) {
29+
# check installed packages ---------------------------------------------------
30+
cards::check_pkg_installed(c("survey", "broom"), reference_pkg = "cardx")
31+
32+
# check/process inputs -------------------------------------------------------
33+
check_not_missing(data)
34+
check_not_missing(variables)
35+
check_not_missing(by)
36+
check_class(data, cls = "survey.design")
37+
cards::process_selectors(data[["variables"]], by = {{ by }}, variables = {{ variables }})
38+
check_scalar(by)
39+
40+
# if no variables selected, return empty tibble ------------------------------
41+
if (is_empty(variables)) {
42+
return(dplyr::tibble())
43+
}
44+
# build ARD ------------------------------------------------------------------
45+
lapply(
46+
variables,
47+
function(variable) {
48+
cards::tidy_as_ard(
49+
lst_tidy =
50+
cards::eval_capture_conditions(
51+
survey::svychisq(stats::reformulate(termlabels = paste(variable, by, sep = "+"), response = NULL), design = data, statistic = statistic, ...) |>
52+
broom::tidy()
53+
),
54+
tidy_result_names = c("statistic", "p.value", "ndf", "ddf", "method"),
55+
passed_args = dots_list(...),
56+
lst_ard_columns = list(group1 = by, variable = variable, context = "svychisq")
57+
) |>
58+
dplyr::mutate(
59+
.after = "stat_name",
60+
stat_label =
61+
dplyr::case_when(
62+
.data$stat_name %in% "statistic" ~ "Statistic",
63+
.data$stat_name %in% "p.value" ~ "p-value",
64+
.data$stat_name %in% "ndf" ~ "Nominator Degrees of Freedom",
65+
.data$stat_name %in% "ddf" ~ "Denominator Degrees of Freedom",
66+
TRUE ~ .data$stat_name,
67+
)
68+
)
69+
}
70+
) |>
71+
dplyr::bind_rows()
72+
}

R/ard_wilcoxtest.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@
3030
#' @examplesIf cards::is_pkg_installed("broom", reference_pkg = "cardx")
3131
#' cards::ADSL |>
3232
#' dplyr::filter(ARM %in% c("Placebo", "Xanomeline High Dose")) |>
33-
#' ard_wilcoxtest(by = "ARM", variable = "AGE")
33+
#' ard_wilcoxtest(by = "ARM", variables = "AGE")
3434
#'
3535
#' # constructing a paired data set,
3636
#' # where patients receive both treatments
3737
#' cards::ADSL[c("ARM", "AGE")] |>
3838
#' dplyr::filter(ARM %in% c("Placebo", "Xanomeline High Dose")) |>
3939
#' dplyr::mutate(.by = ARM, USUBJID = dplyr::row_number()) |>
4040
#' dplyr::arrange(USUBJID, ARM) |>
41-
#' ard_paired_wilcoxtest(by = ARM, variable = AGE, id = USUBJID)
41+
#' ard_paired_wilcoxtest(by = ARM, variables = AGE, id = USUBJID)
4242
NULL
4343

4444
#' @rdname ard_wilcoxtest
@@ -141,7 +141,7 @@ ard_paired_wilcoxtest <- function(data, by, variables, id, ...) {
141141
#' # Pre-processing ADSL to have grouping factor (ARM here) with 2 levels
142142
#' ADSL <- cards::ADSL |>
143143
#' dplyr::filter(ARM %in% c("Placebo", "Xanomeline High Dose")) |>
144-
#' ard_wilcoxtest(by = "ARM", variable = "AGE")
144+
#' ard_wilcoxtest(by = "ARM", variables = "AGE")
145145
#'
146146
#' cardx:::.format_wilcoxtest_results(
147147
#' by = "ARM",
@@ -167,7 +167,7 @@ ard_paired_wilcoxtest <- function(data, by, variables, id, ...) {
167167
),
168168
formals = formals(asNamespace("stats")[["wilcox.test.default"]]),
169169
passed_args = c(list(paired = paired), dots_list(...)),
170-
lst_ard_columns = list(group1 = by, variable = variable, context = "ttest")
170+
lst_ard_columns = list(group1 = by, variable = variable, context = "wilcoxtest")
171171
)
172172

173173
# add the stat label ---------------------------------------------------------

_pkgdown.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ reference:
3131
- ard_moodtest
3232
- ard_mcnemartest
3333
- ard_proptest
34+
- ard_svychisq
3435
- ard_ttest
3536
- ard_wilcoxtest
3637

inst/WORDLIST

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ Jeffreys
88
Lifecycle
99
McNemar's
1010
Newcombe
11+
Rao
1112
Su
1213
VIF
1314
XG
1415
Xin
1516
agresti
17+
chisq
1618
clopper
1719
coull
1820
funder

man/ard_svychisq.Rd

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/ard_wilcoxtest.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/dot-format_wilcoxtest_results.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# ard_svychisq() works
2+
3+
Code
4+
as.data.frame(dplyr::slice_head(dplyr::group_by(dplyr::select(ard_svychisq(
5+
dclus2, variables = c(sch.wide, stype), by = comp.imp, statistic = "adjWald"),
6+
c(1:3, 5:6)), variable), n = 3))
7+
Output
8+
group1 variable context stat_label stat
9+
1 comp.imp sch.wide svychisq Nominator Degrees of Freedom 1
10+
2 comp.imp sch.wide svychisq Denominator Degrees of Freedom 39
11+
3 comp.imp sch.wide svychisq Statistic 11.4203
12+
4 comp.imp stype svychisq Nominator Degrees of Freedom 2
13+
5 comp.imp stype svychisq Denominator Degrees of Freedom 38
14+
6 comp.imp stype svychisq Statistic 4.480236
15+

0 commit comments

Comments
 (0)