import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats
from scipy.stats import kstest,normaltest,levene
from scipy.stats import ttest_1samp,ttest_ind,ttest_rel,mannwhitneyu,kruskal
from statsmodels.stats.multicomp import pairwise_tukeyhsd,MultiComparison
from statsmodels.formula.api import ols
from statsmodels.stats.anova import anova_lm
def one_way_anova(data,alpha,ndigits):
#Define empty dataframe to save normal test and T test results
Result1 = pd.DataFrame(columns=['t-normal','p_normal'])
Result2 = pd.DataFrame(columns=['t-ftest','p_ftest'])
for i in data.columns:
#Normal_test
NormaltestResult = normaltest(data[i].dropna(), axis=None)
Result1.loc[i] = NormaltestResult
Result1 = round(Result1,ndigits)
#F_test
Ftest_result = levene(data['A1'].dropna(),data['A2'].dropna(),data['A3'].dropna())
Result2.loc[i] = Ftest_result
Result2 = round(Result2,ndigits)
#ANOVA
data_new = data.stack().reset_index().rename(columns={0:'value'}).iloc[:,1:]
data_new.columns = ['type','value']
formula = '{} ~ {}'.format(data_new.columns[1], data_new.columns[0])
model = ols(formula, data_new).fit()
anova_result = anova_lm(model)
Result3 = round(anova_result,ndigits)
mc = MultiComparison(data_new['value'], data_new['type'])
result = mc.tukeyhsd()
print(result)
return Result3
one_way_anova(df0,0.05,4)
#Oneway ANOVA package implement
from scipy.stats import f_oneway
f_oneway(df0['A1'].dropna(), df0['A2'].dropna(),df0['A3'].dropna())
2021-01-27
最新推荐文章于 2024-06-01 21:43:54 发布