我似乎无法让线路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