How do I forecast data using ARIMA?

799 Views Asked by At

I wanted to forecast stock prices using the ARIMA model (Autoregressive Moving Average) and wanted to plot the forecasted data over the actual and training data. I'm following this tutorial and have browsed others too. But they all follow the same code. Here is the link to their tutorial for your reference:(https://www.analyticsvidhya.com/blog/2021/07/stock-market-forecasting-using-time-series-analysis-with-arima-model/)

# Forecast
fc, se, conf= fitted.forecast(216, alpha=0.05)  # 95% conf

I was expecting a graph that looks like thisenter image description here

Instead, an error message shows up: ValueError: too many values to unpack (expected 3)

please help :')

Edit: I tried doing that before and it produces an error message in the next code. My next line of codes are as the following:

result = fitted.forecast(216, alpha =0.05)`

# Make as pandas series
fc_series = pd.Series(result, index=test_data.index)
lower_series = pd.Series(result[:, 0], index=test_data.index)
upper_series = pd.Series(result[:, 1], index=test_data.index)

The error message: KeyError: 'key of type tuple not found and not a MultiIndex'

2

There are 2 best solutions below

1
On

It seems, that the forecast function is not returning three return values anymore. This may happen if you don’t use the same version as in the tutorial.

Please try something like:

result = fitted.forecast(216, alpha=0.05)

And then inspect the result if it does contain all the data you need.

0
On

import library

import statsmodels.api as sm

use a model with sm.tsa

model = sm.tsa.ARIMA(train_data, order=(1, 1, 1))  
fitted = model.fit()
print(fitted.summary())

pass a parameter Summary_frame to get a forecast , lower and upper interval

result = fitted.get_forecast(216, alpha =0.05).summary_frame()
print(result)

Make pandas series, dont forget add values to get series not null.

fc_series = pd.Series(result['mean'].values, index=test_data.index)
lower_series = pd.Series(result['mean_ci_lower'].values, index=test_data.index)
upper_series = pd.Series(result['mean_ci_upper'].values, index=test_data.index)

I hope this help you.