Predict and Plot After Fitting arima() Model in R
Last Updated :
06 Aug, 2024
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.
- Autoregressive (AR) part: p is the number of lag observations included in the model.
- Integrated (I) part: d is the number of times that the raw observations are differenced to make the time series stationary.
- 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:
Predict and Plot After Fitting arima() Model in RStep 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:
Predict and Plot After Fitting arima() Model in RConclusion
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.
Similar Reads
Fitting Logarithmic Curve in a Dataset in R Fitting a logarithmic curve to a dataset in R involves finding parameters that best describe the logarithmic relationship between variables. Logarithmic curves are often used to model situations where the growth rate of a variable decreases over time or with increasing values of another variable. He
4 min read
Fit Smooth Curve to Plot of Data in R In this article, we will learn about the concept to fit a smooth curve to the plot of data in R Programming. Smoothing is an important concept in data analysis. It is also known as curve fitting and low pass filtering. The most common non-parametric method used for smoothing is loess() function. Loe
3 min read
Add Moving Average Plot to Time Series Plot in R In time series analysis, a moving average is a widely used technique to smooth out short-term fluctuations and highlight longer-term trends or cycles. R provides several ways to compute and visualize moving averages alongside time series data. This article will guide you through the process of addin
3 min read
Predict() function in R The predict() function in R is a tool used for making predictions from models By using predict(), we can generate predictions based on the fitted model and new input data. The predict() function in R is used to make predictions based on the model object we create. It can predict both the response va
3 min read
Draw Multiple Graphs and Lines in Same Plot in R A visualization can sometimes make more sense when multiple graphs and line plots are combined into one plot. In this article, we will discuss how we can do the same in the R programming language. Method 1: Using base R Base R supports certain methods that can be used to generate the desired plot. I
3 min read
Draw Multiple Function Curves to Same Plot in R In this article, we will discuss how to plot multiple function curves to the same plot in the R programming language. Method 1: In Base R Base R supports a function curve() which can be used to visualize a required function curve. It supports various parameters to edit the curve according to require
2 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 Series that illustrates the number of passengers of an airline per month from the year 1949 to 1960. Time Series Forecasting Time Serie
5 min read
How can I save a plot as an image on the disk in R? In data analysis and visualization, saving the plots as images can be crucial for documentation, reporting, and sharing the results with others. R is a powerful programming language and it can offer various ways to create and save plots. One of the popular packages for creating the plots in the R is
3 min read
Add elements to existing plotly plot in R Plotly is a powerful library in R for creating interactive, web-based visualizations. It allows users to transform static plots into dynamic and interactive graphics, making data exploration more engaging and insightful. This article will guide you through the process of adding elements to existing
3 min read
Area Line Plot in R Area line plots, commonly referred to as filled area plots, are effective data visualisation techniques in R for showing how data evolves over time. They are particularly helpful for displaying trends, distributions, and time series data. In this article, we'll look at how to use the well-liked ggpl
5 min read