通过RPy从R's factanal()中提取相关矩阵

时间:2014-07-30 12:15:08

标签: python r rpy2 factor-analysis

我使用以下代码提取loadings矩阵:

from rpy2.robjects import r, numpy2ri
numpy2ri.activate()
import numpy as np

A = np.random.rand(50,10)
fit = r.factanal(A, 5, rotation='promax')
load = r.loadings(fit)

哪个有用!

但是,如果我尝试对correlation

做同样的事情
from rpy2.robjects import r, numpy2ri
numpy2ri.activate()
import numpy as np

A = np.random.rand(50,10)
fit = r.factanal(A, 5, rotation='promax')
corr = r.correlation(fit)

我得到:AttributeError: 'R' object has no attribute 'correlation'

如果 - 而不是 - 我尝试:

from rpy2.robjects import r, numpy2ri
numpy2ri.activate()
import numpy as np

A = np.random.rand(50,10)
fit = r.factanal(A, 5, rotation='promax')
corr = fit.rx2('correlation')

我得到:ValueError: All parameters must be of type Sexp_Type,or Python int/long, float, bool, or None

我发现这很奇怪,因为相关和加载都应该有效。

print fit.names

给了我:

['converged' 'loadings' 'uniquenesses' 'correlation' 'criteria' 'factors'
 'dof' 'method' 'rotmat' 'STATISTIC' 'PVAL' 'n.obs' 'call']

2 个答案:

答案 0 :(得分:1)

我已经使用rpy2版本2.4.2重现了您的错误。似乎rpy2在字符串索引方面遇到了一些麻烦。

r.correlation(fit)对我不起作用,因为默认情况下我的R中没有这样的函数correlation。但有loadings,所以r.loadings(fit)效果很好。

这段代码给了我相关矩阵:

fit = r.factanal(A, 5, rotation='promax')
corr = fit[3]

答案 1 :(得分:1)

问题是numpy2ri。

据我所知,有两种可能的解决方法。首先,绕过rpy的.rx2功能并使用R的原始[[,我不知道这是什么意思,因为我没有'}知道robjects.vectors.rx2中正在做的所有其他内容,但它可以在这里工作:

In [1]: from rpy2.robjects import r, numpy2ri    
In [2]: numpy2ri.activate()    
In [3]: import numpy as np   
In [4]: A = np.random.rand(50,10)   
In [5]: fit = r.factanal(A, 5, rotation='promax')  
In [6]: corr = fit.rx2('correlation')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-cf8a3601ab8e> in <module>()
----> 1 corr = fit.rx2('correlation')

/ifs/apps/apps/python-2.7.1/lib/python2.7/site-packages/rpy2/robjects/vectors.pyc in __call__(self, *args, **kwargs)
     69         fun = self._extractfunction
     70         conv_args.insert(0, self._parent)
---> 71         res = fun(*conv_args, **kwargs)
     72         res = conversion.py2ro(res)
     73         return res

ValueError: All parameters must be of type Sexp_Type,or Python int/long, float, bool, or None

In [7]: r["[["](fit,'correlation')
Out[7]: 
array([[  1.00000000e+00,   1.13009428e-01,  -1.68749351e-01,
          1.85869656e-01,   2.62402778e-01,   5.11846775e-02,
          1.96957316e-01,   9.83478574e-02,   2.10043867e-02,
          1.34883265e-01],
       ...

第二种方法是停用numpy2ri,之后你可以随时重新激活它:

In [8]: numpy2ri.deactivate()    
In [9]: corr = fit.rx2('correlation')     
In [10]: print corr
             [,1]          [,2]         [,3]        [,4]        [,5]
 [1,]  1.00000000  0.1130094277 -0.168749351  0.18586966  0.26240278
 [2,]  0.11300943  1.0000000000 -0.116878885  0.12378751 -0.05303278
 [3,] -0.16874935 -0.1168788854  1.000000000 -0.26323867 -0.17794088
 [4,]  0.18586966  0.1237875121 -0.263238668  1.00000000  0.03955314
 [5,]  0.26240278 -0.0530327762 -0.177940878  0.03955314  1.00000000
 [6,]  0.05118468 -0.0007762935  0.227475607 -0.11270587 -0.10768763
 [7,]  0.19695732 -0.0423807326  0.036117785 -0.03174723 -0.11218540
 [8,]  0.09834786  0.1055451947  0.221756056  0.01828542 -0.41956986
 [9,]  0.02100439 -0.2173312335 -0.064198166 -0.06230902 -0.05976113
[10,]  0.13488326  0.0810527379  0.005651769 -0.10353872 -0.11954671
...