Semester Finale!
This is our last chapter. We are now upgrading from comparing two means (T-Tests) to comparing multiple means simultaneously using ANOVA.
Today we build the full ANOVA toolkit: understand the F-distribution, run a one-way ANOVA from sums of squares, report the ANOVA table, interpret decisions, and follow up with Tukey or the nonparametric Kruskal–Wallis test.
Scenario: We ask 30 people to rate coffee acidity (0–100).
10 people drink Light, 10 Medium, 10 Dark.
We calculate the average scores:
“Are these taste differences statistically significant, or could they have happened by random chance?”
Why can't we just run t‑tests for Light vs Medium, Medium vs Dark, etc.?
Every time you run a test, you risk a Type I Error (false alarm) of 5%.
Error risk jumps from 5% to ~14%!
Solution: ANOVA tests all groups simultaneously with a single error rate.
How different are the group averages from each other?
Signal: group means are sending different messages.
Large between-group variation means the group centers are separated enough to notice.
How spread out is the data inside each specific group?
Noise: individual scores vary even inside the same group.
Large within-group variation makes true group differences harder to hear.
If the within-group spread suddenly doubled but the group means stayed in the same places, what would happen to \(F\)?
Be ready to explain it using the words signal, noise, numerator, and denominator.
Translating the statistical model into R syntax using aov().
~ symbol (tilde) represents the equals sign in the formula.aov() for standard ANOVA to mirror the linear model
structure.
Simulated F-Curve Shape
Observation:
Notice how the peak shifts and the tail thickness changes. As \(df\) increases, the curve becomes less
skewed and more symmetric (approaching Normal).
# Critical value for alpha = 0.05
qf(p = 0.95, df1 = 2, df2 = 6)
# P-value from an observed F-statistic
1 - pf(1.69, df1 = 2, df2 = 6)
# Draw an F-curve
curve(df(x, df1 = 2, df2 = 6), from = 0, to = 6)
abline(v = qf(0.95, 2, 6), col = "red", lty = 2)
Treatment Mean Square (Signal): \(\mathrm{MSTR} = \frac{\mathrm{SSTR}}{k-1}\)
Error Mean Square (Noise): \(\mathrm{MSE} = \frac{\mathrm{SSE}}{n-k}\)
F-Statistic: \( \displaystyle F = \frac{\mathrm{MSTR}}{\mathrm{MSE}} \)
Suppose two classes have very different average exam scores, but each class also has huge student-to-student variability. Should the \(F\)-statistic necessarily be large?
Answer in one sentence: what has to be true about \(\mathrm{MSTR}\) compared with \(\mathrm{MSE}\)?
If Between variation is large compared to Within variation, the groups are likely different (Large F).
F-Stat estimate: Low
Defining: \( \sum (x_i - \bar{x})^2 \)
Computing: \( \sum x_i^2 - \dfrac{(\sum x_i)^2}{n} \)
Defining: \( \sum n_j(\bar{x}_j - \bar{x})^2 \)
Computing: \( \sum \dfrac{T_j^2}{n_j} - \dfrac{(\sum x_i)^2}{n} \)
Defining: \( \sum (n_j - 1)s_j^2 \) Computing: \( \mathrm{SST} - \mathrm{SSTR} \)
Place each quantity into the ANOVA table before seeing the table: \(\mathrm{SSTR}\), \(\mathrm{SSE}\), \(k-1\), \(n-k\), \(\mathrm{MSTR}\), \(\mathrm{MSE}\).
Which row measures signal? Which row measures noise?
| Source | df | SS (Sum of Squares) | MS (Mean Square) | F-statistic |
|---|---|---|---|---|
| Treatment | \(k-1\) | \(\mathrm{SSTR}\) | \(\mathrm{MSTR}=\mathrm{SSTR}/(k-1)\) | \(F=\mathrm{MSTR}/\mathrm{MSE}\) |
| Error | \(n-k\) | \(\mathrm{SSE}\) | \(\mathrm{MSE}=\mathrm{SSE}/(n-k)\) | |
| Total | \(n-1\) | \(\mathrm{SST}\) |
As Artificial Intelligence continues to evolve rapidly, major tech companies are fiercely competing for developer adoption. A leading tech publication wants to determine if there is a statistically significant difference in the coding capabilities of three flagship models: Claude 3.5, ChatGPT-4o, and Gemini 1.5.
They subjected each model to a rigorous battery of 8 complex algorithmic challenges. The output was evaluated by senior engineers on a scale of 0 to 100 based on efficiency, accuracy, and code readability. Is one model truly superior, or are the observed differences merely random statistical noise? Let's use ANOVA to find out!
92, 94, 91, 95, 93, 93, 94, 92
Mean \(\bar{x}_1 = 93\)88, 90, 87, 89, 91, 89, 88, 90
Mean \(\bar{x}_2 = 89\)85, 87, 86, 88, 84, 86, 85, 87
Mean \(\bar{x}_3 = 86\)Grand Mean \(\bar{\bar{x}} = \frac{93+89+86}{3} = 89.33\)
Hypotheses: \(H_0: \mu_{C} = \mu_{Chat} = \mu_{G}\) vs. \(H_a:\) At least one differs.
How much does each score deviate from its own model's mean?
| Model (\(\bar{x}_i\)) | Raw Scores (\(x\)) | Deviations \((x - \bar{x}_i)\) | Squared \((x - \bar{x}_i)^2\) | Sum |
|---|---|---|---|---|
| Claude (93) | 92, 94, 91, 95, 93, 93, 94, 92 | -1, 1, -2, 2, 0, 0, 1, -1 | 1, 1, 4, 4, 0, 0, 1, 1 | 12 |
| ChatGPT (89) | 88, 90, 87, 89, 91, 89, 88, 90 | -1, 1, -2, 0, 2, 0, -1, 1 | 1, 1, 4, 0, 4, 0, 1, 1 | 12 |
| Gemini (86) | 85, 87, 86, 88, 84, 86, 85, 87 | -1, 1, 0, 2, -2, 0, -1, 1 | 1, 1, 0, 4, 4, 0, 1, 1 | 12 |
| Total Error Sum of Squares (SSE): | 36 | |||
How much does each model's mean deviate from the Grand Mean (\(\bar{\bar{x}} = 89.33\))?
| Model | \(n\) | Model Mean (\(\bar{x}_i\)) | Deviation \((\bar{x}_i - \bar{\bar{x}})\) | Squared \((\bar{x}_i - \bar{\bar{x}})^2\) | Weighted \(n(\bar{x}_i - \bar{\bar{x}})^2\) |
|---|---|---|---|---|---|
| Claude | 8 | 93 | 3.667 | 13.444 | 107.55 |
| ChatGPT | 8 | 89 | -0.333 | 0.111 | 0.89 |
| Gemini | 8 | 86 | -3.333 | 11.111 | 88.89 |
| Total Treatment Sum of Squares (SSTR): | 197.33 | ||||
| Source | df | SS | MS | F |
|---|---|---|---|---|
| Treatment (Between) | \(k-1 = 3-1 = 2\) | \(197.33\) | \(\frac{197.33}{2} = 98.67\) | \(\frac{98.67}{1.71} = 57.56\) |
| Error (Within) | \(n-k = 24-3 = 21\) | \(36.00\) | \(\frac{36}{21} = 1.71\) | |
| Total | \(n-1 = 23\) | \(233.33\) |
Our F-Statistic is 57.56.
Looking at the F-distribution for \(df=(2, 21)\) with \(\alpha=0.05\), the critical value is \(F_{crit} = 3.47\).
Compare exam scores across three study designs: Traditional, Online, and Peer-to-Peer.
Hypotheses: \(H_0:\mu_1=\mu_2=\mu_3\); \(H_a:\) at least one mean differs.
x1 <- c(75, 82, 68)
x2 <- c(90, 78, 85)
x3 <- c(88, 92, 80)
Score <- c(x1, x2, x3)
Method <- factor(rep(c("Traditional", "Online", "Peer"), each = 3))
scores_data <- data.frame(Score, Method)
model <- aov(Score ~ Method, data = scores_data)
summary(model)
boxplot(Score ~ Method, data = scores_data)
\(\eta^2\) is the proportion of total variation explained by the group factor. A small p-value says an effect is statistically detectable; \(\eta^2\) says how much variation the factor accounts for.
model_stats <- summary(model)
eta_squared <- model_stats[[1]]["Method", "Sum Sq"] /
sum(model_stats[[1]][, "Sum Sq"])
eta_squared
Purpose: After ANOVA shows not all means are equal, determine which pairs differ.
If ANOVA rejects \(H_0\), can we immediately say that every pair of group means is different?
Write the correct conclusion in context, then name the follow-up tool we use to compare pairs.
A nonparametric alternative to one-way ANOVA when normality is doubtful but group shapes are similar.
If the \(k\) distributions have the same shape and \(H_0\) (equal means) is true, then \(K\) is approximately \( \chi^2 \) with df \(k-1\).
Recovery times are skewed, so we compare three treatment groups using ranks instead of raw means.
treat_A <- c(5, 7, 6, 8, 4)
treat_B <- c(12, 9, 11, 8, 10)
treat_C <- c(6, 8, 7, 9, 5)
recovery <- c(treat_A, treat_B, treat_C)
groups <- factor(rep(c("A", "B", "C"), each = 5))
kruskal.test(recovery ~ groups)
boxplot(recovery ~ groups)
Three groups (A, B, C) each with n=5 observations. Reported summaries are:
Group A: \(\bar x_1=10\), \(s_1=1.2\) |
Group B: \(\bar x_2=13\), \(s_2=1.2\) |
Group C: \(\bar x_3=12\), \(s_3=1.2\).
Test \(H_0:\mu_1=\mu_2=\mu_3\) at \(\alpha=0.05\) using one-way ANOVA (from summary stats).
No aov(), no external libraries. Enter group sizes, means, and sds; the code mirrors the
hand-calculation steps.
# ---- One-way ANOVA from summary statistics (no aov, no libraries) ----
# Inputs: group sizes n_j, sample means xbar_j, sample sds s_j
n <- c(5, 5, 5)
xbar <- c(10, 13, 12)
s <- c(1.2, 1.2, 1.2)
alpha <- 0.05
# 1) Totals and grand mean
N <- sum(n)
k <- length(n)
xG <- sum(n * xbar) / N
# 2) Sums of squares (defining formulas)
SSTR <- sum( n * (xbar - xG)^2 )
SSE <- sum( (n - 1) * (s^2) )
SST <- SSTR + SSE # identity check
# 3) Degrees of freedom
df1 <- k - 1 # treatment
df2 <- N - k # error
# 4) Mean squares
MSTR <- SSTR / df1
MSE <- SSE / df2
# 5) F statistic, p-value, and critical value
F0 <- MSTR / MSE
pval <- 1 - pf(F0, df1, df2) # upper-tail area
Fcrit <- qf(1 - alpha, df1, df2)
# 6) Optional effect size (eta-squared)
eta2 <- SSTR / SST
# 7) Report the usual values
N; k; xG
SSTR; SSE; SST
df1; df2; MSTR; MSE
F0; pval; Fcrit
eta2
n, xbar, and s with your own summary
statistics to mirror the same hand-steps used in class.
No aov(), no external libraries. Put each group’s numeric vector into the list. Add/remove items
to change the number of groups (works for 2+ groups).
# ---- One-way ANOVA from raw data (math-only, no aov, no libraries) ----
# Put each group's numeric vector in the list below. Add/remove items = change number of groups.
groups <- list(
A = c(10, 9, 11, 10, 10),
B = c(13, 14, 12, 13, 13),
C = c(12, 12, 12, 11, 13),
D = c( 9, 10, 8, 10, 9)
)
alpha <- 0.05
# 1) Create summary stats for each group (n_j, xbar_j, s_j)
n <- sapply(groups, length)
xbar <- sapply(groups, mean)
s <- sapply(groups, sd) # sample sd (denominator n-1)
# 2) Totals and grand mean
N <- sum(n)
k <- length(n)
xG <- sum(n * xbar) / N
# 3) Sums of squares (defining formulas)
SSTR <- sum( n * (xbar - xG)^2 )
SSE <- sum( (n - 1) * (s^2) )
SST <- SSTR + SSE
# 4) Degrees of freedom
df1 <- k - 1
df2 <- N - k
# 5) Mean squares
MSTR <- SSTR / df1
MSE <- SSE / df2
# 6) F-statistic, p-value, critical value
F0 <- MSTR / MSE
pval <- 1 - pf(F0, df1, df2)
Fcrit <- qf(1 - alpha, df1, df2)
# 7) Optional effect size
eta2 <- SSTR / SST
# 8) Values to read off
N; k; xbar; s; xG
SSTR; SSE; SST
df1; df2; MSTR; MSE
F0; pval; Fcrit; eta2
groups.
The code automatically adapts the number of groups and mirrors the hand-calculation workflow.