What object should I be passing?

Issue #192 on hold
Former user created an issue

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
#from rpy2.robjects import numpy2ri

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

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

Comments (4)

  1. Laurent Gautier

    Issue tracking is more for tracking issue with the software (bugs, requests for changes, ideas, etcc...), much less for tracking issues when using software or programming in general (StackOverflow is one place for that).

  2. Skipper Seabold

    Here's the problem. Things like this work in R but not when I got through rpy2. This is used to try to grab column names in R.

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

    But from rpy2

    sub = robjects.r['substitute']
    deparse = robjects.r['deparse']


    [92]: robjects.r.length(deparse(sub(robjects.r('rep(.2, 1000)'))))
    <IntVector - Python:0x7ce6128 / R:0x7e22778>
    [      78]
  3. Laurent Gautier

    Current workaround is:

    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)
  4. Log in to comment