Analysis of variance (ANOVA) tests whether group means differ by comparing variation explained by a categorical variable with residual variation. Here, it evaluates whether distributions of h(L, C, T) change across cost levels, as required by an independence constraint. section 4.5.3
A practical ANOVA workflow connects visualization, model fitting, and hypothesis testing:
- Group the outcome by the categorical variable and inspect overlapping distributions with a box plot. section 4.5.3
- Fit an ordinary least-squares model such as h_func ~ C, then obtain the ANOVA table with an F-test. section 4.5.3
- Interpret PR(>F) as the probability of observing an F-statistic at least this large under the independence null hypothesis. section 4.5.3
- A p-value near 0.1876 for cost does not reject independence, while smaller values for treatment and location indicate dependence. section 4.5.3
Listing 4.18 Using ANOVA to evaluate independence
from statsmodels.formula.api import ols
import statsmodels.api as sm
model = ols('h_func ~ C', data=df_mod).fit() #1
aov_table = sm.stats.anova_lm(model, typ=2) #1
print(aov_table["PR(>F)"]["C"]) #1
model = ols('h_func ~ T', data=df_mod).fit() #2
aov_table = sm.stats.anova_lm(model, typ=2) #2
print(aov_table["PR(>F)"]["T"]) #2
model = ols('h_func ~ L', data=df_mod).fit() #3
aov_table = sm.stats.anova_lm(model, typ=2) #3
print(aov_table["PR(>F)"]["L"]) #3
Inspect executable statsmodels code that fits separate ordinary least-squares models and extracts ANOVA p-values for C, T, and L. section 4.5.3


