ARIMA Modelling and Forecasting Using R
Experiment: ARIMA Modelling and Forecasting Using R
1. Experiment Title
Implementation of ARIMA Model for Time Series Analysis and Forecasting Using R
2. Aim
To study time series data and implement an ARIMA model in R for analysing patterns and forecasting future values.
3. Objectives
After completing this experiment, students should be able to:
- Understand the concept of time series data.
- Create and visualize a time series in R.
- Understand the concept of stationarity.
- Perform differencing to make a time series stationary.
- understand the parameters , , and in an ARIMA model.
- Build an ARIMA model using R.
- Forecast future values using the ARIMA model.
- Visualize the forecasted values.
4. Theory
4.1 Time Series
A time series is a sequence of observations recorded over time at regular intervals.
Examples include:
- Daily temperature
- Monthly sales
- Annual population
- Monthly rainfall
- Daily stock prices
Time series analysis is used to identify patterns in historical data and predict future values.
4.2 Components of a Time Series
A time series may contain the following components:
Trend
A long-term increasing or decreasing movement in the data.
Seasonality
A pattern that repeats at regular intervals.
Cyclical Variation
Long-term fluctuations occurring over a period of time.
Irregular Variation
Random and unpredictable changes in the data.
4.3 Stationarity
A time series is called stationary when its statistical properties remain relatively constant over time.
A stationary series generally has:
- Constant mean
- Constant variance
- No strong trend
Many time series models, including ARIMA, require the data to be stationary or approximately stationary.
4.4 Differencing
If a time series contains a trend, differencing can be used to remove the trend.
The first difference is calculated as:
where:
- = Current observation
- = Previous observation
For example:
Original Data: 100, 110, 125, 140 First Difference: 10, 15, 15
The number of times differencing is performed is represented by the parameter d in ARIMA.
4.5 ARIMA Model
ARIMA stands for:
AR – AutoRegressive
I – Integrated
MA – Moving Average
An ARIMA model is represented as:
where:
| Parameter | Meaning |
|---|---|
| p | Order of the AutoRegressive component |
| d | Number of times the series is differenced |
| q | Order of the Moving Average component |
4.6 AutoRegressive (AR) Component
The AutoRegressive component uses previous observations to predict the current value.
For example:
The current month's sales may depend on the sales of previous months.
The parameter p indicates the number of previous observations considered.
4.7 Integrated (I) Component
The Integrated component represents the number of times the data must be differenced to make it stationary.
The parameter d represents the order of differencing.
For example:
- → No differencing
- → First-order differencing
- → Second-order differencing
4.8 Moving Average (MA) Component
The Moving Average component uses previous forecast errors to model the current value.
The parameter q represents the number of previous error terms used.
5. Problem Statement
A retail company records its monthly sales over a period of three years. The company wants to analyse the sales data and build an ARIMA model to forecast future sales.
The monthly sales data are given below:
| Month | Sales |
|---|---|
| Jan | 120 |
| Feb | 125 |
| Mar | 128 |
| Apr | 135 |
| May | 140 |
| Jun | 145 |
| Jul | 150 |
| Aug | 155 |
| Sep | 160 |
| Oct | 165 |
| Nov | 170 |
| Dec | 175 |
| Jan | 180 |
| Feb | 185 |
| Mar | 190 |
| Apr | 198 |
| May | 205 |
| Jun | 210 |
| Jul | 218 |
| Aug | 225 |
| Sep | 230 |
| Oct | 238 |
| Nov | 245 |
| Dec | 250 |
| Jan | 258 |
| Feb | 265 |
| Mar | 272 |
| Apr | 280 |
| May | 288 |
| Jun | 295 |
| Jul | 305 |
| Aug | 315 |
| Sep | 325 |
| Oct | 335 |
| Nov | 345 |
| Dec | 355 |
Using the given data:
- Create a monthly time series.
- Visualize the time series.
- Perform differencing.
- Plot the differenced series.
- Examine the ACF and PACF plots.
- Build an ARIMA model.
- Forecast sales for the next 6 months.
- Visualize the forecasted values.
6. R Program
Step 1: Install and Load Required Package
The forecast package is useful for ARIMA modelling and forecasting.
# Install the forecast package (run only once) install.packages("forecast") # Load the package library(forecast)
Complete Program
# ------------------------------------------------- # ARIMA Modelling and Forecasting in R # ------------------------------------------------- # Load forecast package library(forecast) # ------------------------------------------------- # Step 1: Create Monthly Sales Data # ------------------------------------------------- sales <- c( 120, 125, 128, 135, 140, 145, 150, 155, 160, 165, 170, 175, 180, 185, 190, 198, 205, 210, 218, 225, 230, 238, 245, 250, 258, 265, 272, 280, 288, 295, 305, 315, 325, 335, 345, 355 ) # ------------------------------------------------- # Step 2: Create Time Series Object # ------------------------------------------------- sales_ts <- ts( sales, start = c(2023, 1), frequency = 12 ) # Display Time Series cat("Monthly Sales Time Series:\n") print(sales_ts) # ------------------------------------------------- # Step 3: Plot Original Time Series # ------------------------------------------------- plot( sales_ts, main = "Monthly Sales Time Series", xlab = "Year", ylab = "Sales" ) # ------------------------------------------------- # Step 4: Perform First Differencing # ------------------------------------------------- sales_diff <- diff(sales_ts) # Plot Differenced Series plot( sales_diff, main = "First Differenced Sales Series", xlab = "Year", ylab = "Differenced Sales" ) # ------------------------------------------------- # Step 5: Plot ACF # ------------------------------------------------- acf( sales_diff, main = "ACF of Differenced Sales" ) # ------------------------------------------------- # Step 6: Plot PACF # ------------------------------------------------- pacf( sales_diff, main = "PACF of Differenced Sales" ) # ------------------------------------------------- # Step 7: Build ARIMA Model # ------------------------------------------------- model <- arima( sales_ts, order = c(1, 1, 1) ) # Display Model cat("\nARIMA Model:\n") print(model) # Display Model Summary cat("\nModel Summary:\n") print(summary(model)) # ------------------------------------------------- # Step 8: Forecast Future Sales # ------------------------------------------------- forecast_values <- forecast( model, h = 6 ) # Display Forecast cat("\nSales Forecast for Next 6 Months:\n") print(forecast_values) # ------------------------------------------------- # Step 9: Plot Forecast # ------------------------------------------------- plot( forecast_values, main = "ARIMA Forecast for Monthly Sales" )Output
Monthly Sales Time Series: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 2023 120 125 128 135 140 145 150 155 160 165 170 175 2024 180 185 190 198 205 210 218 225 230 238 245 250 2025 258 265 272 280 288 295 305 315 325 335 345 355 ARIMA Model: Call: arima(x = sales_diff, order = c(1, 1, 1)) Coefficients: ar1 ma1 -0.2522 -0.5081 s.e. 0.2044 0.1504 sigma^2 estimated as 1.548: log likelihood = -55.97, aic = 117.94 Model Summary: Call: arima(x = sales_diff, order = c(1, 1, 1)) Coefficients: ar1 ma1 -0.2522 -0.5081 s.e. 0.2044 0.1504 sigma^2 estimated as 1.548: log likelihood = -55.97, aic = 117.94 Training set error measures: ME RMSE MAE MPE MAPE MASE Training set 0.3830699 1.226143 0.8629214 2.397357 12.99253 0.4410487 ACF1 Training set -0.183443 Sales Forecast for Next 6 Months: Point Forecast Lo 80 Hi 80 Lo 95 Hi 95 Jan 2026 9.932426 8.338119 11.52673 7.494145 12.37071 Feb 2026 9.949469 8.310021 11.58892 7.442150 12.45679 Mar 2026 9.945171 8.167283 11.72306 7.226127 12.66421 Apr 2026 9.946255 8.066404 11.82611 7.071271 12.82124 May 2026 9.945981 7.963324 11.92864 6.913768 12.97819 Jun 2026 9.946050 7.867141 12.02496 6.766632 13.12547
7. Explanation of the Program
Step 1: Creating Sales Data
sales <- c(120, 125, 128, ...)
The vector contains 36 monthly sales observations representing three years of data.
Step 2: Creating a Time Series
sales_ts <- ts( sales, start = c(2023, 1), frequency = 12 )
The ts() function converts the numerical data into a time series.
Arguments
| Argument | Meaning |
|---|---|
sales | Sales data |
start = c(2023,1) | Starts in January 2023 |
frequency = 12 | 12 observations per year |
Therefore, R understands that the data represent monthly observations.
Step 3: Plotting the Time Series
plot(sales_ts)
This displays the sales pattern over time.
Students can observe whether the data show:
- An increasing trend
- A decreasing trend
- Seasonal patterns
- Random variations
Step 4: Differencing the Series
sales_diff <- diff(sales_ts)
The diff() function calculates the difference between consecutive observations.
Mathematically:
Differencing helps remove trends and make the series more stationary.
Step 5: ACF Plot
acf(sales_diff)
The Autocorrelation Function (ACF) measures the relationship between observations at different time lags.
It helps in identifying the possible value of:
for the Moving Average component.
Step 6: PACF Plot
pacf(sales_diff)
The Partial Autocorrelation Function (PACF) helps measure the direct relationship between observations after removing the effects of intermediate lags.
It helps in identifying the possible value of:
for the AutoRegressive component.
Step 7: Building the ARIMA Model
model <- arima( sales_ts, order = c(1, 1, 1) )
This creates the model:
This means:
- p = 1 → One AutoRegressive term
- d = 1 → First-order differencing
- q = 1 → One Moving Average term
Step 8: Forecasting
forecast_values <- forecast( model, h = 6 )
The parameter:
h = 6
means that the model forecasts values for the next 6 months.
The output generally contains:
- Point forecasts
- Lower confidence limits
- Upper confidence limits
Step 9: Plotting the Forecast
plot(forecast_values)
The graph displays:
- Historical sales values
- Forecasted sales values
- Prediction intervals
This helps students visually understand how ARIMA predicts future observations.
8. Understanding ACF and PACF
ACF – Autocorrelation Function
The ACF plot shows the correlation between:
and its previous values:
It is generally useful for identifying possible MA components.
PACF – Partial Autocorrelation Function
PACF measures the direct relationship between observations at different lags after removing the effects of intermediate observations.
It is generally useful for identifying possible AR components.
9. ARIMA Model Selection
In this experiment, we use:
for learning purposes.
In practice, different values of , , and can be tested.
R also provides the function:
auto.arima()
which automatically searches for a suitable ARIMA model.
Example:
auto_model <- auto.arima(sales_ts) print(auto_model)
For a basic lab experiment, students can compare the manually created model with the automatically selected model.
10. Important Functions Used
| Function | Purpose |
|---|---|
ts() | Creates a time series object |
plot() | Visualizes the time series |
diff() | Performs differencing |
acf() | Creates an ACF plot |
pacf() | Creates a PACF plot |
arima() | Builds an ARIMA model |
forecast() | Forecasts future values |
auto.arima() | Automatically selects an ARIMA model |
11. Result
Thus, the monthly sales data were successfully analysed using time series techniques in R. The time series was visualized, differencing was performed to study stationarity, ACF and PACF plots were examined, an ARIMA(1,1,1) model was developed, and future sales values were forecasted for the next six months.
Comments
Post a Comment