Home > Mobile >  rstatix::anova_test() over multiple columns of data frame
rstatix::anova_test() over multiple columns of data frame

Time:07-10

I like to run multiple one-way ANOVAs over multiple columns of a data frame. My approach to doing this was with a for loop. The first column of my data frame contains the groups. To provide a reproducible example, i here take the iris data set. I want to use rstatix::anova_test() instead of f.ex. aov(), because rstatix::anova_test() is pipe-friendly, seems to be a better option for unbalanced data (like i have it) and allows also to define the type of sums of squares for ANOVA.

When i write a for loop with aov() it works. Unfortunately, I have so far failed doing similar with rstatix::anova_test(). Please can anybody help me?

data <- iris %>% relocate(Species, .before = Sepal.Length)

# Define object which will receive the results
results <- NULL
results <- as.data.frame(results)

with aov() it works

for(i in 2:ncol(data)){
  
  # Put the name of the variable in first column of results object
  results[i-1,1] <- names(data)[i]
  
  # ANOVA test iterating through each column of the data frame and save output in a temporary object.
  temp_anova_results <- broom::tidy(aov(data[,i] ~ Species, data = data))
  
  # write ANOVA p value in second column of results object
  results[i-1,2] <- temp_anova_results$p.value[1]
  
  rm(temp_anova_results)
} 

for several reasons i like to work with rstatix::anova_test(), but failed to get a correct for loop, an example i tried:

for(i in 2:ncol(data)){
 
 # Put the name of the variable in first column of results object
 results[i-1,1] <- names(data)[i]
 
 # ANOVA test iterating through each column of the data frame and save output in a temporary object.
 temp_anova_results <- data %>% anova_test(data[,i] ~ Species, type = 3)
 
 # write ANOVA p value in second column of results object
 results[i-1,2] <- temp_anova_results$p[1]
 
 rm(temp_anova_results)
} 

data %>% anova_test(data[,i] ~ Species) seems to be the problem, but works outside the for loop when inserting a number for i, like f.ex. data %>% anova_test(data[,2] ~ Species)

CodePudding user response:

Maybe somebody else has a better answer, but the only way I can get this to work is to build the formula from the column names, that is replace your anova_test line with:

temp_anova_results <- data %>% anova_test(formula(paste0(names(dat)[i],"~","Species")))

I don't know why your method didn't work. Even outside of the loop using i instead of a numeric constant broke the anova_test function call.

  •  Tags:  
  • r
  • Related