|
|
@@ -4,20 +4,19 @@ |
|
|
|
#' Applying the function on a numerical vector should return one value |
|
|
|
#' Applying the function on a factor should return nlevels + 1 value, or one value per factor level |
|
|
|
#' @param x A vector |
|
|
|
#' @param f The formula applying the function |
|
|
|
#' @param f The function to try to apply, or a formula combining two functions |
|
|
|
#' @param group Grouping factor |
|
|
|
#' @return The results for the function applied on the vector, compatible with the format of the result table |
|
|
|
testify <- function(x, f, group) { |
|
|
|
# Extract the name of the function |
|
|
|
fun <- as.character(f)[2] |
|
|
|
f %>% |
|
|
|
deparse() %>% |
|
|
|
Reduce(f = paste0) %>% |
|
|
|
substring(2) -> fun |
|
|
|
|
|
|
|
fun <- sub("(?<=\\()s*\\.\\s*,?\\s*", "", fun, perl = T) |
|
|
|
fun <- sub("\\(\\)", "", fun) |
|
|
|
|
|
|
|
# Get the function from formula |
|
|
|
# and execute with grouping |
|
|
|
. <- x ~ group |
|
|
|
p <- tryCatch(eval(f[[2]])$p.value[1], |
|
|
|
# Try the function |
|
|
|
f <- eval(f[[2]]) |
|
|
|
p <- tryCatch(f(x ~ group)$p.value[1], |
|
|
|
error = function(e) {message(e);NaN}) |
|
|
|
|
|
|
|
# Return the correct number of rows depending on the variable type |
|
|
@@ -43,25 +42,25 @@ testify <- function(x, f, group) { |
|
|
|
tests_auto <- function(var, grp) { |
|
|
|
grp <- factor(grp) |
|
|
|
|
|
|
|
if (nlevels(grp) < 2) ~no.test(.) |
|
|
|
if (nlevels(grp) < 2) ~no.test |
|
|
|
else if (is.factor(var)) { |
|
|
|
if (tryCatch(is.numeric(fisher.test(var ~ grp)$p.value), error = function(e) F)) ~fisher.test(.) |
|
|
|
else ~chisq.test(.) |
|
|
|
if (tryCatch(is.numeric(fisher.test(var ~ grp)$p.value), error = function(e) F)) ~fisher.test |
|
|
|
else ~chisq.test |
|
|
|
} else { |
|
|
|
all_normal <- all(tapply(var, grp, is.normal)) |
|
|
|
|
|
|
|
if (nlevels(grp) == 2) { |
|
|
|
if (all_normal) { |
|
|
|
if (tryCatch(stats::var.test(var ~ grp)$p.value > .1, warning = function(e) F, error = function(e) F)) ~t.test(., var.equal = T) |
|
|
|
else ~t.test(., var.equal = F) |
|
|
|
if (tryCatch(stats::var.test(var ~ grp)$p.value > .1, warning = function(e) F, error = function(e) F)) ~. %>% t.test(var.equal = T) |
|
|
|
else ~. %>% t.test(var.equal = F) |
|
|
|
} |
|
|
|
else ~wilcox.test(.) |
|
|
|
else ~wilcox.test |
|
|
|
} else { |
|
|
|
if (all_normal) { |
|
|
|
if (tryCatch(stats::bartlett.test(var ~ grp)$p.value > .1, warning = function(e) F, error = function(e) F)) ~oneway.test(., var.equal = T) |
|
|
|
else ~oneway.test(., var.equal = F) |
|
|
|
if (tryCatch(stats::bartlett.test(var ~ grp)$p.value > .1, warning = function(e) F, error = function(e) F)) ~. %>% oneway.test(var.equal = T) |
|
|
|
else ~. %>% oneway.test(var.equal = F) |
|
|
|
} |
|
|
|
else ~kruskal.test(.) |
|
|
|
else ~kruskal.test |
|
|
|
} |
|
|
|
} |
|
|
|
} |