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

construct_model() updates #159

Merged
merged 2 commits into from
May 27, 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
13 changes: 7 additions & 6 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,30 @@ ard_moodtest() -> ard_stats_mood_test()
- `ard_stats_mcnemar_test_long()` for McNemar's test from long data using `stats::mcnemar.test()`.
- `ard_aod_wald_test()` for calculating Wald Tests for regression models using `aod::wald.test()`. (#84)
- `ard_car_anova()` for calculating ANOVA results using `car::Anova()`. (#3)
- `ard_car_vif()` for calculating the variance inflation factor using `car::vif()`. (#10)
- `ard_stats_oneway_test()` for calculating ANOVA results using `stats::oneway.test()`. (#3)
- `ard_effectsize_cohens_d()`, `ard_effectsize_paired_cohens_d()`, `ard_effectsize_hedges_g()`, and `ard_effectsize_paired_hedges_g()` for standardized differences using `effectsize::cohens_d()` and `effectsize::hedges_g()`. (#50)
- `ard_stats_prop_test()` for tests of proportions using `stats::prop.test()`. (#64)
- `ard_regression_basic()` for basic regression models. The function focuses on matching terms to underlying variables names. (#46)
- `ard_emmeans_mean_difference()` for calculating the least-squares mean differences using the {emmeans} package. (#34)
- `ard_smd_smd()` for calculating standardized mean differences using `smd::smd()`. (#4)
- `ard_survival_survfit()` for survival analyses using `survival::survfit()`. (#43)
- `ard_continuous.survey.design()` for calculating univariate summary statistics from weighted/survey data using many functions from the {survey} package. (#68)
- `ard_attributes.survey.design()` for summarizing labels and attributes from weighted/survey data using many functions from the {survey} package.
- `ard_survey_svychisq()` for weighted/survey chi-squared test using `survey::svychisq()`. (#72)
- `ard_survey_svyttest()` for weighted/survey t-tests using `survey::svyttest()`. (#70)
- `ard_survey_svyranktest()` for weighted/survey rank tests using `survey::svyranktest()`. (#71)
- `ard_car_vif()` for calculating the variance inflation factor using `car::vif()`. (#10)
- `ard_emmeans_mean_difference()` for calculating the least-squares mean differences using the {emmeans} package. (#34)
- `ard_stats_wilcox_test_onesample()` for calculating one-sample results.
- `ard_survival_survdiff()` for creating results from `survival::survdiff()`. (#113)
- `ard_stats_prop_test()` for tests of proportions using `stats::prop.test()`. (#64)
- `ard_stats_t_test_onesample()` for calculating one-sample results.
- `ard_stats_wilcox_test_onesample()` for calculating one-sample results.
- `ard_regression_basic()` for basic regression models. The function focuses on matching terms to underlying variables names. (#46)

* Updated functions `ard_stats_t_test()`, `ard_stats_paired_t_test()`, `ard_stats_wilcox_test()`, `ard_stats_paired_wilcox_test()`, `ard_stats_chisq_test()`, `ard_stats_fisher_test()`, `ard_stats_kruskal_test()`, `ard_stats_mcnemar_test()`, and `ard_stats_mood_test()` to accept multiple variables at once. Independent tests are calculated for each variable. The `variable` argument is renamed to `variables`. (#77)

* Updated `ard_stats_t_test()` and `ard_stats_wilcox_test()` to no longer require the `by` argument, which yields central estimates with their confidence intervals. (#82)

* Imported cli call environment functions from `https://github.com/ddsjoberg/standalone/blob/main/R/standalone-cli_call_env.R` and implemented `set_cli_abort_call` in user-facing functions. (#111)

* Added `ard_survival_survdiff()` for creating results from `survival::survdiff()`. (#113)
* Added model construction helpers, `construct_model()`, `reformulate2()`, `bt()`, and `bt_strip()`.

# cardx 0.1.0

Expand Down
2 changes: 1 addition & 1 deletion R/ard_emmeans_mean_difference.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ ard_emmeans_mean_difference <- function(data, formula, method,
# construct primary model ----------------------------------------------------
mod <-
construct_model(
x = data, formula = formula, method = method,
data = data, formula = formula, method = method,
method.args = {{ method.args }},
package = package, env = caller_env()
)
Expand Down
2 changes: 1 addition & 1 deletion R/ard_stats_anova.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ ard_stats_anova.data.frame <- function(x,
lapply(
formulas,
function(formula) {
construct_model(x = x, formula = formula, method = method, method.args = {{ method.args }}, package = package)
construct_model(data = x, formula = formula, method = method, method.args = {{ method.args }}, package = package)
}
)

Expand Down
52 changes: 35 additions & 17 deletions R/construction_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
#'
#' - `bt_strip()`: Removes backticks from a string if it begins and ends with a backtick.
#'
#' @param x
#' @param data
#' - `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 x (`character`)\cr
#' character vector, typically of variable names
#' @param formula (`formula`)\cr
#' a formula
#' @param method (`string`)\cr
#' string naming the function to be called, e.g. `"glm"`.
#' string of function naming the function to be called, e.g. `"glm"`.
#' If function belongs to a library that is not attached, the package name
#' must be specified in the `package` argument.
#' @param method.args (named `list`)\cr
Expand All @@ -47,7 +48,7 @@
#'
#' @examplesIf do.call(asNamespace("cardx")$is_pkg_installed, list(pkg = c("withr", "lme4", "broom.helpers", "broom.mixed"), reference_pkg = "cardx"))
#' construct_model(
#' x = mtcars,
#' data = mtcars,
#' formula = am ~ mpg + (1 | vs),
#' method = "glmer",
#' method.args = list(family = binomial),
Expand All @@ -56,7 +57,7 @@
#' broom.mixed::tidy()
#'
#' construct_model(
#' x = mtcars |> dplyr::rename(`M P G` = mpg),
#' data = mtcars |> dplyr::rename(`M P G` = mpg),
#' formula = reformulate2(c("M P G", "cyl"), response = "hp"),
#' method = "lm"
#' ) |>
Expand All @@ -66,13 +67,13 @@ NULL

#' @rdname construction_helpers
#' @export
construct_model <- function(x, ...) {
construct_model <- function(data, ...) {
UseMethod("construct_model")
}

#' @rdname construction_helpers
#' @export
construct_model.data.frame <- function(x, formula, method, method.args = list(), package = "base", env = caller_env(), ...) {
construct_model.data.frame <- function(data, formula, method, method.args = list(), package = "base", env = caller_env(), ...) {
set_cli_abort_call()
# check pkg installations ----------------------------------------------------
check_dots_empty()
Expand All @@ -82,23 +83,23 @@ construct_model.data.frame <- function(x, formula, method, method.args = list(),
check_class(formula, cls = "formula")

check_not_missing(method)
check_string(method)
check_not_namespaced(method)
check_string_or_function(method)
if (is_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, data = x, !!!method.args) |>
call2(.fn = method, formula = formula, data = data, !!!method.args) |>
eval_tidy(env = env)
)
}

#' @rdname construction_helpers
#' @export
construct_model.survey.design <- function(x, formula, method, method.args = list(), package = "survey", env = caller_env(), ...) {
construct_model.survey.design <- function(data, formula, method, method.args = list(), package = "survey", env = caller_env(), ...) {
set_cli_abort_call()
# check pkg installations ----------------------------------------------------
check_dots_empty()
Expand All @@ -108,7 +109,7 @@ construct_model.survey.design <- function(x, formula, method, method.args = list
check_class(formula, cls = "formula")

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

# convert method.args to list of expressions (to account for NSE inputs) -----
Expand All @@ -117,7 +118,7 @@ construct_model.survey.design <- function(x, formula, method, method.args = list
# build model ----------------------------------------------------------------
withr::with_namespace(
package = package,
call2(.fn = method, formula = formula, design = x, !!!method.args) |>
call2(.fn = method, formula = formula, design = data, !!!method.args) |>
eval_tidy(env = env)
)
}
Expand Down Expand Up @@ -176,10 +177,27 @@ check_not_namespaced <- function(x,
check_string(x, arg_name = arg_name, call = call, class = "check_not_namespaced")

if (str_detect(x, "::")) {
c("Argument {.arg {arg_name}} cannot be namespaced.",
i = "Put the package name in the {.arg package} argument."
) |>
cli::cli_abort(call = call, class = class)
cli::cli_abort(
"Argument {.arg {arg_name}} cannot be namespaced when passed as a {.cls string}.",
call = call,
class = class
)
}

invisible(x)
}


check_string_or_function <- function(x,
arg_name = rlang::caller_arg(x),
class = "check_string_or_function",
call = get_cli_abort_call()) {
if (!is.function(x) && !is_string(x)) {
cli::cli_abort(
c("Argument {.arg {arg_name}} must be a {.cls string} or {.cls function}."),
call = call,
class = class
)
}

invisible(x)
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ To accomplish this we include model construction helpers.

```{r}
construct_model(
x = cards::ADSL,
data = cards::ADSL,
formula = reformulate2("ARM", response = "AGE"),
method = "lm"
) |>
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ this we include model construction helpers.

``` r
construct_model(
x = cards::ADSL,
data = cards::ADSL,
formula = reformulate2("ARM", response = "AGE"),
method = "lm"
) |>
Expand Down
2 changes: 1 addition & 1 deletion man/ard_emmeans_mean_difference.Rd

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

2 changes: 1 addition & 1 deletion man/ard_stats_anova.Rd

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

18 changes: 10 additions & 8 deletions man/construction_helpers.Rd

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

19 changes: 18 additions & 1 deletion tests/testthat/_snaps/construction_helpers.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# construct_model() works

Code
dplyr::filter(as.data.frame(ard_regression(construct_model(x = dplyr::rename(
dplyr::filter(as.data.frame(ard_regression(construct_model(data = dplyr::rename(
mtcars, `M P G` = mpg), formula = reformulate2(c("M P G", "cyl"), response = "hp"),
method = "lm"))), stat_name %in% c("term", "estimate", "p.value"))
Output
Expand All @@ -13,3 +13,20 @@
5 cyl regression estimate Coefficient 23.97863 1
6 cyl regression p.value p-value 0.002814958 1

# construct_model() messaging

Code
construct_model(data = mtcars, method = "survival::coxph", formula = survival::Surv(
mpg, am) ~ cyl)
Condition
Error in `construct_model()`:
! Argument `method` cannot be namespaced when passed as a <string>.

---

Code
construct_model(data = mtcars, method = letters, formula = am ~ cyl)
Condition
Error in `construct_model()`:
! Argument `method` must be a <string> or <function>.

Loading
Loading