Skip to content

Commit 73fa432

Browse files
authored
Merge branch 'main' into remove_lookups
2 parents f12fcad + 2712e98 commit 73fa432

File tree

5 files changed

+93
-15
lines changed

5 files changed

+93
-15
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: cardx
22
Title: Extra Analysis Results Data Utilities
3-
Version: 0.2.2.9018
3+
Version: 0.2.2.9020
44
Authors@R: c(
55
person("Daniel D.", "Sjoberg", , "danield.sjoberg@gmail.com", role = c("aut", "cre"),
66
comment = c(ORCID = "0000-0003-0862-2018")),
@@ -20,7 +20,7 @@ BugReports: https://github.com/insightsengineering/cardx/issues
2020
Depends:
2121
R (>= 4.1)
2222
Imports:
23-
cards (>= 0.4.0),
23+
cards (>= 0.4.0.9028),
2424
cli (>= 3.6.1),
2525
dplyr (>= 1.1.2),
2626
glue (>= 1.6.2),

NEWS.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# cardx 0.2.2.9018
1+
# cardx 0.2.2.9020
2+
3+
* Added `as_cards_fun()` to `ard_categorical_ci()` so when there is an error, the user gets an ARD with the expected ARD structure. (#262)
24

35
* Little `n` is now returned with the results of the `proportion_ci_*()` functions, which then flows into the results of `ard_proportion_ci()`. (#256)
46

R/ard_categorical.survey.design.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,10 +517,10 @@ case_switch <- function(..., .default = NULL) {
517517
return(fmt_fn)
518518
}
519519
if (stat_name %in% c("p", "p_miss", "p_nonmiss", "p_unweighted")) {
520-
return(cards::label_cards(digits = 1, scale = 100))
520+
return(cards::label_round(digits = 1, scale = 100))
521521
}
522522
if (stat_name %in% c("n", "N", "N_miss", "N_nonmiss", "N_obs", "n_unweighted", "N_unweighted")) {
523-
return(cards::label_cards(digits = 0))
523+
return(cards::label_round(digits = 0))
524524
}
525525
if (is.integer(stat)) {
526526
return(0L)

R/ard_categorical_ci.R

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ ard_categorical_ci.data.frame <- function(data,
8282
data[variables],
8383
value = value
8484
)
85-
# drop any missing values in the analysis variables
86-
data <- tidyr::drop_na(data, any_of(c(variables, by, strata)))
8785

8886
# if there is no by variable, then treat cell as column because it's the same.
8987
if (denominator == "cell" && is_empty(by)) {
@@ -199,7 +197,15 @@ ard_categorical_ci.data.frame <- function(data,
199197
conf.level = conf.level, correct = FALSE
200198
)
201199
}
202-
)
200+
) |>
201+
cards::as_cards_fn(
202+
stat_names =
203+
case_switch(
204+
method %in% c("strat_wilsoncc", "strat_wilsoncc") ~
205+
c("N", "n", "estimate", "conf.low", "conf.high", "conf.level", "weights", "method"),
206+
.default = c("N", "n", "estimate", "conf.low", "conf.high", "conf.level", "method")
207+
)
208+
)
203209
}
204210

205211
.unique_values_sort <- function(data, variable, value = NULL) {
@@ -265,9 +271,11 @@ ard_categorical_ci.data.frame <- function(data,
265271
# if there are no by variables, then all row percents are 100%
266272
if (is_empty(by)) {
267273
df_res <-
268-
cards::nest_for_ard(
269-
data = data[c(variable, by, strata)],
270-
by = variable
274+
suppressMessages(
275+
cards::nest_for_ard(
276+
data = data[c(variable, by, strata)],
277+
by = variable
278+
)
271279
) |>
272280
dplyr::rename(variable = "group1", variable_level = "group1_level") %>%
273281
{
@@ -313,9 +321,11 @@ ard_categorical_ci.data.frame <- function(data,
313321
df_grouping_cols <- cards::nest_for_ard(data, by = by, include_data = FALSE)
314322
levels <- .levels_for_row(data = data, by = by)
315323

316-
cards::nest_for_ard(
317-
data = data[c(variable, by, strata)],
318-
by = variable
324+
suppressMessages(
325+
cards::nest_for_ard(
326+
data = data[c(variable, by, strata)],
327+
by = variable
328+
)
319329
) |>
320330
dplyr::rename(variable = "group1", variable_level = "group1_level") %>%
321331
{
@@ -359,7 +369,9 @@ ard_categorical_ci.data.frame <- function(data,
359369
conf.level, strata, weights, max.iterations) {
360370
# create the base of what the grouping and variable ARD will look like
361371
df_groups_variable <-
362-
cards::nest_for_ard(data, by = c(by, variable), include_data = FALSE) |>
372+
suppressMessages(
373+
cards::nest_for_ard(data, by = c(by, variable), include_data = FALSE)
374+
) |>
363375
dplyr::rename(
364376
variable = glue::glue("group{length(c(variable, by))}"),
365377
variable_level = glue::glue("group{length(c(variable, by))}_level")

tests/testthat/test-ard_categorical_ci.data.frame.R

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ test_that("ard_continuous_ci.data.frame() follows ard structure", {
168168
)
169169
})
170170

171+
171172
test_that("ard_continuous_ci.data.frame(denominator='row')", {
172173
# check the structure of the output
173174
expect_silent(
@@ -337,3 +338,66 @@ test_that("ard_continuous_ci.data.frame(denominator='cell')", {
337338
)
338339
)
339340
})
341+
342+
test_that("ard_continuous_ci.data.frame(denominator='column') with all NA column", {
343+
expect_silent(
344+
ard <-
345+
mtcars |>
346+
dplyr::mutate(vs = ifelse(am == 0, NA, vs)) |>
347+
ard_categorical_ci(
348+
variables = vs,
349+
by = am,
350+
denominator = "column",
351+
method = "wilson"
352+
)
353+
)
354+
355+
expect_equal(
356+
ard |>
357+
dplyr::filter(group1_level %in% 0) |>
358+
dplyr::pull(stat_name),
359+
c("N", "n", "estimate", "conf.low", "conf.high", "conf.level", "method")
360+
)
361+
})
362+
363+
test_that("ard_continuous_ci.data.frame(denominator='row') with all NA column", {
364+
expect_silent(
365+
ard <-
366+
mtcars |>
367+
dplyr::mutate(vs = ifelse(am == 0, NA, vs)) |>
368+
ard_categorical_ci(
369+
variables = vs,
370+
by = am,
371+
denominator = "row",
372+
method = "wilson"
373+
)
374+
)
375+
376+
expect_equal(
377+
ard |>
378+
dplyr::filter(group1_level %in% 0) |>
379+
dplyr::pull(stat_name),
380+
c("N", "n", "conf.level", "estimate", "statistic", "p.value", "parameter", "conf.low", "conf.high", "method", "alternative")
381+
)
382+
})
383+
384+
test_that("ard_continuous_ci.data.frame(denominator='cell') with all NA column", {
385+
expect_silent(
386+
ard <-
387+
mtcars |>
388+
dplyr::mutate(vs = ifelse(am == 0, NA, vs)) |>
389+
ard_categorical_ci(
390+
variables = vs,
391+
by = am,
392+
denominator = "cell",
393+
method = "wilson"
394+
)
395+
)
396+
397+
expect_equal(
398+
ard |>
399+
dplyr::filter(group1_level %in% 0) |>
400+
dplyr::pull(stat_name),
401+
c("N", "n", "conf.level", "estimate", "statistic", "p.value", "parameter", "conf.low", "conf.high", "method", "alternative")
402+
)
403+
})

0 commit comments

Comments
 (0)