Skip to content

Commit 76ab1d8

Browse files
267 add unit test@main (#293)
closes #267 -------------------------------------------------------------------------------- Pre-review Checklist (if item does not apply, mark is as complete) - [ ] **All** GitHub Action workflows pass with a ✅ - [ ] PR branch has pulled the most recent updates from master branch: `usethis::pr_merge_main()` - [ ] If a bug was fixed, a unit test was added. - [ ] If a new `ard_*()` function was added, it passes the ARD structural checks from `cards::check_ard_structure()`. - [ ] If a new `ard_*()` function was added, `set_cli_abort_call()` has been set. - [ ] If a new `ard_*()` function was added and it depends on another package (such as, `broom`), `is_pkg_installed("broom")` 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""))` - [ ] 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) - [ ] If a bug was fixed, a unit test was added. - [ ] Code coverage is suitable for any new functions/features: `devtools::test_coverage()` When the branch is ready to be merged: - [ ] 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). - [ ] **All** GitHub Action workflows pass with a ✅ - [ ] Approve Pull Request - [ ] Merge the PR. Please use "Squash and merge" or "Rebase and merge". --------- Signed-off-by: Daniel Sjoberg <[email protected]> Co-authored-by: Daniel Sjoberg <[email protected]>
1 parent f6d8dab commit 76ab1d8

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

R/ard_categorical_ci.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ ard_categorical_ci.data.frame <- function(data,
7878
cards::process_selectors(data, strata = {{ strata }})
7979
check_scalar(strata)
8080
}
81+
82+
# if the method is strat_wilson, `weights` and `strata` cannot contain NA values
83+
if (method %in% c("strat_wilson")) {
84+
if (any(is.na({{ weights }}))) {
85+
cli::cli_warn("{.field weights} cannot contain {.val NA} values.")
86+
}
87+
}
8188
cards::process_formula_selectors(
8289
data[variables],
8390
value = value

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

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ test_that("ard_continuous_ci.data.frame(denominator='row')", {
231231
variables = am,
232232
denominator = "row"
233233
) |>
234-
dplyr::filter(group1_level %in% 4, group2_level %in% 3) |>
234+
dplyr::filter(group1_level %in% 4, unlist(group2_level) == 3) |>
235235
cards::get_ard_statistics(),
236236
proportion_ci_wald(
237237
x = (mtcars$cyl == 4 & mtcars$gear == 3)[mtcars$am == 1],
@@ -316,7 +316,7 @@ test_that("ard_continuous_ci.data.frame(denominator='cell')", {
316316
variables = am,
317317
denominator = "cell"
318318
) |>
319-
dplyr::filter(group1_level %in% 4, group2_level %in% 3) |>
319+
dplyr::filter(group1_level %in% 4, unlist(group2_level) %in% 3) |>
320320
cards::get_ard_statistics(),
321321
proportion_ci_wald(
322322
x = (mtcars$cyl == 4 & mtcars$gear == 3 & mtcars$am == 1),
@@ -428,3 +428,66 @@ test_that("ard_continuous_ci.data.frame() NA handling", {
428428
proportion_ci_wald((df$am == 0) + (df$cyl == 4) > 1)
429429
)
430430
})
431+
432+
test_that("ard_categorical_ci(method = 'strat_wilson') NA handling", {
433+
# no NAs
434+
rsp <- c(
435+
sample(c(TRUE, FALSE), size = 40, prob = c(3 / 4, 1 / 4), replace = TRUE),
436+
sample(c(TRUE, FALSE), size = 40, prob = c(1 / 2, 1 / 2), replace = TRUE)
437+
)
438+
grp <- factor(rep(c("A", "B"), each = 40), levels = c("B", "A"))
439+
strata_data <- data.frame(
440+
"f1" = sample(c("a", "b"), 80, TRUE),
441+
"f2" = sample(c("x", "y", "z"), 80, TRUE),
442+
stringsAsFactors = TRUE
443+
)
444+
445+
weights <- 1:6 / sum(1:6)
446+
447+
# data with NA values
448+
449+
strata_na <- rbind(strata_data, data.frame(f1 = c(NA, NA), f2 = c(NA, NA)))
450+
rsp_na <- c(rsp, NA, NA)
451+
weights_na <- c(weights, NA, NA)
452+
453+
# NA in the strata
454+
expect_equal(
455+
ard_categorical_ci(
456+
data = data.frame(
457+
rsp = rsp,
458+
strata = interaction(strata_data)
459+
),
460+
variables = rsp,
461+
strata = strata,
462+
weights = weights,
463+
max.iterations = 10,
464+
method = "strat_wilson"
465+
),
466+
ard_categorical_ci(
467+
data = data.frame(
468+
rsp = rsp_na,
469+
strata = interaction(strata_na)
470+
),
471+
variables = rsp,
472+
strata = strata,
473+
weights = weights,
474+
max.iterations = 10,
475+
method = "strat_wilson"
476+
)
477+
)
478+
479+
# NA in weights should return a cli warning message
480+
expect_warning(
481+
ard_categorical_ci(
482+
data = data.frame(
483+
rsp = rsp_na,
484+
strata = interaction(strata_na)
485+
),
486+
variables = rsp,
487+
strata = strata,
488+
weights = weights_na,
489+
max.iterations = 10,
490+
method = "strat_wilson"
491+
)
492+
)
493+
})

0 commit comments

Comments
 (0)