I can't seem to get the fit = forecast.Arima(series, order=order, xreg=r_exog_train) to work. It works without the xreg parameter, so I'm sure this is a numpy array to convert the rpy2 matrix, which creates a problem.
Does anyone see 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 a sample code:
# 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])