Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ard_aov and ard_onewaytest #44

Merged
merged 41 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
dc6b2c7
add `ard_aov` and `ard_onewaytest`
ayogasekaram Feb 7, 2024
7cef734
- update pkgdown
ayogasekaram Feb 7, 2024
f1c9d74
oneway test ard snap
ayogasekaram Feb 7, 2024
b68ea27
pass style check
ayogasekaram Feb 7, 2024
eb81d45
Merge branch 'main' into ard_anova
ddsjoberg Feb 9, 2024
4716921
- remove `...` parameter as it is not used.
ayogasekaram Feb 9, 2024
4008d91
Merge branch 'ard_anova' of github.com:insightsengineering/cardx into…
ayogasekaram Feb 9, 2024
55ac09f
Merge branch 'main' into ard_anova
ddsjoberg Feb 9, 2024
897e940
remove fun_args_to_record
ayogasekaram Feb 9, 2024
2857573
Merge branch 'ard_anova' of github.com:insightsengineering/cardx into…
ayogasekaram Feb 12, 2024
68452ee
Merge branch 'main' into ard_anova
ddsjoberg Feb 13, 2024
793a610
Merge commit '8b4f7cfdceedb0add3be9c523e12ab36a7ecb505'
ddsjoberg Feb 13, 2024
95a1e93
Merge branch 'main' into ard_anova
ddsjoberg Feb 14, 2024
4f40405
Merge branch 'main' into ard_anova
ddsjoberg Feb 14, 2024
c80a724
Merge branch 'main' into ard_anova
ddsjoberg Mar 1, 2024
d6a75f5
lil updates from main
ddsjoberg Mar 1, 2024
0bbad53
Merge branch 'main' into ard_anova
ddsjoberg Mar 2, 2024
0445ed8
Merge branch 'ard_anova' of github.com:insightsengineering/cardx into…
ayogasekaram Mar 6, 2024
25ce346
merge main
ayogasekaram Mar 6, 2024
52faf53
use regression formula
ayogasekaram Mar 6, 2024
e52c6a8
merge main
ayogasekaram Mar 6, 2024
99b60c0
revert to formula notation
ayogasekaram Mar 7, 2024
7c174af
Merge branch 'main' into ard_anova
ddsjoberg Mar 11, 2024
79da08d
Merge branch 'main' into ard_anova
ddsjoberg Mar 15, 2024
5f056ce
Merge branch 'main' into ard_anova
ddsjoberg Mar 19, 2024
d8c988c
initial update to formula param
ayogasekaram Mar 20, 2024
515f7e8
refactor `aov` and `onewaytest` to accept regression model input
ayogasekaram Mar 28, 2024
7ef44b2
Merge branch 'main' into ard_anova
ayogasekaram Mar 28, 2024
c5fbeb0
update for styler
ayogasekaram Mar 28, 2024
6a3167e
Merge branch 'ard_anova' of github.com:insightsengineering/cardx into…
ayogasekaram Mar 28, 2024
bab408d
update test example
ayogasekaram Mar 28, 2024
92b212c
Merge branch 'main' into ard_anova
ddsjoberg Apr 3, 2024
01618a2
Merge branch 'main' into ard_anova
ayogasekaram Apr 4, 2024
1d83d32
Update function to accept formula
ayogasekaram Apr 4, 2024
93cdc88
changes for checks
ayogasekaram Apr 4, 2024
85bf722
update for roxygen check
ayogasekaram Apr 4, 2024
72704b9
Merge branch 'main' into ard_anova
ddsjoberg Apr 4, 2024
7dc9674
Review updates
ddsjoberg Apr 4, 2024
d34f076
Merge branch 'main' into ard_anova
ddsjoberg Apr 4, 2024
2e291a6
Update NEWS.md
ddsjoberg Apr 4, 2024
9af5439
Merge branch 'ard_anova' of https://github.com/insightsengineering/ca…
ddsjoberg Apr 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
export("%>%")
export(all_of)
export(any_of)
export(ard_aov)
export(ard_onewaytest)
export(ard_paired_ttest)
export(ard_ttest)
export(contains)
Expand Down
60 changes: 60 additions & 0 deletions R/ard_aov.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#' ARD ANOVA
#'
#' @description
#' Analysis results data for Analysis of Variance.
#' Calculated with `aov(data[[variable]] ~ as.factor(data[[by]]), data = data, ...)`
#'
#' @param data (`data.frame`)\cr
#' a data frame.
#' @param by ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
#' column name to compare by. Should be a factor variable.
#' @param variable ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
#' column name to be compared. Should be a continuous variable.
#'
#' @return ARD data frame
#' @export
#'
#' @examples
#' cards::ADSL |>
#' ard_aov(by = "ARM", variable = "AGE")
ard_aov <- function(data, by, variable) {
# check installed packages ---------------------------------------------------
cards::check_pkg_installed("broom", reference_pkg = "cardx")

# check/process inputs -------------------------------------------------------
check_not_missing(data)
check_not_missing(variable)
check_not_missing(by)
check_class_data_frame(x = data)
cards::process_selectors(data, by = {{ by }}, variable = {{ variable }})
check_scalar(by)
check_scalar(variable)

# build ARD ------------------------------------------------------------------
cards::tidy_as_ard(
lst_tidy =
cards::eval_capture_conditions(
stats::aov(stats::reformulate(by, response = variable), data = data) |>
ayogasekaram marked this conversation as resolved.
Show resolved Hide resolved
broom::tidy() |>
dplyr::slice_head()
),
tidy_result_names = c("term", "df", "sumsq", "meansq", "statistic", "p.value"),
fun_args_to_record =
c("formula"),
ayogasekaram marked this conversation as resolved.
Show resolved Hide resolved
formals = formals(stats::aov),
lst_ard_columns = list(group1 = by, variable = variable, context = "aov")
) |>
dplyr::mutate(
.after = "stat_name",
stat_label =
dplyr::case_when(
.data$stat_name %in% "term" ~ "Term",
.data$stat_name %in% "sumsq" ~ "Sum of Squares",
.data$stat_name %in% "meansq" ~ "Mean of Sum of Squares",
.data$stat_name %in% "statistic" ~ "F Statistic",
.data$stat_name %in% "p.value" ~ "p-value",
.data$stat_name %in% "df" ~ "Degrees of Freedom",
TRUE ~ .data$stat_name,
)
)
}
60 changes: 60 additions & 0 deletions R/ard_onewaytest.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#' ARD One-way Test
#'
#' @description
#' Analysis results data for Testing Equal Means in a One-Way Layout.
#' Calculated with `oneway.test(data[[variable]] ~ as.factor(data[[by]]), data = data, ...)`
#'
#' @param data (`data.frame`)\cr
#' a data frame
#' @param by ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
#' column name to compare by
#' @param variable ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
#' column name to be compared
#' @param ... additional arguments passed to `oneway.test(...)`
#'
#' @return ARD data frame
#' @export
#'
#' @examples
#' cards::ADSL |>
#' ard_onewaytest(by = "ARM", variable = "AGE")
ard_onewaytest <- function(data, by, variable, ...) {
# check installed packages ---------------------------------------------------
cards::check_pkg_installed("broom", reference_pkg = "cardx")

# check/process inputs -------------------------------------------------------
check_not_missing(data)
check_not_missing(variable)
check_not_missing(by)
check_class_data_frame(x = data)
cards::process_selectors(data, by = {{ by }}, variable = {{ variable }})
check_scalar(by)
check_scalar(variable)

# build ARD ------------------------------------------------------------------
cards::tidy_as_ard(
lst_tidy =
cards::eval_capture_conditions(
stats::oneway.test(stats::reformulate(by, response = variable), data = data) |>
broom::tidy()
),
tidy_result_names = c("num.df", "den.df", "statistic", "p.value", "method"),
fun_args_to_record =
c("var.equal"),
formals = formals(stats::oneway.test),
passed_args = dots_list(...),
lst_ard_columns = list(group1 = by, variable = variable, context = "Oneway.test")
) |>
dplyr::mutate(
.after = "stat_name",
stat_label =
dplyr::case_when(
.data$stat_name %in% "num.df" ~ "Degrees of Freedom",
.data$stat_name %in% "den.df" ~ "Denominator Degrees of Freedom",
.data$stat_name %in% "statistic" ~ "F Statistic",
.data$stat_name %in% "p.value" ~ "p-value",
.data$stat_name %in% "method" ~ "Method",
TRUE ~ .data$stat_name,
)
)
}
3 changes: 3 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ reference:
- title: "ARD Creation"
- subtitle: "Inference"
- contents:
- ard_aov
- ard_onewaytest
- ard_ttest

29 changes: 29 additions & 0 deletions man/ard_aov.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions man/ard_onewaytest.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions tests/testthat/_snaps/ard_aov.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# ard_aov() works
ayogasekaram marked this conversation as resolved.
Show resolved Hide resolved

Code
as.data.frame(dplyr::select(ard_aov(cards::ADSL, by = ARM, variable = AGEGR1),
c("group1", "variable", "stat_name", "error")))
Output
group1 variable stat_name error
1 ARM AGEGR1 term NA/NaN/Inf in 'y'
2 ARM AGEGR1 df NA/NaN/Inf in 'y'
3 ARM AGEGR1 sumsq NA/NaN/Inf in 'y'
4 ARM AGEGR1 meansq NA/NaN/Inf in 'y'
5 ARM AGEGR1 statistic NA/NaN/Inf in 'y'
6 ARM AGEGR1 p.value NA/NaN/Inf in 'y'
7 ARM AGEGR1 formula NA/NaN/Inf in 'y'

14 changes: 14 additions & 0 deletions tests/testthat/_snaps/ard_onewaytest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# ard_onewaytest() works

Code
as.data.frame(dplyr::select(ard_onewaytest(cards::ADSL, by = ARM, variable = AGEGR1),
c("group1", "variable", "stat_name", "error")))
Output
group1 variable stat_name error
1 ARM AGEGR1 num.df NULL
2 ARM AGEGR1 den.df NULL
3 ARM AGEGR1 statistic NULL
4 ARM AGEGR1 p.value NULL
5 ARM AGEGR1 method NULL
6 ARM AGEGR1 var.equal NULL

30 changes: 30 additions & 0 deletions tests/testthat/test-ard_aov.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
test_that("ard_aov() works", {
expect_error(
ard_aov <-
cards::ADSL |>
ard_aov(by = ARM, variable = AGE),
NA
)

expect_equal(
ard_aov |>
cards::get_ard_statistics(stat_name %in% c("term", "sumsq", "statistic")),
aov(
AGE ~ ARM,
data = cards::ADSL
) |>
broom::tidy() |>
dplyr::slice_head() |>
dplyr::select(term, sumsq, statistic) |>
unclass(),
ignore_attr = TRUE
)

# errors are properly handled - "variable" should be continuous, not factor
expect_snapshot(
cards::ADSL |>
ard_aov(by = ARM, variable = AGEGR1) |>
dplyr::select(c("group1", "variable", "stat_name", "error")) |>
as.data.frame()
)
})
29 changes: 29 additions & 0 deletions tests/testthat/test-ard_onewaytest.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
test_that("ard_onewaytest() works", {
expect_error(
ard_onewaytest <-
cards::ADSL |>
ard_onewaytest(by = ARM, variable = AGE),
NA
)

expect_equal(
ard_onewaytest |>
cards::get_ard_statistics(stat_name %in% c("num.df", "statistic", "method")),
oneway.test(
AGE ~ ARM,
data = cards::ADSL
) |>
broom::tidy() |>
dplyr::select(num.df, statistic, method) |>
unclass(),
ignore_attr = TRUE
)

# errors are properly handled - "variable" should be continuous, not factor
expect_snapshot(
cards::ADSL |>
ard_onewaytest(by = ARM, variable = AGEGR1) |>
dplyr::select(c("group1", "variable", "stat_name", "error")) |>
as.data.frame()
)
})