Skip to content

Commit ed6a26c

Browse files
add ard_aod_wald_test function (#99)
**What changes are proposed in this pull request?** add `ard_aod_wald_test` function closes #84 -------------------------------------------------------------------------------- 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] Code coverage is suitable for any new functions/features (generally, 100% coverage for new code): `devtools::test_coverage()` - [x] Request a reviewer Reviewer Checklist (if item does not apply, mark is as complete) - [x] If a bug was fixed, a unit test was added. - [x] Run `pkgdown::build_site()`. Check the R console for errors, and review the rendered website. - [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 "`# 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). - [x] **All** GitHub Action workflows pass with a ✅ - [x] Approve Pull Request - [x] Merge the PR. Please use "Squash and merge" or "Rebase and merge". --------- Signed-off-by: Daniel Sjoberg <danield.sjoberg@gmail.com> Signed-off-by: Abinaya Yogasekaram <73252787+ayogasekaram@users.noreply.github.com> Co-authored-by: Daniel Sjoberg <danield.sjoberg@gmail.com>
1 parent 8fa77ab commit ed6a26c

File tree

9 files changed

+198
-2
lines changed

9 files changed

+198
-2
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Imports:
2525
rlang (>= 1.1.1),
2626
tidyr (>= 1.3.0)
2727
Suggests:
28+
aod (>= 1.3.3),
2829
broom (>= 1.0.5),
2930
broom.helpers (>= 1.13.0),
3031
car (>= 3.0-11),

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ S3method(ard_regression,default)
44
export("%>%")
55
export(all_of)
66
export(any_of)
7+
export(ard_aod_wald_test)
78
export(ard_car_anova)
89
export(ard_car_vif)
910
export(ard_effectsize_cohens_d)

NEWS.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
### Breaking Changes
44

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

77
```r
88
ard_ttest() -> ard_stats_t_test()
@@ -19,7 +19,8 @@ ard_moodtest() -> ard_stats_mood_test()
1919
### New Features
2020

2121
* Added the following functions for calculating Analysis Results Data (ARD).
22-
- `ard_aov()` for calculating ANOVA results using `stats::aov()`. (#3)
22+
- `ard_stats_aov()` for calculating ANOVA results using `stats::aov()`. (#3)
23+
- `ard_aod_wald_test()` for calculating Wald Tests for regression models using `aod::wald.test()`. (#3)
2324
- `ard_car_anova()` for calculating ANOVA results using `car::Anova()`. (#3)
2425
- `ard_onewaytest()` for calculating ANOVA results using `stats::oneway.test()`. (#3)
2526
- `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)

R/ard_aod_wald_test.R

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#' ARD Wald Test
2+
#'
3+
#' @description
4+
#' Function takes a regression model object and calculates Wald
5+
#' statistical test using [`aod::wald.test()`].
6+
#'
7+
#' @param x regression model object
8+
#' @param ... arguments passed to `aod::wald.test(...)`
9+
#'
10+
#' @return data frame
11+
#' @export
12+
#'
13+
#' @examplesIf cards::is_pkg_installed(c("aod"), reference_pkg = "cardx")
14+
#' lm(AGE ~ ARM, data = cards::ADSL) |>
15+
#' ard_aod_wald_test()
16+
ard_aod_wald_test <- function(x, ...) {
17+
# check installed packages ---------------------------------------------------
18+
cards::check_pkg_installed("aod", reference_pkg = "cardx")
19+
20+
# check inputs ---------------------------------------------------------------
21+
check_not_missing(x)
22+
23+
# run regression() -----------------------------------------------------------
24+
reg_model <- cards::eval_capture_conditions(
25+
ard_regression_basic(x, intercept = TRUE, stats_to_remove = c(
26+
"var_type",
27+
"var_label",
28+
"var_class", "label",
29+
"contrasts_type", "contrasts", "var_nlevels", "std.error",
30+
"conf.low", "conf.high", "statistic", "p.value", "estimate"
31+
))
32+
)
33+
34+
if (!is.null(reg_model[["error"]])) {
35+
cli::cli_abort(c(
36+
"Unable to identify underlying variable names in regression model.",
37+
i = "Is this model type supported by {.fun broom.helpers::tidy_plus_plus}, which is the function used to identify variable names?"
38+
))
39+
}
40+
aod <-
41+
reg_model[["result"]] %>%
42+
dplyr::select(c(
43+
variable = "variable",
44+
model_terms = "stat"
45+
)) %>%
46+
dplyr::mutate(term_id = dplyr::row_number()) %>%
47+
tidyr::nest(data = -"variable") %>%
48+
dplyr::rowwise() %>%
49+
dplyr::mutate(
50+
model_terms = unlist(.data$data[["model_terms"]]) %>% list(),
51+
model_terms_id = rlang::set_names(.data$data[["term_id"]]) %>% list()
52+
)
53+
# run wald.test() -----------------------------------------------------------
54+
wald_test <-
55+
cards::eval_capture_conditions(lapply(seq_len(length(aod$model_terms_id)), function(terms_id) {
56+
aod::wald.test(
57+
Sigma = stats::vcov(x),
58+
b = stats::coef(x), Terms = aod$model_terms_id[[terms_id]]
59+
)
60+
}))
61+
62+
63+
df_list <- do.call(rbind, lapply(wald_test$result, .extract_wald_results))
64+
65+
cbind(aod$variable, df_list) %>%
66+
tidyr::pivot_longer(
67+
cols = !"aod$variable",
68+
names_to = "stat_name",
69+
values_to = "stat"
70+
) %>%
71+
dplyr::rename(
72+
"variable" = "aod$variable"
73+
) |>
74+
dplyr::mutate(
75+
stat = as.list(.data$stat),
76+
stat_label =
77+
dplyr::case_when(
78+
.data$stat_name %in% "statistic" ~ "Statistic",
79+
.data$stat_name %in% "df" ~ "Degrees of Freedom",
80+
.data$stat_name %in% "p.value" ~ "p-value",
81+
TRUE ~ .data$stat_name
82+
),
83+
fmt_fn =
84+
map(
85+
.data$stat,
86+
function(.x) {
87+
# styler: off
88+
if (is.integer(.x)) return(0L)
89+
if (is.numeric(.x)) return(1L)
90+
# styler: on
91+
NULL
92+
}
93+
),
94+
context = "aod_wald_test",
95+
warning = wald_test["warning"],
96+
error = wald_test["error"]
97+
) |>
98+
cards::tidy_ard_column_order() %>%
99+
{structure(., class = c("card", class(.)))} # styler: off
100+
}
101+
102+
#' Extract data from wald.test object
103+
#'
104+
#'
105+
#' @param wald_test (`data.frame`)\cr wald test object object from `aod::wald.test()`
106+
#'
107+
#' @return a data frame containing the wald test results.
108+
#' @keywords internal
109+
110+
.extract_wald_results <- function(wald_test) {
111+
df <- wald_test$result$chi2[("df")]
112+
statistic <- wald_test$result$chi2[("chi2")]
113+
p.value <- wald_test$result$chi2[("P")]
114+
data.frame(df, statistic, p.value)
115+
}

_pkgdown.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ reference:
2424
- title: "ARD Creation"
2525
- subtitle: "Inference"
2626
- contents:
27+
- ard_aod_wald_test
2728
- ard_car_anova
2829
- ard_stats_aov
2930
- ard_stats_chisq_test

man/ard_aod_wald_test.Rd

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/dot-extract_wald_results.Rd

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# ard_aod_wald_test() works
2+
3+
Code
4+
glm_ard_aod_waldtest[, 1:6]
5+
Message
6+
{cards} data frame: 6 x 6
7+
Output
8+
variable context stat_name stat_label stat fmt_fn
9+
1 (Intercept) aod_wald… df Degrees … 1 1
10+
2 (Intercept) aod_wald… statistic Statistic 7126.713 1
11+
3 (Intercept) aod_wald… p.value p-value 0 1
12+
4 ARM aod_wald… df Degrees … 2 1
13+
5 ARM aod_wald… statistic Statistic 1.046 1
14+
6 ARM aod_wald… p.value p-value 0.593 1
15+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
test_that("ard_aod_wald_test() works", {
2+
# works for a generic case
3+
expect_error(
4+
glm_ard_aod_waldtest <-
5+
suppressWarnings(lm(AGE ~ ARM, data = cards::ADSL)) |>
6+
ard_aod_wald_test(),
7+
NA
8+
)
9+
expect_equal(nrow(glm_ard_aod_waldtest), 6L)
10+
expect_snapshot(glm_ard_aod_waldtest[, 1:6])
11+
12+
# error returned when a regression model isn't passed
13+
14+
expect_error(
15+
ard_aod_wald_test(cards::ADSL) |>
16+
dplyr::select(c(context, error))
17+
)
18+
})

0 commit comments

Comments
 (0)