Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added construct_model.survey.design() method #128

Merged
merged 2 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ S3method(ard_regression,default)
S3method(ard_stats_anova,anova)
S3method(ard_stats_anova,data.frame)
S3method(construct_model,data.frame)
S3method(construct_model,survey.design)
export("%>%")
export(all_of)
export(any_of)
Expand Down
40 changes: 35 additions & 5 deletions R/construction_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
#'
#' - `bt_strip()`: Removes backticks from a string if it begins and ends with a backtick.
#'
#' @param x (`data.frame`)\cr
#' a data frame
#' @param x
#' - `construct_model.data.frame()` (`data.frame`) a data frame
#' - `construct_model.survey.design()` (`survey.design`) a survey design object
#' - `bt()`/`bt_strip()` (`character`) character vector, typically of variable names
#' @param formula (`formula`)\cr
#' a formula
#' @param method (`string`)\cr
Expand All @@ -26,8 +28,6 @@
#' @param package (`string`)\cr
#' string of package name that will be temporarily loaded when function
#' specified in `method` is executed.
#' @param x (`character`)\cr
#' character vector, typically of variable names
#' @param pattern (`string`)\cr
#' regular expression string. If the regex matches, backticks are added
#' to the string. When `NULL`, backticks are not added.
Expand Down Expand Up @@ -80,7 +80,7 @@ construct_model.data.frame <- function(x, formula, method, method.args = list(),
check_not_namespaced(method)

# convert method.args to list of expressions (to account for NSE inputs) -----
method.args <- call_args(enexpr(method.args))
method.args <- .as_list_of_exprs({{ method.args }})

# build model ----------------------------------------------------------------
withr::with_namespace(
Expand All @@ -90,6 +90,36 @@ construct_model.data.frame <- function(x, formula, method, method.args = list(),
)
}

#' @rdname construction_helpers
#' @export
construct_model.survey.design <- function(x, formula, method, method.args = list(), package = "survey", env = caller_env(), ...) {
set_cli_abort_call()
# check pkg installations ----------------------------------------------------
check_dots_empty()
check_pkg_installed(c("withr", package), reference_pkg = "cardx")

check_not_missing(formula)
check_class(formula, cls = "formula")

check_not_missing(method)
check_string(method)
check_not_namespaced(method)

# convert method.args to list of expressions (to account for NSE inputs) -----
method.args <- .as_list_of_exprs({{ method.args }})

# build model ----------------------------------------------------------------
withr::with_namespace(
package = package,
call2(.fn = method, formula = formula, design = x, !!!method.args) |>
eval_tidy(env = env)
)
}

.as_list_of_exprs <- function(x) {
call_args(enexpr(x))
}

#' @rdname construction_helpers
#' @export
reformulate2 <- function(termlabels, response = NULL, intercept = TRUE,
Expand Down
18 changes: 16 additions & 2 deletions man/construction_helpers.Rd

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

21 changes: 21 additions & 0 deletions tests/testthat/test-construction_helpers.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
skip_if_not(is_pkg_installed(c("withr", "survey"), reference_pkg = "cardx"))

test_that("construct_model() works", {
expect_snapshot(
construct_model(
Expand Down Expand Up @@ -27,4 +29,23 @@ test_that("construct_model() works", {
check_not_namespaced("geepack::geeglm"),
"cannot be namespaced"
)

# now the survey method -------
# styler: off
expect_equal({
data(api, package = "survey")
# stratified sample
survey::svydesign(id = ~1, strata = ~stype, weights = ~pw, data = apistrat, fpc = ~fpc) |>
construct_model(formula = api00 ~ api99, method = "svyglm") |>
ard_regression() |>
cards::get_ard_statistics(stat_name %in% "estimate")},
survey::svyglm(
api00 ~ api99,
design = survey::svydesign(id = ~1, strata = ~stype, weights = ~pw, data = apistrat, fpc = ~fpc)
) |>
coef() |>
getElement(2L) |>
list(estimate = _)
)
# styler: on
})
Loading