1

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'

OnsenEgg
  • 35
  • 6
  • Can you please show the content of the result variable and also your test_data.index. And also mark in which line you get the error. – T.W. Mar 28 '22 at 20:16

2 Answers2

1

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.

T.W.
  • 149
  • 2
  • 6
  • Hi, thank you T.W. for the suggestion. However, it produces another error code as shown in the edited ver of my question. – OnsenEgg Mar 28 '22 at 02:13
0

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.