def twoway_anova_no_interaction(data,alpha,ndigits):
data_new = data.iloc[:,1:]
data_new.index=pd.Index(['A1','A2','A3','A4'],name='A')
data_new.columns=pd.Index(['B1','B1','B2','B2','B3','B3'], name='B')
data_new1 = data_new.stack().reset_index().rename(columns={0:'value'})
formula = '{} ~ {}+{}'.format(data_new1.columns[2], data_new1.columns[0],data_new1.columns[1])
model = ols(formula, data_new1).fit()
Result = round(anova_lm(model),ndigits)
return Result
twoway_anova_no_interaction(df2,0.05,3)
def twoway_anova_interaction(data,rowname,colname,r_obs,c_obs,alpha,ndigits):
data_new = data.iloc[:,1:]
#Reset row index
new_index = []
for i in range(1,len(df2.iloc[:,1:].index)//r_obs+1):
new_index.append(rowname+str(i))
new_index = np.sort(new_index*r_obs)#sort
data_new.index=pd.Index(new_index,name=rowname)
#Reset columns
new_col = []
for j in range(1,len(df2.iloc[:,1:].columns)//c_obs+1):
new_col.append(colname+str(j))
new_col = np.sort(new_col*c_obs)#sort
data_new.columns=pd.Index(new_col, name=colname)
#Data stack
data_new1 = data_new.stack().reset_index().rename(columns={0:'value'})
#ANOVA
formula = '{} ~ {}+{}+{}:{}'.format(data_new1.columns[2], data_new1.columns[0],data_new1.columns[1],
data_new1.columns[0],data_new1.columns[1])
model = ols(formula, data_new1).fit()
Result = round(anova_lm(model),ndigits)
return Result
twoway_anova_interaction(df2,'ad','price',1,2,0.05,3)
2021-01-27
最新推荐文章于 2024-01-01 16:07:57 发布