python annova sample size estimation code example
Example 1: how to performe anova on grouped variable in python
import scipy.stats as stats
fvalue, pvalue = stats.f_oneway(d['A'], d['B'], d['C'], d['D'])
print(fvalue, pvalue)
import statsmodels.api as sm
from statsmodels.formula.api import ols
d_melt = pd.melt(d.reset_index(), id_vars=['index'], value_vars=['A', 'B', 'C', 'D'])
d_melt.columns = ['index', 'treatments', 'value']
model = ols('value ~ C(treatments)', data=d_melt).fit()
anova_table = sm.stats.anova_lm(model, typ=2)
anova_table
| | df | sum_sq | mean_sq | F | PR(>F) |
|---------------|--------|---------|----------|----------|----------|
| C(treatments) | 3.0 | 3010.95 | 1003.650 | 17.49281 | 0.000026 |
| Residual | 16.0 | 918.00 | 57.375 | NaN | NaN |
Example 2: python anova
import pandas as pd
d = pd.read_csv("https://reneshbedre.github.io/assets/posts/anova/onewayanova.txt", sep="\t")
d.boxplot(column=['A', 'B', 'C', 'D'], grid=False)