用ARIMA进行预测

时间:2018-12-05 10:16:45

标签: python arima

我正在使用ARIMA模型预测时间序列数据。我使用以下代码找到了最适合的ARIMA模型:

def run_arima_model(df, ts, p,d,q):
from statsmodels.tsa.arima_model import ARIMA
model=ARIMA(df[ts], order=(p,d,q))
results_=model.fit(disp=-1)
len_results = len(results_.fittedvalues)
ts_modified = df[ts][-len_results:]

# calculate root mean square error (RMSE) and residual sum of squares (RSS)
rss = sum((results_.fittedvalues - ts_modified)**2)
rmse = np.sqrt(rss / len(df[ts]))

# plot fit
plt.plot(df[ts])
plt.plot(results_.fittedvalues, color = 'red')
plt.title('For ARIMA model (%i, %i, %i) for ts %s, RSS: %.4f, RMSE: %.4f' %(p, d, q, ts, rss, rmse))

plt.show()
plt.close()

return results_
model_AR = run_arima_model(df, 
                       ts = 'I', 
                       p = 1, 
                       d = 0, 
                       q = 0)

# MA model with 1st order differencing - ARIMA (0,0,1)
model_MA = run_arima_model(df, 
                       ts = 'I', 
                       p = 0, 
                       d = 0, 
                       q = 1)

# ARMA model with 1st order differencing - ARIMA (1,0,1)
model_MA = run_arima_model(df, 
                       ts = 'I', 
                       p = 1, 
                       d = 0, 
                       q = 1)

ARIMA(1,0,1)最适合我的当前数据,如何获取它来预测未来点?

2 个答案:

答案 0 :(得分:1)

最简单的方法:

model00 = ARIMA(np.array(dataframe.ix[:,4]), dates=None,order=(2,1,0))
model11 = model00.fit(disp=1)
model11.forecast()
model11.summary()

您将获得预测加:

enter image description here

答案 1 :(得分:0)

您可以使用以下代码获得3个未来点

 fcast<-forecast(fit,h=3)
 fcast<-data.frame(fcast)