Skip to content

Commit

Permalink
Merge branch 'main' into 139_survfit_update@main
Browse files Browse the repository at this point in the history
  • Loading branch information
edelarua authored May 23, 2024
2 parents 27e08f6 + e2e448c commit 1b23ba7
Show file tree
Hide file tree
Showing 21 changed files with 531 additions and 48 deletions.
8 changes: 4 additions & 4 deletions 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.9042
Version: 0.1.0.9043
Authors@R: c(
person("Daniel", "Sjoberg", , "[email protected]", role = c("aut", "cre")),
person("Abinaya", "Yogasekaram", , "[email protected]", role = "aut"),
Expand All @@ -18,7 +18,7 @@ BugReports: https://github.com/insightsengineering/cardx/issues
Depends:
R (>= 4.1)
Imports:
cards (>= 0.1.0.9014),
cards (>= 0.1.0.9026),
cli (>= 3.6.1),
dplyr (>= 1.1.2),
glue (>= 1.6.2),
Expand All @@ -33,13 +33,13 @@ Suggests:
effectsize (>= 0.6.0),
emmeans (>= 1.7.3),
geepack (>= 1.3.2),
ggsurvfit (>= 1.0.0),
ggsurvfit (>= 1.1.0),
lme4 (>= 1.1-31),
parameters (>= 0.20.2),
smd (>= 0.6.6),
spelling,
survey (>= 4.1),
survival (>= 3.2-11),
survival (>= 3.6-4),
testthat (>= 3.2.0),
withr (>= 2.5.0)
Remotes:
Expand Down
15 changes: 14 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Generated by roxygen2: do not edit by hand

S3method(ard_attributes,survey.design)
S3method(ard_continuous,survey.design)
S3method(ard_regression,default)
S3method(ard_stats_anova,anova)
S3method(ard_stats_anova,data.frame)
Expand All @@ -9,13 +11,18 @@ export("%>%")
export(all_of)
export(any_of)
export(ard_aod_wald_test)
export(ard_attributes)
export(ard_car_anova)
export(ard_car_vif)
export(ard_categorical)
export(ard_continuous)
export(ard_dichotomous)
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_missing)
export(ard_proportion_ci)
export(ard_regression)
export(ard_regression_basic)
Expand All @@ -33,9 +40,10 @@ export(ard_stats_paired_t_test)
export(ard_stats_paired_wilcox_test)
export(ard_stats_prop_test)
export(ard_stats_t_test)
export(ard_stats_t_test_onesample)
export(ard_stats_wilcox_test)
export(ard_stats_wilcox_test_onesample)
export(ard_survey_svychisq)
export(ard_survey_svycontinuous)
export(ard_survey_svyranktest)
export(ard_survey_svyttest)
export(ard_survival_survdiff)
Expand All @@ -60,6 +68,11 @@ export(reformulate2)
export(starts_with)
export(where)
import(rlang)
importFrom(cards,ard_attributes)
importFrom(cards,ard_categorical)
importFrom(cards,ard_continuous)
importFrom(cards,ard_dichotomous)
importFrom(cards,ard_missing)
importFrom(dplyr,"%>%")
importFrom(dplyr,across)
importFrom(dplyr,all_of)
Expand Down
7 changes: 5 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# cardx 0.1.0.9042
# cardx 0.1.0.9043

### Breaking Changes

Expand Down Expand Up @@ -30,12 +30,15 @@ ard_moodtest() -> ard_stats_mood_test()
- `ard_regression_basic()` for basic regression models. The function focuses on matching terms to underlying variables names. (#46)
- `ard_smd_smd()` for calculating standardized mean differences using `smd::smd()`. (#4)
- `ard_survival_survfit()` for survival analyses using `survival::survfit()`. (#43)
- `ard_survey_svycontinuous()` for calculating univariate summary statistics from weighted/survey data using many functions from the {survey} package. (#68)
- `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_stats_t_test_onesample()` for calculating one-sample results.

* 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
37 changes: 37 additions & 0 deletions R/ard_attributes.survey.design.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#' ARD Attributes
#'
#' @description
#' Add variable attributes to an ARD data frame.
#' - The `label` attribute will be added for all columns, and when no label
#' is specified and no label has been set for a column using the `label=` argument,
#' the column name will be placed in the label statistic.
#' - The `class` attribute will also be returned for all columns.
#' - Any other attribute returned by `attributes()` will also be added, e.g. factor levels.
#'
#' @rdname ard_attributes
#' @param data (`survey.design`)\cr
#' a design object often created with [`survey::svydesign()`].
#' @param variables ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
#' variables to include
#' @param label (named `list`)\cr
#' named list of variable labels, e.g. `list(cyl = "No. Cylinders")`.
#' Default is `NULL`
#' @inheritParams rlang::args_dots_empty
#'
#' @return an ARD data frame of class 'card'
#' @export
#'
#' @examplesIf do.call(asNamespace("cardx")$is_pkg_installed, list(pkg = "survey", reference_pkg = "cardx"))
#' data(api, package = "survey")
#' dclus1 <- survey::svydesign(id = ~dnum, weights = ~pw, data = apiclus1, fpc = ~fpc)
#'
#' ard_attributes(
#' data = dclus1,
#' variables = c(sname, dname),
#' label = list(sname = "School Name", dname = "District Name")
#' )
ard_attributes.survey.design <- function(data, variables = everything(), label = NULL, ...) {
set_cli_abort_call()

cards::ard_attributes(data = data[["variables"]], variables = {{ variables }}, label = label, ...)
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#' the list element is either a named list or a list of formulas defining the
#' statistic labels, e.g. `everything() ~ list(mean = "Mean", sd = "SD")` or
#' `everything() ~ list(mean ~ "Mean", sd ~ "SD")`.
#' @inheritParams rlang::args_dots_empty
#'
#' @section statistic argument:
#'
Expand All @@ -38,16 +39,18 @@
#' data(api, package = "survey")
#' dclus1 <- survey::svydesign(id = ~dnum, weights = ~pw, data = apiclus1, fpc = ~fpc)
#'
#' ard_survey_svycontinuous(
#' ard_continuous(
#' data = dclus1,
#' variables = api00,
#' by = stype
#' )
ard_survey_svycontinuous <- function(data, variables, by = NULL,
statistic = everything() ~ c("median", "p25", "p75"),
fmt_fn = NULL,
stat_label = NULL) {
ard_continuous.survey.design <- function(data, variables, by = NULL,
statistic = everything() ~ c("median", "p25", "p75"),
fmt_fn = NULL,
stat_label = NULL,
...) {
set_cli_abort_call()
check_dots_empty()

# check installed packages ---------------------------------------------------
check_pkg_installed(pkg = "survey", reference_pkg = "cardx")
Expand All @@ -68,7 +71,7 @@ ard_survey_svycontinuous <- function(data, variables, by = NULL,
)
cards::fill_formula_selectors(
data$variables[variables],
statistic = formals(ard_survey_svycontinuous)[["statistic"]] |> eval()
statistic = formals(asNamespace("cardx")[["ard_continuous.survey.design"]])[["statistic"]] |> eval()
)
cards::check_list_elements(
x = statistic,
Expand Down
71 changes: 71 additions & 0 deletions R/ard_stats_t_test_onesample.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#' ARD one-sample t-test
#'
#' @description
#' Analysis results data for one-sample t-tests.
#' Result may be stratified by including the `by` argument.
#'
#' @param data (`data.frame`)\cr
#' a data frame. See below for details.
#' @param variables ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
#' column names to be analyzed. Independent t-tests will be computed for
#' each variable.
#' @param by ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
#' optional column name to stratify results by.
#' @inheritParams ard_stats_t_test
#'
#' @return ARD data frame
#' @export
#'
#' @examplesIf do.call(asNamespace("cardx")$is_pkg_installed, list(pkg = "broom", reference_pkg = "cardx"))
#' cards::ADSL |>
#' ard_stats_t_test_onesample(by = ARM, variables = AGE)
ard_stats_t_test_onesample <- function(data, variables, by = dplyr::group_vars(data), conf.level = 0.95, ...) {
set_cli_abort_call()

# check installed packages ---------------------------------------------------
check_pkg_installed("broom", reference_pkg = "cardx")

# check/process inputs -------------------------------------------------------
check_not_missing(data)
check_not_missing(variables)
check_data_frame(data)
data <- dplyr::ungroup(data)
cards::process_selectors(data, by = {{ by }}, variables = {{ variables }})
check_scalar_range(conf.level, range = c(0, 1))

# if no variables selected, return empty tibble ------------------------------
if (is_empty(variables)) {
return(dplyr::tibble())
}

cards::ard_continuous(
data = data,
variables = all_of(variables),
by = all_of(by),
statistic = all_of(variables) ~ list(t_test_onesample = \(x) stats::t.test(x = x, conf.level = conf.level, ...) |> broom::tidy())
) |>
cards::bind_ard(
cards::ard_continuous(
data = data,
variables = all_of(variables),
by = all_of(by),
statistic =
all_of(variables) ~
list(conf.level = \(x) {
formals(asNamespace("stats")[["t.test.default"]])["mu"] |>
utils::modifyList(list(conf.level = conf.level, ...))
})
)
) |>
dplyr::select(-"stat_label") |>
dplyr::left_join(
.df_ttest_stat_labels(by = NULL),
by = "stat_name"
) |>
dplyr::mutate(
stat_label = dplyr::coalesce(.data$stat_label, .data$stat_name),
context = "ard_stats_t_test_onesample",
) |>
cards::tidy_ard_row_order() |>
cards::tidy_ard_column_order()
}
72 changes: 72 additions & 0 deletions R/ard_stats_wilcox_test_onesample.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#' ARD one-sample Wilcox Rank-sum
#'
#' @description
#' Analysis results data for one-sample Wilcox Rank-sum.
#' Result may be stratified by including the `by` argument.
#'
#' @param data (`data.frame`)\cr
#' a data frame. See below for details.
#' @param variables ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
#' column names to be analyzed. Independent Wilcox Rank-sum tests will be computed for
#' each variable.
#' @param by ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
#' optional column name to stratify results by.
#' @inheritParams ard_stats_wilcox_test
#'
#' @return ARD data frame
#' @export
#'
#' @examplesIf do.call(asNamespace("cardx")$is_pkg_installed, list(pkg = "broom", reference_pkg = "cardx"))
#' cards::ADSL |>
#' ard_stats_wilcox_test_onesample(by = ARM, variables = AGE)
ard_stats_wilcox_test_onesample <- function(data, variables, by = dplyr::group_vars(data), conf.level = 0.95, ...) {
set_cli_abort_call()

# check installed packages ---------------------------------------------------
check_pkg_installed("broom", reference_pkg = "cardx")

# check/process inputs -------------------------------------------------------
check_not_missing(data)
check_not_missing(variables)
check_data_frame(data)
data <- dplyr::ungroup(data)
cards::process_selectors(data, by = {{ by }}, variables = {{ variables }})
check_scalar_range(conf.level, range = c(0, 1))

# if no variables selected, return empty tibble ------------------------------
if (is_empty(variables)) {
return(dplyr::tibble())
}

cards::ard_continuous(
data = data,
variables = all_of(variables),
by = all_of(by),
statistic = all_of(variables) ~ list(t_test_onesample = \(x) stats::wilcox.test(x = x, conf.level = conf.level, ...) |> broom::tidy())
) |>
cards::bind_ard(
cards::ard_continuous(
data = data,
variables = all_of(variables),
by = all_of(by),
statistic =
all_of(variables) ~
list(conf.level = \(x) {
formals(asNamespace("stats")[["wilcox.test.default"]])[c("mu", "exact", "conf.int", "tol.root", "digits.rank")] |>
utils::modifyList(list(conf.level = conf.level, ...)) |>
compact()
})
)
) |>
dplyr::select(-"stat_label") |>
dplyr::left_join(
.df_ttest_stat_labels(by = NULL),
by = "stat_name"
) |>
dplyr::mutate(
stat_label = dplyr::coalesce(.data$stat_label, .data$stat_name),
context = "ard_stats_wilcox_test_onesample",
) |>
cards::tidy_ard_row_order() |>
cards::tidy_ard_column_order()
}
21 changes: 21 additions & 0 deletions R/reexports.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
# cards ------------------------------------------------------------------------
#' @importFrom cards ard_continuous
#' @export
cards::ard_continuous

#' @importFrom cards ard_categorical
#' @export
cards::ard_categorical

#' @importFrom cards ard_dichotomous
#' @export
cards::ard_dichotomous

#' @importFrom cards ard_missing
#' @export
cards::ard_missing

#' @importFrom cards ard_attributes
#' @export
cards::ard_attributes

# dplyr ------------------------------------------------------------------------
#' @export
#' @importFrom dplyr %>%
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ editor_options:
wrap: 72
---

# cardx <a href="https://insightsengineering.github.io/cardx"><img src="man/figures/logo.png" alt="cardx website" align="right" height="138"/></a>
# cardx <a href="https://insightsengineering.github.io/cardx/"><img src="man/figures/logo.png" alt="cardx website" align="right" height="138"/></a>

[![R-CMD-check](https://github.com/insightsengineering/cardx/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/insightsengineering/cardx/actions/workflows/R-CMD-check.yaml)
[![Codecov test
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# cardx <a href="https://insightsengineering.github.io/cardx"><img src="man/figures/logo.png" alt="cardx website" align="right" height="138"/></a>
# cardx <a href="https://insightsengineering.github.io/cardx/"><img src="man/figures/logo.png" alt="cardx website" align="right" height="138"/></a>

[![R-CMD-check](https://github.com/insightsengineering/cardx/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/insightsengineering/cardx/actions/workflows/R-CMD-check.yaml)
[![Codecov test
Expand Down
5 changes: 4 additions & 1 deletion _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ reference:
- ard_stats_oneway_test
- ard_stats_prop_test
- ard_stats_t_test
- ard_stats_t_test_onesample
- ard_stats_wilcox_test
- ard_stats_wilcox_test_onesample

- subtitle: "{aod} package"
- contents:
Expand All @@ -60,8 +62,9 @@ reference:

- subtitle: "{survey} package"
- contents:
- ard_continuous.survey.design
- ard_attributes.survey.design
- ard_survey_svychisq
- ard_survey_svycontinuous
- ard_survey_svyranktest
- ard_survey_svyttest

Expand Down
Loading

0 comments on commit 1b23ba7

Please sign in to comment.