diff --git a/tests/testthat/_snaps/ard_dichotomous.survey.design.md b/tests/testthat/_snaps/ard_dichotomous.survey.design.md new file mode 100644 index 000000000..547ff4bef --- /dev/null +++ b/tests/testthat/_snaps/ard_dichotomous.survey.design.md @@ -0,0 +1,30 @@ +# ard_dichotomous.survey.design() returns an error with erroneous input + + Code + ard_dichotomous(svy_dicho, by = vs, variables = c(cyl, am), value = list(cyl = 4), + denominator = "row") + Condition + Error in `ard_dichotomous()`: + ! Error in argument `value` for variable "cyl". + i A value of 4 was passed, but must be one of TRUE and FALSE. + +--- + + Code + ard_dichotomous(svy_dicho, by = cyl, variables = c(vs, am), value = list(vs = 4), + denominator = "row") + Condition + Error in `ard_dichotomous()`: + ! Error in argument `value` for variable "vs". + i A value of 4 was passed, but must be one of 0 and 1. + +--- + + Code + ard_dichotomous(svy_dicho, by = cyl, variables = c(vs, disp), value = list( + disp = "turn"), denominator = "row") + Condition + Error in `ard_dichotomous()`: + ! Error in argument `value` for variable "disp". + i A value of "turn" was passed, but must be one of 71.1, 75.7, 78.7, 79, 95.1, 108, 120.1, 120.3, 121, 140.8, 145, 146.7, 160, 167.6, 225, 258, 275.8, 301, ..., 460, and 472. + diff --git a/tests/testthat/test-ard_categorical.survey.design.R b/tests/testthat/test-ard_categorical.survey.design.R index b19de0e55..00aa88f40 100644 --- a/tests/testthat/test-ard_categorical.survey.design.R +++ b/tests/testthat/test-ard_categorical.survey.design.R @@ -507,6 +507,7 @@ test_that("ard_categorical.survey.design() works for unobserved factor levels", NA ) expect_invisible(cards::check_ard_structure(ard_svy_cat_cell, method = FALSE)) + expect_equal( cards::get_ard_statistics(ard_svy_cat_row, stat_name %in% "n") |> unlist() |> sort(), ard_categorical(df_titanic, variables = c(Class, Age), by = Survived, denominator = "row") |> @@ -525,21 +526,27 @@ test_that("ard_categorical.survey.design() works for unobserved factor levels", cards::get_ard_statistics(stat_name %in% "p") |> unlist() |> sort() ) - # replace with by row combo - # expect_equal( - # cards::get_ard_statistics(ard_svy_cat_row |> dplyr::arrange_all(), stat_name %in% "p.std.error") |> unlist() |> unname() |> sort(), - # c(dplyr::tibble( - # variable_level = unique(svy_titanic$variables[["Class"]]) |> sort() |> as.character(), - # p = 1, - # p.std.error = 0, - # deff = NaN - # ) |> dplyr::select("p.std.error") |> unlist() |> unname(), dplyr::tibble( - # variable_level = unique(svy_titanic$variables[["Age"]]) |> sort() |> as.character(), - # p = 1, - # p.std.error = 0, - # deff = NaN - # ) |> dplyr::select("p.std.error") |> unlist() |> unname()) - # ) + expect_equal( + cards::get_ard_statistics(ard_svy_cat_row |> dplyr::arrange_all(), stat_name %in% "p.std.error") |> unlist() |> unname() |> sort(), + unname(c( + survey::svyby( + formula = reformulate2("Survived"), + by = reformulate2("Age"), + design = svy_titanic, + FUN = survey::svymean, + na.rm = TRUE, + deff = "Design Effect" + )[, c("se.SurvivedYes", "se.SurvivedNo", "se.SurvivedUnknown")] |> unlist(), + survey::svyby( + formula = reformulate2("Survived"), + by = reformulate2("Class"), + design = svy_titanic, + FUN = survey::svymean, + na.rm = TRUE, + deff = "Design Effect" + )[, c("se.SurvivedYes", "se.SurvivedNo", "se.SurvivedUnknown")] |> unlist() + )) |> sort() + ) expect_equal( cards::get_ard_statistics(ard_svy_cat_row, stat_name %in% "n_unweighted") |> unlist() |> unname() |> sort(), @@ -576,18 +583,28 @@ test_that("ard_categorical.survey.design() works for unobserved factor levels", cards::ard_categorical(df_titanic, variables = c(Class, Age), by = Survived, denominator = "column") |> cards::get_ard_statistics(stat_name %in% "p") |> unlist() |> sort() ) - # replace with by col combo - # expect_equal( - # cards::get_ard_statistics(ard_svy_cat_col, stat_name %in% "p.std.error") |> unlist() |> unname(), - # c( - # survey::svymean(reformulate2("Class"), design = svy_titanic, na.rm = TRUE, deff = "Design Effect") |> - # dplyr::as_tibble(rownames = "var_level") |> - # dplyr::select("SE") |> unlist() |> unname(), - # survey::svymean(reformulate2("Age"), design = svy_titanic, na.rm = TRUE, deff = "Design Effect") |> - # dplyr::as_tibble(rownames = "var_level") |> - # dplyr::select("SE") |> unlist() |> unname() - # ) - # ) + + expect_equal( + unname(cards::get_ard_statistics(ard_svy_cat_col |> dplyr::arrange_all(), stat_name %in% "p.std.error")) |> unlist() |> sort(), + unname(c( + survey::svyby( + formula = reformulate2("Age"), + by = reformulate2("Survived"), + design = svy_titanic, + FUN = survey::svymean, + na.rm = TRUE, + deff = "Design Effect" + )[, c("se.AgeAdult", "se.AgeChild")] |> unlist(), + survey::svyby( + formula = reformulate2("Class"), + by = reformulate2("Survived"), + design = svy_titanic, + FUN = survey::svymean, + na.rm = TRUE, + deff = "Design Effect" + )[, c("se.Class1st", "se.Class2nd", "se.Class3rd", "se.ClassCrew")] |> unlist() + )) |> sort() + ) expect_equal( cards::get_ard_statistics(ard_svy_cat_col, stat_name %in% "n_unweighted") |> unlist() |> unname() |> sort(), @@ -625,17 +642,23 @@ test_that("ard_categorical.survey.design() works for unobserved factor levels", cards::get_ard_statistics(stat_name %in% "p") |> unlist() |> sort() ) - # expect_equal( - # cards::get_ard_statistics(ard_svy_cat_cell, stat_name %in% "p.std.error") |> unlist() |> unname(), - # c( - # survey::svymean(reformulate2("Class"), design = svy_titanic, na.rm = TRUE, deff = "Design Effect") |> - # dplyr::as_tibble(rownames = "var_level") |> - # dplyr::select("SE") |> unlist() |> unname(), - # survey::svymean(reformulate2("Age"), design = svy_titanic, na.rm = TRUE, deff = "Design Effect") |> - # dplyr::as_tibble(rownames = "var_level") |> - # dplyr::select("SE") |> unlist() |> unname() - # ) - # ) + expect_equal( + cards::get_ard_statistics(ard_svy_cat_cell |> dplyr::arrange_all(), stat_name %in% "p.std.error") |> unlist() |> unname() |> sort(), + unname(c( + as.data.frame(survey::svymean( + x = inject(~ interaction(!!sym(bt("Survived")), !!sym(bt("Class")))), + design = svy_titanic, + na.rm = TRUE, + deff = "Design Effect" + ))[, "SE"] |> unlist(), + as.data.frame(survey::svymean( + x = inject(~ interaction(!!sym(bt("Survived")), !!sym(bt("Age")))), + design = svy_titanic, + na.rm = TRUE, + deff = "Design Effect" + ))[, "SE"] |> unlist() + )) |> sort() + ) expect_equal( cards::get_ard_statistics(ard_svy_cat_cell, stat_name %in% "n_unweighted") |> unlist() |> unname() |> sort() , @@ -737,7 +760,13 @@ 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(fct_expand(svy_titanic$variables$Survived, FALSE)) + + df_titanic <- as.data.frame(Titanic) |> tidyr::uncount(weights = Freq) + df_titanic$Survived <- rep(TRUE, length(df_titanic$Survived)) + + # for unweighted + df_uw <- as.data.frame(Titanic) + df_uw$Survived <- rep(TRUE, length(df_uw$Survived)) expect_error( ard_svy_cat_row <- @@ -775,6 +804,176 @@ test_that("ard_categorical.survey.design() works for unobserved logical levels", ) expect_invisible(cards::check_ard_structure(ard_svy_cat_cell, method = FALSE)) + expect_equal( + cards::get_ard_statistics(ard_svy_cat_row, stat_name %in% "n") |> unlist() |> sort(), + ard_categorical(df_titanic, variables = c(Class, Age), by = Survived, denominator = "row") |> + cards::get_ard_statistics(stat_name %in% "n") |> unlist() |> sort() + ) + + expect_equal( + cards::get_ard_statistics(ard_svy_cat_row, stat_name %in% "N") |> unlist() |> sort(), + cards::ard_categorical(df_titanic, variables = c(Class, Age), by = Survived, denominator = "row") |> + cards::get_ard_statistics(stat_name %in% "N") |> unlist() |> sort() + ) + + expect_equal( + cards::get_ard_statistics(ard_svy_cat_row, stat_name %in% "p") |> unlist() |> sort(), + cards::ard_categorical(df_titanic, variables = c(Class, Age), by = Survived, denominator = "row") |> + cards::get_ard_statistics(stat_name %in% "p") |> unlist() |> sort() + ) + + expect_equal( + cards::get_ard_statistics(ard_svy_cat_row |> dplyr::arrange_all(), stat_name %in% "p.std.error") |> unlist() |> unname() |> sort(), + unname(c( + survey::svyby( + formula = reformulate2("Survived"), + by = reformulate2("Age"), + design = svy_titanic, + FUN = survey::svymean, + na.rm = TRUE, + deff = "Design Effect" + )[, c("se.SurvivedFALSE", "se.SurvivedTRUE")] |> unlist(), + survey::svyby( + formula = reformulate2("Survived"), + by = reformulate2("Class"), + design = svy_titanic, + FUN = survey::svymean, + na.rm = TRUE, + deff = "Design Effect" + )[, c("se.SurvivedFALSE", "se.SurvivedTRUE")] |> unlist() + )) |> sort() + ) + + expect_equal( + cards::get_ard_statistics(ard_svy_cat_row, stat_name %in% "n_unweighted") |> unlist() |> unname() |> sort(), + cards::ard_categorical(df_uw, variables = c(Class, Age), by = Survived, denominator = "row") |> + cards::get_ard_statistics(stat_name %in% "n") |> unlist() |> unname() |> sort() + ) + + expect_equal( + cards::get_ard_statistics(ard_svy_cat_row, stat_name %in% "N_unweighted") |> unlist() |> unname() |> sort(), + cards::ard_categorical(df_uw, variables = c(Class, Age), by = Survived, denominator = "row") |> + cards::get_ard_statistics(stat_name %in% "N") |> unlist() |> unname() |> sort() + ) + + expect_equal( + cards::get_ard_statistics(ard_svy_cat_row, stat_name %in% "p_unweighted") |> unlist() |> unname() |> sort(), + cards::ard_categorical(df_uw, variables = c(Class, Age), by = Survived, denominator = "row") |> + cards::get_ard_statistics(stat_name %in% "p") |> unlist() |> unname() |> sort() + ) + + expect_equal( + cards::get_ard_statistics(ard_svy_cat_col, stat_name %in% "n") |> unlist() |> sort(), + cards::ard_categorical(df_titanic, variables = c(Class, Age), by = Survived, denominator = "column") |> + cards::get_ard_statistics(stat_name %in% "n") |> unlist() |> sort() + ) + + expect_equal( + cards::get_ard_statistics(ard_svy_cat_col, stat_name %in% "N") |> unlist() |> sort(), + cards::ard_categorical(df_titanic, variables = c(Class, Age), by = Survived, denominator = "column") |> + cards::get_ard_statistics(stat_name %in% "N") |> unlist() |> sort() + ) + + expect_equal( + cards::get_ard_statistics(ard_svy_cat_col, stat_name %in% "p") |> unlist() |> sort(), + cards::ard_categorical(df_titanic, variables = c(Class, Age), by = Survived, denominator = "column") |> + cards::get_ard_statistics(stat_name %in% "p") |> unlist() |> sort() + ) + + expect_equal( + unname(cards::get_ard_statistics(ard_svy_cat_col |> dplyr::arrange_all(), stat_name %in% "p.std.error")) |> unlist() |> sort(), + unname(c( + survey::svyby( + formula = reformulate2("Age"), + by = reformulate2("Survived"), + design = svy_titanic, + FUN = survey::svymean, + na.rm = TRUE, + deff = "Design Effect" + )[, c("se.AgeAdult", "se.AgeChild")] |> unlist(), + survey::svyby( + formula = reformulate2("Class"), + by = reformulate2("Survived"), + design = svy_titanic, + FUN = survey::svymean, + na.rm = TRUE, + deff = "Design Effect" + )[, c("se.Class1st", "se.Class2nd", "se.Class3rd", "se.ClassCrew")] |> unlist() + )) |> sort() + ) + + expect_equal( + cards::get_ard_statistics(ard_svy_cat_col, stat_name %in% "n_unweighted") |> unlist() |> unname() |> sort(), + cards::ard_categorical(df_uw, variables = c(Class, Age), by = Survived, denominator = "column") |> + cards::get_ard_statistics(stat_name %in% "n") |> unlist() |> unname() |> sort() + ) + + expect_equal( + cards::get_ard_statistics(ard_svy_cat_col, stat_name %in% "N_unweighted") |> unlist() |> unname() |> sort(), + cards::ard_categorical(df_uw, variables = c(Class, Age), by = Survived, denominator = "column") |> + cards::get_ard_statistics(stat_name %in% "N") |> unlist() |> unname() |> sort() + ) + + expect_equal( + cards::get_ard_statistics(ard_svy_cat_col, stat_name %in% "p_unweighted") |> unlist() |> unname() |> sort(), + cards::ard_categorical(df_uw, variables = c(Class, Age), by = Survived, denominator = "column") |> + cards::get_ard_statistics(stat_name %in% "p") |> unlist() |> unname() |> sort() + ) + + expect_equal( + cards::get_ard_statistics(ard_svy_cat_cell, stat_name %in% "n") |> unlist() |> sort(), + cards::ard_categorical(df_titanic, variables = c(Class, Age), by = Survived, denominator = "cell") |> + cards::get_ard_statistics(stat_name %in% "n") |> unlist() |> sort() + ) + + expect_equal( + cards::get_ard_statistics(ard_svy_cat_cell, stat_name %in% "N") |> unlist(), + cards::ard_categorical(df_titanic, variables = c(Class, Age), by = Survived, denominator = "cell") |> + cards::get_ard_statistics(stat_name %in% "N") |> unlist() + ) + + expect_equal( + cards::get_ard_statistics(ard_svy_cat_cell, stat_name %in% "p") |> unlist() |> sort(), + cards::ard_categorical(df_titanic, variables = c(Class, Age), by = Survived, denominator = "cell") |> + cards::get_ard_statistics(stat_name %in% "p") |> unlist() |> sort() + ) + + expect_equal( + cards::get_ard_statistics(ard_svy_cat_cell |> dplyr::arrange_all(), stat_name %in% "p.std.error") |> unlist() |> unname() |> sort(), + unname(c( + as.data.frame(survey::svymean( + x = inject(~ interaction(!!sym(bt("Survived")), !!sym(bt("Class")))), + design = svy_titanic, + na.rm = TRUE, + deff = "Design Effect" + ))[, "SE"] |> unlist(), + as.data.frame(survey::svymean( + x = inject(~ interaction(!!sym(bt("Survived")), !!sym(bt("Age")))), + design = svy_titanic, + na.rm = TRUE, + deff = "Design Effect" + ))[, "SE"] |> unlist() + )) |> sort() + ) + + expect_equal( + cards::get_ard_statistics(ard_svy_cat_cell, stat_name %in% "n_unweighted") |> unlist() |> unname() |> sort() , + cards::ard_categorical(df_uw, variables = c(Class, Age), by = Survived, denominator = "cell") |> + cards::get_ard_statistics(stat_name %in% "n") |> unlist() |> unname() |> sort() + ) + + expect_equal( + cards::get_ard_statistics(ard_svy_cat_cell, stat_name %in% "N_unweighted") |> unlist() |> unname() |> sort(), + cards::ard_categorical(df_uw, variables = c(Class, Age), by = Survived, denominator = "cell") |> + cards::get_ard_statistics(stat_name %in% "N") |> unlist() |> unname() |> sort() + ) + + expect_equal( + cards::get_ard_statistics(ard_svy_cat_cell, stat_name %in% "p_unweighted") |> unlist() |> unname() |> sort(), + cards::ard_categorical(df_uw, variables = c(Class, Age), by = Survived, denominator = "cell") |> + cards::get_ard_statistics(stat_name %in% "p") |> unlist() |> unname() |> sort() + ) + # variables have unobserved levels, no by variable data(api, package = "survey") svy_titanic <- survey::svydesign(~1, data = as.data.frame(Titanic), weights = ~Freq) diff --git a/tests/testthat/test-ard_dichotomous.survey.design.R b/tests/testthat/test-ard_dichotomous.survey.design.R index 359f2e137..9a27e8489 100644 --- a/tests/testthat/test-ard_dichotomous.survey.design.R +++ b/tests/testthat/test-ard_dichotomous.survey.design.R @@ -32,7 +32,7 @@ test_that("ard_dichotomous.survey.design() works", { ) expect_invisible(cards::check_ard_structure(ard_dichotomous_col, method = FALSE)) - # col denom with by var + # cell denom with by var expect_error( ard_dichotomous_cell <- ard_dichotomous(svy_dicho, by = vs, @@ -309,3 +309,144 @@ test_that("ard_dichotomous.survey.design() works", { ) expect_invisible(cards::check_ard_structure(ard_dichotomous_cell, method = FALSE)) }) + +test_that("ard_dichotomous.survey.design() works with various input types", { + svy_dicho <- survey::svydesign(ids = ~1, data = mtcars, weights = ~1) + + # logical variables + # convert variables to logical + svy_dicho$variables <- svy_dicho$variables |> + dplyr::mutate(across(c("cyl", "am", "vs"), as.logical)) + + + # row denom with by var + expect_error( + ard_dichotomous_row <- + ard_dichotomous(svy_dicho, + by = vs, + variables = c(cyl, am), + value = list(cyl = TRUE), + denominator = "row" + ), + NA + ) + expect_invisible(cards::check_ard_structure(ard_dichotomous_row, method = FALSE)) + + # col denom with by var + expect_error( + ard_dichotomous_col <- ard_dichotomous(svy_dicho, + by = vs, + variables = c(cyl, am), + value = list(cyl = TRUE), + denominator = "column" + ), + NA + ) + expect_invisible(cards::check_ard_structure(ard_dichotomous_col, method = FALSE)) + + # cell denom with by var + expect_error( + ard_dichotomous_cell <- ard_dichotomous(svy_dicho, + by = vs, + variables = c(cyl, am), + value = list(cyl = TRUE), + denominator = "cell" + ), + NA + ) + expect_invisible(cards::check_ard_structure(ard_dichotomous_cell, method = FALSE)) + + + # variables that are neither logical or factor + svy_dicho <- survey::svydesign(ids = ~1, data = mtcars, weights = ~1) + + svy_dicho$variables <- svy_dicho$variables |> + dplyr::mutate(across(c("cyl", "am"), as.numeric)) |> + dplyr::mutate(across("vs", as.character)) + + # row denom with by var + expect_error( + ard_dichotomous_row <- + ard_dichotomous(svy_dicho, + by = vs, + variables = c(cyl, am), + value = list(cyl = 4), + denominator = "row" + ), + NA + ) + expect_invisible(cards::check_ard_structure(ard_dichotomous_row, method = FALSE)) + + # col denom with by var + expect_error( + ard_dichotomous_col <- ard_dichotomous(svy_dicho, + by = vs, + variables = c(cyl, am), + value = list(cyl = 4), + denominator = "column" + ), + NA + ) + expect_invisible(cards::check_ard_structure(ard_dichotomous_col, method = FALSE)) + + # cell denom with by var + expect_error( + ard_dichotomous_cell <- ard_dichotomous(svy_dicho, + by = vs, + variables = c(cyl, am), + value = list(cyl = 4), + denominator = "cell" + ), + NA + ) + expect_invisible(cards::check_ard_structure(ard_dichotomous_cell, method = FALSE)) + +}) + + +test_that("ard_dichotomous.survey.design() returns an error with erroneous input", { + + # value passed in is not logical should return an error + svy_dicho <- survey::svydesign(ids = ~1, data = mtcars, weights = ~1) + + svy_dicho$variables <- svy_dicho$variables |> + dplyr::mutate(across(c("cyl", "am"), as.logical)) + + expect_snapshot( + ard_dichotomous(svy_dicho, + by = vs, + variables = c(cyl, am), + value = list(cyl = 4), + denominator = "row" + ), + error = TRUE +) + + # supplied factor value is not a level + svy_dicho$variables <- svy_dicho$variables |> + dplyr::mutate(across("vs", as.factor)) + + svy_dicho$variables$vs + expect_snapshot( + ard_dichotomous(svy_dicho, + by = cyl, + variables = c(vs, am), + value = list(vs = 4), + denominator = "row" + ), + error = TRUE + ) + + svy_dicho$variables <- svy_dicho$variables |> + dplyr::mutate(across("disp", as.numeric)) + + expect_snapshot( + ard_dichotomous(svy_dicho, + by = cyl, + variables = c(vs, disp), + value = list(disp = "turn"), + denominator = "row" + ), + error = TRUE + ) +})