Two-Way ANOVA: A Comprehensive Guide with Examples
What is Two-Way ANOVA?
Two-way ANOVA (Analysis of Variance) is a statistical method used to examine the effects of two independent variables (factors) on a continuous dependent variable, as well as their interaction. It's widely used in experimental designs and data analysis to determine if differences between group means are statistically significant.
- Main Effects: The independent impact of each factor on the dependent variable.
- Interaction Effect: Whether the effect of one factor depends on the level of the other factor.
When to Use Two-Way ANOVA?
Two-way ANOVA is appropriate when your research design includes:
- Two independent variables (factors) that are categorical (e.g., gender and diet type).
- A continuous dependent variable (e.g., weight loss, test scores).
- Independent groups within each factor (no overlap).
Example
You're studying how diet type (low-carb vs. low-fat) and exercise intensity (low vs. high) affect weight loss. Both factors may independently impact weight loss, and their combination may have an interaction effect.
Assumptions of Two-Way ANOVA
Before conducting a two-way ANOVA, it's important to ensure that the following assumptions are met:
1. Normality
- The dependent variable should be approximately normally distributed within each group
- Can be checked using:
- Q-Q plots
- Shapiro-Wilk test
- Histogram visualization
2. Homogeneity of Variance
- Variances should be similar across all groups
- Can be tested using:
- Levene's test
- Bartlett's test
- If violated, consider transforming data or using non-parametric alternatives
3. Independence
- Observations must be independent within and between groups
- Ensure:
- Random sampling
- No repeated measurements
- No systematic relationships between observations
Weight Loss Study: Step-by-Step Guide
Let's analyze a study examining how diet type (Low-fat vs. High-fat) and exercise (Yes vs. No) affect weight loss in pounds.
1. Define Your Hypotheses
Null Hypotheses
- Main Effect A: No effect of diet type on weight loss
- Main Effect B: No effect of exercise on weight loss
- Interaction: No interaction between diet type and exercise
Alternative Hypotheses
2. Collect and Organize Data
Diet | Exercise | Weight Loss (pounds) |
---|---|---|
Low-fat | Yes | 8 |
Low-fat | Yes | 10 |
Low-fat | Yes | 9 |
Low-fat | No | 6 |
Low-fat | No | 7 |
Low-fat | No | 8 |
High-fat | Yes | 5 |
High-fat | Yes | 7 |
High-fat | Yes | 6 |
High-fat | No | 3 |
High-fat | No | 4 |
High-fat | No | 5 |
3. Check Assumptions
For this tutorial, we will assume that all assumptions for a two-way ANOVA have been met:
- Independence of observations
- Normal distribution of residuals
- Homogeneity of variances
- No significant outliers
4. Perform Two-Way ANOVA
By using our Two-Way ANOVA Calculator, we can perform the analysis and obtain the following results:
Interaction Plot
Parallel lines suggest no interaction between diet and exercise.
5. Interpretation
1. Main Effect of Diet (p = 0.0008):
There is a significant effect of diet type on weight loss. The low-fat diet resulted in significantly greater weight loss compared to the high-fat diet.
2. Main Effect of Exercise (p = 0.0085):
Exercise has a significant effect on weight loss. Participants who exercised lost significantly more weight than those who didn't.
3. Interaction Effect (p = 1):
There is no significant interaction between diet and exercise. This means the effect of exercise on weight loss is similar regardless of diet type, and vice versa.
Example Report
A two-way ANOVA revealed that diet type significantly affects weight loss (F(1,8) = 27.00, p < 0.001), as does exercise (F(1,8) = 12.00, p = 0.009). The interaction between diet and exercise was not significant (F(1,8) = 0.00, p = 1.000). These results suggest that both diet and exercise independently influence weight loss, with no interaction between the factors. Specifically, the low-fat diet resulted in greater weight loss (M = 8.00, SD = 1.41) compared to the high-fat diet (M = 5.00, SD = 1.41), and exercise led to greater weight loss (M = 7.50, SD = 1.87) compared to no exercise (M = 5.50, SD = 1.87).
Implementation Examples
Python Implementation:
1import pandas as pd
2import numpy as np
3from scipy import stats
4import statsmodels.api as sm
5from statsmodels.formula.api import ols
6
7# Create DataFrame
8data = pd.DataFrame({
9 'Diet': ['Low-fat']*6 + ['High-fat']*6,
10 'Exercise': ['Yes']*3 + ['No']*3 + ['Yes']*3 + ['No']*3,
11 'Weight_Loss': [8, 10, 9, 6, 7, 8, 5, 7, 6, 3, 4, 5]
12})
13
14# Fit the model
15model = ols('Weight_Loss ~ C(Diet) + C(Exercise) + C(Diet):C(Exercise)', data=data).fit()
16
17# Perform ANOVA
18anova_table = sm.stats.anova_lm(model, typ=2)
19print("ANOVA Results:")
20print(anova_table)
21
22# Calculate means for each group
23print("Group Means:")
24print(data.groupby(['Diet', 'Exercise'])['Weight_Loss'].mean())
R Implementation:
1library(tidyverse)
2
3# Create the dataset
4data <- tibble(
5 Diet = rep(c("Low-fat", "High-fat"), each = 6),
6 Exercise = rep(rep(c("Yes", "No"), each = 3), 2),
7 Weight_Loss = c(8, 10, 9, 6, 7, 8, 5, 7, 6, 3, 4, 5)
8)
9
10# Perform two-way ANOVA
11model <- aov(Weight_Loss ~ Diet * Exercise, data = data)
12
13# View results
14summary(model)
15
16# Calculate means
17group_means <- data %>%
18 group_by(Diet, Exercise) %>%
19 summarise(
20 mean_weight_loss = mean(Weight_Loss),
21 .groups = 'drop'
22 )
23
24print(group_means)
25
26# Create interaction plot
27interaction.plot(
28 data$Exercise,
29 data$Diet,
30 data$Weight_Loss,
31 type = "b",
32 xlab = "Exercise",
33 ylab = "Weight Loss",
34 main = "Interaction Plot"
35)
Wrapping Up
Two-way ANOVA is a powerful statistical tool that allows researchers to examine the effects of two independent variables simultaneously, along with their potential interaction. Let's recap the key points:
- Main Effects: Two-way ANOVA helps identify the individual impact of each independent variable on the dependent variable
- Interaction Effects: It reveals whether the effect of one variable depends on the levels of the other variable
- Assumptions: Remember to check for normality, homogeneity of variance, and independence before conducting the analysis
- Interpretation: Focus on both statistical significance (p-values) and practical significance (effect sizes) when drawing conclusions
Pro Tips
- Always visualize your data using interaction plots to better understand relationships
- Consider post-hoc tests when significant effects are found
- Report effect sizes alongside p-values for a more complete analysis
- When assumptions are violated, consider non-parametric alternatives or data transformations
Additional Resources
Help us improve
Found an error or have a suggestion? Let us know!