Converting numpy array to rpy2 matrix (forecast package, xreg parameter)
Question: I don't seem to be able to get the line `fit = forecast.Arima(series,
order=order, xreg=r_exog_train)` to work. It does work without the xreg
parameter so I'm pretty sure it's the numpy array to rpy2 matrix conversion
that makes a problem.

Does anyone observe a mistake here? Thanks!

This is the error I get (parts in German unfortunately):

    
    
    Fehler in `colnames<-`(`*tmp*`, value = if (ncol(xreg) == 1) nmxreg else paste(n
    mxreg,  :
      Länge von 'dimnames' [2] ungleich der Arrayausdehnung
    Traceback (most recent call last):
      File "r.py", line 58, in <module>
        res = do_forecast(series, horizon=horizon, exog=(exog_train, exog_test))
      File "r.py", line 39, in do_forecast
        fit = forecast.Arima(series, order=order, xreg=exog_train)
      File "C:\Python27\lib\site-packages\rpy2\robjects\functions.py", line 86, in _
    _call__
        return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
      File "C:\Python27\lib\site-packages\rpy2\robjects\functions.py", line 35, in _
    _call__
        res = super(Function, self).__call__(*new_args, **new_kwargs)
    rpy2.rinterface.RRuntimeError: Fehler in `colnames<-`(`*tmp*`, value = if (ncol(
    xreg) == 1) nmxreg else paste(nmxreg,  :
      Lõnge von 'dimnames' [2] ungleich der Arrayausdehnung   
    

Here is the code example:

    
    
    # Python wrapper for R forecast stuff
    import numpy as np
    
    print 'Start importing R.'
    from rpy2 import robjects 
    from rpy2.robjects.packages import importr
    from rpy2.robjects.numpy2ri import numpy2ri
    robjects.conversion.py2ri = numpy2ri
    
    base = importr('base')
    forecast = importr('forecast')
    stats = importr('stats')
    ts = robjects.r['ts']
    print 'Finished importing R.'
    
    
    def nparray2rmatrix(x):
      nr, nc = x.shape
      xvec = robjects.FloatVector(x.transpose().reshape((x.size)))
      xr = robjects.r.matrix(xvec, nrow=nr, ncol=nc)
      return xr
    
    def nparray2rmatrix_alternative(x):
      nr, nc = x.shape
      xvec = robjects.FloatVector(x.reshape((x.size)))
      xr = robjects.r.matrix(xvec, nrow=nr, ncol=nc, byrow=True)
      return xr
    
    def do_forecast(series, frequency=None, horizon=30, summary=False, exog=None):
      if frequency:
        series = ts(series, frequency=frequency)
      else:
        series = ts(series)
      if exog:
        exog_train, exog_test = exog
        r_exog_train = nparray2rmatrix(exog_train)
        r_exog_test = nparray2rmatrix(exog_test)
        order = robjects.IntVector([1, 0, 2]) # c(1,0,2) # TODO find right model
        fit = forecast.Arima(series, order=order, xreg=r_exog_train)
        forecast_result = forecast.forecast(fit, h=horizon, xreg=r_exog_test)
      else:
        # fit = forecast.auto_arima(series)
        #robjects.r.plot(series)
        fit = stats.HoltWinters(series)
        forecast_result = forecast.forecast(fit, h=horizon)
      if summary:
        modsummary = base.summary(fit)
        print modsummary
      forecast_values = np.array(list(forecast_result.rx2('mean')))
      return forecast_values
    
    
    # Example
    series = np.arange(100)
    exog_train = np.ones((100,2))
    exog_test = np.ones((100,2))
    horizon = 100
    res = do_forecast(series, horizon=horizon, exog=(exog_train, exog_test))
    print res
    


Answer: I got it working by explicitely stating "dimnames" as in: `xr =
robjects.r.matrix(xvec, nrow=nr, ncol=nc, dimnames=dimnames)`. Apparently the
forecast package requires this.

