Skip to content

Commit 443e6f5

Browse files
authored
Adding ard_total_n.survey.design() S3 method (#199)
**What changes are proposed in this pull request?** * Bug fix in `ard_categorical.survey.design()` where all unweighted statistics were returned, even in the case where they were explicitly not requested. * Added S3 method `ard_total_n.survey.design()` which returns an ARD with both the survey-weighted and unweighted total sample size. -------------------------------------------------------------------------------- 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] If a new `ard_*()` function was added, it passes the ARD structural checks from `cards::check_ard_structure()`. - [x] If a new `ard_*()` function was added, `set_cli_abort_call()` has been set. - [x] If a new `ard_*()` function was added and it depends on another package (such as, `broom`), `is_pkg_installed("broom", reference_pkg = "cardx")` has been set in the function call and the following added to the roxygen comments: `@examplesIf do.call(asNamespace("cardx")$is_pkg_installed, list(pkg = "broom"", reference_pkg = "cardx"))` - [x] Code coverage is suitable for any new functions/features (generally, 100% coverage for new code): `devtools::test_coverage()` Reviewer Checklist (if item does not apply, mark is as complete) - [x] If a bug was fixed, a unit test was added. - [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 "`# cardx (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 - [ ] Merge the PR. Please use "Squash and merge" or "Rebase and merge".
1 parent cdf2443 commit 443e6f5

12 files changed

+153
-22
lines changed

DESCRIPTION

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ BugReports: https://github.com/insightsengineering/cardx/issues
1919
Depends:
2020
R (>= 4.1)
2121
Imports:
22-
cards (>= 0.2.0),
22+
cards (>= 0.2.1.9003),
2323
cli (>= 3.6.1),
2424
dplyr (>= 1.1.2),
2525
glue (>= 1.6.2),
@@ -30,7 +30,7 @@ Suggests:
3030
broom (>= 1.0.5),
3131
broom.helpers (>= 1.15.0),
3232
broom.mixed (>= 0.2.9),
33-
car (>= 3.0-11),
33+
car (>= 3.1-2),
3434
effectsize (>= 0.8.8),
3535
emmeans (>= 1.7.3),
3636
geepack (>= 1.3.2),
@@ -43,6 +43,7 @@ Suggests:
4343
survival (>= 3.6-4),
4444
testthat (>= 3.2.0),
4545
withr (>= 2.5.0)
46+
Remotes: insightsengineering/cards
4647
Config/Needs/website: insightsengineering/nesttemplate
4748
Config/testthat/edition: 3
4849
Config/testthat/parallel: true

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ S3method(ard_missing,survey.design)
1212
S3method(ard_regression,default)
1313
S3method(ard_stats_anova,anova)
1414
S3method(ard_stats_anova,data.frame)
15+
S3method(ard_total_n,survey.design)
1516
S3method(construct_model,data.frame)
1617
S3method(construct_model,survey.design)
1718
export("%>%")
@@ -57,6 +58,7 @@ export(ard_survey_svyttest)
5758
export(ard_survival_survdiff)
5859
export(ard_survival_survfit)
5960
export(ard_survival_survfit_diff)
61+
export(ard_total_n)
6062
export(bt)
6163
export(bt_strip)
6264
export(construct_model)
@@ -83,6 +85,7 @@ importFrom(cards,ard_categorical)
8385
importFrom(cards,ard_continuous)
8486
importFrom(cards,ard_dichotomous)
8587
importFrom(cards,ard_missing)
88+
importFrom(cards,ard_total_n)
8689
importFrom(dplyr,"%>%")
8790
importFrom(dplyr,across)
8891
importFrom(dplyr,all_of)

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# cardx 0.2.0.9005
22

3+
* Bug fix in `ard_categorical.survey.design()` where all unweighted statistics were returned, even in the case where they were explicitly not requested.
4+
5+
* Added S3 method `ard_total_n.survey.design()` which returns an ARD with both the survey-weighted and unweighted total sample size.
6+
37
# cardx 0.2.0
48

59
### Breaking Changes

R/add_total_n.survey.design.R

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#' ARD Total N
2+
#'
3+
#' Returns the total N for a survey object.
4+
#' The placeholder variable name returned in the object is `"..ard_total_n.."`
5+
#'
6+
#' @inheritParams ard_dichotomous.survey.design
7+
#' @inheritParams rlang::args_dots_empty
8+
#'
9+
#' @return an ARD data frame of class 'card'
10+
#' @export
11+
#'
12+
#' @examplesIf cardx:::is_pkg_installed("survey", reference_pkg = "cardx")
13+
#' svy_titanic <- survey::svydesign(~1, data = as.data.frame(Titanic), weights = ~Freq)
14+
#'
15+
#' ard_total_n(svy_titanic)
16+
ard_total_n.survey.design <- function(data, ...) {
17+
# process inputs -------------------------------------------------------------
18+
set_cli_abort_call()
19+
check_dots_empty()
20+
21+
# calculate total N ----------------------------------------------------------
22+
data$variables <-
23+
data$variables |>
24+
dplyr::mutate(..ard_total_n.. = TRUE)
25+
26+
data |>
27+
ard_dichotomous(
28+
variables = "..ard_total_n..",
29+
statistic = list(..ard_total_n.. = c("N", "N_unweighted"))
30+
) |>
31+
dplyr::mutate(context = "total_n") |>
32+
dplyr::select(-cards::all_ard_variables("levels"))
33+
}

R/ard_categorical.survey.design.R

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -163,26 +163,33 @@ ard_categorical.survey.design <- function(data,
163163
)
164164

165165
# add unweighted statistics --------------------------------------------------
166-
cards_unweighted <-
167-
ard_categorical(
168-
data = data[["variables"]],
169-
variables = all_of(variables),
170-
by = any_of(by),
171-
denominator = denominator
172-
) |>
173-
# all the survey levels are reported as character, so we do the same here.
174-
dplyr::mutate(
175-
across(
176-
c(cards::all_ard_groups("levels"), cards::all_ard_variables("levels")),
177-
~ map(.x, as.character)
166+
statistic_unweighted <- statistic |>
167+
lapply(\(x) keep(x, ~ endsWith(.x, "_unweighted")) |> str_remove("_unweighted$")) |>
168+
compact()
169+
170+
if (!is_empty(statistic_unweighted)) {
171+
cards_unweighted <-
172+
ard_categorical(
173+
data = data[["variables"]],
174+
variables = all_of(names(statistic_unweighted)),
175+
by = any_of(by),
176+
statistic = statistic_unweighted,
177+
denominator = denominator
178+
) |>
179+
# all the survey levels are reported as character, so we do the same here.
180+
dplyr::mutate(
181+
across(
182+
c(cards::all_ard_groups("levels"), cards::all_ard_variables("levels")),
183+
~ map(.x, as.character)
184+
)
185+
) |>
186+
dplyr::select(-c("stat_label", "fmt_fn", "warning", "error")) |>
187+
dplyr::mutate(
188+
stat_name =
189+
dplyr::case_match(.data$stat_name, "n" ~ "n_unweighted", "N" ~ "N_unweighted", "p" ~ "p_unweighted")
178190
)
179-
) |>
180-
dplyr::select(-c("stat_label", "fmt_fn", "warning", "error")) |>
181-
dplyr::mutate(
182-
stat_name =
183-
dplyr::case_match(.data$stat_name, "n" ~ "n_unweighted", "N" ~ "N_unweighted", "p" ~ "p_unweighted")
184-
)
185-
cards <- cards |> dplyr::bind_rows(cards_unweighted) # styler: off
191+
cards <- cards |> dplyr::bind_rows(cards_unweighted) # styler: off
192+
}
186193

187194
# final processing of fmt_fn -------------------------------------------------
188195
cards <- cards |>

R/reexports.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ cards::ard_missing
1919
#' @export
2020
cards::ard_attributes
2121

22+
#' @importFrom cards ard_total_n
23+
#' @export
24+
cards::ard_total_n
25+
2226
# dplyr ------------------------------------------------------------------------
2327
#' @export
2428
#' @importFrom dplyr %>%

_pkgdown.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ reference:
6969
- ard_attributes.survey.design
7070
- ard_continuous_ci.survey.design
7171
- ard_categorical_ci.survey.design
72+
- ard_total_n.survey.design
7273
- ard_survey_svychisq
7374
- ard_survey_svyranktest
7475
- ard_survey_svyttest

man/ard_total_n.survey.design.Rd

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

man/reexports.Rd

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# ard_total_n.survey.design() works
2+
3+
Code
4+
ard_total_n(survey::svydesign(~1, data = as.data.frame(Titanic), weights = ~
5+
Freq))
6+
Message
7+
{cards} data frame: 2 x 8
8+
Output
9+
variable context stat_name stat_label stat fmt_fn
10+
1 ..ard_total_n.. total_n N N 2201 <fn>
11+
2 ..ard_total_n.. total_n N_unweighted Unweight… 32 <fn>
12+
Message
13+
i 2 more variables: warning, error
14+

0 commit comments

Comments
 (0)