# ETL
library(tidyverse)
# Charts
library(highcharter)
# Supply Chain
library(planr)proj_cov
Let’s upload the libraries we’re going to use :
Let’s introduce the function cov_vol() from the R packageplanr.
This function allows to convert and project in volume a targeted coverage initially expressed in periods, based on Demand Forecasts (for example some Sales Forecasts).
Demo dataset
We’re going to use the built-in demo dataset cov_vol_data from the package planr.
It’s a data frame with 4 variables :
- a DFU : 10 items, from “item 000001” until “item 000010”.
- a Period of time : in weekly bucket.
- a Demand : some (weekly) sales forecasts.
- a targeted Coverage : expressed in number of Periods of time.
# get data
df1 <- cov_vol_data
# keep results
initial_data <- df1
# display
initial_data# A tibble: 520 × 4
DFU Period Demand Coverage
<chr> <date> <dbl> <dbl>
1 Item 000001 2022-07-03 364 4
2 Item 000001 2022-07-10 364 4
3 Item 000001 2022-07-17 364 4
4 Item 000001 2022-07-24 260 4
5 Item 000001 2022-07-31 736 4
6 Item 000001 2022-08-07 859 4
7 Item 000001 2022-08-14 859 4
8 Item 000001 2022-08-21 859 4
9 Item 000001 2022-08-28 273 4
10 Item 000001 2022-09-04 349 4
# ℹ 510 more rows
Let’s get a summary.
We can see that we have 10 items, with different targeted Coverage values.
The idea is to calculate and convert those targeted Coverage values, initially expressed in periods of times into volumes, in units, based on the Demand Forecasts.
For example, if we aim to have 4 weeks of coverage on the “Item 000001” : how much stock, in units, does this represent (considering the next 4 weeks Demand Forecasts) ?
# set a working df
df1 <- initial_data
# keep only needed variables
df1 <- df1 |> select(DFU, Coverage)
# keep only needed variables
df1 <- unique(df1)
# display
df1# A tibble: 10 × 2
DFU Coverage
<chr> <dbl>
1 Item 000001 4
2 Item 000002 4
3 Item 000003 4
4 Item 000004 4
5 Item 000005 6
6 Item 000006 6
7 Item 000007 6
8 Item 000008 6
9 Item 000009 6
10 Item 000010 6
Calculate Projected Coverages in volumes
We just need to apply the function cov_vol().
It’s pretty straight forward, as described below, using the 4 needed variables.
The output is a new variable Coverage.Volume which is the conversion of the input variable Coverage, from periods of times into volumes.
# set a working df
df1 <- initial_data
# apply cov_vol() function
df1 <- planr::cov_vol(dataset = df1,
DFU = DFU,
Period = Period,
Demand = Demand,
Coverage = Coverage)
# keep results
calculated_data <- df1
# display
head(calculated_data) DFU Period Demand Coverage Coverage.Volume
1 Item 000001 2022-07-03 364 4 1724
2 Item 000001 2022-07-10 364 4 2219
3 Item 000001 2022-07-17 364 4 2714
4 Item 000001 2022-07-24 260 4 3313
5 Item 000001 2022-07-31 736 4 2850
6 Item 000001 2022-08-07 859 4 2340
If we analyze quickly the table for the “Item 000001” and the Period “2022-07-03” we see :
a Coverage of 4 weeks : means that the
Coverage.Volumemust be equal to the next 4 weeks of Demand Forecasts.the sum of next 4 weeks of Demand Forecasts is 364+364+260+736 = 1724 units.
- which is the value of the variable
Coverage.Volumeduring that Period.
- which is the value of the variable
Let’s look at it through a chart.
Chart
Now, let’s visualize the results, looking at the item “item 000001”, using the library highcharter.
The Coverage value is 4, which means that it’s equivalent to 4 periods of time.
As we saw previously, the new variable Coverage.Volume is equal to the sum of the Demand Forecasts of the 4 next periods of time.
We’re going to display a chart with 2 lines :
the weekly Demand, in units.
the related weekly projected Coverage, in units.
- equivalent to the Coverage, initially expressed in periods of time.
# set working dataframe
df1 <- calculated_data
# filter
df1 <- df1 |> filter(DFU == "Item 000001")
# formatting for a better display
df1$Coverage.Volume <- as.integer(df1$Coverage.Volume)
# chart
highchart() |>
hc_title(text = "Projected Coverages & Demand") |>
hc_subtitle(text = "in units") |>
hc_add_theme(hc_theme_google()) |>
hc_xAxis(categories = df1$Period) |>
hc_add_series(name = "Coverage",
color = "steelblue",
dataLabels = list(align = "center", enabled = TRUE), # we can add this line
data = df1$Coverage.Volume) |>
hc_add_series(name = "Demand",
color = "gold",
dataLabels = list(align = "center", enabled = TRUE), # we can add this line
data = df1$Demand)