Frage

Ich benutze den folgenden Code, um das zu extrahieren 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)

Was funktioniert!

Aber wenn ich versuche, dasselbe zu tun für 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)

Ich bekomme: AttributeError: 'R' object has no attribute 'correlation'

Wenn - stattdessen - ich es versuche:

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

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

Ich finde das seltsam, zumal sowohl Korrelation als auch Ladungen gültig sein sollten.

print fit.names

gibt mir:

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

Lösung

Das Problem ist numpy2ri.

Soweit ich das beurteilen kann, gibt es zwei mögliche Problemumgehungen.Erstens umgehen Sie Rpys .rx2 funktion und Verwendung von Rs raw [[, Ich weiß nicht, was die Auswirkungen davon sind, da ich nicht weiß, was all die anderen Sachen in robjects sind.Bewegungsvektoren.rx2 macht, aber es funktioniert hier:

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

Die zweite Alternative besteht darin, numpy2ri zu deaktivieren, Sie können es danach jederzeit wieder aktivieren:

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

Andere Tipps

Ich habe Ihren Fehler mit rpy2 Version 2.4.2 reproduziert.Es scheint rpy2 hat einige Probleme mit der Zeichenfolgenindexierung.

r.correlation(fit) funktioniert bei mir nicht, weil es keine solche Funktion gibt correlation in meinem R standardmäßig.Aber es gibt loadings, so r.loadings(fit) funktioniert gut.

Dieser Code gab mir eine Korrelationsmatrix:

fit = r.factanal(A, 5, rotation='promax')
corr = fit[3]
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top