我一直在努力使用python中的sklearn进行因子分析。
在运行以下代码后的R中:
x.f <- factanal(data_final, factors = 2, rotation="varimax", scores="regression", lower = 0.01)
我得到以下结果:
Call:
factanal(x = data_final, factors = 2, scores = "regression", rotation = "varimax", lower = 0.01)
Uniquenesses:
WTI GOLD CAC40 DAX EUR_DOLL YEN_DOLL SP500 NIKKEI GILT TEN_TRES
0.740 0.971 0.115 0.056 0.789 0.775 0.283 0.022 0.849 0.754
Loadings:
Factor1 Factor2
WTI 0.400 0.317
GOLD 0.169
CAC40 0.857 0.387
DAX 0.903 0.359
EUR_DOLL 0.371 0.271
YEN_DOLL -0.472
SP500 0.511 0.675
NIKKEI 0.337 0.930
GILT -0.334 -0.197
TEN_TRES -0.343 -0.358
Factor1 Factor2
SS loadings 2.482 2.163
Proportion Var 0.248 0.216
Cumulative Var 0.248 0.465
Test of the hypothesis that 2 factors are sufficient.
The chi square statistic is 686.84 on 26 degrees of freedom.
The p-value is 4.16e-128
我可以轻松地解释此代码,并了解其背后的输出和过程。
但是,当我在python中运行以下代码时,我不确定发生了什么以及它是否正确。
from sklearn import decomposition
from sklearn.decomposition import FactorAnalysis
factor = decomposition.FactorAnalysis(n_components=2)
factor.fit(data_final.iloc[:, 1::])
factor.components_
array([[-0.01175024, -0.00157749, -0.01547956, -0.01353783, -0.00322834,
0.00225613, -0.01085127, -0.01219159, 0.00247041, 0.00210084],
[ 0.00021618, -0.00135881, -0.00419973, -0.00435391, -0.00012713,
-0.00225637, 0.00275685, 0.00686218, 0.00034337, -0.00035002]])
有没有更简单的方法在python中执行因子分析?如果没有,我如何从我的python代码中获取因子组件?
如果它有用,我使用的数据集是10个资产的期货合约中的一组日志回报。
提前致谢
所以我找到了为什么我得不到相同输出的原因。在R facanal()的情况下,Sklearn函数不会使您的数据标准化。我必须扩展我的数据才能获得类似的输出。另一个问题是,当我使用日志返回而不是价格时,这会产生如此大的差异。