點選檢視程式碼
import numpy as np
import statsmodels.api as sm
data = np.loadtxt('F:\python數學建模與演算法\源程式\《Python數學建模演算法與應用》程式和資料\第9章 資料的描述性統計方法/ti9_5.txt')
x1 = np.vstack([np.ones((4, 4)), np.ones((4, 4))*2, np.ones((4, 4))*3])
x2 = np.tile(np.array([[1, 1, 2, 2]]).T, (3, 4))
x3 = np.tile(np.array([[1, 2]]).T, (6, 4))
model_dic = {
'x1': x1.flatten(),
'x2': x2.flatten(),
'x3': x3.flatten(),
'y': data.flatten()
}
model = sm.formula.ols('y~C(x1)+C(x2)+C(x3)', model_dic).fit()
sm.stats.anova_lm(model)