As of 2.4.0 can't use some .rx2 keys anymore (example "correlation" on factanal object)

Issue #219 resolved
Horea Christian created an issue

Please refer to this StackOverflow question for the full details.

In short, for:

from rpy2.robjects import r, numpy2ri
numpy2ri.activate()
fit = r.factanal(A, 5, rotation='promax')

I can't use:

corr = r.correlation(fit)

or

corr = fit.rx2('correlation')

And I am forced to use:

corr = fit[3]

This is especially strange since:

load = r.loadings(fit)

works

Comments (9)

  1. Laurent Gautier

    Hi, Can you provide a self-contained example to reproduce the issue ? (rx2 seems to be generally working on my end).

    Thanks.

  2. Horea Christian reporter
    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)
    

    or

    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')
    
  3. Laurent Gautier

    This seems to be caused by the fact that in that case fit is not a list. It probably should not happen.

    In the meantime, try the following workaround:

    def rx2(obj, name):
        return obj[tuple(obj.names).index('name')]
    
  4. Ian Sudbery

    Hi,

    This appears to be a more general problem. I can reproduce just using a simple two-column data frame.

    In [1]: import rpy2.robjects as ro
    In [2]: from rpy2.robjects import r, numpy2ri
    In [3]: numpy2ri.activate()
    In [4]: df = ro.DataFrame({"A": ro.IntVector([1,2,3]), "B": ro.IntVector([1,2,3])})
    In [5]: df.rx2('B')
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-5-0c1de28e88ea> in <module>()
    ----> 1 df.rx2('B')
    
    /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
    

    As far as I can tell the problem is numpy2ri because this works:

    In [6]: numpy2ri.deactivate()
    In [7]: df.rx2('B')
    Out[7]: 
    <IntVector - Python:0x1a37290 / R:0x25a27d8>
    [       1,        2,        3]
    

    This also works:

    In [8]: numpy2ri.activate()
    In [9]: r["[["](df,'B')
    Out[9]: array([1, 2, 3], dtype=int32)
    

    These work arounds also work in Christian's situation (i've posted an answer on StackExchange).

  5. Log in to comment