diff --git a/NEWS.md b/NEWS.md index 174de83c..977f1ee2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,8 @@ * Update in `ard_categorical.survey.design()` for factor variables that are all missing. These variables can now be tabulated, where previously this resulted in an error. +* Update in `ard_missing.survey.design()` where we can now tabulate the missing rate of design variables, such as the weights. + # cardx 0.2.2 * Added a `data.frame` method to `ard_survival_survfit()`. diff --git a/R/ard_missing.survey.design.R b/R/ard_missing.survey.design.R index 499c69ef..cef1c8b9 100644 --- a/R/ard_missing.survey.design.R +++ b/R/ard_missing.survey.design.R @@ -54,7 +54,7 @@ ard_missing.survey.design <- function(data, # convert all variables to T/F whether it's missing -------------------------- data$variables <- data$variables |> - dplyr::mutate(across(all_of(variables), Negate(is.na))) + dplyr::mutate(across(all_of(variables), Negate(is.na), .names = "lgl_{.col}")) cards::process_formula_selectors( data$variables[variables], @@ -86,9 +86,12 @@ ard_missing.survey.design <- function(data, result <- ard_categorical( data = data, - variables = all_of(variables), + variables = all_of(paste0("lgl_", variables)), by = any_of(by), statistic = everything() ~ c("n", "N", "p", "n_unweighted", "N_unweighted", "p_unweighted") + ) |> + dplyr::mutate( + variable = str_remove(.data$variable, pattern = "^lgl_") ) # rename the stats for missingness ------------------------------------------- diff --git a/tests/testthat/test-ard_missing.survey.design.R b/tests/testthat/test-ard_missing.survey.design.R index 53dedda9..26bd060c 100644 --- a/tests/testthat/test-ard_missing.survey.design.R +++ b/tests/testthat/test-ard_missing.survey.design.R @@ -112,3 +112,36 @@ test_that("ard_missing.survey.design() follows ard structure", { cards::check_ard_structure(method = FALSE) ) }) + +# testing bug reported here: https://github.com/ddsjoberg/gtsummary/issues/2092 +test_that("ard_missing.survey.design() works on design columns", { + expect_equal( + suppressWarnings( + dplyr::tribble( + ~region, ~year, ~weights, + 3, 1972, 0.663196271930943, + 3, 1972, 0.917370028585327, + 3, 1972, 0.897412512251031, + 3, 1972, 1.06634082743438, + 3, 1972, 0.94432371066466, + 3, 1972, 0.526887241987567, + 3, 1972, 0.526887241987567, + 3, 1972, 0.546578869586901, + 7, 1972, 0.283198307048893, + 7, 1972, 0.494322145606406 + ) %>% + survey::svydesign( + data = ., + ids = ~region, + strata = ~year, + weights = ~weights, + nest = TRUE + ) |> + ard_missing(variables = weights) |> + dplyr::filter(stat_name == "N_nonmiss") |> + dplyr::pull("stat") |> + getElement(1L) + ), + 6.86651716 + ) +})