问题是numpy2ri。
据我所知,有两种可能的解决方法。首先,绕过 rpy 的.rx2
函数并使用 R 的 raw [[
,我不知道这意味着什么,因为我不知道 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
...