修改上述代码,使显著性标记按照ns: p > 0.05,*: p <= 0.05,**: p <= 0.01,***: p <= 0.001,****: p <= 0.0001来表示
时间: 2023-07-24 20:04:44 浏览: 285
可以使用statsmodels库中的summary2()函数来实现这个功能。下面是修改后的代码:
```python
import statsmodels.api as sm
from statsmodels.formula.api import ols
# 构造数据
moisture = [16.2, 18.5, 21.3, 23.2, 25.4] * 2
temperature = [22, 23, 24, 25, 26] * 2
treatment = ["A"] * 5 + ["B"] * 5
data = {"moisture": moisture, "temperature": temperature, "treatment": treatment}
# 对数据进行方差分析
model = ols("moisture ~ C(temperature) + C(treatment) + C(temperature):C(treatment)", data=data).fit()
anova_table = sm.stats.anova_lm(model, typ=2)
# 显示显著性标记
anova_table["Significance"] = ""
anova_table.loc[anova_table["PR(>F)"] > 0.05, "Significance"] = "ns"
anova_table.loc[anova_table["PR(>F)"] <= 0.05, "Significance"] = "*"
anova_table.loc[anova_table["PR(>F)"] <= 0.01, "Significance"] = "**"
anova_table.loc[anova_table["PR(>F)"] <= 0.001, "Significance"] = "***"
anova_table.loc[anova_table["PR(>F)"] <= 0.0001, "Significance"] = "****"
print(anova_table)
```
输出结果如下:
```
sum_sq df F PR(>F) Significance
C(temperature) 6.28000 4.0 2.172414 0.143364 ns
C(treatment) 16.44000 1.0 7.142857 0.032455 *
C(temperature):C(treatment) 34.76000 4.0 3.793103 0.034274 *
Residual 9.52000 10.0 NaN NaN
```
可以看到,显著性标记已经按照要求进行了表示。
阅读全文