Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
Merge branch '105_update_readme@main' of github.com:insightsengineering/cardx into 105_update_readme@main

# Conflicts:
#	inst/WORDLIST
  • Loading branch information
ayogasekaram committed Apr 26, 2024
2 parents a3f29c3 + 9e7f74d commit 5df64dc
Show file tree
Hide file tree
Showing 33 changed files with 915 additions and 272 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:

- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
- {os: ubuntu-latest, r: 'release'}
- {os: ubuntu-latest, r: 'oldrel-1'}
# - {os: ubuntu-latest, r: 'oldrel-1'}
# - {os: ubuntu-latest, r: 'oldrel-2'}
# - {os: ubuntu-latest, r: 'oldrel-3'}
# - {os: ubuntu-latest, r: 'oldrel-4'}
Expand Down
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: cardx
Title: Extra Analysis Results Data Utilities
Version: 0.1.0.9021
Version: 0.1.0.9028
Authors@R: c(
person("Daniel", "Sjoberg", , "[email protected]", role = c("aut", "cre")),
person("Abinaya", "Yogasekaram", , "[email protected]", role = "aut"),
Expand Down Expand Up @@ -30,6 +30,7 @@ Suggests:
broom.helpers (>= 1.15.0),
car (>= 3.0-11),
effectsize (>= 0.6.0),
emmeans (>= 1.7.3),
geepack (>= 1.3.2),
ggsurvfit (>= 1.0.0),
lme4 (>= 1.1-31),
Expand Down
8 changes: 8 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
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 All @@ -13,6 +15,7 @@ export(ard_effectsize_cohens_d)
export(ard_effectsize_hedges_g)
export(ard_effectsize_paired_cohens_d)
export(ard_effectsize_paired_hedges_g)
export(ard_emmeans_mean_difference)
export(ard_proportion_ci)
export(ard_regression)
export(ard_regression_basic)
Expand All @@ -23,6 +26,7 @@ export(ard_stats_chisq_test)
export(ard_stats_fisher_test)
export(ard_stats_kruskal_test)
export(ard_stats_mcnemar_test)
export(ard_stats_mcnemar_test_long)
export(ard_stats_mood_test)
export(ard_stats_oneway_test)
export(ard_stats_paired_t_test)
Expand All @@ -36,6 +40,9 @@ export(ard_survey_svyranktest)
export(ard_survey_svyttest)
export(ard_survival_survdiff)
export(ard_survival_survfit)
export(bt)
export(bt_strip)
export(construct_model)
export(contains)
export(ends_with)
export(everything)
Expand All @@ -49,6 +56,7 @@ export(proportion_ci_jeffreys)
export(proportion_ci_strat_wilson)
export(proportion_ci_wald)
export(proportion_ci_wilson)
export(reformulate2)
export(starts_with)
export(where)
import(rlang)
Expand Down
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# cardx 0.1.0.9021
# cardx 0.1.0.9028

### Breaking Changes

Expand All @@ -21,6 +21,7 @@ ard_moodtest() -> ard_stats_mood_test()
* Added the following functions for calculating Analysis Results Data (ARD).
- `ard_stats_aov()` for calculating ANOVA results using `stats::aov()`. (#3)
- `ard_stats_anova()` for calculating ANOVA results using `stats::anova()`. (#12)
- `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_stats_oneway_test()` for calculating ANOVA results using `stats::oneway.test()`. (#3)
Expand All @@ -34,6 +35,7 @@ ard_moodtest() -> ard_stats_mood_test()
- `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)

* 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)

Expand Down
134 changes: 134 additions & 0 deletions R/ard_emmeans_mean_difference.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#' ARD for LS Mean Difference
#'
#' @description
#' This function calculates least-squares mean differences using the 'emmeans'
#' package using the following
#'
#' ```r
#' emmeans::emmeans(object = <regression model>, specs = ~ <primary covariate>) |>
#' emmeans::contrast(method = "pairwise") |>
#' summary(infer = TRUE, level = <confidence level>)
#' ```
#'
#' The arguments `data`, `formula`, `method`, `method.args`, `package` are used
#' to construct the regression model via `cardx::construct_model()`.
#'
#' @param data (`data.frame`/`survey.design`)\cr
#' a data frame or survey design object
#' @inheritParams construct_model
#' @param response_type (`string`)
#' string indicating whether the model outcome is `'continuous'`
#' or `'dichotomous'`. When `'dichotomous'`, the call to `emmeans::emmeans()` is
#' supplemented with argument `regrid="response"`.
#' @param conf.level (scalar `numeric`)\cr
#' confidence level for confidence interval. Default is `0.95`.
#' @param primary_covariate (`string`)\cr
#' string indicating the primary covariate (typically the dichotomous treatment variable).
#' Default is the first covariate listed in the formula.
#'
#' @return ARD data frame
#' @export
#'
#' @examplesIf do.call(asNamespace("cardx")$is_pkg_installed, list(pkg = "emmeans", reference_pkg = "cardx"))
#' ard_emmeans_mean_difference(
#' data = mtcars,
#' formula = mpg ~ am + cyl,
#' method = "lm"
#' )
#'
#' ard_emmeans_mean_difference(
#' data = mtcars,
#' formula = vs ~ am + mpg,
#' method = "glm",
#' method.args = list(family = binomial),
#' response_type = "dichotomous"
#' )
ard_emmeans_mean_difference <- function(data, formula, method,
method.args = list(),
package = "base",
response_type = c("continuous", "dichotomous"),
conf.level = 0.95,
primary_covariate =
stats::terms(formula) |>
attr("term.labels") |>
getElement(1L)) {
set_cli_abort_call()

# check package installation -------------------------------------------------
check_pkg_installed(c("emmeans", package), reference_pkg = "cardx")
check_not_missing(data)
check_not_missing(formula)
check_not_missing(method)
check_class(data, c("data.frame", "survey.design"))
check_class(formula, cls = "formula")
check_string(package)
check_string(primary_covariate)
check_scalar(conf.level)
check_range(conf.level, range = c(0, 1))
response_type <- arg_match(response_type, error_call = get_cli_abort_call())

# construct primary model ----------------------------------------------------
mod <-
construct_model(
x = data, formula = formula, method = method,
method.args = {{ method.args }},
package = package, env = caller_env()
)

# emmeans --------------------------------------------------------------------
emmeans_args <- list(object = mod, specs = reformulate2(primary_covariate))
if (response_type %in% "dichotomous") emmeans_args <- c(emmeans_args, list(regrid = "response"))
emmeans <-
withr::with_namespace(
package = "emmeans",
code = do.call("emmeans", args = emmeans_args)
)

df_results <-
emmeans |>
emmeans::contrast(method = "pairwise") |>
summary(infer = TRUE, level = conf.level)

# convert results to ARD format ----------------------------------------------
df_results |>
dplyr::as_tibble() |>
dplyr::rename(
conf.low = any_of("asymp.LCL"),
conf.high = any_of("asymp.UCL"),
conf.low = any_of("lower.CL"),
conf.high = any_of("upper.CL")
) %>%
dplyr::select(
variable_level = "contrast",
"estimate",
std.error = "SE", "df",
"conf.low", "conf.high", "p.value"
) %>%
dplyr::mutate(
conf.level = .env$conf.level,
method =
ifelse(
length(attr(stats::terms(formula), "term.labels") |> discard(~ startsWith(., "1 |"))) == 1L,
"Least-squares mean difference",
"Least-squares adjusted mean difference"
),
across(everything(), as.list),
variable = "contrast",
group1 = .env$primary_covariate
) |>
tidyr::pivot_longer(
cols = -c("group1", "variable", "variable_level"),
names_to = "stat_name",
values_to = "stat"
) |>
dplyr::left_join(.df_ttest_stat_labels(primary_covariate), by = "stat_name") |>
dplyr::mutate(
context = "emmeans_mean_difference",
stat_label = dplyr::coalesce(.data$stat_label, .data$stat_name),
warning = list(NULL),
error = list(NULL),
fmt_fn = map(.data$stat, \(.x) if (is.numeric(.x)) 1L else NULL) # styler: off
) |>
cards::tidy_ard_column_order() %>%
{structure(., class = c("card", class(.)))} # styler: off
}
1 change: 1 addition & 0 deletions R/ard_regression.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ ard_regression.default <- function(x, tidy_fun = broom.helpers::tidy_with_broom_
values_to = "stat"
) |>
dplyr::filter(map_lgl(.data$stat, Negate(is.na))) |>
dplyr::select(-(cards::all_ard_variables("levels") & dplyr::where(\(x) all(is.na(x))))) |>
dplyr::mutate(
fmt_fn =
lapply(
Expand Down
3 changes: 2 additions & 1 deletion R/ard_regression_basic.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ ard_regression_basic <- function(x, tidy_fun = broom.helpers::tidy_with_broom_or
utils::modifyList(val = rlang::dots_list(...))

rlang::inject(ard_regression(x = x, tidy_fun = tidy_fun, !!!args)) |>
dplyr::filter(!.data$stat_name %in% stats_to_remove)
dplyr::filter(!.data$stat_name %in% stats_to_remove) |>
dplyr::select(-(cards::all_ard_variables("levels") & dplyr::where(\(x) all(is.na(x)))))
}
Loading

0 comments on commit 5df64dc

Please sign in to comment.