####双因素方差分析,可进行有交互作用与无交互作用
def f_twoway(df_c,col_fac1,col_fac2,col_sta,interaction=False):
df=df_c.copy()
list_fac1=df[col_fac1].unique()
list_fac2=df[col_fac2].unique()
r=len(list_fac1)
s=len(list_fac2)
x_bar=df[col_sta].mean()
list_Qa=[]
list_Qb=[]
for i in list_fac1:
series_i=df[df[col_fac1]==i][col_sta]
xi_bar=series_i.mean()
list_Qa.append((xi_bar-x_bar)**2)
for j in list_fac2:
series_j=df[df[col_fac2]==j][col_sta]
xj_bar=series_j.mean()
list_Qb.append((xj_bar-x_bar)**2)
Q=((df[col_sta]-x_bar)**2).sum()
df_res=pd.DataFrame(columns=['方差来源','平方和','自由度','均方','F值','Sig.'])
if interaction==False:
Qa=s*sum(list_Qa)
Qb=r*sum(list_Qb)
Qw=Q-Qa-Qb
Sa=Qa/(r-1)
Sb=Qb/(s-1)
Sw=Qw/((r-1)*(s-1))
sig1=stats.f.sf(Sa/Sw,r-1,(r-1)*(s-1))
sig2=stats.f.sf(Sb/Sw,s-1,(r-1)*(s-1))
df_res['方差来源']=[col_fac1,col_fac2,'误差','总和']
df_res['平方和']=[Qa,Qb,Qw,Q]
df_res['自由度']=[r-1,s-1,(r-1)*(s-1),r*s-1]
df_res['均方']=[Sa,Sb,Sw,'-']
df_res['F值']=[Sa/Sw,Sb/Sw,'-','-']
df_res['Sig.']=[sig1,sig2,'-','-']
return df_res
elif interaction==True:
list_Qw=[]
t=len(df[(df[col_fac1]==list_fac1[0]) & (df[col_fac2]==list_fac2[0])])
for i in list_fac1:
for j in list_fac2:
series_ij=df[(df[col_fac1]==i) & (df[col_fac2]==j)][col_sta]
list_Qw.append(((series_ij-series_ij.mean())**2).sum())
Qa=s*t*sum(list_Qa)
Qb=r*t*sum(list_Qb)
Qw=sum(list_Qw)
Qab=Q-Qa-Qb-Qw
Sa=Qa/(r-1)
Sb=Qb/(s-1)
Sab=Qab/((r-1)*(s-1))
Sw=Qw/(r*s*(t-1))
sig1=stats.f.sf(Sa/Sw,r-1,r*s*(t-1))
sig2=stats.f.sf(Sb/Sw,s-1,r*s*(t-1))
sig3=stats.f.sf(Sab/Sw,(r-1)*(s-1),r*s*(t-1))
df_res['方差来源']=[col_fac1,col_fac2,col_fac1+'*'+col_fac2,'误差','总和']
df_res['平方和']=[Qa,Qb,Qab,Qw,Q]
df_res['自由度']=[r-1,s-1,(r-1)*(s-1),r*s*(t-1),r*s*t-1]
df_res['均方']=[Sa,Sb,Sab,Sw,'-']
df_res['F值']=[Sa/Sw,Sb/Sw,Sab/Sw,'-','-']
df_res['Sig.']=[sig1,sig2,sig3,'-','-']
return df_res
else:
return 'interaction参数错误'
实例测试:
dic_t3=[{'燃料':'A1','推进器':'B1','射程':58.2},{'燃料':'A1','推进器':'B1','射程':52.6},
{'燃料':'A1','推进器':'B2','射程':56.2},{'燃料':'A1','推进器':'B2','射程':41.2},
{'燃料':'A1','推进器':'B3','射程':65.3},{'燃料':'A1','推进器':'B3','射程':60.8},
{'燃料':'A2','推进器':'B1','射程':49.1},{'燃料':'A2','推进器':'B1','射程':42.8},
{'燃料':'A2','推进器':'B2','射程':54.1},{'燃料':'A2','推进器':'B2','射程':50.5},
{'燃料':'A2','推进器':'B3','射程':51.6},{'燃料':'A2','推进器':'B3','射程':48.4},
{'燃料':'A3','推进器':'B1','射程':60.1},{'燃料':'A3','推进器':'B1','射程':58.3},
{'燃料':'A3','推进器':'B2','射程':70.9},{'燃料':'A3','推进器':'B2','射程':73.2},
{'燃料':'A3','推进器':'B3','射程':39.2},{'燃料':'A3','推进器':'B3','射程':40.7},
{'燃料':'A4','推进器':'B1','射程':75.8},{'燃料':'A4','推进器':'B1','射程':71.5},
{'燃料':'A4','推进器':'B2','射程':58.2},{'燃料':'A4','推进器':'B2','射程':51.0},
{'燃料':'A4','推进器':'B3','射程':48.7},{'燃料':'A4','推进器':'B3','射程':41.4},]
df_t3=pd.DataFrame(dic_t3,columns=['燃料','推进器','射程'])
f_twoway(df_t3,'燃料','推进器','射程',interaction=True)