Skip to content

Commit

Permalink
Merge branch 'svy-ard_categorical_unit_tests' of https://github.com/i…
Browse files Browse the repository at this point in the history
…nsightsengineering/cardx into svy-ard_categorical_unit_tests
  • Loading branch information
ddsjoberg committed Jun 24, 2024
2 parents a5e63ec + 592833a commit 46abb78
Show file tree
Hide file tree
Showing 2 changed files with 327 additions and 8 deletions.
9 changes: 1 addition & 8 deletions tests/testthat/test-ard_categorical.survey.design.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
# - A trick to test survey data is to take a normal data frame, convert it to survey using equal weights.
# Then all the results should equal the unweighted summaries and we can perform expect_equal() checks against the data.frame S3 methods.
# dplyr::tibble(y = rep(FALSE, 15), x = rep(TRUE, 15)) |>
# survey::svydesign(ids = ~1, data = _, weights = ~1) |>
# ard_categorical(by = y)

test_that("multiplication works", {
expect_equal(2 * 2, 4)
})
Expand Down Expand Up @@ -382,8 +376,7 @@ test_that("ard_categorical.survey.design() works when variables have all NAs", {
variables = c(Class, Age),
by = Survived,
denominator = "column"
),
NA
)
)

# cell denom
Expand Down
326 changes: 326 additions & 0 deletions tests/testthat/test-ard_dichotomous.survey.design.R
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()
)
})



0 comments on commit 46abb78

Please sign in to comment.