回線を機能させることができないようですfit = forecast.Arima(series, order=order, xreg=r_exog_train)
。xregパラメーターがなくても機能するので、問題になるのはnumpy配列からrpy2マトリックスへの変換であると確信しています。
誰かがここで間違いを観察しますか?ありがとう!
これは私が得るエラーです(残念ながらドイツ語の部分):
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
コード例は次のとおりです。
# 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