ANOVA tests using R Worksheet

  • In this worksheet you will learn how to use R to perform a one way (unpaired) or repeated measures (paired) ANOVA. This test takes 3 or more groups which are unpaired or paired (see suggested video below) and calculates if there is a statistically significant difference between them.

Required prerequisite(s)

Suggested prerequisite(s)

Dataset

Unpaired Steps

  1. Open RStudio
  2. Read in the UnpairedDataset1.tsv file. The read.delim will automatically assign row 1 as a header so no extra flags need to be passed to it
    unpaired <- read.delim(UnpairedDataset1.tsv")
    
  3. To perform an ANOVA the data must in a ‘melted’ long format. Install the reshape 2 package (if needed) and then load the library
    install.packages("reshape2")
    library("reshape2")
    
  4. Melt the data frame so it is in the right format for the T-test
    meltedUnpaired=melt(unpaired)
    
  5. Perform the ANOVA and store in a variable
    unpairedAnov <- aov(value ~ variable, data=meltedUnpaired)
    
  6. View the summary of the ANOVA. The Pr(>F) column tells you the p-value
    summary(unpairedAnov)
    
  7. If there is a significant difference between groups, a post-hoc test must be performed to determine which groups differ. We will use a Tukey HSD test to compare all groups to each other. The ‘p adj’ column tells you the adjusted (corrected) p-value for all pairwise comparisons in the data.
    TukeyHSD(unpairedAnov, conf.level=.95)
    

Paired Steps

  1. Open RStudio
  2. Read in the Paired.Dataset1.tsv file. Although the file has sample names in the first column, we want to keep these as their own column as they are needed for paired data
    paired <- read.delim("PairedDataset1.tsv")
    
  3. To perform an ANOVA the data must in a ‘melted’ long format. Install the reshape 2 package (if needed) and then load the library
    install.packages("reshape2")
    library("reshape2")
    
  4. Melt the data frame so it is in the right format for the T-test
    meltedPaired=melt(paired)
    
  5. Perform the ANOVA and store in a variable. We must pass the sample names via the Error method to tell the ANOVA we have paired samples
    pairedAnov <- aov(value ~factor(variable)+Error(factor(Sample.Name)), data=meltedPaired)
    
  6. View the summary of the ANOVA. The Pr(>F) column tells you the p-value
    summary(pairedAnov)
    
  7. If there is a significant difference between groups, a post-hoc test must be performed to determine which groups differ. We will use a Tukey HSD test to compare all groups to each other. To do this for repeated measures (paired) ANOVA we have to use the emmeans library. Install (if needed) and load the library
    install.packages("emmeans")
    library("emmeans")
    
  8. Convert the ANOVA result to the right format
    emm <- emmeans(pairedAnov, ~ variable)
    
  9. Run the Tukey HSD test. The ‘p-value’ column tells you the adjusted (corrected) p-value for all pairwise comparisons in the data.
    pairs(emm)