R output tukey results only where factors or interactions are significant (conditional statements)

Viewed 274

I'm relatively new to the R language, and using it to analyse my data. I am using a package called 'agricolae' and its HSD.test component to output tukeyHSD results based on the anova proceeding it. I have simplified the code below, but essentially the tukey code is run inside a for loop running through a list of factors/interactions. It works fine, however, I only want the loop to run where the anova result for the corresponding factor is significant. (i.e. <= 0.05).

f1 <- as.formula(paste(m,'~ Treatment + Genotype + GENOTYPExTREATMENT', sep = ''))
anova.result <- aov(f1, data = licor.data2)

treatment.list <- c('Treatment','Genotype', 'GENOTYPExTREATMENT')
        
for(t in treatment.list){ 
   tukey.result <- HSD.test(anova.result, trt = t)$groups
}

using rownames(summary(anova.result)[[1]]) returns [1] "Treatment " "Genotype " "GENOTYPExTREATMENT" "Residuals " and str(summary(anova.result)[[1]]) returns:

Classes ‘anova’ and 'data.frame':   4 obs. of  5 variables:
 $ Df     : num  4 6 24 99
 $ Sum Sq : num  0.01215 0.0019 0.00255 0.00527
 $ Mean Sq: num  3.04e-03 3.16e-04 1.06e-04 5.33e-05
 $ F value: num  57.02 5.94 1.99 NA
 $ Pr(>F) : num  7.22e-25 2.50e-05 9.68e-03 NA

Just wondering if someone knows how to construct a when or if/else statement which will only run the loop code where the $Pr(>F) value for the corresponding 't' loop factor or interaction is less than 0.05.

thanks in advance,

Luke

2 Answers

Here's a solution using dplyr and DescTools you may also want to look at my Plot2WayANOVA function which has this built in...

library(dplyr)
library(DescTools)

mtcars$am <- factor(mtcars$am)
mtcars$cyl <- factor(mtcars$cyl)
MyAOV <- aov(mpg ~ am * cyl, mtcars)

sigfactors <- 
  filter(summary(MyAOV)[[1]], `Pr(>F)` <= 1 - .95) %>% 
  rownames %>% 
  trimws

if (length(sigfactors) > 0) {
  posthocresults <- PostHocTest(MyAOV,
                                method = "hsd",
                                conf.level = .95,
                                which = sigfactors)
} else {
  posthocresults <- "No signfiicant effects"
}

posthocresults

Which yields

  Posthoc multiple comparisons of means : Tukey HSD 
    95% family-wise confidence level

$am
        diff  lwr.ci   upr.ci    pval    
1-0 7.244939 5.00149 9.488388 4.8e-07 ***

$cyl
         diff     lwr.ci     upr.ci    pval    
6-4 -4.756706  -8.399753 -1.1136596  0.0088 ** 
8-4 -7.329581 -10.365453 -4.2937086 7.2e-06 ***
8-6 -2.572874  -6.060826  0.9150773  0.1788    

---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

I got there using an if statement, and changing the for loop code to 1:length to output the numerical positional in the list rather than the text itself. This in turn meant i had to update the treatment in the HSD.test function, not to output a number in place of 't' but the text using treatment.list[t] hope it makes sense!

for(t in 1:length(treatment.list)){ 
   if(summary(anova.result)[[1]][['Pr(>F)']][[t]] <= 0.05){
   tukey.result <- HSD.test(anova.result, trt = treatment.list[t])$groups


 } # end of if statement
} # end of for loop
Related