Skip to content

Commit

Permalink
smd for weighted data
Browse files Browse the repository at this point in the history
  • Loading branch information
ddsjoberg committed Mar 8, 2024
1 parent 76a026f commit 4097b35
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Suggests:
effectsize (>= 0.6.0),
parameters (>= 0.20.2),
smd (>= 0.6.6),
survey (>= 4.1),
spelling,
testthat (>= 3.2.0),
withr
Expand Down
15 changes: 14 additions & 1 deletion R/ard_smd.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ ard_smd <- function(data, by, variable, ...) {
check_not_missing(data)
check_not_missing(variable)
check_not_missing(by)

# grab design object if from `survey` ----------------------------------------
is_survey <- inherits(data, "survey.design")
if (is_survey) {
design <- data
data <- design$variables
}

# continue check/process inputs ----------------------------------------------
check_data_frame(data)
data <- dplyr::ungroup(data)
cards::process_selectors(data, by = {{ by }}, variable = {{ variable }})
Expand All @@ -37,7 +46,11 @@ ard_smd <- function(data, by, variable, ...) {
variable = variable,
lst_tidy =
cards::eval_capture_conditions(
smd::smd(x = data[[variable]], g = data[[by]], na.rm = TRUE, ...) |>
switch(
as.character(is_survey),
"TRUE" = smd::smd(x = data[[variable]], g = data[[by]], w = stats::weights(design), na.rm = TRUE, ...),
"FALSE" = smd::smd(x = data[[variable]], g = data[[by]], na.rm = TRUE, ...)
) |>
dplyr::select(-any_of("term"))
),
...
Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test-ard_smd.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,28 @@ test_that("ard_smd() works", {
)
})

test_that("ard_smd() works with survey data", {
skip_if_not(cards::is_pkg_installed("survey", reference_pkg = "cardx"))
data(api, package = "survey")
dclus1 <- survey::svydesign(id=~dnum, weights=~pw, data=apiclus1, fpc=~fpc)

expect_error(
ard_smd <-
dclus1 |>
ard_smd(by = both, variable = api00, std.error = TRUE),
NA
)

expect_equal(
ard_smd |>
cards::get_ard_statistics(stat_name %in% c("estimate", "std.error")),
smd::smd(x = apiclus1$api00, g = apiclus1$both, w = weights(dclus1), std.error = TRUE) |>
dplyr::select(-term) |>
unclass(),
ignore_attr = TRUE
)
})

test_that("ard_proptest() error messaging", {
# mis-specify the gref argument
expect_error(
Expand Down

0 comments on commit 4097b35

Please sign in to comment.