Skip to content

Commit

Permalink
Merge branch 'main' into propagation-march-2024
Browse files Browse the repository at this point in the history
  • Loading branch information
walkowif authored Apr 18, 2024
2 parents e504ff4 + f2fe696 commit 3efd1b5
Show file tree
Hide file tree
Showing 130 changed files with 3,481 additions and 801 deletions.
6 changes: 3 additions & 3 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ Pre-review Checklist (if item does not apply, mark is as complete)
- [ ] 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()`
- [ ] Request a reviewer

Reviewer Checklist (if item does not apply, mark is as complete)

- [ ] If a bug was fixed, a unit test was added.
- [ ] Run `pkgdown::build_site()`. Check the R console for errors, and review the rendered website.
- [ ] 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 "`# cards (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).
- [ ] 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 :white_check_mark:
- [ ] Approve Pull Request
- [ ] Merge the PR. Please use "Squash and merge" or "Rebase and merge".
1 change: 0 additions & 1 deletion .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: R-CMD-check

Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ on:
- synchronize
- reopened
- ready_for_review
branches:
- main
push:
branches:
- main
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: test-coverage

Expand Down
14 changes: 10 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
Package: cardx
Title: Extra Analysis Results Data Utilities
Version: 0.1.0.9008
Version: 0.1.0.9021
Authors@R: c(
person("Daniel", "Sjoberg", , "[email protected]", role = c("aut", "cre")),
person("Abinaya", "Yogasekaram", , "[email protected]", role = "aut"),
person("Emily", "de la Rua", , "[email protected]", role = "aut"),
person("F. Hoffmann-La Roche AG", role = c("cph", "fnd"))
)
Description: Create extra Analysis Results Data (ARD) summary objects.
Expand All @@ -17,23 +18,28 @@ BugReports: https://github.com/insightsengineering/cardx/issues
Depends:
R (>= 4.1)
Imports:
cards (>= 0.1.0.9002),
cards (>= 0.1.0.9014),
cli (>= 3.6.1),
dplyr (>= 1.1.2),
glue (>= 1.6.2),
rlang (>= 1.1.1),
tidyr (>= 1.3.0)
Suggests:
aod (>= 1.3.3),
broom (>= 1.0.5),
broom.helpers (>= 1.13.0),
broom.helpers (>= 1.15.0),
car (>= 3.0-11),
effectsize (>= 0.6.0),
geepack (>= 1.3.2),
ggsurvfit (>= 1.0.0),
lme4 (>= 1.1-31),
parameters (>= 0.20.2),
smd (>= 0.6.6),
spelling,
survey (>= 4.1),
survival (>= 3.2-11),
testthat (>= 3.2.0),
withr
withr (>= 2.5.0)
Remotes:
insightsengineering/cards
Config/Needs/website: insightsengineering/nesttemplate
Expand Down
48 changes: 29 additions & 19 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
# Generated by roxygen2: do not edit by hand

S3method(ard_regression,default)
S3method(ard_stats_anova,anova)
S3method(ard_stats_anova,data.frame)
export("%>%")
export(all_of)
export(any_of)
export(ard_aod_wald_test)
export(ard_car_anova)
export(ard_chisqtest)
export(ard_cohens_d)
export(ard_fishertest)
export(ard_hedges_g)
export(ard_kruskaltest)
export(ard_mcnemartest)
export(ard_moodtest)
export(ard_paired_cohens_d)
export(ard_paired_hedges_g)
export(ard_paired_ttest)
export(ard_paired_wilcoxtest)
export(ard_car_vif)
export(ard_effectsize_cohens_d)
export(ard_effectsize_hedges_g)
export(ard_effectsize_paired_cohens_d)
export(ard_effectsize_paired_hedges_g)
export(ard_proportion_ci)
export(ard_proptest)
export(ard_regression)
export(ard_regression_basic)
export(ard_smd)
export(ard_svychisq)
export(ard_svycontinuous)
export(ard_svyttest)
export(ard_ttest)
export(ard_vif)
export(ard_wilcoxtest)
export(ard_smd_smd)
export(ard_stats_anova)
export(ard_stats_aov)
export(ard_stats_chisq_test)
export(ard_stats_fisher_test)
export(ard_stats_kruskal_test)
export(ard_stats_mcnemar_test)
export(ard_stats_mood_test)
export(ard_stats_oneway_test)
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_wilcox_test)
export(ard_survey_svychisq)
export(ard_survey_svycontinuous)
export(ard_survey_svyranktest)
export(ard_survey_svyttest)
export(ard_survival_survdiff)
export(ard_survival_survfit)
export(contains)
export(ends_with)
export(everything)
Expand All @@ -44,6 +53,7 @@ export(starts_with)
export(where)
import(rlang)
importFrom(dplyr,"%>%")
importFrom(dplyr,across)
importFrom(dplyr,all_of)
importFrom(dplyr,any_of)
importFrom(dplyr,contains)
Expand Down
48 changes: 38 additions & 10 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,47 @@
# cardx 0.1.0.9008
# cardx 0.1.0.9021

### Breaking Changes

* Updated function names to follow the pattern `ard_<pkgname>_<fnname>()`. Former functions names have _not_ been deprecated. (#106)

```r
ard_ttest() -> ard_stats_t_test()
ard_paired_ttest() -> ard_stats_paired_t_test()
ard_wilcoxtest() -> ard_stats_wilcox_test()
ard_paired_wilcoxtest() -> ard_stats_paired_wilcox_test()
ard_chisqtest() -> ard_stats_chisq_test()
ard_fishertest() -> ard_stats_fisher_test()
ard_kruskaltest() -> ard_stats_kruskal_test()
ard_mcnemartest() -> ard_stats_mcnemar_test()
ard_moodtest() -> ard_stats_mood_test()
```

### New Features

* 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_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_cohens_d()`, `ard_paired_cohens_d()`, `ard_hedges_g()`, and `ard_paired_hedges_g()` for standardized differences using `effectsize::cohens_d()` and `effectsize::hedges_g()`. (#50)
- `ard_proptest()` for tests of proportions using `stats::prop.test()`. (#64)
- `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_smd()` for calculating standardized mean differences using `smd::smd()`. (#4)
- `ard_svycontinuous()` for calculating univariate summary statistics from weighted/survey data using many functions from the {survey} package. (#68)
- `ard_svychisq()` for weighted/survey chi-squared test using `survey::svychisq()`. (#72)
- `ard_svyttest()` for weighted/survey t-tests using `survey::svyttest()`. (#70)
- `ard_vif()` for calculating the variance inflation factor using `car::vif()`. (#10)
- `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_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)

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

* Updated functions `ard_ttest()`, `ard_paired_ttest()`, `ard_wilcoxtest()`, `ard_paired_wilcoxtest()`, `ard_chisqtest()`, `ard_fishertest()`, `ard_kruskaltest()`, `ard_mcnemartest()`, and `ard_moodtest()` to accept multiple variables at once. Independent tests are calculated for each variable. The `variable` argument is renamed to `variables`. (#77)
* 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)

* Updated `ard_ttest()` and `ard_wilcoxtest()` to no longer require the `by` argument, which yields central estimates with their confidence intervals. (#82)
* Added `ard_survival_survdiff()` for creating results from `survival::survdiff()`. (#113)

# cardx 0.1.0

Expand Down
116 changes: 116 additions & 0 deletions R/ard_aod_wald_test.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#' ARD Wald Test
#'
#' @description
#' Function takes a regression model object and calculates Wald
#' statistical test using [`aod::wald.test()`].
#'
#' @param x regression model object
#' @param ... arguments passed to `aod::wald.test(...)`
#'
#' @return data frame
#' @export
#'
#' @examplesIf do.call(asNamespace("cardx")$is_pkg_installed, list(pkg = "aod", reference_pkg = "cardx"))
#' lm(AGE ~ ARM, data = cards::ADSL) |>
#' ard_aod_wald_test()
ard_aod_wald_test <- function(x, ...) {
set_cli_abort_call()
# check installed packages ---------------------------------------------------
check_pkg_installed("aod", reference_pkg = "cardx")

# check inputs ---------------------------------------------------------------
check_not_missing(x)

# run regression() -----------------------------------------------------------
reg_model <- cards::eval_capture_conditions(
ard_regression_basic(x, intercept = TRUE, stats_to_remove = c(
"var_type",
"var_label",
"var_class", "label",
"contrasts_type", "contrasts", "var_nlevels", "std.error",
"conf.low", "conf.high", "statistic", "p.value", "estimate"
))
)

if (!is.null(reg_model[["error"]])) {
cli::cli_abort(
c("Unable to identify underlying variable names in regression model.",
i = "Is this model type supported by {.fun broom.helpers::tidy_plus_plus}, which is the function used to identify variable names?"
),
call = get_cli_abort_call()
)
}
aod <-
reg_model[["result"]] %>%
dplyr::select(c(
variable = "variable",
model_terms = "stat"
)) %>%
dplyr::mutate(term_id = dplyr::row_number()) %>%
tidyr::nest(data = -"variable") %>%
dplyr::rowwise() %>%
dplyr::mutate(
model_terms = unlist(.data$data[["model_terms"]]) %>% list(),
model_terms_id = rlang::set_names(.data$data[["term_id"]]) %>% list()
)
# run wald.test() -----------------------------------------------------------
wald_test <-
cards::eval_capture_conditions(lapply(seq_len(length(aod$model_terms_id)), function(terms_id) {
aod::wald.test(
Sigma = stats::vcov(x),
b = stats::coef(x), Terms = aod$model_terms_id[[terms_id]]
)
}))


df_list <- do.call(rbind, lapply(wald_test$result, .extract_wald_results))

cbind(aod$variable, df_list) %>%
tidyr::pivot_longer(
cols = !"aod$variable",
names_to = "stat_name",
values_to = "stat"
) %>%
dplyr::rename(
"variable" = "aod$variable"
) |>
dplyr::mutate(
stat = as.list(.data$stat),
stat_label =
dplyr::case_when(
.data$stat_name %in% "statistic" ~ "Statistic",
.data$stat_name %in% "df" ~ "Degrees of Freedom",
.data$stat_name %in% "p.value" ~ "p-value",
TRUE ~ .data$stat_name
),
fmt_fn =
map(
.data$stat,
function(.x) {
# styler: off
if (is.integer(.x)) return(0L)
if (is.numeric(.x)) return(1L)
# styler: on
NULL
}
),
context = "aod_wald_test",
warning = wald_test["warning"],
error = wald_test["error"]
) |>
cards::tidy_ard_column_order() %>%
{structure(., class = c("card", class(.)))} # styler: off
}

#' Extract data from wald.test object
#'
#' @param wald_test (`data.frame`)\cr wald test object object from `aod::wald.test()`
#'
#' @return a data frame containing the wald test results.
#' @keywords internal
.extract_wald_results <- function(wald_test) {
df <- wald_test$result$chi2[("df")]
statistic <- wald_test$result$chi2[("chi2")]
p.value <- wald_test$result$chi2[("P")]
data.frame(df, statistic, p.value)
}
17 changes: 11 additions & 6 deletions R/ard_car_anova.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
#' @return data frame
#' @export
#'
#' @examplesIf cards::is_pkg_installed(c("broom.helpers", "car"), reference_pkg = "cardx")
#' @examplesIf do.call(asNamespace("cardx")$is_pkg_installed, list(pkg = c("broom.helpers", "car"), reference_pkg = "cardx"))
#' lm(AGE ~ ARM, data = cards::ADSL) |>
#' ard_car_anova()
#'
#' glm(vs ~ factor(cyl) + factor(am), data = mtcars, family = binomial) |>
#' ard_car_anova(test.statistic = "Wald")
ard_car_anova <- function(x, ...) {
set_cli_abort_call()

# check installed packages ---------------------------------------------------
cards::check_pkg_installed(c("broom.helpers", "car"), reference_pkg = "cardx")
check_pkg_installed(pkg = c("broom.helpers", "car"), reference_pkg = "cardx")

# check inputs ---------------------------------------------------------------
check_not_missing(x)
Expand All @@ -25,10 +27,13 @@ ard_car_anova <- function(x, ...) {
car_anova <- cards::eval_capture_conditions(car::Anova(x, ...))

if (!is.null(car_anova[["error"]])) {
cli::cli_abort(c(
"There was an error running {.fun car::Anova}. See error message below.",
x = car_anova[["error"]]
))
cli::cli_abort(
c(
"There was an error running {.fun car::Anova}. See error message below.",
x = car_anova[["error"]]
),
call = get_cli_abort_call()
)
}

car_anova[["result"]] |>
Expand Down
Loading

0 comments on commit 3efd1b5

Please sign in to comment.