Skip to Content
Course content

55: ANOVA and Post-Hoc Tests

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Up until now, we've spent a lot of time comparing two groups using t-tests. But in the real world, you're rarely just comparing A and B. Usually, you've got A, B, C, and maybe a control group. If you try to run a dozen separate t-tests to compare every possible pair, you run into a massive statistical problem: the family-wise error rate. Basically, the more tests you run, the more likely you are to find a "significant" result just by pure chance. That's where ANOVA comes in.

Setting up our Plant Growth Experiment

Let's imagine we're testing three different types of fertilizer—let's call them 'NutriGrow', 'GreenLeaf', and 'BioBoost'—to see which one makes tomato plants grow the tallest. I've put together a small dataset to represent this. I'm keeping it simple so we can focus on the logic of the test rather than data cleaning.

# Creating the dataset
fertilizer <- c(rep("NutriGrow", 10), rep("GreenLeaf", 10), rep("BioBoost", 10))
height <- c(15, 16, 14, 17, 15, 16, 14, 15, 17, 16,  # NutriGrow
            20, 22, 19, 21, 20, 23, 18, 21, 20, 22,  # GreenLeaf
            16, 17, 15, 18, 16, 17, 15, 16, 18, 17)  # BioBoost

plant_data <- data.frame(fertilizer, height)

Running the One-Way ANOVA

The One-Way ANOVA asks a very specific question: "Is there any statistically significant difference between the means of these groups?" It doesn't tell us which group is different, just that they aren't all the same. I'll use the aov() function, which is the standard way to handle this in R.

# This is where I usually jump straight in...
res_anova <- aov(height ~ fertilizer, data = plant_data)
summary(res_anova)

Wait, I just noticed something. If I had loaded this data from a CSV where the fertilizer column was just strings, R usually handles it, but if the column was coded as integers (1, 2, 3), aov() would treat it as a continuous variable—a regression—instead of a grouping factor. I always double-check my structure. Let's make sure fertilizer is explicitly a factor to avoid any weird behavior.

# Fixing the data type to be safe
plant_data$fertilizer <- as.factor(plant_data$fertilizer)
res_anova <- aov(height ~ fertilizer, data = plant_data)
summary(res_anova)

Looking at the output, if the p-value (Pr(>F)) is less than 0.05, we can reject the null hypothesis. In this case, the p-value is tiny, so we know at least one fertilizer is performing differently than the others. But we're still in the dark about which one is the winner.

Pinpointing the winner with Tukey's HSD

This is where the "Post-Hoc" part comes in. Since the ANOVA gave us a green light, we can now safely run a Tukey Honestly Significant Difference (HSD) test. This test compares all possible pairs while adjusting the p-values to account for the fact that we're doing multiple comparisons. It's the "safety net" that prevents us from claiming a discovery that's actually just noise.

# Performing the Tukey Post-Hoc test
tukey_result <- TukeyHSD(res_anova)
print(tukey_result)

The output gives us a comparison table. You'll see pairs like GreenLeaf-NutriGrow. If the p adj for that pair is below 0.05, that specific difference is significant. In our example, you'll likely see that GreenLeaf significantly outperforms both NutriGrow and BioBoost, while the difference between NutriGrow and BioBoost is negligible. Now we have an answer we can actually use.




📋 Practical Task

Analyzing Coffee Roast Caffeine Levels

You have been given a dataset containing caffeine levels (in mg) from three different roast levels of the same coffee bean: 'Light', 'Medium', and 'Dark'. Your goal is to determine if roast level significantly affects caffeine content.

Use the following data to start:

roast <- c(rep("Light", 8), rep("Medium", 8), rep("Dark", 8))
caffeine <- c(120, 125, 118, 122, 121, 124, 119, 123, 
              110, 115, 108, 112, 111, 114, 109, 113, 
              105, 110, 102, 108, 106, 109, 104, 107)
coffee_data <- data.frame(roast, caffeine)

Your task:

  1. Convert the roast column to a factor.
  2. Perform a One-Way ANOVA to see if there is a significant difference in caffeine levels across the three roasts.
  3. If the ANOVA is significant, perform a Tukey HSD post-hoc test to determine exactly which roast levels differ from one another.
  4. Print the final Tukey results to the console.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.