You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
3.3KB

  1. #' Transform any test function into a valid test function for the table
  2. #'
  3. #' Transform a function into a valid test function for the table
  4. #' Applying the function on a numerical vector should return one value
  5. #' Applying the function on a factor should return nlevels + 1 value, or one value per factor level
  6. #' @param x A vector
  7. #' @param f The function to try to apply, or a formula combining two functions
  8. #' @param group Grouping factor
  9. #' @return The results for the function applied on the vector, compatible with the format of the result table
  10. testify <- function(x, f, group) {
  11. # Extract the name of the function
  12. f %>%
  13. deparse() %>%
  14. Reduce(f = paste0) %>%
  15. substring(2) -> fun
  16. # Try the function
  17. f <- eval(f[[2]])
  18. p <- tryCatch(f(x ~ group)$p.value[1],
  19. error = function(e) {message(e);NaN})
  20. # Return the correct number of rows depending on the variable type
  21. if (is.factor(x)) data.frame(p = c(p, rep(NA, nlevels(x))),
  22. test = c(fun, rep(NA, nlevels(x))),
  23. row.names = NULL, check.names = F, stringsAsFactors = F)
  24. else data.frame(p = p,
  25. test = fun,
  26. row.names = NULL, check.names = F, stringsAsFactors = F)
  27. }
  28. #' Functions to choose a statistical test
  29. #'
  30. #' These functions take a variable and a grouping variable as arguments, and return a statistcal test to use, expressed as a single-term formula.
  31. #'
  32. #' Currently, only \code{tests_auto} is defined, and picks between t test, wilcoxon, anova, kruskal-wallis and fisher depending on the number of groups, the type of the variable, the normality and homoskedasticity of the distributions.
  33. #'
  34. #' @param var The variable to test
  35. #' @param grp The variable for the groups
  36. #' @return A statistical test function
  37. #' @export
  38. tests_auto <- function(var, grp) {
  39. grp <- factor(grp)
  40. if (nlevels(grp) < 2) ~no.test
  41. else if (is.factor(var)) {
  42. if (tryCatch(is.numeric(fisher.test(var ~ grp)$p.value), error = function(e) F)) ~fisher.test
  43. else ~chisq.test
  44. } else {
  45. all_normal <- all(tapply(var, grp, is.normal))
  46. if (nlevels(grp) == 2) {
  47. if (all_normal) {
  48. if (tryCatch(stats::var.test(var ~ grp)$p.value > .1, warning = function(e) F, error = function(e) F)) ~. %>% t.test(var.equal = T)
  49. else ~. %>% t.test(var.equal = F)
  50. }
  51. else ~wilcox.test
  52. } else {
  53. if (all_normal) {
  54. if (tryCatch(stats::bartlett.test(var ~ grp)$p.value > .1, warning = function(e) F, error = function(e) F)) ~. %>% oneway.test(var.equal = T)
  55. else ~. %>% oneway.test(var.equal = F)
  56. }
  57. else ~kruskal.test
  58. }
  59. }
  60. }