多因数分析的格式:
model = osl(‘样本某一列 ~ 样本另一列 + 样本另一列 ’)
如下代码:
- from statsmodels.stats.anova import anova_lm
- from statsmodels.formula.api import ols
- import pandas as pd
-
- df = pd.DataFrame(
- [
- [1.4, "one", "common"], [1.5, "one", "common"], [1.6, "one", "vip"], [1.5, "one", "vip"], [1.6, "one", "vip"], [1.7, "one", "vip"],
- [0.1, "two", "lamb"], [0.2, "two", "lamb"], [0.3, "two", "lamb"], [0.4, "two", "lamb"], [0.5, "two", "lamb"], [0.6, "two", "common"]
- ],
- columns=["rate", "type", "rank"]
- )
-
- if __name__ == '__main__':
- model = ols("rate ~type + rank", data=df)
- data = model.fit()
- print(anova_lm(data))
- pass
运行截图如下:
同样:
结果中可以知道,p很小,默认情况下显著水平为0.05,也就是0.05以上代表2个类型,很相似,无关性小,0.05以下,代表2个类型不相似,也就是说不同行业股票收益水平不同。
如果是析因素方差
就是这样的
model = osl(‘样本某一列 ~ 样本另一列 * 样本另一列’,data=样本).fit()