Skip to content

Commit

Permalink
Closes #133 proliferate conf.level argument as appropriate across p…
Browse files Browse the repository at this point in the history
…ackage (#142)

**What changes are proposed in this pull request?**
Adds `conf.level` argument to variety of functions for consistency

Provide more detail here as needed.

Closes #133


--------------------------------------------------------------------------------

Pre-review Checklist (if item does not apply, mark is as complete)
- [x] **All** GitHub Action workflows pass with a ✅
- [x] PR branch has pulled the most recent updates from master branch:
`usethis::pr_merge_main()`
- [x] If a bug was fixed, a unit test was added.
- [x] If a new `ard_*()` function was added, it passes the ARD
structural checks from `cards::check_ard_structure()`.
- [x] If a new `ard_*()` function was added, `set_cli_abort_call()` has
been set.
- [x] 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"))`
- [x] 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)

- [x] If a bug was fixed, a unit test was added.
- [x] Code coverage is suitable for any new functions/features:
`devtools::test_coverage()`

When the branch is ready to be merged:
- [x] 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".

---------

Co-authored-by: Daniel Sjoberg <[email protected]>
  • Loading branch information
zdz2101 and ddsjoberg authored May 3, 2024
1 parent 85bc8f2 commit 817694d
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 32 deletions.
13 changes: 8 additions & 5 deletions R/ard_effectsize_cohens_d.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#' Independent tests will be run for each variable.
#' @param id ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
#' column name of the subject or participant ID
#' @param conf.level (scalar `numeric`)\cr
#' confidence level for confidence interval. Default is `0.95`.
#' @param ... arguments passed to `effectsize::cohens_d(...)`
#'
#' @return ARD data frame
Expand Down Expand Up @@ -46,7 +48,7 @@ NULL

#' @rdname ard_effectsize_cohens_d
#' @export
ard_effectsize_cohens_d <- function(data, by, variables, ...) {
ard_effectsize_cohens_d <- function(data, by, variables, conf.level = 0.95, ...) {
set_cli_abort_call()

# check installed packages ---------------------------------------------------
Expand All @@ -60,7 +62,7 @@ ard_effectsize_cohens_d <- function(data, by, variables, ...) {
data <- dplyr::ungroup(data)
cards::process_selectors(data, by = {{ by }}, variables = {{ variables }})
check_scalar(by)

check_range(conf.level, range = c(0, 1))
# if no variables selected, return empty tibble ------------------------------
if (is_empty(variables)) {
return(dplyr::tibble())
Expand All @@ -75,7 +77,7 @@ ard_effectsize_cohens_d <- function(data, by, variables, ...) {
variable = variable,
lst_tidy =
cards::eval_capture_conditions(
effectsize::cohens_d(data[[variable]] ~ data[[by]], data = data, paired = FALSE, ...) |>
effectsize::cohens_d(data[[variable]] ~ data[[by]], data = data, paired = FALSE, ci = conf.level, ...) |>
parameters::standardize_names(style = "broom")
),
paired = FALSE,
Expand All @@ -89,7 +91,7 @@ ard_effectsize_cohens_d <- function(data, by, variables, ...) {

#' @rdname ard_effectsize_cohens_d
#' @export
ard_effectsize_paired_cohens_d <- function(data, by, variables, id, ...) {
ard_effectsize_paired_cohens_d <- function(data, by, variables, id, conf.level = 0.95, ...) {
set_cli_abort_call()

# check installed packages ---------------------------------------------------
Expand All @@ -105,6 +107,7 @@ ard_effectsize_paired_cohens_d <- function(data, by, variables, id, ...) {
cards::process_selectors(data, by = {{ by }}, variables = {{ variables }}, id = {{ id }})
check_scalar(by)
check_scalar(id)
check_range(conf.level, range = c(0, 1))

# if no variables selected, return empty tibble ------------------------------
if (is_empty(variables)) {
Expand All @@ -123,7 +126,7 @@ ard_effectsize_paired_cohens_d <- function(data, by, variables, id, ...) {
# adding this reshape inside the eval, so if there is an error it's captured in the ARD object
data_wide <- .paired_data_pivot_wider(data, by = by, variable = variable, id = id)
# perform paired cohen's d test
effectsize::cohens_d(x = data_wide[["by1"]], y = data_wide[["by2"]], paired = TRUE, ...) |>
effectsize::cohens_d(x = data_wide[["by1"]], y = data_wide[["by2"]], paired = TRUE, ci = conf.level, ...) |>
parameters::standardize_names(style = "broom")
}),
paired = TRUE,
Expand Down
12 changes: 8 additions & 4 deletions R/ard_effectsize_hedges_g.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#' tests will be run for each variable
#' @param id ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
#' column name of the subject or participant ID
#' @param conf.level (scalar `numeric`)\cr
#' confidence level for confidence interval. Default is `0.95`.
#' @param ... arguments passed to `effectsize::hedges_g(...)`
#'
#' @return ARD data frame
Expand Down Expand Up @@ -46,7 +48,7 @@ NULL

#' @rdname ard_effectsize_hedges_g
#' @export
ard_effectsize_hedges_g <- function(data, by, variables, ...) {
ard_effectsize_hedges_g <- function(data, by, variables, conf.level = 0.95, ...) {
set_cli_abort_call()

# check installed packages ---------------------------------------------------
Expand All @@ -59,6 +61,7 @@ ard_effectsize_hedges_g <- function(data, by, variables, ...) {
data <- dplyr::ungroup(data)
cards::process_selectors(data, by = {{ by }}, variables = {{ variables }})
check_scalar(by)
check_range(conf.level, range = c(0, 1))

# if no variables selected, return empty tibble ------------------------------
if (is_empty(variables)) {
Expand All @@ -79,7 +82,7 @@ ard_effectsize_hedges_g <- function(data, by, variables, ...) {
# Will also need to remove `hedges_g` from globalVariables()
withr::with_namespace(
package = "effectsize",
code = hedges_g(data[[variable]] ~ data[[by]], paired = FALSE, ...)
code = hedges_g(data[[variable]] ~ data[[by]], paired = FALSE, ci = conf.level, ...)
) |>
parameters::standardize_names(style = "broom")
),
Expand All @@ -93,7 +96,7 @@ ard_effectsize_hedges_g <- function(data, by, variables, ...) {

#' @rdname ard_effectsize_hedges_g
#' @export
ard_effectsize_paired_hedges_g <- function(data, by, variables, id, ...) {
ard_effectsize_paired_hedges_g <- function(data, by, variables, id, conf.level = 0.95, ...) {
set_cli_abort_call()

# check installed packages ---------------------------------------------------
Expand All @@ -109,6 +112,7 @@ ard_effectsize_paired_hedges_g <- function(data, by, variables, id, ...) {
cards::process_selectors(data, by = {{ by }}, variables = {{ variables }}, id = {{ id }})
check_scalar(by)
check_scalar(id)
check_range(conf.level, range = c(0, 1))

# if no variables selected, return empty tibble ------------------------------
if (is_empty(variables)) {
Expand All @@ -129,7 +133,7 @@ ard_effectsize_paired_hedges_g <- function(data, by, variables, id, ...) {
# perform paired cohen's d test
withr::with_namespace(
package = "effectsize",
code = hedges_g(x = data_wide[["by1"]], y = data_wide[["by2"]], paired = TRUE, ...)
code = hedges_g(x = data_wide[["by1"]], y = data_wide[["by2"]], paired = TRUE, ci = conf.level, ...)
) |>
parameters::standardize_names(style = "broom")
}),
Expand Down
7 changes: 5 additions & 2 deletions R/ard_stats_fisher_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#' @param variables ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
#' column names to be compared. Independent tests will be computed for
#' each variable.
#' @param conf.level (scalar `numeric`)\cr
#' confidence level for confidence interval. Default is `0.95`.
#' @param ... additional arguments passed to `fisher.test(...)`
#'
#' @return ARD data frame
Expand All @@ -20,7 +22,7 @@
#' @examplesIf do.call(asNamespace("cardx")$is_pkg_installed, list(pkg = "broom", reference_pkg = "cardx"))
#' cards::ADSL[1:30, ] |>
#' ard_stats_fisher_test(by = "ARM", variables = "AGEGR1")
ard_stats_fisher_test <- function(data, by, variables, ...) {
ard_stats_fisher_test <- function(data, by, variables, conf.level = 0.95, ...) {
set_cli_abort_call()

# check installed packages ---------------------------------------------------
Expand All @@ -33,6 +35,7 @@ ard_stats_fisher_test <- function(data, by, variables, ...) {
check_data_frame(data)
cards::process_selectors(data, by = {{ by }}, variables = {{ variables }})
check_scalar(by)
check_range(conf.level, range = c(0, 1))

# if no variables selected, return empty tibble ------------------------------
if (is_empty(variables)) {
Expand All @@ -45,7 +48,7 @@ ard_stats_fisher_test <- function(data, by, variables, ...) {
cards::tidy_as_ard(
lst_tidy =
cards::eval_capture_conditions(
stats::fisher.test(x = data[[variable]], y = data[[by]], ...) |>
stats::fisher.test(x = data[[variable]], y = data[[by]], conf.level = conf.level, ...) |>
broom::tidy()
),
tidy_result_names =
Expand Down
6 changes: 5 additions & 1 deletion R/ard_stats_prop_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#' @param variables ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
#' column names to be compared. Must be a binary column coded as `TRUE`/`FALSE`
#' or `1`/`0`. Independent tests will be computed for each variable.
#' @param conf.level (scalar `numeric`)\cr
#' confidence level for confidence interval. Default is `0.95`.
#' @param ... arguments passed to `prop.test(...)`
#'
#' @return ARD data frame
Expand All @@ -18,7 +20,7 @@
#' @examplesIf do.call(asNamespace("cardx")$is_pkg_installed, list(pkg = "broom", reference_pkg = "cardx"))
#' mtcars |>
#' ard_stats_prop_test(by = vs, variables = am)
ard_stats_prop_test <- function(data, by, variables, ...) {
ard_stats_prop_test <- function(data, by, variables, conf.level = 0.95, ...) {
set_cli_abort_call()

# check installed packages ---------------------------------------------------
Expand All @@ -29,6 +31,7 @@ ard_stats_prop_test <- function(data, by, variables, ...) {
check_not_missing(variables)
check_not_missing(by)
check_data_frame(data)
check_range(conf.level, range = c(0, 1))

# process inputs -------------------------------------------------------------
cards::process_selectors(data, by = {{ by }}, variables = {{ variables }})
Expand Down Expand Up @@ -72,6 +75,7 @@ ard_stats_prop_test <- function(data, by, variables, ...) {
stats::prop.test(
x = data_counts[["x"]],
n = data_counts[["n"]],
conf.level = conf.level,
...
) |>
broom::tidy() |>
Expand Down
11 changes: 7 additions & 4 deletions R/ard_stats_t_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#' each variable.
#' @param id ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
#' column name of the subject or participant ID
#' @param conf.level (scalar `numeric`)\cr
#' confidence level for confidence interval. Default is `0.95`.
#' @param ... arguments passed to `t.test(...)`
#'
#' @return ARD data frame
Expand Down Expand Up @@ -43,7 +45,7 @@ NULL

#' @rdname ard_stats_t_test
#' @export
ard_stats_t_test <- function(data, variables, by = NULL, ...) {
ard_stats_t_test <- function(data, variables, by = NULL, conf.level = 0.95, ...) {
set_cli_abort_call()

# check installed packages ---------------------------------------------------
Expand All @@ -56,6 +58,7 @@ ard_stats_t_test <- function(data, variables, by = NULL, ...) {
data <- dplyr::ungroup(data)
cards::process_selectors(data, by = {{ by }}, variables = {{ variables }})
check_scalar(by, allow_empty = TRUE)
check_range(conf.level, range = c(0, 1))

# if no variables selected, return empty tibble ------------------------------
if (is_empty(variables)) {
Expand All @@ -72,7 +75,7 @@ ard_stats_t_test <- function(data, variables, by = NULL, ...) {
lst_tidy =
# styler: off
cards::eval_capture_conditions(
if (!is_empty(by)) stats::t.test(data[[variable]] ~ data[[by]], ...) |> broom::tidy()
if (!is_empty(by)) stats::t.test(data[[variable]] ~ data[[by]], conf.level = conf.level, ...) |> broom::tidy()
else stats::t.test(data[[variable]], ...) |> broom::tidy()
),
# styler: on
Expand All @@ -86,7 +89,7 @@ ard_stats_t_test <- function(data, variables, by = NULL, ...) {

#' @rdname ard_stats_t_test
#' @export
ard_stats_paired_t_test <- function(data, by, variables, id, ...) {
ard_stats_paired_t_test <- function(data, by, variables, id, conf.level = 0.95, ...) {
set_cli_abort_call()

# check installed packages ---------------------------------------------------
Expand Down Expand Up @@ -120,7 +123,7 @@ ard_stats_paired_t_test <- function(data, by, variables, id, ...) {
# adding this reshape inside the eval, so if there is an error it's captured in the ARD object
data_wide <- .paired_data_pivot_wider(data, by = by, variable = variable, id = id)
# perform paired t-test
stats::t.test(x = data_wide[["by1"]], y = data_wide[["by2"]], paired = TRUE, ...) |>
stats::t.test(x = data_wide[["by1"]], y = data_wide[["by2"]], paired = TRUE, conf.level = conf.level, ...) |>
broom::tidy()
}),
paired = TRUE,
Expand Down
11 changes: 7 additions & 4 deletions R/ard_stats_wilcox_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#' each variable.
#' @param id ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
#' column name of the subject or participant ID.
#' @param conf.level (scalar `numeric`)\cr
#' confidence level for confidence interval. Default is `0.95`.
#' @param ... arguments passed to `wilcox.test(...)`
#'
#' @return ARD data frame
Expand Down Expand Up @@ -43,7 +45,7 @@ NULL

#' @rdname ard_stats_wilcox_test
#' @export
ard_stats_wilcox_test <- function(data, variables, by = NULL, ...) {
ard_stats_wilcox_test <- function(data, variables, by = NULL, conf.level = 0.95, ...) {
set_cli_abort_call()

# check installed packages ---------------------------------------------------
Expand All @@ -56,6 +58,7 @@ ard_stats_wilcox_test <- function(data, variables, by = NULL, ...) {
data <- dplyr::ungroup(data)
cards::process_selectors(data, by = {{ by }}, variables = {{ variables }})
check_scalar(by, allow_empty = TRUE)
check_range(conf.level, range = c(0, 1))

# if no variables selected, return empty tibble ------------------------------
if (is_empty(variables)) {
Expand All @@ -73,7 +76,7 @@ ard_stats_wilcox_test <- function(data, variables, by = NULL, ...) {
# styler: off
cards::eval_capture_conditions(
if (!is_empty(by)) {
stats::wilcox.test(data[[variable]] ~ data[[by]], ...) |>
stats::wilcox.test(data[[variable]] ~ data[[by]], conf.level = conf.level, ...) |>
broom::tidy()
}
else {
Expand All @@ -92,7 +95,7 @@ ard_stats_wilcox_test <- function(data, variables, by = NULL, ...) {

#' @rdname ard_stats_wilcox_test
#' @export
ard_stats_paired_wilcox_test <- function(data, by, variables, id, ...) {
ard_stats_paired_wilcox_test <- function(data, by, variables, id, conf.level = 0.95, ...) {
set_cli_abort_call()

# check installed packages ---------------------------------------------------
Expand Down Expand Up @@ -126,7 +129,7 @@ ard_stats_paired_wilcox_test <- function(data, by, variables, id, ...) {
# adding this reshape inside the eval, so if there is an error it's captured in the ARD object
data_wide <- .paired_data_pivot_wider(data, by = by, variable = variable, id = id)
# perform paired wilcox test
stats::wilcox.test(x = data_wide[["by1"]], y = data_wide[["by2"]], paired = TRUE, ...) |>
stats::wilcox.test(x = data_wide[["by1"]], y = data_wide[["by2"]], paired = TRUE, conf.level = conf.level, ...) |>
broom::tidy()
}),
paired = TRUE,
Expand Down
2 changes: 1 addition & 1 deletion R/ard_survey_svychisq.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#' Only two-way comparisons are supported.
#'
#' @param data (`survey.design`)\cr
#' a survey design object often created with the {survey} package
#' a survey design object often created with the \{survey\} package
#' @param by ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
#' column name to compare by.
#' @param variables ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
Expand Down
7 changes: 5 additions & 2 deletions man/ard_effectsize_cohens_d.Rd

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

7 changes: 5 additions & 2 deletions man/ard_effectsize_hedges_g.Rd

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

5 changes: 4 additions & 1 deletion man/ard_stats_fisher_test.Rd

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

5 changes: 4 additions & 1 deletion man/ard_stats_prop_test.Rd

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

Loading

0 comments on commit 817694d

Please sign in to comment.