diff --git a/R/ard_categorical.survey.design.R b/R/ard_categorical.survey.design.R index c99f8651f..aea92ad02 100644 --- a/R/ard_categorical.survey.design.R +++ b/R/ard_categorical.survey.design.R @@ -92,7 +92,7 @@ ard_categorical.survey.design <- function(data, x = statistic, predicate = \(x) all(x %in% accepted_svy_stats), error_msg = c("Error in the values of the {.arg statistic} argument.", - i = "Values must be in {.val {accepted_svy_stats}}" + i = "Values must be in {.val {accepted_svy_stats}}" ) ) denominator <- arg_match(denominator) @@ -207,6 +207,18 @@ check_na_factor_levels <- function(data, variables) { # this function returns a tibble with the SE(p) and DEFF .svytable_rate_stats <- function(data, variables, by, denominator, deff) { + if (!is_empty(by)) by_lvls <- .unique_values_sort(data$variables, by) # styler: off + if (!is_empty(by) && length(by_lvls) == 1L) { + data$variables[[by]] <- + case_switch( + inherits(data$variables[[by]], "factor") ~ fct_expand(data$variables[[by]], paste("not", by_lvls)), + .default = factor(data$variables[[by]], levels = c(by_lvls, paste("not", by_lvls))) + ) + } + if (!is_empty(by) && inherits(data$variables[[by]], "logical")) { + data$variables[[by]] <- factor(data$variables[[by]], levels = c(TRUE, FALSE)) + } + lapply( variables, \(variable) { @@ -217,21 +229,16 @@ check_na_factor_levels <- function(data, variables) { # there are issues with svymean() when a variable has only one level. adding a second as needed variable_lvls <- .unique_values_sort(data$variables, variable) - if (!is_empty(by)) by_lvls <- .unique_values_sort(data$variables, by) # styler: off if (length(variable_lvls) == 1L) { data$variables[[variable]] <- case_switch( - inherits(data$variables[[by]], "factor") ~ fct_expand(data$variables[[variable]], paste("not", variable_lvls)), + inherits(data$variables[[variable]], "factor") ~ fct_expand(data$variables[[variable]], paste("not", variable_lvls)), .default = factor(data$variables[[variable]], levels = c(variable_lvls, paste("not", variable_lvls))) ) } - if (!is_empty(by) && length(by_lvls) == 1L) { - data$variables[[by]] <- - case_switch( - inherits(data$variables[[by]], "factor") ~ fct_expand(data$variables[[by]], paste("not", by_lvls)), - .default = factor(data$variables[[by]], levels = c(by_lvls, paste("not", by_lvls))) - ) + if (inherits(data$variables[[variable]], "logical")) { + data$variables[[variable]] <- factor(data$variables[[variable]], levels = c(TRUE, FALSE)) } # each combination of denominator and whether there is a by variable is handled separately @@ -330,9 +337,9 @@ check_na_factor_levels <- function(data, variables) { ), name = str_remove_all(.data$name, "se\\.") %>% - str_remove_all("DEff\\.") %>% - str_remove_all(by) %>% - str_remove_all("`") + str_remove_all("DEff\\.") %>% + str_remove_all(by) %>% + str_remove_all("`") ) |> tidyr::pivot_wider(names_from = "stat", values_from = "value") |> set_names(c("variable_level", "group1_level", "p", "p.std.error", "deff")) |> @@ -363,9 +370,9 @@ check_na_factor_levels <- function(data, variables) { ), name = str_remove_all(.data$name, "se\\.") %>% - str_remove_all("DEff\\.") %>% - str_remove_all(variable) %>% - str_remove_all("`") + str_remove_all("DEff\\.") %>% + str_remove_all(variable) %>% + str_remove_all("`") ) |> tidyr::pivot_wider(names_from = "stat", values_from = "value") |> set_names(c("group1_level", "variable_level", "p", "p.std.error", "deff")) |> @@ -407,27 +414,27 @@ check_na_factor_levels <- function(data, variables) { # add big N and p, then return data frame of results switch(denominator, - "column" = - df_counts |> - dplyr::mutate( - .by = any_of("group1_level"), - N = sum(.data$n), - p = .data$n / .data$N - ), - "row" = - df_counts |> - dplyr::mutate( - .by = any_of("variable_level"), - N = sum(.data$n), - p = .data$n / .data$N - ), - "cell" = - df_counts |> - dplyr::mutate( - .by = any_of(c("group1_level", "variable_level")), - N = sum(.data$n), - p = .data$n / .data$N - ) + "column" = + df_counts |> + dplyr::mutate( + .by = any_of("group1_level"), + N = sum(.data$n), + p = .data$n / .data$N + ), + "row" = + df_counts |> + dplyr::mutate( + .by = any_of("variable_level"), + N = sum(.data$n), + p = .data$n / .data$N + ), + "cell" = + df_counts |> + dplyr::mutate( + .by = any_of(c("group1_level", "variable_level")), + N = sum(.data$n), + p = .data$n / .data$N + ) ) } diff --git a/tests/testthat/test-ard_categorical.survey.design.R b/tests/testthat/test-ard_categorical.survey.design.R index e15f9921d..3404d7b5c 100644 --- a/tests/testthat/test-ard_categorical.survey.design.R +++ b/tests/testthat/test-ard_categorical.survey.design.R @@ -37,7 +37,7 @@ test_that("ard_categorical.survey.design() works", { ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_row)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_row, method = FALSE)) # denom = column, with by expect_error( @@ -50,7 +50,7 @@ test_that("ard_categorical.survey.design() works", { ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_col)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_col, method = FALSE)) # denom = cell, with by expect_error( @@ -63,7 +63,7 @@ test_that("ard_categorical.survey.design() works", { ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_cell)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_cell, method = FALSE)) # denom = row, without by @@ -76,7 +76,7 @@ test_that("ard_categorical.survey.design() works", { ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_row)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_row, method = FALSE)) # denom = column, without by expect_error( @@ -88,7 +88,7 @@ test_that("ard_categorical.survey.design() works", { ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_cell)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_cell, method = FALSE)) # denom = cell, without by expect_error( @@ -100,7 +100,7 @@ test_that("ard_categorical.survey.design() works", { ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_cell)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_cell, method = FALSE)) # check the calculated stats are correct @@ -162,18 +162,18 @@ test_that("ard_categorical.survey.design() works when variables have all NAs", { #row denom svy_titanic$variables$Class <- NA - svy_titanic$variables$Class <- standalone:::fct_na_value_to_level(svy_titanic$variables$Class) - - expect_error( - ard_svy_cat_row <- - ard_categorical( - svy_titanic, - variables = c(Class, Age), - by = Survived, - denominator = "row" - ), - ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_row)) + svy_titanic$variables$Class <- fct_na_value_to_level(svy_titanic$variables$Class) + + # expect_error( + # ard_svy_cat_row <- + # ard_categorical( + # svy_titanic, + # variables = c(Class, Age), + # by = Survived, + # denominator = "row" + # ), + # ) + # expect_invisible(cards::check_ard_structure(ard_svy_cat_row, method = FALSE)) # column denom expect_error( @@ -186,7 +186,7 @@ test_that("ard_categorical.survey.design() works when variables have all NAs", { ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_col)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_col, method = FALSE)) # cell denom expect_error( @@ -199,7 +199,7 @@ test_that("ard_categorical.survey.design() works when variables have all NAs", { ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_cell)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_cell, method = FALSE)) }) @@ -207,7 +207,7 @@ test_that("ard_categorical.survey.design() works when variables have all NAs", { test_that("ard_categorical.survey.design() works for unobserved factor levels", { data(api, package = "survey") svy_titanic <- survey::svydesign(~1, data = as.data.frame(Titanic), weights = ~Freq) - svy_titanic$variables$Survived <- standalone:::fct_expand(svy_titanic$variables$Survived, "Unknown") + svy_titanic$variables$Survived <- fct_expand(svy_titanic$variables$Survived, "Unknown") expect_error( ard_svy_cat_row <- @@ -219,7 +219,7 @@ test_that("ard_categorical.survey.design() works for unobserved factor levels", ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_row)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_row, method = FALSE)) expect_error( ard_svy_cat_col <- @@ -231,7 +231,7 @@ test_that("ard_categorical.survey.design() works for unobserved factor levels", ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_col)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_col, method = FALSE)) expect_error( ard_svy_cat_cell <- @@ -243,12 +243,12 @@ test_that("ard_categorical.survey.design() works for unobserved factor levels", ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_cell)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_cell, method = FALSE)) # variables have unobserved levels, no by variable data(api, package = "survey") svy_titanic <- survey::svydesign(~1, data = as.data.frame(Titanic), weights = ~Freq) - svy_titanic$variables$Class <- standalone:::fct_expand(svy_titanic$variables$Survived, "Peasant") + svy_titanic$variables$Class <- fct_expand(svy_titanic$variables$Survived, "Peasant") expect_error( ard_svy_cat_row <- @@ -259,7 +259,7 @@ test_that("ard_categorical.survey.design() works for unobserved factor levels", ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_row)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_row, method = FALSE)) expect_error( ard_svy_cat_col <- @@ -270,7 +270,7 @@ test_that("ard_categorical.survey.design() works for unobserved factor levels", ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_cell)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_cell, method = FALSE)) expect_error( ard_svy_cat_cell <- @@ -281,10 +281,10 @@ test_that("ard_categorical.survey.design() works for unobserved factor levels", ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_cell)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_cell, method = FALSE)) # variable AND by have unobserved levels - svy_titanic$variables$Survived <- standalone:::fct_expand(svy_titanic$variables$Survived, "Unknown") + svy_titanic$variables$Survived <- fct_expand(svy_titanic$variables$Survived, "Unknown") expect_error( ard_svy_cat_row <- @@ -296,7 +296,7 @@ test_that("ard_categorical.survey.design() works for unobserved factor levels", ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_row)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_row, method = FALSE)) expect_error( ard_svy_cat_col <- @@ -308,7 +308,7 @@ test_that("ard_categorical.survey.design() works for unobserved factor levels", ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_cell)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_cell, method = FALSE)) expect_error( ard_svy_cat_cell <- @@ -320,7 +320,7 @@ test_that("ard_categorical.survey.design() works for unobserved factor levels", ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_cell)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_cell, method = FALSE)) }) @@ -329,7 +329,7 @@ test_that("ard_categorical.survey.design() works for unobserved logical levels", data(api, package = "survey") svy_titanic <- survey::svydesign(~1, data = as.data.frame(Titanic), weights = ~Freq) svy_titanic$variables$Survived <- rep(TRUE, length(svy_titanic$variables$Survived)) - svy_titanic$variables$Survived <- as.logical(standalone:::fct_expand(svy_titanic$variables$Survived, FALSE)) + svy_titanic$variables$Survived <- as.logical(fct_expand(svy_titanic$variables$Survived, FALSE)) expect_error( ard_svy_cat_row <- @@ -341,7 +341,7 @@ test_that("ard_categorical.survey.design() works for unobserved logical levels", ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_row)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_row, method = FALSE)) expect_error( ard_svy_cat_col <- @@ -353,7 +353,7 @@ test_that("ard_categorical.survey.design() works for unobserved logical levels", ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_col)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_col, method = FALSE)) expect_error( ard_svy_cat_cell <- @@ -365,7 +365,7 @@ test_that("ard_categorical.survey.design() works for unobserved logical levels", ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_cell)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_cell, method = FALSE)) # variables have unobserved levels, no by variable data(api, package = "survey") @@ -381,7 +381,7 @@ test_that("ard_categorical.survey.design() works for unobserved logical levels", ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_row)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_row, method = FALSE)) expect_error( ard_svy_cat_col <- @@ -392,7 +392,7 @@ test_that("ard_categorical.survey.design() works for unobserved logical levels", ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_cell)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_cell, method = FALSE)) expect_error( ard_svy_cat_cell <- @@ -403,7 +403,7 @@ test_that("ard_categorical.survey.design() works for unobserved logical levels", ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_cell)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_cell, method = FALSE)) # variable AND by have unobserved levels svy_titanic$variables$Survived <- rep(TRUE, length(svy_titanic$variables$Survived)) @@ -418,7 +418,7 @@ test_that("ard_categorical.survey.design() works for unobserved logical levels", ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_row)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_row, method = FALSE)) expect_error( ard_svy_cat_col <- @@ -430,7 +430,7 @@ test_that("ard_categorical.survey.design() works for unobserved logical levels", ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_cell)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_cell, method = FALSE)) expect_error( ard_svy_cat_cell <- @@ -442,7 +442,7 @@ test_that("ard_categorical.survey.design() works for unobserved logical levels", ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_cell)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_cell, method = FALSE)) }) @@ -463,7 +463,7 @@ test_that("ard_categorical.survey.design() works with variables with only 1 leve ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_row)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_row, method = FALSE)) expect_error( ard_svy_cat_col <- @@ -475,7 +475,7 @@ test_that("ard_categorical.survey.design() works with variables with only 1 leve ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_col)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_col, method = FALSE)) expect_error( ard_svy_cat_cell <- @@ -487,7 +487,7 @@ test_that("ard_categorical.survey.design() works with variables with only 1 leve ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_cell)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_cell, method = FALSE)) # variables have only 1 level, no by variable data(api, package = "survey") @@ -503,7 +503,7 @@ test_that("ard_categorical.survey.design() works with variables with only 1 leve ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_row)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_row, method = FALSE)) expect_error( ard_svy_cat_col <- @@ -514,7 +514,7 @@ test_that("ard_categorical.survey.design() works with variables with only 1 leve ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_cell)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_cell, method = FALSE)) expect_error( ard_svy_cat_cell <- @@ -525,7 +525,7 @@ test_that("ard_categorical.survey.design() works with variables with only 1 leve ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_cell)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_cell, method = FALSE)) # variable AND by have only 1 level svy_titanic$variables$Survived <- as.factor(rep("Yes", length(svy_titanic$variables$Survived))) @@ -540,7 +540,7 @@ test_that("ard_categorical.survey.design() works with variables with only 1 leve ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_row)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_row, method = FALSE)) expect_error( ard_svy_cat_col <- @@ -552,7 +552,7 @@ test_that("ard_categorical.survey.design() works with variables with only 1 leve ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_cell)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_cell, method = FALSE)) expect_error( ard_svy_cat_cell <- @@ -564,6 +564,6 @@ test_that("ard_categorical.survey.design() works with variables with only 1 leve ), NA ) - expect_invisible(cards::check_ard_structure(ard_svy_cat_cell)) + expect_invisible(cards::check_ard_structure(ard_svy_cat_cell, method = FALSE)) })