Page1.jpg

Page2.jpg
Page3.jpg
Page4.jpg
Page5.jpg

代码练习部分

from scipy import stats
from statsmodels.formula.api import ols
from statsmodels.stats.anova import anova_lm
from statsmodels.stats.multicomp import pairwise_tukeyhsd
import warnings
warnings.filterwarnings("ignore")
import numpy as np
import pandas as pd
import itertools

if __name__ == '__main__':
    #生成数据
    df = {'ctl': list(np.random.normal(10, 5, 100)),
          'treat1': list(np.random.normal(15, 5, 100)), \
          'treat2': list(np.random.normal(20, 5, 100)), \
          'treat3': list(np.random.normal(30, 5, 100)), \
          'treat4': list(np.random.normal(31, 5, 100))}
    df = pd.DataFrame(df)
    print(df.head())
    #转换数据
    df_melt = df.melt()
    df_melt.columns = ['Treat', 'Value']
    print(df_melt.head())
    #调用模型方差分析
    model = ols('Value~C(Treat)', data=df_melt).fit()
    anova_table = anova_lm(model, typ=2)
    print(anova_table)
    #多重检验
    from statsmodels.stats.multicomp import MultiComparison
    mc = MultiComparison(df_melt['Value'], df_melt['Treat'])
    tukey_result = mc.tukeyhsd(alpha=0.5)
    print(tukey_result)

标签: none

添加新评论