One-way ANOVA

Using SciPy to compare multiple groups with one-way ANOVA

Python
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt

# Generate sample data
np.random.seed(0)
group1 = np.random.normal(loc=5, scale=1, size=30)
group2 = np.random.normal(loc=5.5, scale=1, size=30)
group3 = np.random.normal(loc=6, scale=1, size=30)

# Perform one-way ANOVA
f_statistic, p_value = stats.f_oneway(group1, group2, group3)

print(f"F-statistic: {f_statistic}")
print(f"P-value: {p_value}")

# Visualize the data
plt.figure(figsize=(10, 6))
plt.boxplot([group1, group2, group3], labels=['Group 1', 'Group 2', 'Group 3'])
plt.title("Comparison of Three Groups")
plt.ylabel("Values")
plt.show()

# Interpret the results
alpha = 0.05
if p_value < alpha:
    print("Reject the null hypothesis: There are significant differences among the groups.")
else:
    print("Fail to reject the null hypothesis: There are no significant differences among the groups.")