Skip to content

Commit c849ad4

Browse files
authored
Adding ard_car_anova() (#67)
**What changes are proposed in this pull request?** * Added `ard_car_anova()` for tabulating results from `car::Anova()`. (#3) **Reference GitHub issue associated with pull request.** _e.g., 'closes #<issue number>'_ closes #3 -------------------------------------------------------------------------------- Pre-review Checklist (if item does not apply, mark is as complete) - [ ] **All** GitHub Action workflows pass with a ✅ - [ ] 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. - [ ] 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). - [ ] **All** GitHub Action workflows pass with a ✅ - [ ] Approve Pull Request - [ ] Merge the PR. Please use "Squash and merge" or "Rebase and merge". --------- Signed-off-by: Daniel Sjoberg <danield.sjoberg@gmail.com>
1 parent 7067e26 commit c849ad4

File tree

8 files changed

+136
-1
lines changed

8 files changed

+136
-1
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Imports:
2424
Suggests:
2525
broom (>= 1.0.5),
2626
broom.helpers (>= 1.13.0),
27+
car (>= 3.0-11),
2728
effectsize (>= 0.6.0),
2829
parameters (>= 0.20.2),
2930
smd (>= 0.6.6),

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_car_anova)
78
export(ard_chisqtest)
89
export(ard_cohens_d)
910
export(ard_fishertest)

R/ard_car_anova.R

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#' ARD ANOVA from car Package
2+
#'
3+
#' Function takes a regression model object and calculated ANOVA using [`car::Anova()`].
4+
#'
5+
#' @param x regression model object
6+
#' @param ... arguments passed to `car::Anova(...)`
7+
#'
8+
#' @return data frame
9+
#' @export
10+
#'
11+
#' @examplesIf cards::is_pkg_installed("car", reference_pkg = "cardx")
12+
#' lm(AGE ~ ARM, data = cards::ADSL) |>
13+
#' ard_car_anova()
14+
#'
15+
#' glm(vs ~ factor(cyl) + factor(am), data = mtcars, family = binomial) |>
16+
#' ard_car_anova(test.statistic = "Wald")
17+
ard_car_anova <- function(x, ...) {
18+
# check installed packages ---------------------------------------------------
19+
cards::check_pkg_installed(c("broom.helpers", "car"), reference_pkg = "cardx")
20+
21+
# check inputs ---------------------------------------------------------------
22+
check_not_missing(x)
23+
24+
# run car::Anova() -----------------------------------------------------------
25+
car_anova <- cards::eval_capture_conditions(car::Anova(x, ...))
26+
27+
if (!is.null(car_anova[["error"]])) {
28+
cli::cli_abort(c(
29+
"There was an error running {.fun car::Anova}. See error message below.",
30+
x = car_anova[["error"]]
31+
))
32+
}
33+
34+
car_anova[["result"]] |>
35+
broom.helpers::tidy_parameters(conf.int = FALSE) |> # using broom.helpers, because it handle non-syntactic names for us
36+
dplyr::filter(!(dplyr::row_number() == dplyr::n() & .data$term %in% "Residuals")) |> # removing Residual rows
37+
dplyr::rename(variable = "term") |>
38+
tidyr::pivot_longer(
39+
cols = -"variable",
40+
names_to = "stat_name",
41+
values_to = "stat"
42+
) |>
43+
dplyr::mutate(
44+
stat = as.list(.data$stat),
45+
stat_label =
46+
dplyr::case_when(
47+
.data$stat_name %in% "statistic" ~ "Statistic",
48+
.data$stat_name %in% "df" ~ "Degrees of Freedom",
49+
.data$stat_name %in% "p.value" ~ "p-value",
50+
TRUE ~ .data$stat_name
51+
),
52+
fmt_fn =
53+
map(
54+
.data$stat,
55+
function(.x) {
56+
# styler: off
57+
if (is.integer(.x)) return(0L)
58+
if (is.numeric(.x)) return(1L)
59+
# styler: on
60+
NULL
61+
}
62+
),
63+
context = "car_anova",
64+
warning = car_anova["warning"],
65+
error = car_anova["error"]
66+
) |>
67+
cards::tidy_ard_column_order() %>%
68+
{structure(., class = c("card", class(.)))} # styler: off
69+
}

R/ard_regression.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ ard_regression.default <- function(x, tidy_fun = broom.helpers::tidy_with_broom_
2929
cards::check_pkg_installed("broom.helpers", reference_pkg = "cardx")
3030

3131
# check inputs ---------------------------------------------------------------
32-
check_not_missing(x, "model")
32+
check_not_missing(x)
3333

3434
# summarize model ------------------------------------------------------------
3535
broom.helpers::tidy_plus_plus(

_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_car_anova
2728
- ard_chisqtest
2829
- ard_fishertest
2930
- ard_kruskaltest

man/ard_car_anova.Rd

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# ard_car_anova() works
2+
3+
Code
4+
glm_ard_car_anova
5+
Message
6+
{cards} data frame: 6 x 8
7+
Output
8+
variable context stat_name stat_label stat fmt_fn
9+
1 factor(cyl) car_anova statistic Statistic 0 1
10+
2 factor(cyl) car_anova df Degrees … 2 1
11+
3 factor(cyl) car_anova p.value p-value 1 1
12+
4 factor(am) car_anova statistic Statistic 0 1
13+
5 factor(am) car_anova df Degrees … 1 1
14+
6 factor(am) car_anova p.value p-value 0.998 1
15+
Message
16+
i 2 more variables: warning, error
17+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
test_that("ard_car_anova() works", {
2+
# works for a generic case
3+
expect_error(
4+
glm_ard_car_anova <-
5+
suppressWarnings(glm(vs ~ factor(cyl) + factor(am), data = mtcars, family = binomial)) |>
6+
ard_car_anova(test.statistic = "Wald"),
7+
NA
8+
)
9+
expect_equal(nrow(glm_ard_car_anova), 6L)
10+
expect_snapshot(glm_ard_car_anova)
11+
})
12+
13+
test_that("ard_car_anova() messaging", {
14+
expect_snapshot(
15+
error = TRUE,
16+
ard_car_anova(mtcars)
17+
)
18+
})

0 commit comments

Comments
 (0)