-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'svy-ard_categorical_unit_tests' of https://github.com/i…
…nsightsengineering/cardx into svy-ard_categorical_unit_tests
- Loading branch information
Showing
2 changed files
with
327 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,326 @@ | ||
# Test survey.design works | ||
test_that("ard_dichotomous.survey.design() works", { | ||
svy_dicho <- survey::svydesign(ids = ~1, data = mtcars, weights = ~1) | ||
|
||
# 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)) | ||
|
||
# col 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 individual stats | ||
|
||
# section 1: by variable, row denominator | ||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_row, stat_name %in% "n") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = vs, variables = c(cyl, am), value = list(cyl = 4), denominator = "row") |> | ||
cards::get_ard_statistics(stat_name %in% "n") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_row, stat_name %in% "N") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = vs, variables = c(cyl, am), value = list(cyl = 4), denominator = "row") |> | ||
cards::get_ard_statistics(stat_name %in% "N") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_row, stat_name %in% "p") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = vs, variables = c(cyl, am), value = list(cyl = 4), denominator = "row") |> | ||
cards::get_ard_statistics(stat_name %in% "p") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_row, stat_name %in% "p.std.error") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = vs, variables = c(cyl, am), value = list(cyl = 4), denominator = "row") |> | ||
cards::get_ard_statistics(stat_name %in% "p.std.error") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_row, stat_name %in% "deff") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = vs, variables = c(cyl, am), value = list(cyl = 4), denominator = "row") |> | ||
cards::get_ard_statistics(stat_name %in% "deff") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_row, stat_name %in% "n_unweighted") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = vs, variables = c(cyl, am), value = list(cyl = 4), denominator = "row") |> | ||
cards::get_ard_statistics(stat_name %in% "n_unweighted") |> unlist() | ||
) | ||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_row, stat_name %in% "N_unweighted") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = vs, variables = c(cyl, am), value = list(cyl = 4), denominator = "row") |> | ||
cards::get_ard_statistics(stat_name %in% "N_unweighted") |> unlist() | ||
) | ||
|
||
# section 2: by variable, column denominator | ||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_col, stat_name %in% "n") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = vs, variables = c(cyl, am), value = list(cyl = 4), denominator = "column") |> | ||
cards::get_ard_statistics(stat_name %in% "n") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_col, stat_name %in% "N") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = vs, variables = c(cyl, am), value = list(cyl = 4), denominator = "column") |> | ||
cards::get_ard_statistics(stat_name %in% "N") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_col, stat_name %in% "p") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = vs, variables = c(cyl, am), value = list(cyl = 4), denominator = "column") |> | ||
cards::get_ard_statistics(stat_name %in% "p") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_col, stat_name %in% "p.std.error") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = vs, variables = c(cyl, am), value = list(cyl = 4), denominator = "column") |> | ||
cards::get_ard_statistics(stat_name %in% "p.std.error") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_col, stat_name %in% "deff") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = vs, variables = c(cyl, am), value = list(cyl = 4), denominator = "column") |> | ||
cards::get_ard_statistics(stat_name %in% "deff") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_col, stat_name %in% "n_unweighted") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = vs, variables = c(cyl, am), value = list(cyl = 4), denominator = "column") |> | ||
cards::get_ard_statistics(stat_name %in% "n_unweighted") |> unlist() | ||
) | ||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_col, stat_name %in% "N_unweighted") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = vs, variables = c(cyl, am), value = list(cyl = 4), denominator = "column") |> | ||
cards::get_ard_statistics(stat_name %in% "N_unweighted") |> unlist() | ||
) | ||
|
||
# section 3: by variable, cell denominator | ||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_cell, stat_name %in% "n") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = vs, variables = c(cyl, am), value = list(cyl = 4), denominator = "cell") |> | ||
cards::get_ard_statistics(stat_name %in% "n") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_cell, stat_name %in% "N") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = vs, variables = c(cyl, am), value = list(cyl = 4), denominator = "cell") |> | ||
cards::get_ard_statistics(stat_name %in% "N") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_cell, stat_name %in% "p") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = vs, variables = c(cyl, am), value = list(cyl = 4), denominator = "cell") |> | ||
cards::get_ard_statistics(stat_name %in% "p") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_cell, stat_name %in% "p.std.error") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = vs, variables = c(cyl, am), value = list(cyl = 4), denominator = "cell") |> | ||
cards::get_ard_statistics(stat_name %in% "p.std.error") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_cell, stat_name %in% "deff") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = vs, variables = c(cyl, am), value = list(cyl = 4), denominator = "cell") |> | ||
cards::get_ard_statistics(stat_name %in% "deff") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_cell, stat_name %in% "n_unweighted") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = vs, variables = c(cyl, am), value = list(cyl = 4), denominator = "cell") |> | ||
cards::get_ard_statistics(stat_name %in% "n_unweighted") |> unlist() | ||
) | ||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_cell, stat_name %in% "N_unweighted") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = vs, variables = c(cyl, am), value = list(cyl = 4), denominator = "cell") |> | ||
cards::get_ard_statistics(stat_name %in% "N_unweighted") |> unlist() | ||
) | ||
|
||
|
||
# row denom without by var | ||
expect_error( | ||
ard_dichotomous_row <- ard_dichotomous(svy_dicho, by = NULL, | ||
variables = c(cyl, am), | ||
value = list(cyl = 4), | ||
denominator = "row"), | ||
NA | ||
) | ||
expect_invisible(cards::check_ard_structure(ard_dichotomous_row, method = FALSE)) | ||
|
||
# col denom without by var | ||
expect_error( | ||
ard_dichotomous_col <- ard_dichotomous(svy_dicho, by = NULL, | ||
variables = c(cyl, am), | ||
value = list(cyl = 4), | ||
denominator = "column"), | ||
NA | ||
) | ||
expect_invisible(cards::check_ard_structure(ard_dichotomous_col, method = FALSE)) | ||
|
||
# col denom without by var | ||
expect_error( | ||
ard_dichotomous_cell <- ard_dichotomous(svy_dicho, by = NULL, | ||
variables = c(cyl, am), | ||
value = list(cyl = 4), | ||
denominator = "cell"), | ||
NA | ||
) | ||
expect_invisible(cards::check_ard_structure(ard_dichotomous_cell, method = FALSE)) | ||
|
||
|
||
# test individual stats | ||
|
||
# section 1: by variable, row denominator | ||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_row, stat_name %in% "n") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = NULL, variables = c(cyl, am), value = list(cyl = 4), denominator = "row") |> | ||
cards::get_ard_statistics(stat_name %in% "n") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_row, stat_name %in% "N") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = NULL, variables = c(cyl, am), value = list(cyl = 4), denominator = "row") |> | ||
cards::get_ard_statistics(stat_name %in% "N") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_row, stat_name %in% "p") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = NULL, variables = c(cyl, am), value = list(cyl = 4), denominator = "row") |> | ||
cards::get_ard_statistics(stat_name %in% "p") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_row, stat_name %in% "p.std.error") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = NULL, variables = c(cyl, am), value = list(cyl = 4), denominator = "row") |> | ||
cards::get_ard_statistics(stat_name %in% "p.std.error") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_row, stat_name %in% "deff") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = NULL, variables = c(cyl, am), value = list(cyl = 4), denominator = "row") |> | ||
cards::get_ard_statistics(stat_name %in% "deff") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_row, stat_name %in% "n_unweighted") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = NULL, variables = c(cyl, am), value = list(cyl = 4), denominator = "row") |> | ||
cards::get_ard_statistics(stat_name %in% "n_unweighted") |> unlist() | ||
) | ||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_row, stat_name %in% "N_unweighted") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = NULL, variables = c(cyl, am), value = list(cyl = 4), denominator = "row") |> | ||
cards::get_ard_statistics(stat_name %in% "N_unweighted") |> unlist() | ||
) | ||
|
||
# section 2: by variable, column denominator | ||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_col, stat_name %in% "n") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = NULL, variables = c(cyl, am), value = list(cyl = 4), denominator = "column") |> | ||
cards::get_ard_statistics(stat_name %in% "n") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_col, stat_name %in% "N") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = NULL, variables = c(cyl, am), value = list(cyl = 4), denominator = "column") |> | ||
cards::get_ard_statistics(stat_name %in% "N") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_col, stat_name %in% "p") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = NULL, variables = c(cyl, am), value = list(cyl = 4), denominator = "column") |> | ||
cards::get_ard_statistics(stat_name %in% "p") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_col, stat_name %in% "p.std.error") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = NULL, variables = c(cyl, am), value = list(cyl = 4), denominator = "column") |> | ||
cards::get_ard_statistics(stat_name %in% "p.std.error") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_col, stat_name %in% "deff") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = NULL, variables = c(cyl, am), value = list(cyl = 4), denominator = "column") |> | ||
cards::get_ard_statistics(stat_name %in% "deff") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_col, stat_name %in% "n_unweighted") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = NULL, variables = c(cyl, am), value = list(cyl = 4), denominator = "column") |> | ||
cards::get_ard_statistics(stat_name %in% "n_unweighted") |> unlist() | ||
) | ||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_col, stat_name %in% "N_unweighted") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = NULL, variables = c(cyl, am), value = list(cyl = 4), denominator = "column") |> | ||
cards::get_ard_statistics(stat_name %in% "N_unweighted") |> unlist() | ||
) | ||
|
||
# section 3: by variable, cell denominator | ||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_cell, stat_name %in% "n") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = NULL, variables = c(cyl, am), value = list(cyl = 4), denominator = "cell") |> | ||
cards::get_ard_statistics(stat_name %in% "n") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_cell, stat_name %in% "N") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = NULL, variables = c(cyl, am), value = list(cyl = 4), denominator = "cell") |> | ||
cards::get_ard_statistics(stat_name %in% "N") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_cell, stat_name %in% "p") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = NULL, variables = c(cyl, am), value = list(cyl = 4), denominator = "cell") |> | ||
cards::get_ard_statistics(stat_name %in% "p") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_cell, stat_name %in% "p.std.error") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = NULL, variables = c(cyl, am), value = list(cyl = 4), denominator = "cell") |> | ||
cards::get_ard_statistics(stat_name %in% "p.std.error") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_cell, stat_name %in% "deff") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = NULL, variables = c(cyl, am), value = list(cyl = 4), denominator = "cell") |> | ||
cards::get_ard_statistics(stat_name %in% "deff") |> unlist() | ||
) | ||
|
||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_cell, stat_name %in% "n_unweighted") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = NULL, variables = c(cyl, am), value = list(cyl = 4), denominator = "cell") |> | ||
cards::get_ard_statistics(stat_name %in% "n_unweighted") |> unlist() | ||
) | ||
expect_equal( | ||
cards::get_ard_statistics(ard_dichotomous_cell, stat_name %in% "N_unweighted") |> unlist(), | ||
cards::ard_dichotomous(svy_dicho, by = NULL, variables = c(cyl, am), value = list(cyl = 4), denominator = "cell") |> | ||
cards::get_ard_statistics(stat_name %in% "N_unweighted") |> unlist() | ||
) | ||
}) | ||
|
||
|
||
|