0% found this document useful (0 votes)
12 views3 pages

R Script for Demand Forecasting in SCM

The document provides an R script for demand forecasting in supply chain management using a time-series ARIMA model. It includes steps for data loading, cleaning, preparation, model fitting, forecasting, and plotting results. Key concepts such as demand aggregation, time series objects, and ARIMA modeling are also discussed to aid understanding.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

R Script for Demand Forecasting in SCM

The document provides an R script for demand forecasting in supply chain management using a time-series ARIMA model. It includes steps for data loading, cleaning, preparation, model fitting, forecasting, and plotting results. Key concepts such as demand aggregation, time series objects, and ARIMA modeling are also discussed to aid understanding.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Here is my data.

Need R scripts for Demand forecasting based on supply chain


management.
That's an excellent request! Demand forecasting is a crucial aspect of Supply Chain
Management (SCM).
Since you've provided a CSV file, I'll assume you want to use the data in the file
for a time-series based forecasting model. The R script below uses the forecast
package, which is standard for time series analysis in R, and specifically the
[Link]() function, as it automatically selects the best ARIMA model (a powerful
forecasting technique).

Here is an R script that reads your data, prepares it for time series analysis by
aggregating and converting it to a time series object, fits an ARIMA model, and
then generates a 12-period forecast (e.g., 12 weeks or 12 months, depending on the
data's inherent frequency).

R Script for Demand Forecasting


This script first cleans and prepares the grocery_chain_data.csv data, then applies
an ARIMA model for forecasting.

# 1. Load Required Libraries


# [Link](c("tidyverse", "readr", "xts", "forecast")) # Uncomment if you
need to install them
library(readr)
library(tidyverse)
library(xts)
library(forecast)

# --- Configuration ---


FILE_PATH <- "grocery_chain_data.csv"
DATE_COLUMN <- "transaction_date"
DEMAND_COLUMN <- "quantity"
FORECAST_HORIZON <- 12 # Forecast for the next 12 periods (e.g., 12 weeks/months)

# 2. Data Loading and Cleaning


df <- read_csv(FILE_PATH)

# Convert the date column to a proper Date format


df[[DATE_COLUMN]] <- [Link](df[[DATE_COLUMN]])

# 3. Data Preparation: Aggregate Demand by Date


# We aggregate the 'quantity' (demand) by the 'transaction_date'
demand_ts_data <- df %>%
group_by(!!sym(DATE_COLUMN)) %>%
summarise(Total_Demand = sum(!!sym(DEMAND_COLUMN), [Link] = TRUE)) %>%
ungroup() %>%
# Arrange by date to ensure proper time series order
arrange(!!sym(DATE_COLUMN))

# 4. Convert to Time Series Object (ts)


# To create a 'ts' object, we need to determine the frequency.
# A daily time series is often too sparse/irregular for basic 'ts'.
# A common practice is to convert to a lower frequency (e.g., weekly or monthly).
# Here, we will use a frequency of 7, assuming weekly data for simplicity and if
the data spans many months/years.
# Note: The choice of frequency (e.g., 7 for weekly, 12 for monthly) depends on the
true
# periodicity and sampling of your data. If your data is truly daily, you might use
'xts' instead.

# Determine the start date


start_date <- min(demand_ts_data[[DATE_COLUMN]])
# Convert to a numeric representation (year, week or year, month)
start_period <- c([Link](format(start_date, "%Y")),
[Link](format(start_date, "%W")))

# Create the time series object (using the aggregated 'Total_Demand')


# Using frequency=52 for weekly data as a robust starting point.
demand_ts <- ts(demand_ts_data$Total_Demand,
start = start_period,
frequency = 52) # Assuming weekly frequency (52 weeks in a year)

# 5. Model Fitting (ARIMA)


# [Link]() automatically selects the best ARIMA model (p, d, q) based on
AIC/AICc
arima_model <- [Link](demand_ts)

# Print the model summary to see the selected parameters


print("--- ARIMA Model Summary ---")
print(summary(arima_model))

# 6. Forecasting
# Generate a forecast for the next 12 periods (defined by FORECAST_HORIZON)
forecast_result <- forecast(arima_model, h = FORECAST_HORIZON)

# Print the forecast values


print("--- Demand Forecast (Next 12 Periods) ---")
print(forecast_result)

# 7. Plotting the Forecast


png("demand_forecast_plot.png", width = 800, height = 500)
plot(forecast_result, main = "ARIMA Demand Forecast",
ylab = "Total Demand (Quantity)", xlab = "Time (Weeks)")
[Link]()

# Display the fitted values and residuals for diagnostics (optional)


# print("--- Fitted Values and Residuals ---")
# print(cbind(Fitted = fitted(arima_model), Residuals = residuals(arima_model)))

# 8. Extracting Forecast Data to a CSV (Optional but Recommended)


forecast_data_df <- [Link](
Period = 1:FORECAST_HORIZON,
Point_Forecast = forecast_result$mean,
Lo_80 = forecast_result$lower[, 1],
Hi_80 = forecast_result$upper[, 1],
Lo_95 = forecast_result$lower[, 2],
Hi_95 = forecast_result$upper[, 2]
)

write_csv(forecast_data_df, "demand_forecast_results.csv")
print("Saved forecast results to demand_forecast_results.csv")
💡 Key SCM and R Concepts
Demand Aggregation (Step 3): In SCM, you typically forecast for a specific planning
horizon. Here, we aggregated all transactional demand (quantity) by day. If you
needed to forecast for a specific product or store, you would add those columns to
the group_by() function.
Time Series Object (ts - Step 4): R's core time series functions require the data
to be in a ts object, which explicitly defines the start time and frequency (e.g.,
52 for weekly, 12 for monthly). Incorrect frequency is a common error in time
series analysis.

ARIMA Model (Step 5): AutoRegressive Integrated Moving Average. It models the time
series based on its past values (AR), the relationship between an observation and a
residual error (MA), and differencing (I) to make the series stationary.
[Link]() automates the complex process of selecting the best parameters
(p,d,q).

Forecast Horizon (h - Step 6): This is the number of future periods you want to
forecast for, directly tied to your SCM lead times and planning cycles (e.g., 12
weeks for production planning).

Let me know if you would like a script for a different model (like Prophet for
forecasting with holiday/seasonality effects) or if you want to focus on
forecasting a single product!

You might also like