Lesson 18 of 18
One-Way ANOVA
One-Way ANOVA
Analysis of Variance (ANOVA) tests whether the means of three or more groups are equal. It works by comparing the variance between groups to the variance within groups.
Hypotheses:
- : all group means are equal ()
- : at least one group mean differs
The F-Statistic
where:
- = number of groups, = total observations
- — variation due to group differences
- — variation within groups
A large means the groups differ more than random variation would explain.
Example
Groups: , ,
| Source | SS | df | MS | F |
|---|---|---|---|---|
| Between | 54 | 2 | 27 | 27.0 |
| Within | 6 | 6 | 1 |
Implementation
def anova_f(groups):
n_total = sum(len(g) for g in groups)
k = len(groups)
grand_mean = sum(sum(g) for g in groups) / n_total
group_means = [sum(g)/len(g) for g in groups]
ss_between = sum(len(g)*(m - grand_mean)**2
for g, m in zip(groups, group_means))
ss_within = sum((x - m)**2
for g, m in zip(groups, group_means)
for x in g)
ms_between = ss_between / (k - 1)
ms_within = ss_within / (n_total - k)
return round(ms_between / ms_within, 4)
Your Task
Implement anova_f(groups) that computes the F-statistic from a list of groups (each a list of numbers).
Pyodide loading...
Loading...
Click "Run" to execute your code.