[英]Factor analysis in python (similar to factal() from R)
我一直在努力使用 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 個資產的期貨合約中的一組日志回報。
提前致謝
有一個 python 包可以包裝 R factanal 函數,這樣你就可以從 python 中使用 Pandas 數據框調用它,如下所示:
from factanal.wrapper import factanal
fa_res = factanal(pdf, factors=4, scores='regression', rotation='promax',
verbose=True, return_dict=True)
更多信息: https : //pypi.org/project/factanal/
安裝:
pip install factanal
所以我找到了我沒有得到相同輸出的原因。 Sklearn 函數不會標准化您的數據,而 R facanal() 會。 我必須縮放我的數據以獲得類似的輸出。 另一個問題是,當我處理對數回報而不是價格時,為什么這會產生如此大的差異。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.