9.4 为分析4种化肥和3个小麦品种对小麦产量的影响,把一块试验田等分成36小块,对种子和化肥的每一种组合种植3小块田,产量如表所示,(表格为同一文件夹中的excel文件,列为品种A1,A2,A3,共3行,行为化肥B1,B2,B3,B4,共4列,每个里面有三个数值)问品种、化肥及二者的相互作用对小麦产量有无显著影响
import pandas as pd
import statsmodels.api as sm
from statsmodels.formula.api import ols
from statsmodels.stats.anova import anova_lm
file_path = '9.4.xlsx'
df = pd.read_excel(file_path, header=0, index_col=0)
def split_to_float_list(s):
try:
return [float(x) for x in s.split(',')]
except (ValueError, TypeError):
return []
df_processed = df.applymap(split_to_float_list)
data = []
for var_name, var_data in df_processed.iterrows():
for fert_name, yields in var_data.iteritems():
for yield_value in yields:
data.append({'品种': var_name, '化肥': fert_name, '产量': yield_value})
df_long = pd.DataFrame(data)
df_long['品种'] = df_long['品种'].astype('category')
df_long['化肥'] = df_long['化肥'].astype('category')
model = ols('产量 ~ C(品种) + C(化肥) + C(品种):C(化肥)', data=df_long).fit()
anova_table = anova_lm(model, typ=2)
print(anova_table)