Skip to content

Commit 1b93221

Browse files
authored
added construct_model.survey.design() method (#128)
**What changes are proposed in this pull request?** * Style this entry in a way that can be copied directly into `NEWS.md`. (#<issue number>, @<username>) Provide more detail here as needed. **Reference GitHub issue associated with pull request.** _e.g., 'closes #<issue number>'_ -------------------------------------------------------------------------------- Pre-review Checklist (if item does not apply, mark is as complete) - [ ] **All** GitHub Action workflows pass with a ✅ - [ ] PR branch has pulled the most recent updates from master branch: `usethis::pr_merge_main()` - [ ] If a bug was fixed, a unit test was added. - [ ] If a new `ard_*()` function was added, it passes the ARD structural checks from `cards::check_ard_structure()`. - [ ] If a new `ard_*()` function was added, `set_cli_abort_call()` has been set. - [ ] If a new `ard_*()` function was added and it depends on another package (such as, `broom`), `is_pkg_installed("broom", reference_pkg = "cardx")` has been set in the function call and the following added to the roxygen comments: `@examplesIf do.call(asNamespace("cardx")$is_pkg_installed, list(pkg = "broom"", reference_pkg = "cardx"))` - [ ] Code coverage is suitable for any new functions/features (generally, 100% coverage for new code): `devtools::test_coverage()` Reviewer Checklist (if item does not apply, mark is as complete) - [ ] If a bug was fixed, a unit test was added. - [ ] Code coverage is suitable for any new functions/features: `devtools::test_coverage()` When the branch is ready to be merged: - [ ] Update `NEWS.md` with the changes from this pull request under the heading "`# cardx (development version)`". If there is an issue associated with the pull request, reference it in parentheses at the end update (see `NEWS.md` for examples). - [ ] **All** GitHub Action workflows pass with a ✅ - [ ] Approve Pull Request - [ ] Merge the PR. Please use "Squash and merge" or "Rebase and merge".
1 parent 6e06253 commit 1b93221

File tree

4 files changed

+73
-7
lines changed

4 files changed

+73
-7
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ S3method(ard_regression,default)
44
S3method(ard_stats_anova,anova)
55
S3method(ard_stats_anova,data.frame)
66
S3method(construct_model,data.frame)
7+
S3method(construct_model,survey.design)
78
export("%>%")
89
export(all_of)
910
export(any_of)

R/construction_helpers.R

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
#'
1414
#' - `bt_strip()`: Removes backticks from a string if it begins and ends with a backtick.
1515
#'
16-
#' @param x (`data.frame`)\cr
17-
#' a data frame
16+
#' @param x
17+
#' - `construct_model.data.frame()` (`data.frame`) a data frame
18+
#' - `construct_model.survey.design()` (`survey.design`) a survey design object
19+
#' - `bt()`/`bt_strip()` (`character`) character vector, typically of variable names
1820
#' @param formula (`formula`)\cr
1921
#' a formula
2022
#' @param method (`string`)\cr
@@ -26,8 +28,6 @@
2628
#' @param package (`string`)\cr
2729
#' string of package name that will be temporarily loaded when function
2830
#' specified in `method` is executed.
29-
#' @param x (`character`)\cr
30-
#' character vector, typically of variable names
3131
#' @param pattern (`string`)\cr
3232
#' regular expression string. If the regex matches, backticks are added
3333
#' to the string. When `NULL`, backticks are not added.
@@ -80,7 +80,7 @@ construct_model.data.frame <- function(x, formula, method, method.args = list(),
8080
check_not_namespaced(method)
8181

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

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

93+
#' @rdname construction_helpers
94+
#' @export
95+
construct_model.survey.design <- function(x, formula, method, method.args = list(), package = "survey", env = caller_env(), ...) {
96+
set_cli_abort_call()
97+
# check pkg installations ----------------------------------------------------
98+
check_dots_empty()
99+
check_pkg_installed(c("withr", package), reference_pkg = "cardx")
100+
101+
check_not_missing(formula)
102+
check_class(formula, cls = "formula")
103+
104+
check_not_missing(method)
105+
check_string(method)
106+
check_not_namespaced(method)
107+
108+
# convert method.args to list of expressions (to account for NSE inputs) -----
109+
method.args <- .as_list_of_exprs({{ method.args }})
110+
111+
# build model ----------------------------------------------------------------
112+
withr::with_namespace(
113+
package = package,
114+
call2(.fn = method, formula = formula, design = x, !!!method.args) |>
115+
eval_tidy(env = env)
116+
)
117+
}
118+
119+
.as_list_of_exprs <- function(x) {
120+
call_args(enexpr(x))
121+
}
122+
93123
#' @rdname construction_helpers
94124
#' @export
95125
reformulate2 <- function(termlabels, response = NULL, intercept = TRUE,

man/construction_helpers.Rd

Lines changed: 16 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-construction_helpers.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
skip_if_not(is_pkg_installed(c("withr", "survey"), reference_pkg = "cardx"))
2+
13
test_that("construct_model() works", {
24
expect_snapshot(
35
construct_model(
@@ -27,4 +29,23 @@ test_that("construct_model() works", {
2729
check_not_namespaced("geepack::geeglm"),
2830
"cannot be namespaced"
2931
)
32+
33+
# now the survey method -------
34+
# styler: off
35+
expect_equal({
36+
data(api, package = "survey")
37+
# stratified sample
38+
survey::svydesign(id = ~1, strata = ~stype, weights = ~pw, data = apistrat, fpc = ~fpc) |>
39+
construct_model(formula = api00 ~ api99, method = "svyglm") |>
40+
ard_regression() |>
41+
cards::get_ard_statistics(stat_name %in% "estimate")},
42+
survey::svyglm(
43+
api00 ~ api99,
44+
design = survey::svydesign(id = ~1, strata = ~stype, weights = ~pw, data = apistrat, fpc = ~fpc)
45+
) |>
46+
coef() |>
47+
getElement(2L) |>
48+
list(estimate = _)
49+
)
50+
# styler: on
3051
})

0 commit comments

Comments
 (0)