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
213: Monitoring Model Drift in Production
Imagine you bought a high-end GPS map for a city in 2015. For a few years, it's perfect. You get everywhere on time. But then, the city builds a new highway system, closes a main bridge for permanent pedestrian use, and rezones the industrial district into luxury condos. The GPS isn't "broken"—the software is running exactly as it did on day one—but the world it was designed to navigate has changed. If you keep following those 2015 directions, you're going to end up driving into a river.
In R, model drift is that exact disconnect. Your model is the GPS, and the production data is the city. When the statistical properties of your input data shift (data drift) or the relationship between your features and the target variable changes (concept drift), your model's accuracy plummets even though the code hasn't changed a single line.
Spotting the Shift in Your Features
The first thing we need to do is realize that we can't just monitor the model's accuracy. Why? Because in production, you often don't get the "ground truth" (the actual result) for weeks or months. If you're predicting loan defaults, you won't know if you were wrong until the customer actually defaults. You need a leading indicator. That's where we monitor the distribution of the features themselves.
I usually lean on the Kolmogorov-Smirnov test in R for this. It's a non-parametric test that tells us if two samples come from the same distribution. If the distribution of a feature like monthly_spend in your training set differs significantly from the monthly_spend you're seeing in production today, your "map" is outdated.
# Let's say 'train_data' is what we used to build the model
# and 'prod_data' is the last 30 days of live traffic.
# Check if the distribution of 'account_age' has shifted
drift_test <- ks.test(train_data$account_age, prod_data$account_age)
if(drift_test$p.value < 0.05) {
message("Alert: Significant data drift detected in account_age!")
}
Operationalizing with Vetiver
Doing manual KS tests for twenty different features is a nightmare. I don't want to write a dozen if-statements every time I deploy. In the modern R ecosystem, we use the vetiver package. It's designed specifically to bridge the gap between a trained model and a production API.
vetiver allows you to version your models and, more importantly, it integrates with pins to track the data the model is seeing. Instead of guessing, you can log your production predictions and their inputs to a board. When you periodically compare these logs against your training baseline, you can visualize the drift. I find that plotting the density of a feature over time is far more convincing to stakeholders than a p-value; seeing a distribution curve physically slide to the left makes the need for retraining obvious.
When to Hit the Panic Button
Here is a bit of a professional opinion: not all drift requires a full retrain. Sometimes you have "seasonal drift." If you're predicting retail sales, your data will drift every November because of Black Friday. That's not a failure of the model; it's a characteristic of the business.
You have to distinguish between transient drift (temporary spikes) and structural drift (a fundamental change in consumer behavior). I always recommend setting up a "drift threshold." Don't retrain the moment a p-value hits 0.049. Instead, look for sustained drift over a window of time. If the distribution stays shifted for two weeks, that's when you pull the trigger on a new training run.
📋 Practical Task
Build a Feature Drift Detector for Customer Churn
You are maintaining a churn prediction model. You've noticed that the model's performance has dipped, and you suspect the avg_session_duration feature has drifted since the latest app update.
Your Task: Write an R script that performs the following:
- Create a synthetic training vector
train_sessionwith 1,000 values drawn from a normal distribution (mean = 120, sd = 20). - Create a synthetic production vector
prod_sessionwith 1,000 values drawn from a normal distribution (mean = 105, sd = 25) to simulate a shift in user behavior. - Use the
ks.test()function to compare these two distributions. - Write a conditional statement that prints "RETRAIN MODEL" if the p-value is less than 0.01, and "STABLE" otherwise.
There are no comments for now.