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:

  1. Understand the concept of time series data.
  2. Create and visualize a time series in R.
  3. Understand the concept of stationarity.
  4. Perform differencing to make a time series stationary.
  5. understand the parameters pp, dd, and qq in an ARIMA model.
  6. Build an ARIMA model using R.
  7. Forecast future values using the ARIMA model.
  8. 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:

Yt′=Yt−Yt−1Y'_t = Y_t-Y_{t-1}

where:

  • YtY_t = Current observation
  • Yt−1Y_{t-1} = 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:

ParameterMeaning
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:

  • d=0d=0 → No differencing
  • d=1d=1 → First-order differencing
  • d=2d=2 → 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:

MonthSales
Jan120
Feb125
Mar128
Apr135
May140
Jun145
Jul150
Aug155
Sep160
Oct165
Nov170
Dec175
Jan180
Feb185
Mar190
Apr198
May205
Jun210
Jul218
Aug225
Sep230
Oct238
Nov245
Dec250
Jan258
Feb265
Mar272
Apr280
May288
Jun295
Jul305
Aug315
Sep325
Oct335
Nov345
Dec355

Using the given data:

  1. Create a monthly time series.
  2. Visualize the time series.
  3. Perform differencing.
  4. Plot the differenced series.
  5. Examine the ACF and PACF plots.
  6. Build an ARIMA model.
  7. Forecast sales for the next 6 months.
  8. 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

ArgumentMeaning
salesSales data
start = c(2023,1)Starts in January 2023
frequency = 1212 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:

Yt′=Yt−Yt−1Y'_t=Y_t-Y_{t-1}

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:

qq

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:

pp

for the AutoRegressive component.


Step 7: Building the ARIMA Model

model <- arima(
  sales_ts,
  order = c(1, 1, 1)
)

This creates the model:

ARIMA(1,1,1)ARIMA(1,1,1)

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:

YtY_t

and its previous values:

Yt−1,Yt−2,Yt−3,...Y_{t-1},Y_{t-2},Y_{t-3},...

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:

ARIMA(1,1,1)ARIMA(1,1,1)

for learning purposes.

In practice, different values of pp, dd, and qq 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

FunctionPurpose
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

Popular posts from this blog

Statistical Methods Lab ( R Language) PCCBL308 Semester 3 KTU BTech CB and CU 2024 Scheme - Dr Binu V P

Programs in R - using control statements - Assignment 2

Programs to try using Functions in R - Assignment 3