AttributeError ARIMAResults Object Has No Attribute plot_predict

Working with time series objects is always challenging and it is difficult to interpret the results. We usually use different visualization tools in order to understand the predictions. In the ARIMA model, the plot_predict is used to visualize the predictions of the model but if you will use this function now, you will get AttributeError: ARIMAResults object has no attribute plot_predict. The reason for getting this error is that the plot_predict method has been removed from the updated version of the ARIMA model. So, in order to visualize the predictions, we either have to install the old version of the ARIMA model or we need to use a different method to visualize the predictions.

In this article, we will discuss the reasons for getting AttributeError: ARIMAResults object has no attribute plot_predict error and will go through various solutions to plot the predictions of the ARIMA model. If you have any questions or the given solution couldn’t help you to solve the problem, please let us know through comments and we will try to help you to solve the issue.

Solve AttributeError ARIMAResults object has no attribute plot_predict

The main reason for getting AttributeError: ARIMAResults object has no attribute plot_predict error is because there is no plot_predict function in the updated version ARIMA model. However, there are different methods to visualize the predictions of the ARIMA model without using the plot_predict method.

Before going to the solutions, let us first understand when and which part of the code is giving this error: Let us say that we had applied the ARIMA model on the time series dataset and predicted the future values. And now, we want to visualize the future values using any plot to see if the predicted values follow the trend or not:

# importing modules
from statsmodels.tsa.arima.model import ARIMA
from statsmodels.graphics.tsaplots import plot_predict

#creating a model 
model = ARIMA(df1.PTS, order=(1, 1,1))

# fitting the model
res = model.fit()

# visualizing the model's predictions
res.plot_predict(start='2021-10-19', end='2022-04-05')
plt.show()

Output:

AttributeError: 'ARIMAResults' object has no attribute 'plot_predict'

We are getting the error because we are trying to use the plot_predict function which is no more available in ARIMA.

Let us see how we can solve the ARIMAResults Object Has No Attribute plot_predict using different methods.

Using Subplots in Python

Subplots are different graphs on the same plot which help us to analyze various graphs on the same axe. In our case, we can create subplots for actual values, predictions, and confidence intervals and visualize them all in one plot using the subplot method in Python.

Let us first take an example of a subplot in Python which will help you to understand and visualize the predictions of the ARIMA model. Here, we will take a simple example and will visualize the subplots in Python using the Matplotlib module.

import matplotlib.pyplot as plt
import numpy as np

# dataset for plot
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(2, 1, 1)
plt.plot(x,y)

#dataset for plot2
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 0])

plt.subplot(2, 1, 2)
plt.plot(x,y)

plt.show()

Output:

AttributeError-ARIMAResults-Object-Has-No-Attribute-plot_predict

Another important feature of the plots in Python is that we can plot two datasets on the same plot with different coloring as shown below:

import matplotlib.pyplot as plt
import numpy as np

# dataset for plot
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
y1 = np.array([10, 20, 30, 0])

# plotting two graphs
plt.plot(x,y)
plt.plot(x,y1)


plt.show()

Output:

AttributeError-ARIMAResults-Object-Has-No-Attribute-plot_predict

Once you understand these two plots then it will be easy for you to plot the predictions of the ARIMA model. For the plot_predict function, we just need to do something similar to the above graphs. We first need to specify the subplots and then plot the predictions and actual values on the same plot. See the example below:

# importing the modules
import matplotlib.pyplot as plt
import pandas as pd
import statsmodels.api as sm
from statsmodels.graphics.tsaplots import plot_predict
from statsmodels.tsa.arima.model import ARIMA

# importing random dataset
dta = sm.datasets.sunspots.load_pandas().data[['SUNACTIVITY']]
dta.index = pd.date_range(start='1700', end='2009', freq='A')

# training the model
res = ARIMA(dta, order=(1,2,1)).fit()

# creating subpltos
fig, ax = plt.subplots()
ax = dta.loc['1950':].plot(ax=ax)

# visualizeing the predictions
plot_predict(res, '1990', '2012', ax=ax)
plt.show()

Output:

visualize-ARIMA-predictions

You can use your own creativity and can plot the graph in different formats as well.

Visualize Predictions of the ARIMA model using Matplotlib Module

Another way to visualize the predictions of the ARIMA model is using the Matplotlib module. This method is very simple. In order to use the Matplotlib module for the visualization in the ARIMA model, we need to split the dataset into testing and training parts so that we can evaluate the performance of the model as well.

Let us say that I have a dataset known as data. First, I will split the dataset into testing and training parts.

train_size = int(len(data) * 0.75)
train, test = data[0:train_size], data[train_size:len(data)]

Once this is complete, I will train the model and predictions and store them in a list so that I can compare the results later with the actual values.

history = [x for x in train]
predictions = list()

import statsmodels.api as sm

for t in range(len(test)):
    model = sm.tsa.arima.ARIMA(history, order=(5,1,0))
    model_fit = model.fit()
    pred = model_fit.forecast()
    yhat = pred[0]
    predictions.append(yhat)
    # Append test observation into overall record
    obs = test[t]
    history.append(obs)

Now, we have predictions stored in a list and we can use the actual and predicted values to find the residuals and plot them as shown below:

# importing the module
import matplotlib.pyplot as plt

# figure size
plt.figure(figsize=(8, 4))

# acutal values
plt.plot([i for i in range(len(test))] ,test, c='b', label="actual values")

# predicted values
plt.plot([i for i in range(len(test))],predictions, c='r',label="predicted values", linestyle='dashed')
plt.legend()
plt.show()

Output:

actual-and-predicted-values-in-ARIMA-model

As you can see, the model was quite good in making predictions and it follows the trend in the time series dataset. Now, we can make predictions and then visualize those predictions.

date = pd.date_range(start='2022-09-01', end='2022-09-07')
pre=model_fit.predict(start=438,end=444,dynamic=True)

pre = pd.Series(pre)
pre.index  = date

pre.plot()

Output:

future-predictions-using-ARIMA

As you can see, now we got a really good graph that is forecasting the predictions of the ARIMA model for the given time period.

Summary

ARIMA models can be used to predict and forecast future values in time series. Sometimes, we get an error because of using an old version of the ARIMA module. In this article, we discussed how we can solve AttributeError: ARIMAResults Object Has No Attribute plot_predict which occurs because of using plot_predict which is functional in an older version of the ARIMA module.

Other Related Issues

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top