习题9.4
点击查看代码
import numpy as np
import pandas as pd
import scipy.stats as ss
import statsmodels.api as sm
data = np.loadtxt('F:\python数学建模与算法\源程序\《Python数学建模算法与应用》程序和数据\第9章 数据的描述性统计方法/ti9_4.txt')
x1 = np.tile(np.arange(1, 4), (12, 1)).T
x2 = np.tile(np.array([1,1,1,2,2,2,3,3,3,4,4,4]), (3, 1))
model_dic = {'x1': x1.flatten(), 'x2': x2.flatten(), 'y': data.flatten()}
model = sm.formula.ols('y~C(x1)*C(x2)', model_dic).fit()
sm.stats.anova_lm(model)
model = sm.formula.ols('y~C(x1)*C(x2)', model_dic).fit()
print(model.summary()) # 打印模型摘要
anova_result = sm.stats.anova_lm(model)
print(anova_result) # 打印ANOVA结果