In data analysis, it’s common to ask whether one factor influences an outcome — for example, does the quarter of the year affect sales? That’s where one-way ANOVA comes in. But business problems are rarely that simple. Sales may not only depend on the quarter but also on other factors, like region, marketing strategy, or product category.
If we only look at one factor, we risk missing the bigger picture. That’s where two-way ANOVA becomes useful. It lets us test the effect of two independent factors at the same time, and more importantly, whether there’s an interaction between them.
Take our sales example:
Using two-way ANOVA, we can analyze:
Let's extend the same quarterly sales example we used in one-way ANOVA. Previously, we tested whether sales varied across quarters. Now, we will introduce a second factor "region", to explore whether sales patterns differ not just by quarter, but also by geography, and whether the two factors interact.
We’ll simulate sales data for:
Each row will have:
Python Code for Two-Way ANOVA
sum_sq (Sum of Squares)
Measures the variation in sales explained by each factor (Quarter, Region, Interaction).
Larger values mean that factor explains more of the variation in sales.
df (Degrees of Freedom)
Related to how many groups or categories the factor has.
Example: 4 quarters → 3 degrees of freedom (always groups − 1).
F (F-statistic)
Ratio of the variation explained by the factor to the unexplained variation (error).
Higher F = stronger evidence that the factor significantly affects the outcome.
PR(>F) (p-value)
Probability of seeing these results if the null hypothesis (no effect) were true.
Small p-value (usually < 0.05) → the factor has a statistically significant effect.
Residual
The leftover variation in sales that can’t be explained by Quarter, Region, or their interaction.
Think of it as “random noise” or unmeasured factors.
Let’s break down the result of Two-Way ANOVA test step by step in simple terms:
1. C(Quarter)
2. C(Region)
3. C(Quarter):C(Region) (Interaction effect)
4. Residual (Error)
Overall, both Quarter and Region individually affect sales. But the combination of Quarter and Region doesn’t matter much — sales differences between quarters look roughly the same in all regions.