Skip to content

Commit

Permalink
style files. update description
Browse files Browse the repository at this point in the history
  • Loading branch information
ayogasekaram committed Jun 21, 2024
1 parent 9181530 commit 821a4bd
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 83 deletions.
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ Suggests:
parameters (>= 0.20.2),
smd (>= 0.6.6),
spelling,
standalone,
survey (>= 4.1),
survival (>= 3.6-4),
testthat (>= 3.2.0),
Expand Down
57 changes: 28 additions & 29 deletions R/ard_categorical.survey.design.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -235,7 +235,6 @@ check_na_factor_levels <- function(data, variables) {
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 (inherits(data$variables[[variable]], "logical")) {
data$variables[[variable]] <- factor(data$variables[[variable]], levels = c(TRUE, FALSE))
Expand Down Expand Up @@ -337,9 +336,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")) |>
Expand Down Expand Up @@ -370,9 +369,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")) |>
Expand Down Expand Up @@ -414,27 +413,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
)
)
}

Expand Down
101 changes: 48 additions & 53 deletions tests/testthat/test-ard_categorical.survey.design.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,54 +104,53 @@ test_that("ard_categorical.survey.design() works", {


# check the calculated stats are correct
# expect_equal(
# cards::get_ard_statistics(ard_svy_cat_row, stat_name %in% "n") |> unlist(),
# survey::svymean(x = ~api00, dclus1, na.rm = TRUE)[1] |> unlist(),
# ignore_attr = TRUE
# )
# expect_equal(
# cards::get_ard_statistics(ard_svy_cat_row, stat_name %in% "N") |> unlist(),
# survey::svyquantile(x = ~api00, dclus1, na.rm = TRUE, quantiles = 0.5)[[1]][1] |> unlist(),
# ignore_attr = TRUE
# )
# expect_equal(
# cards::get_ard_statistics(ard_svy_cat_row, stat_name %in% "p") |> unlist(),
# dclus1$variables$api00 |> min(na.rm = TRUE),
# ignore_attr = TRUE
# )
# expect_equal(
# cards::get_ard_statistics(ard_svy_cat_row, stat_name %in% "deff") |> unlist(),
# dclus1$variables$api00 |> max(na.rm = TRUE),
# ignore_attr = TRUE
# )
# expect_equal(
# cards::get_ard_statistics(ard_svy_cat_row, stat_name %in% "p.std.error") |> unlist(),
# survey::svyvar(x = ~api00, dclus1, na.rm = TRUE)[1] |> unlist(),
# ignore_attr = TRUE
# )
# expect_equal(
# cards::get_ard_statistics(ard_uni_svy_cont, stat_name %in% "sd") |> unlist(),
# survey::svyvar(x = ~api00, dclus1, na.rm = TRUE)[1] |> unlist() |> sqrt(),
# ignore_attr = TRUE
# )
# expect_equal(
# cards::get_ard_statistics(ard_uni_svy_cont, stat_name %in% "mean.std.error") |> unlist(),
# survey::svymean(x = ~api00, dclus1, na.rm = TRUE) |> survey::SE() |> unlist(),
# ignore_attr = TRUE
# )
# expect_equal(
# cards::get_ard_statistics(ard_uni_svy_cont, stat_name %in% "deff") |> unlist(),
# survey::svymean(x = ~api00, dclus1, na.rm = TRUE, deff = TRUE) |>
# as.data.frame() |>
# dplyr::pull(deff),
# ignore_attr = TRUE
# )
# expect_equal(
# cards::get_ard_statistics(ard_uni_svy_cont, stat_name %in% "p75") |> unlist(),
# survey::svyquantile(x = ~api00, dclus1, na.rm = TRUE, quantiles = 0.75)[[1]][1] |> unlist(),
# ignore_attr = TRUE
# )

expect_equal(
cards::get_ard_statistics(ard_svy_cat_row, stat_name %in% "n") |> unlist(),
survey::svymean(x = ~api00, dclus1, na.rm = TRUE)[1] |> unlist(),
ignore_attr = TRUE
)
expect_equal(
cards::get_ard_statistics(ard_svy_cat_row, stat_name %in% "N") |> unlist(),
survey::svyquantile(x = ~api00, dclus1, na.rm = TRUE, quantiles = 0.5)[[1]][1] |> unlist(),
ignore_attr = TRUE
)
expect_equal(
cards::get_ard_statistics(ard_svy_cat_row, stat_name %in% "p") |> unlist(),
dclus1$variables$api00 |> min(na.rm = TRUE),
ignore_attr = TRUE
)
expect_equal(
cards::get_ard_statistics(ard_svy_cat_row, stat_name %in% "deff") |> unlist(),
dclus1$variables$api00 |> max(na.rm = TRUE),
ignore_attr = TRUE
)
expect_equal(
cards::get_ard_statistics(ard_svy_cat_row, stat_name %in% "p.std.error") |> unlist(),
survey::svyvar(x = ~api00, dclus1, na.rm = TRUE)[1] |> unlist(),
ignore_attr = TRUE
)
expect_equal(
cards::get_ard_statistics(ard_uni_svy_cont, stat_name %in% "sd") |> unlist(),
survey::svyvar(x = ~api00, dclus1, na.rm = TRUE)[1] |> unlist() |> sqrt(),
ignore_attr = TRUE
)
expect_equal(
cards::get_ard_statistics(ard_uni_svy_cont, stat_name %in% "mean.std.error") |> unlist(),
survey::svymean(x = ~api00, dclus1, na.rm = TRUE) |> survey::SE() |> unlist(),
ignore_attr = TRUE
)
expect_equal(
cards::get_ard_statistics(ard_uni_svy_cont, stat_name %in% "deff") |> unlist(),
survey::svymean(x = ~api00, dclus1, na.rm = TRUE, deff = TRUE) |>
as.data.frame() |>
dplyr::pull(deff),
ignore_attr = TRUE
)
expect_equal(
cards::get_ard_statistics(ard_uni_svy_cont, stat_name %in% "p75") |> unlist(),
survey::svyquantile(x = ~api00, dclus1, na.rm = TRUE, quantiles = 0.75)[[1]][1] |> unlist(),
ignore_attr = TRUE
)
})

# - What happens with a variable that is all NA? How does that behavior compare to `ard_categorical()` for data frames ----
Expand All @@ -160,7 +159,7 @@ test_that("ard_categorical.survey.design() works when variables have all NAs", {
data(api, package = "survey")
svy_titanic <- survey::svydesign(~1, data = as.data.frame(Titanic), weights = ~Freq)

#row denom
# row denom
svy_titanic$variables$Class <- NA
svy_titanic$variables$Class <- fct_na_value_to_level(svy_titanic$variables$Class)

Expand Down Expand Up @@ -200,7 +199,6 @@ test_that("ard_categorical.survey.design() works when variables have all NAs", {
# NA
# )
# expect_invisible(cards::check_ard_structure(ard_svy_cat_cell, method = FALSE))

})

# - Do we get results for unobserved factor levels in the `by` and `variable` variables?
Expand Down Expand Up @@ -321,7 +319,6 @@ test_that("ard_categorical.survey.design() works for unobserved factor levels",
NA
)
expect_invisible(cards::check_ard_structure(ard_svy_cat_cell, method = FALSE))

})

# - Do we get results for unobserved logical levels in the `by` and `variable` variables?
Expand Down Expand Up @@ -443,7 +440,6 @@ test_that("ard_categorical.survey.design() works for unobserved logical levels",
NA
)
expect_invisible(cards::check_ard_structure(ard_svy_cat_cell, method = FALSE))

})

# - Does the work around apply for variables with only 1 level
Expand Down Expand Up @@ -565,5 +561,4 @@ test_that("ard_categorical.survey.design() works with variables with only 1 leve
NA
)
expect_invisible(cards::check_ard_structure(ard_svy_cat_cell, method = FALSE))

})

0 comments on commit 821a4bd

Please sign in to comment.