41133

What object to pass to R from rpy2?

I'm unable to make the following code work, though I don't see this error working strictly in R.

from rpy2.robjects.packages import importr from rpy2 import robjects import numpy as np forecast = importr('forecast') ts = robjects.r['ts'] y = np.random.randn(50) X = np.random.randn(50) y = ts(robjects.FloatVector(y), start=robjects.IntVector((2004, 1)), frequency=12) X = ts(robjects.FloatVector(X), start=robjects.IntVector((2004, 1)), frequency=12) forecast.Arima(y, xreg=X, order=robjects.IntVector((1, 0, 0)))

It's especially confusing considering the following code works fine

forecast.auto_arima(y, xreg=X)

I see the following traceback no matter what I give for X, using numpy interface or not. Any ideas?

--------------------------------------------------------------------------- RRuntimeError Traceback (most recent call last) <ipython-input-20-b781220efb93> in <module>() 13 X = ts(robjects.FloatVector(X), start=robjects.IntVector((2004, 1)), frequency=12) 14 ---> 15 forecast.Arima(y, xreg=X, order=robjects.IntVector((1, 0, 0))) /home/skipper/.local/lib/python2.7/site-packages/rpy2/robjects/functions.pyc in __call__(self, *args, **kwargs) 84 v = kwargs.pop(k) 85 kwargs[r_k] = v ---> 86 return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs) /home/skipper/.local/lib/python2.7/site-packages/rpy2/robjects/functions.pyc in __call__(self, *args, **kwargs) 33 for k, v in kwargs.iteritems(): 34 new_kwargs[k] = conversion.py2ri(v) ---> 35 res = super(Function, self).__call__(*new_args, **new_kwargs) 36 res = conversion.ri2py(res) 37 return res RRuntimeError: Error in `colnames<-`(`*tmp*`, value = if (ncol(xreg) == 1) nmxreg else paste(nmxreg, : length of 'dimnames' [2] not equal to array extent

<strong>Edit</strong>:

The problem is that the following lines of code do not evaluate to a column name, which seems to be the expectation on the R side.

sub = robjects.r['substitute'] deparse = robjects.r['deparse'] deparse(sub(X))

I don't know well enough what the expectations of this code should be in R, but I can't find an RPy2 object that passes this check by returning something of length == 1. This really looks like a bug to me.

R> length(deparse(substitute((rep(.2, 1000))))) [1] 1

But in Rpy2

[~/] [94]: robjects.r.length(robjects.r.deparse(robjects.r.substitute(robjects.r('rep(.2, 1000)')))) [94]: <IntVector - Python:0x7ce1560 / R:0x80adc28> [ 78]

Answer1:

This is one manifestation (see this other related issue for example) of the same underlying issue: R expressions are evaluated lazily and can be manipulated within R and this leads to idioms that do not translate well (in Python expression are evaluated immediately, and one has to move to the AST to manipulate code).

An answers to the second part of your question. In R, substitute(rep(.2, 1000)) is passing the unevaluated expression rep(.2, 1000) to substitute(). Doing in rpy2

substitute('rep(.2, 1000)')`

is passing a string; the R equivalent would be

substitute("rep(.2, 1000)")

The following is letting you get close to R's deparse(substitute()):

from rpy2.robjects.packages import importr base = importr('base') from rpy2 import rinterface # expression e = rinterface.parse('rep(.2, 1000)') dse = base.deparse(base.substitute(e)) >>> len(dse) 1 >>> print(dse) # not identical to R "expression(rep(0.2, 1000))"

Currently, one way to work about this is to bind R objects to R symbols (preferably in a dedicated environment rather than in GlobalEnv), and use the symbols in an R call written as a string:

from rpy2.robjects import Environment, reval env = Environment() for k,v in (('y', y), ('xreg', X), ('order', robjects.IntVector((1, 0, 0)))): env[k] = v # make an expression expr = rinterface.parse("forecast.Arima(y, xreg=X, order=order)") # evaluate in the environment res = reval(expr, envir=env)

This is not something I am happy about as a solution, but I have never found the time to work on a better solution.

<strong>edit:</strong> With rpy2-2.4.0 it becomes possible to use R symbols and do the following:

RSymbol = robjects.rinterface.SexpSymbol pairlist = (('x', RSymbol('y')), ('xreg', RSymbol('xreg')), ('order', RSymbol('order'))) res = forecast.Arima.rcall(pairlist, env)

This is not yet the most intuitive interface. May be something using a context manager would be better.

Recommend

  • Python Dictionary:removing a string from a tuple( WHICH is a key .)
  • Python - Searching JSON
  • Binding two comboboxes to one function
  • How to split a python dictionary for its values on matching a key
  • Sorting keys of same values alphabetically
  • Remove keys from object not in a list in python? [duplicate]
  • Get a list of all keys in nested dictionary
  • Print a dict sorted by values
  • Combine two dictionaries and remove duplicates in python
  • How to properly add custom logging filters in Python modules
  • Loading a defaultdict in Hadoop using pickle and sys.stdin
  • How to append columns based on other column values to pandas dataframe
  • dictionary shared between objects for no reason?
  • Having different results every run with GMM Classifier
  • update one dictionary with another by adding values rather then replacing it
  • Creating multiple nested dictionaries from .txt file
  • Python KeyboardInterrupt in Multithreading
  • How to update sqlalchemy orm object by a python dict
  • Get a unique list of items that occur more than once in a list
  • I have a twisted reactor running, how do I connect to it?
  • What object to pass to R from rpy2?
  • Multidimensional data and plot legends
  • product of two functions
  • get the content when the div does not have the id of the div … only has the class
  • How to register a @staticmethod password checker callback for a ttk.Entry widget?
  • How to pass arrays into Scipy Interpolate RectBivariateSpline?
  • PySpark Yarn Application fails on groupBy
  • Access 2007 forms with parameterized RecordSource
  • What is wrong with this emulation of CMPXCHG16B instruction?
  • Django Class view didn't return an HttpResponse object. It returned None instead
  • Einsum optimize fails for basic operation
  • is this code truly private? (python)
  • redirect user after update in class based view in django
  • How do I translate LR(1) Parse into a Abstract syntax tree?
  • How to Cache Real-time Data?
  • Alert pop up with LWUIT
  • Spring Data JPA custom method causing PropertyReferenceException
  • Possible to stop flickering java tooltip in heavyweight mode?
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Checking variable from a different class in C#