R
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Data Structures
-
Section 4: Data Manipulation
-
Section 5: Visualization and Statistics
-
Section 6: Advanced R
-
Section 7: Practical Projects
-
Section 8: Interview Practice
-
Section 9: More Practice Exercises
-
Section 10: Shiny Apps in Depth
-
Section 11: More Data Wrangling
-
Section 12: Tidyverse Deep Dive
-
Section 13: Statistical Modeling Deep Dive
-
Section 14: Machine Learning in R
-
Section 15: R Visualization Deep Dive
-
Section 16: R Package Development Deep Dive
-
Section 17: R for Reproducible Research
-
Section 18: R and Databases
-
Section 19: R Performance Optimization
-
Section 20: Bioinformatics and Specialized R
-
Section 21: More Shiny Practice
-
Section 22: More Practice Exercises
-
Section 23: R Data Structures Deep Dive
-
Section 24: More Interview and Review
-
Section 25: R for Business Analytics
-
Section 26: R Text Mining and NLP
-
Section 27: R Spatial Data Analysis
-
Section 28: R Deep Learning
-
Section 29: Advanced Statistical Techniques
-
Section 30: R Object Systems Deep Dive
-
Section 31: R Environments and Metaprogramming
-
Section 32: R for Finance
-
Section 33: R for Clinical and Health Data
-
Section 34: More Shiny Advanced Practice
-
Section 35: R Data Cleaning Deep Dive
-
Section 36: R Reporting Automation
-
Section 37: More Practical Projects Round 2
-
Section 38: R Ecosystem and Career
-
Section 39: More Visualization Practice
-
Section 40: R for Experimentation
-
Section 41: R for Genomics and Bioinformatics Deep Dive
-
Section 42: R for Marketing Analytics
-
Section 43: R Data Import/Export Deep Dive
-
Section 44: R String Processing Deep Dive
-
Section 45: R for Actuarial and Insurance Analytics
-
Section 46: R Testing and Quality Assurance Deep Dive
-
Section 47: R Version Control and Collaboration
-
Section 48: R Advanced Functional Programming
-
Section 49: R for Supply Chain and Operations
-
Section 50: More Practice Exercises Round 3
-
Section 51: R Dashboards and BI Integration
-
Section 52: R Data Governance and Ethics
-
Section 53: More Modeling Practice
-
Section 54: R Final Capstone Projects
-
Section 55: R for Sports Analytics
-
Section 56: More Interview Practice Round 2
-
Section 57: R Networking and APIs
-
Section 58: R for Environmental Science
-
Section 59: R Notebook and Documentation Practices
-
Section 60: More Data Wrangling Mastery
-
Section 61: R for A/B Testing at Scale
-
Section 62: R Package Ecosystem Deep Dive
195: Demand Forecasting for Supply Chains
I've seen this happen a dozen times in warehouse management systems: a developer is asked to build a "demand forecast," and they treat it as a standard regression problem. They take the last two years of sales for something—let's say, high-end organic espresso beans—and run a simple linear model against a time index. On a scatter plot, it looks like a nice upward line. It feels intuitive. But in a supply chain, that approach is a recipe for either a bankrupt warehouse full of rotting beans or a series of angry phone calls from clients who are out of stock.
The Linear Trap and the Myth of the Average
The naive approach usually looks like this in R: you load your data into a data frame and call lm(sales ~ date_index). The problem is that linear regression assumes each data point is independent. In supply chain demand, that's almost never true. If you sold a massive amount of espresso beans last Tuesday, it’s highly likely you’ll sell a significant amount this Tuesday. There is autocorrelation—the data remembers where it was.
# The "Naive" way - Avoid this for supply chains model_naive <- lm(units_sold ~ time_index, data = coffee_data) forecast_naive <- predict(model_naive, newdata = future_dates)When you use a linear model, you're essentially forecasting the "average trend." But supply chains don't live in the average. They live in the peaks and valleys. If your espresso beans peak every December for the holiday season, a linear model will under-forecast in December (leading to stockouts) and over-forecast in January (leading to wasted capital). You're ignoring seasonality and the stochastic nature of demand, treating a rhythmic pulse like a straight line.
Moving to State-Space and the fpp3 Ecosystem
If we're going to do this right, we need to stop thinking about "regression" and start thinking about "time series." I always steer my team toward the
fpp3suite (which includestsibble,feasts, andfable). Instead of a flat data frame, we use atsibble, which explicitly understands that the data is indexed by time. This allows us to use ETS (Error, Trend, Seasonality) models.An ETS model doesn't just look at the slope; it decomposes the signal. It asks: "How much of this is a long-term trend? How much is a repeating seasonal pattern? And how much is just random noise?" For our coffee beans, an ETS model can recognize that every Q4 sees a 30% bump, regardless of whether the overall yearly trend is growing or shrinking.
library(fpp3) # The "Better" way - Using a tsibble and ETS coffee_ts <- coffee_data %>% as_tsibble(index = date) fit <- coffee_ts %>% model( ets = ETS(units_sold) ) forecast_better <- fit %>% forecast(h = "3 months")The trade-off here is complexity. You're no longer looking at a simple
y = mx + bequation. You're dealing with smoothing parameters. But the payoff is a forecast that actually mirrors the reality of a warehouse. You get a "fan chart" of uncertainty rather than a single line, which is critical for calculating safety stock.When the Sophisticated Model Breaks
Now, I should warn you: don't treat ETS or ARIMA as magic. These models are backward-looking. They assume the future will behave like a weighted version of the past. If your coffee supplier suddenly goes bankrupt or a new competitor opens a shop across the street, your
fablemodel will confidently predict demand based on a world that no longer exists.I usually tell my colleagues that the model provides the baseline, but the human provides the "overlay." If you know a massive marketing campaign is hitting next month, you manually adjust the model's output. The goal isn't to replace the supply chain manager; it's to stop them from having to do the tedious math of seasonality by hand so they can focus on the anomalies the model can't see.
📋 Practical Task
Implementing an Espresso Bean Demand Forecast with MAPE Validation
You have been provided with a dataset coffee_sales_data containing two years of weekly sales for "Artisan Roast Coffee - 1kg Bag". Your goal is to compare a naive seasonal forecast against an automated ETS model and determine which one is more reliable using Mean Absolute Percentage Error (MAPE).
Requirements:
- Convert the raw data frame into a
tsibble. - Create a training set (first 18 months) and a test set (last 6 months).
- Fit two models to the training set: a
SNAIVE(Seasonal Naive) model and anETSmodel. - Forecast the test set period for both models.
- Calculate the MAPE for both forecasts to identify which model more accurately predicted the actual sales.
Expected Output: A comparison table showing the MAPE for both the SNAIVE and ETS models, and a short print statement indicating which model should be deployed to the production supply chain system.
There are no comments for now.