Question

I am using the following code to extract the loadings matrix:

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)

Which works!

But, if I try to do the same for 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)

I get: AttributeError: 'R' object has no attribute 'correlation'

If - instead - I try:

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')

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

I find this strange especially seeing as both correlation and loadings should be valid.

print fit.names

gives me:

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

Solution

The problem is numpy2ri.

As far as I can tell, there are two possible work arounds. Firstly, by-pass rpy's .rx2 function and use R's raw [[, I don't know what the implications of this are, as I don't know what all the other stuff in robjects.vectors.rx2 is doing, but it works here:

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],
       ...

The second alternative is to deactivate numpy2ri, you can always reactivate it again afterwards:

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
...

OTHER TIPS

I've reproduced your error with rpy2 version 2.4.2. It seems rpy2 has some troubles with string indexation.

r.correlation(fit) doesn't works for me, because there no such function correlation in my R by default. But there is loadings, so r.loadings(fit) works well.

This code gave me correlation matrix:

fit = r.factanal(A, 5, rotation='promax')
corr = fit[3]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top