Open In App

Predict and Plot After Fitting arima() Model in R

Last Updated : 06 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Autoregressive Integrated Moving Average (ARIMA) models are a powerful class of models for forecasting time series data. This article will cover the theoretical foundation of ARIMA models, their components, and a step-by-step guide to fitting, predicting, and plotting results using ARIMA models in R.

Components of ARIMA

An ARIMA model is characterized by three parameters: p, d, and q.

  1. Autoregressive (AR) part: p is the number of lag observations included in the model.
  2. Integrated (I) part: d is the number of times that the raw observations are differenced to make the time series stationary.
  3. Moving Average (MA) part: q is the size of the moving average window.

Now we will discuss step by step implementation of Fitting an ARIMA Model in R Programming Language.

Step 1: Install and Load Necessary Libraries

First, ensure that the forecast package is installed and loaded, as it provides functions for fitting and forecasting ARIMA models.

R
install.packages("forecast")
library(forecast)

Step 2: Load and Inspect the Data

For this example, we'll use a built-in dataset from the datasets package in R.

R
# Load the AirPassengers dataset
data("AirPassengers")
ts_data <- AirPassengers

# Plot the time series data
plot(ts_data, main = "Monthly Air Passengers", ylab = "Number of Passengers", 
     xlab = "Time")

Output:

fg
Predict and Plot After Fitting arima() Model in R

Step 3: Fit the ARIMA Model

Use the auto.arima() function to automatically select the best ARIMA model based on AIC, AICc, or BIC value.

R
# Fit the ARIMA model
fit <- auto.arima(ts_data)
summary(fit)

Output:

Series: ts_data 
ARIMA(2,1,1)(0,1,0)[12]

Coefficients:
ar1 ar2 ma1
0.5960 0.2143 -0.9819
s.e. 0.0888 0.0880 0.0292

sigma^2 = 132.3: log likelihood = -504.92
AIC=1017.85 AICc=1018.17 BIC=1029.35

Training set error measures:
ME RMSE MAE MPE MAPE MASE ACF1
Training set 1.3423 10.84619 7.86754 0.420698 2.800458 0.245628 -0.00124847

Step 4: Make Predictions

Use the fitted ARIMA model to forecast future values.

R
# Forecast the next 24 periods (e.g., months)
forecasted_values <- forecast(fit, h = 24)
print(forecasted_values)

Output:

         Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
Jan 1961 445.6349 430.8903 460.3795 423.0851 468.1847
Feb 1961 420.3950 403.0907 437.6993 393.9304 446.8596
Mar 1961 449.1983 429.7726 468.6240 419.4892 478.9074
Apr 1961 491.8399 471.0270 512.6529 460.0092 523.6707
May 1961 503.3945 481.5559 525.2330 469.9953 536.7937
Jun 1961 566.8624 544.2637 589.4612 532.3007 601.4242
Jul 1961 654.2602 631.0820 677.4383 618.8122 689.7081
Aug 1961 638.5975 614.9704 662.2246 602.4630 674.7320
Sep 1961 540.8837 516.9028 564.8647 504.2081 577.5594
Oct 1961 494.1266 469.8624 518.3909 457.0177 531.2356
Nov 1961 423.3327 398.8381 447.8273 385.8715 460.7939
Dec 1961 465.5076 440.8229 490.1923 427.7556 503.2596
Jan 1962 479.2908 448.9986 509.5831 432.9629 525.6188
Feb 1962 454.1768 421.7184 486.6353 404.5359 503.8178
Mar 1962 483.0870 448.7343 517.4396 430.5491 535.6248
Apr 1962 525.8193 490.1122 561.5263 471.2101 580.4284
May 1962 537.4507 500.6863 574.2151 481.2244 593.6770
Jun 1962 600.9839 563.3924 638.5754 543.4927 658.4752
Jul 1962 688.4370 650.1834 726.6907 629.9331 746.9410
Aug 1962 672.8213 634.0292 711.6134 613.4940 732.1487
Sep 1962 575.1475 535.9102 614.3847 515.1393 635.1557
Oct 1962 528.4242 488.8131 568.0352 467.8443 589.0040
Nov 1962 457.6590 417.7293 497.5886 396.5918 518.7261
Dec 1962 499.8582 459.6529 540.0634 438.3695 561.3468

Step 5: Plot the Forecast

Plot the original time series data along with the forecasted values and prediction intervals.

R
# Plot the forecast
plot(forecasted_values, main = "ARIMA Forecast for Air Passengers")

Output:

fg
Predict and Plot After Fitting arima() Model in R

Conclusion

ARIMA models are versatile and widely used for time series forecasting. This guide provided a comprehensive overview of the theory behind ARIMA models and demonstrated how to fit, predict, and plot forecasts using ARIMA in R. By following these steps, you can effectively model and forecast your own time series data.


Next Article

Similar Reads