Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ddsjoberg committed Mar 9, 2024
1 parent 9339fb6 commit 3b139d1
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 9 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ LICENSE
^\.git$
^\.github$
^\.gitlab-ci\.yml$
^codecov\.yml$

# lintr
^\.lintr$
Expand Down
16 changes: 8 additions & 8 deletions R/ard_svyttest.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#'
#' @examplesIf cards::is_pkg_installed(c("survey", "broom"), reference_pkg = "cardx")
#' data(api, package = "survey")
#' dclus2 <- survey::svydesign(id = ~dnum + snum, fpc = ~fpc1 + fpc2, data = apiclus2)
#' dclus2 <- survey::svydesign(id = ~ dnum + snum, fpc = ~ fpc1 + fpc2, data = apiclus2)
#'
#' ard_svyttest(dclus2, variable = enroll, by = comp.imp, conf.level = 0.9)
ard_svyttest <- function(data, by, variable, conf.level = 0.95, ...) {
Expand All @@ -44,15 +44,16 @@ ard_svyttest <- function(data, by, variable, conf.level = 0.95, ...) {
cards::eval_capture_conditions(
survey::svyttest(stats::reformulate(by, response = variable), design = data, ...) %>%
# a slightly enhanced tidier that allows us to specify the conf.level
{dplyr::bind_cols(
broom::tidy(.) |> dplyr::select(-c("conf.low", "conf.high")),
dplyr::tibble(!!!stats::confint(., level = conf.level) |> set_names(c("conf.low", "conf.high"))) |>
dplyr::mutate(conf.level = conf.level)
)}
{
dplyr::bind_cols(
broom::tidy(.) |> dplyr::select(-c("conf.low", "conf.high")),
dplyr::tibble(!!!stats::confint(., level = conf.level) |> set_names(c("conf.low", "conf.high"))) |>
dplyr::mutate(conf.level = conf.level)
)
}
),
...
)

}

.format_svyttest_results <- function(by, variable, lst_tidy, ...) {
Expand All @@ -79,4 +80,3 @@ ard_svyttest <- function(data, by, variable, conf.level = 0.95, ...) {
dplyr::mutate(stat_label = dplyr::coalesce(.data$stat_label, .data$stat_name)) |>
cards::tidy_ard_column_order()
}

1 change: 1 addition & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
comment: false
2 changes: 1 addition & 1 deletion man/ard_svyttest.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions tests/testthat/test-ard_svyttest.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
skip_if_not(cards::is_pkg_installed(c("survey", "broom"), reference_pkg = "cardx"))

test_that("ard_svyttest() works", {
data(api, package = "survey")
dclus2 <- survey::svydesign(id = ~ dnum + snum, fpc = ~ fpc1 + fpc2, data = apiclus2)

expect_error(
ard_svyttest <-
ard_svyttest(
dclus2,
variable = enroll,
by = comp.imp,
conf.level = 0.9
),
NA
)

expect_equal(
cards::get_ard_statistics(
ard_svyttest,
stat_name %in% c("estimate", "p.value")
),
survey::svyttest(enroll ~ comp.imp, dclus2)[c("estimate", "p.value")],
ignore_attr = TRUE
)

expect_equal(
cards::get_ard_statistics(
ard_svyttest,
stat_name %in% c("conf.low", "conf.high")
),
survey::svyttest(enroll ~ comp.imp, dclus2) |>
confint(level = 0.9) |>
as.list(),
ignore_attr = TRUE
)
})

test_that("ard_svyttest() messaging", {
data(api, package = "survey")
dclus2 <- survey::svydesign(id = ~ dnum + snum, fpc = ~ fpc1 + fpc2, data = apiclus2)

expect_error(
ard_svyttest <-
ard_svyttest(
dclus2,
variable = enroll,
by = stype
),
NA
)
expect_equal(
ard_svyttest$error |> unique() |> unlist(),
"group must be binary"
)
})

0 comments on commit 3b139d1

Please sign in to comment.