Autoregressive (AR) Model For Time Series Forecasting - GeeksforGeeks
Autoregressive (AR) Model For Time Series Forecasting - GeeksforGeeks
Autoregressive Models
Autoregressive models belong to the family of time series models. These
models capture the relationship between an observation and several lagged
observations (previous time steps). The core idea is that the current value of a
time series can be expressed as a linear combination of its past values, with
some random noise.
Where:
c is a constant.
AR(1) Model:
In the AR(1) model, the current value depends only on the previous value.
It is expressed as:
AR(p) Model:
The general autoregressive model of order p includes p lagged values.
It is expressed as shown in the introduction.
Python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
data.dropna(inplace=True)
The data is visualized in this step.
Python
Output:
In the first step, the lag features are added to the data frame.
Then the rows with null values are completely removed.
The data is then split into training and testing datasets.
The input features and target variable are defined.
Python3
# Drop rows with NaN values resulting from creating lag features
data.dropna(inplace=True)
ACF Plot
The Autocorrelation Function (ACF) plot is a graphical tool used to visualize
and assess the autocorrelation of a time series data at different lags. The ACF
plot helps you understand how the current value of a time series is correlated
with its past values. You can create an ACF plot in Python using the plot_acf
function from the Stats models library.
Python3
Output:
ACF Plot
The graph shows, the autocorrelation values for the first 20 lags. The plot
displays autocorrelation values at different lags, with lags on x-axis and
autocorrelation values on the y-axis. The graph helps us to identify the
significant lags where autocorrelation values are outside the confidence
interval (indicated by the shaded region).
Python3
Output:
0.7997281316018658
Python
We then make predictions using the AutoReg model and label it as y_pred.
MAE and RMSE metrics are calculated to evaluate the performance of
AutoReg model.
Python
Output:
Mean Absolute Error: 1.59
Root Mean Squared Error: 2.30
In the code, ar_results is an ARIMA model fitted to our time series data. To
make predictions on the test set, we use the predict method of the ARIMA
model. Here’s how it works:
start specifies the starting point for prediction. In this case, we start the
prediction right after the last data point in our training data, which is
equivalent to the first data point in our test set.
end specifies the ending point for prediction. We set it to the last data point
in our test set.
dynamic=False indicates that we are using out-of-sample forecasting. This
means that each forecasted point uses the true values of the previous
observations. This is typically used for model evaluation on the test set.
The predictions are stored in y_pred, which contains the forecasted values
for the test set.
Step 5: Visualization
Visualize the model’s predictions against the actual temperature data. Finally,
the predictions made by the AutoReg model are visualized using Matplotlib
library.
Python
Forecast Plot:
Python
# Define the number of future time steps you want to predict (1 week)
forecast_steps = 7
# Plot the actual data, existing predictions, and one year of future predictions
plt.figure(figsize=(12, 6))
plt.plot(test_data['Date'], y_test, label='Actual Temperature')
plt.plot(test_data['Date'], y_pred, label='Predicted Temperature', linestyle='--')
plt.plot(future_dates, future_predictions[-forecast_steps:], label='Future Predict
plt.xlabel('Date')
plt.ylabel('Temperature')
plt.legend()
plt.title('Temperature Prediction with Autoregressive Model')
plt.show()
Output:
Benefits and Drawbacks of Autoregressive Models
Autoregressive models (AR models) are a class of time series models that have
their own set of benefits and drawbacks. Understanding these can help in
choosing when to use them and when to consider alternative modeling
approaches.
Conclusion
Three 90 Challenge is back on popular demand! After processing refunds worth INR
1CR+, we are back with the offer if you missed it the first time. Get 90% course fee
refund in 90 days. Avail now!
Are you passionate about data and looking to make one giant leap into your
career? Our Data Science Course will help you change your game and, most
importantly, allow students, professionals, and working adults to tide over into
the data science immersion. Master state-of-the-art methodologies, powerful
tools, and industry best practices, hands-on projects, and real-world
applications. Become the executive head of industries related to Data Analysis,
Machine Learning, and Data Visualization with these growing skills. Ready to
Transform Your Future? Enroll Now to Be a Data Science Expert!
E excit… Follow 1
Next Article
Python | ARIMA Model for Time Series
Forecasting
Similar Reads
Difference Between Autoregressive And Non-Autoregressive Models
In the realm of natural language processing (NLP) and time series analysis, two
fundamental approaches for generating sequences are autoregressive (AR)…
5 min read
Python | ARIMA Model for Time Series Forecasting
A Time Series is defined as a series of data points indexed in time order. The time
order can be daily, monthly, or even yearly. Given below is an example of a Time…
5 min read
Languages DSA
Python Data Structures
Java Algorithms
C++ DSA for Beginners
PHP Basic DSA Problems
GoLang DSA Roadmap
SQL DSA Interview Questions
R Language Competitive Programming
Android Tutorial