# Lecture 18: Analysis of Variance (ANOVA)

## Statistics / Biostatistics

---

## Learning Objectives

By the end of this lecture, students will be able to:

1. Explain the rationale for ANOVA over multiple t-tests
2. State the assumptions and hypotheses of one-way ANOVA
3. Partition total variability into between-group and within-group components
4. Interpret the F-statistic and ANOVA table
5. Perform post-hoc multiple comparison procedures
6. Describe extensions to two-way ANOVA and repeated measures

---

## Lecture Content

### I. Why ANOVA Instead of Multiple t-Tests?

When comparing more than two groups, performing all pairwise t-tests inflates the Type I error rate. With k = 3 groups there are 3 pairwise comparisons, and with k = 5 there are 10. If each test uses alpha = 0.05, the family-wise error rate quickly exceeds 0.05. For example, with 10 comparisons at alpha = 0.05, the probability of at least one Type I error is 1 - 0.95^10 = 0.40.

ANOVA solves this problem by testing all group means simultaneously in a single test. The null hypothesis is that all group means are equal (mu_1 = mu_2 = ... = mu_k), while the alternative hypothesis is that at least one group mean differs from the others.

### II. The Logic of ANOVA: Partitioning Variability

ANOVA works by decomposing the total variability in the data into two components. **Between-group variability (SSB)** captures the variation due to differences among group means. **Within-group variability (SSW)** captures the variation due to individual differences within groups. The total sum of squares equals the sum of these components: SS_Total = SS_Between + SS_Within.

If the group means are truly different, SS_Between will be large relative to SS_Within. The **F-statistic** quantifies this ratio: F = MS_Between / MS_Within, where MS_Between = SS_Between / (k - 1) and MS_Within = SS_Within / (N - k), with k being the number of groups and N the total sample size.

<image>A visual explanation of ANOVA partitioning. Three groups (A, B, C) are shown as dot plots on a number line. Panel A: The grand mean is marked with a horizontal line across all groups. Arrows from each group mean to the grand mean represent "between-group variability." Panel B: Arrows from individual data points to their group mean represent "within-group variability." Panel C: A pie chart showing the total variance split into between-group (SS_Between) and within-group (SS_Within) components.</image>

### III. One-Way ANOVA: Assumptions

Three assumptions underlie one-way ANOVA. **Independence** requires that observations are independent within and between groups. **Normality** requires that data within each group are approximately normally distributed, which can be checked with Q-Q plots or the Shapiro-Wilk test within each group. ANOVA is robust to moderate violations of normality, especially with equal group sizes. **Homogeneity of variances (homoscedasticity)** requires that all groups have approximately equal population variances. This can be checked with Levene's test or Bartlett's test, with a rule of thumb that the largest SD divided by the smallest SD should be less than 2. If this assumption is violated, Welch's ANOVA, which does not assume equal variances, should be used instead.

### IV. The ANOVA Table

| Source | SS | df | MS | F |
|--------|-----|-----|-----|---|
| Between groups | SS_B | k - 1 | SS_B / (k-1) | MS_B / MS_W |
| Within groups (Error) | SS_W | N - k | SS_W / (N-k) | |
| Total | SS_T | N - 1 | | |

The F-distribution is a right-skewed distribution defined by two degrees of freedom parameters (df1 = k-1, df2 = N-k). A large F value produces a small p-value, leading to rejection of H0. An F value near 1 suggests that group means are similar, because the variability between groups is comparable to the variability within groups.

### V. Worked Example

Three drug treatments are compared on a pain score scale (0-10). Drug A (n=15) has x-bar = 4.2, s = 1.8; Drug B (n=15) has x-bar = 5.8, s = 2.0; Drug C (n=15) has x-bar = 5.0, s = 1.9. The overall mean is 5.0. SS_Between = 15[(4.2-5.0)^2 + (5.8-5.0)^2 + (5.0-5.0)^2] = 15[0.64 + 0.64 + 0] = 19.2. SS_Within (from pooled within-group variability) = (n-1)(s1^2 + s2^2 + s3^2) = 14(3.24 + 4.00 + 3.61) = 151.9. The F-statistic is (19.2/2) / (151.9/42) = 9.6 / 3.617 = 2.654. The critical value F_(0.05, 2, 42) is approximately 3.22. Since F = 2.654 < 3.22, we fail to reject H0 and conclude there is no significant difference among the three drugs.

### VI. Post-Hoc Multiple Comparisons

If ANOVA rejects H0, follow-up pairwise comparisons are needed to identify which groups differ. Several methods control the family-wise error rate. **Tukey's HSD (Honestly Significant Difference)** is best for all pairwise comparisons with equal sample sizes. **Bonferroni correction** divides alpha by the number of comparisons; it is conservative but simple to apply. **Scheffe's method** is the most conservative and allows all possible contrasts, not just pairwise ones. **Dunnett's test** compares each treatment to a single control group. **Planned contrasts** (a priori) are specified before data collection and do not require adjustment if limited in number. The method used should always be stated and justified.

<image>A results figure from a one-way ANOVA with post-hoc testing. A bar chart shows three group means with error bars (95% CIs). Horizontal brackets above the bars connect pairs of groups, with asterisks indicating significant pairwise differences (e.g., * p < 0.05, ** p < 0.01, ns = not significant). The ANOVA F-statistic and overall p-value are displayed above the figure. A legend indicates the post-hoc method used (e.g., Tukey's HSD).</image>

### VII. Effect Size for ANOVA

**Eta-squared (eta^2)** is the proportion of total variance explained by the grouping variable, calculated as eta^2 = SS_Between / SS_Total. It is analogous to R^2 in regression. Benchmarks suggest 0.01 is small, 0.06 is medium, and 0.14 is large. **Partial eta-squared** is used in multi-factor designs. **Omega-squared (omega^2)** provides a less biased estimate of explained variance, calculated as omega^2 = (SS_Between - (k-1)*MS_Within) / (SS_Total + MS_Within).

### VIII. Extensions

**Two-way ANOVA** examines two factors simultaneously and their interaction. For example, one might study the effects of treatment (3 levels) and sex (2 levels) on blood pressure. This design tests three hypotheses: the main effect of factor A, the main effect of factor B, and the interaction between A and B.

**Repeated measures ANOVA** is used when the same subjects are measured at multiple time points. It accounts for within-subject correlation and relies on the assumption of sphericity (similar variance of differences between all pairs of time points), which can be tested with Mauchly's test. If sphericity is violated, Greenhouse-Geisser or Huynh-Feldt corrections are applied.

**ANCOVA (Analysis of Covariance)** combines ANOVA with a continuous covariate, adjusting group comparisons for a confounding variable such as comparing treatment effects while controlling for baseline severity.

<image>A two-way ANOVA interaction plot. The x-axis shows Factor A (e.g., Treatment: Drug vs. Placebo). The y-axis shows the outcome (mean pain score). Two lines represent Factor B (e.g., Sex: Male vs. Female). Panel A: Parallel lines indicate no interaction -- both sexes respond similarly to treatment. Panel B: Crossing lines indicate a significant interaction -- the treatment effect differs by sex. Each panel is annotated with the interpretation and the relevant F-test results.</image>

---
