Skip to content

Commit

Permalink
New function welch_anova_test
Browse files Browse the repository at this point in the history
  • Loading branch information
kassambara committed Aug 1, 2019
1 parent 0c0220c commit 713be68
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 1 deletion.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,6 @@ Collate:
'shapiro_test.R'
'sign_test.R'
'tukey_hsd.R'
'welch_anova_test.R'
'wilcox_effsize.R'
'wilcox_test.R'
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export(t_test)
export(tibble)
export(tidy)
export(tukey_hsd)
export(welch_anova_test)
export(wilcox_effsize)
export(wilcox_test)
importFrom(broom,tidy)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## New features

- New function `welch_anova_test()`: Welch one-Way ANOVA test. A wrapper around the base function `stats::oneway.test()`. This is is an alternative to the standard one-way ANOVA in the situation where the homogeneity of variance assumption is violated.
- New function `friedman_effsize()`, computes the effect size of Friedman test using the Kendall's W value.
- New function `friedman_test()`, provides a pipe-friendly framework to perform a Friedman rank sum test, which is the non-parametric alternative to the one-way repeated measures ANOVA test.
- New function `games_howell_test()`: Performs Games-Howell test, which is used to compare all possible combinations of group differences when the assumption of homogeneity of variances is violated.
Expand Down
6 changes: 5 additions & 1 deletion R/get_test_label.R
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ get_test_label <- function(stat.test, description = NULL, p.col = "p",
get_pairwise_comparison_methods(),
kruskal_test = "Kruskal-Wallis",
friedman_test = "Friedman test",
anova_test = "Anova"
anova_test = "Anova",
welch_anova_test = "Welch ANOVA"
)
stop_ifnot_class(stat.test, .class = names(allowed.tests))
is_anova_test <- inherits(stat.test, "anova_test")
Expand Down Expand Up @@ -323,6 +324,7 @@ get_statistic_text <- function(stat.test, type = c("text", "expression")){
kruskal_test = quote(italic(chi)^2),
friedman_test = quote(italic(chi)^2),
anova_test = quote(italic("F")),
weltch_anova_test = quote(italic("F")),
quote(italic("Stat"))
)
}
Expand All @@ -338,6 +340,7 @@ get_statistic_text <- function(stat.test, type = c("text", "expression")){
kruskal_test = "X2",
friedman_test = "X2",
anova_test = "F",
welch_anova_test = "F",
"Stat"
)
}
Expand Down Expand Up @@ -406,6 +409,7 @@ get_description <- function(stat.test){
emmeans_test = "Emmeans test",
tukey_hsd = "Tukey HSD",
anova_test = "Anova",
welch_anova_test = "Welch Anova",
kruskal_test = "Kruskal-Wallis",
friedman_test = "Friedman test",
cor_test = "Correlation"
Expand Down
69 changes: 69 additions & 0 deletions R/welch_anova_test.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#' @include utilities.R
NULL
#' Welch One-Way ANOVA Test
#'
#' @description Tests for equal means in a one-way design (not assuming equal
#' variance). A wrapper around the base function
#' \code{\link[stats]{oneway.test}()}. This is is an alternative to the
#' standard one-way ANOVA in the situation where the homogeneity of variance
#' assumption is violated.
#' @param data a data frame containing the variables in the formula.
#' @param formula a formula specifying the ANOVA model similar to aov. Can be of
#' the form y ~ group where y is a numeric variable giving the data values and
#' group is a factor with one or multiple levels giving the corresponding
#' groups. For example, formula = TP53 ~ cancer_group.
#' @return return a data frame with the following columns: \itemize{ \item
#' \code{.y.}: the y variable used in the test. \item \code{n}: sample count.
#' \item \code{statistic}: the value of the test statistic. \item \code{p}:
#' p-value. \item \code{method}: the statistical test used to compare groups.}
#' @examples
#' # Load data
#' #:::::::::::::::::::::::::::::::::::::::
#' data("ToothGrowth")
#' df <- ToothGrowth
#' df$dose <- as.factor(df$dose)
#'
#' # Welch one-way ANOVA test (not assuming equal variance)
#' #:::::::::::::::::::::::::::::::::::::::::
#' df %>% welch_anova_test(len ~ dose)
#'
#' # Grouped data
#' #:::::::::::::::::::::::::::::::::::::::::
#' df %>%
#' group_by(supp) %>%
#' welch_anova_test(len ~ dose)
#' @name welch_anova_test
#' @export
welch_anova_test <- function(data, formula){

args <- as.list(environment()) %>%
.add_item(method = "welch_anova_test")
outcome <- get_formula_left_hand_side(formula)
group <- get_formula_right_hand_side(formula)

if(is_grouped_df(data)){
results <- data %>%
doo(welch_anova_test, formula) %>%
set_attrs(args = args) %>%
add_class(c("rstatix_test", "welch_anova_test"))
return(results)
}
term <- statistic <- p <- df <- method <- NULL
res <- oneway_test(data, formula) %>%
add_columns(.y. = outcome, n = nrow(data), .before = "statistic")
res %>%
set_attrs(args = args) %>%
add_class(c("rstatix_test", "welch_anova_test"))
}

oneway_test <- function(data, formula){
res <- stats::oneway.test(formula, data = data, var.equal = FALSE)
tibble(
statistic = round_value(res$statistic, 2),
DFn = res$parameter[1],
DFd = res$parameter[2],
p = round_value(res$p.value, 3),
method = "Welch ANOVA"
)
}

1 change: 1 addition & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ It's also possible to compute several effect size metrics, including "eta square
- `sign_test()`: perform sign test to determine whether there is a median difference between paired or matched observations.
- `anova_test()`: an easy-to-use wrapper around `car::Anova()` to perform different types of ANOVA tests, including **independent measures ANOVA**, **repeated measures ANOVA** and **mixed ANOVA**.
- `get_anova_test_table()`: extract ANOVA table from `anova_test()` results. Can apply sphericity correction automatically in the case of within-subject (repeated measures) designs.
`- welch_anova_test()`: Welch one-Way ANOVA test. A pipe-frindly wrapper around the base function `stats::oneway.test()`. This is is an alternative to the standard one-way ANOVA in the situation where the homogeneity of variance assumption is violated.
- `kruskal_test()`: perform kruskal-wallis rank sum test
- `friedman_test()`: Provides a pipe-friendly framework to perform a Friedman rank sum test, which is the non-parametric alternative to the one-way repeated measures ANOVA test.
- `get_comparisons()`: Create a list of possible pairwise comparisons between groups.
Expand Down
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ reference:
- wilcox_test
- sign_test
- anova_test
- welch_anova_test
- kruskal_test
- friedman_test
- get_comparisons
Expand Down
46 changes: 46 additions & 0 deletions man/welch_anova_test.Rd

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

0 comments on commit 713be68

Please sign in to comment.