SARIMAX – No supported index is available. Prediction results will be given with an integer index beginning at `start`

  arima, python, time-series, yahoo-finance

I wanna predict Apple’s stock price with SARIMAX. I used Yahoo Finance API and collected data. However, when I wanna apply the model on test data, I face this error:

KeyError: 'The `start` argument could not be matched to a location related to the index of the data.'

Here are the codes:

# Import the plotting library 
import matplotlib.pyplot as plt 
 
# Import the yfinance. If you get module not found error the run !pip install yfiannce from your Jupyter notebook 
import yfinance as yf   
 
# Get the data of the stock AAPL 
data = yf.download('AAPL','2010-01-01','2021-05-02') 
 
# Plot the close price of the AAPL 
data.Close.plot() 
plt.show() 

train_df = data.loc[:"2020-12-31"]
test_df = data.loc["2021-01-02":]

print("Training Set Shape - ", train_df.shape)
print("Testing Set Shape - ", test_df.shape)

import statsmodels.graphics.tsaplots as sgt

# Fixing plot size
plt.rcParams["figure.figsize"] = 18, 5

# Defining Subplots
fig, axes = plt.subplots(1, 2)

# Plotting ACF and PACF 
sgt.plot_acf(train_df.Close[1:], zero = False, lags = 100, ax = axes[0])
sgt.plot_pacf(train_df.Close[1:], zero = False, lags = 100, ax = axes[1])

# Display the Plot
plt.show()

# MODEL FITTING
# Importing Required Package
from statsmodels.tsa.statespace.sarimax import SARIMAX

# Defining the Model
model = SARIMAX(train_df["Close"][1:], order = (10, 0, 1))
# Fitting the Model
model_results = model.fit()

# Printing the model summary
print(model_results.summary())

# FORECASTING
# Building Forecast Object to generate Confidence Intervals
arma_forecast = model_results.get_forecast(len(test_df.index))
arma_predictions_df = arma_forecast.conf_int(alpha = 0.05) # Confidence level of 95%
# Predictions
arma_predictions_df["Predictions"] = model_results.predict(start = test_df.index[0], end = test_df.index[-1])

# RMSE for the Predictions
arma_rmse = np.sqrt(mean_squared_error(test_df["Close"].values, arma_predictions_df["Predictions"]))

I face the error in the following line of code:

model_results.predict(start = test_df.index[0], end = test_df.index[-1])

Source: Python Questions

LEAVE A COMMENT