14.6 ANOVA (Analysis of Variance)
One-Way ANOVA
ANOVA (analysis of variance) can be used to test whether the means of more than two groups are statistically different from each other.
Example: The data set below (URL) contains the students test1_score, gender, level, section etc.
- Write a program to read the data set and display the first and last five rows.
- Write a program to find
test1_scoremeans, std and size for each level
my_csv_file.csv
import pandas as pd
url='my_csv_file.csv'
x_df = pd.read_csv(url)
x_df # print the first and last five rows
| id | gender | section | time | level | test1_score | test2_score | test3_score | |
|---|---|---|---|---|---|---|---|---|
| 0 | 1 | female | C | 17.3 | low | 65.4 | 84.3 | 51.9 |
| 1 | 2 | female | D | 5.1 | low | 72.4 | 86.1 | 52.0 |
| 2 | 3 | female | A | 2.4 | low | 73.8 | 84.2 | 53.1 |
| 3 | 4 | female | A | 8.8 | low | 70.2 | 87.9 | 52.0 |
| 4 | 5 | male | B | 15.0 | low | 60.3 | 81.4 | 55.5 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 175 | 176 | male | B | 13.2 | high | 78.8 | 89.3 | 52.5 |
| 176 | 177 | female | B | 12.6 | high | 76.5 | 83.4 | 50.0 |
| 177 | 178 | male | D | 8.4 | high | 78.1 | 86.3 | 51.0 |
| 178 | 179 | male | C | 9.5 | high | 76.6 | 86.6 | 52.4 |
| 179 | 180 | male | C | 16.2 | high | 83.1 | 89.4 | 54.1 |
x_df.groupby(['level']).agg({'test1_score': ['mean', 'std','size']})
| test1_score | |||
|---|---|---|---|
| mean | std | size | |
| level | |||
| high | 79.613333 | 3.617306 | 60 |
| low | 68.775000 | 4.350282 | 60 |
| medium | 74.841667 | 3.638881 | 60 |
Example: The data set below (URL) contains the students test1_score, gender, level, section etc.
- Write a program to perform ANOVA to test if there is sufficient evidence to indicate a difference in the averages of
test1_scoresamong the threelevels(low, medium, high). Use 10% level of significance.
my_csv_file.csv
import statsmodels.api as sm
from statsmodels.formula.api import ols
model = ols('test1_score ~ C(level)',data = x_df).fit()
table = sm.stats.anova_lm(model,type=2) # type 2 for equal or non-equal sample sizes
print(table)
Show expected output
df sum_sq mean_sq F PR(>F)
C(level) 2.0 3540.854333 1770.427167 117.372972 3.555808e-33
Residual 177.0 2669.827667 15.083772 NaN NaNInterpretation: Since the p-value 3.555808e-33 < = 0.10, reject the null hypothesis that the averages for all three levels are equal. That is, there is a significant difference between the averages of test1_score of the three levels. The sum of squares residual is 2669.827667. The sum of squares treatment (level) is 3540.854333.
Adapted from Python for Introductory Statistics, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.