Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting numpy array to rpy2 matrix (forecast package, xreg parameter)
    primarykey
    data
    text
    <p>I don't seem to be able to get the line <code>fit = forecast.Arima(series, order=order, xreg=r_exog_train)</code> 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. </p> <p>Does anyone observe a mistake here? Thanks!</p> <p>This is the error I get (parts in German unfortunately):</p> <pre><code>Fehler in `colnames&lt;-`(`*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 &lt;module&gt; 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&lt;-`(`*tmp*`, value = if (ncol( xreg) == 1) nmxreg else paste(nmxreg, : Lõnge von 'dimnames' [2] ungleich der Arrayausdehnung </code></pre> <p>Here is the code example:</p> <pre><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]) # 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload