Your First End-to-End Model in R
Three lessons of groundwork come together here. You learned to frame a business question as a prediction task (Lesson 1), why a more flexible model can still fail on new data (Lesson 2), and how to set aside honest data with no leaks (Lesson 3). Now you build the whole thing: one model, start to finish.
Remember Priya from Lesson 1? She runs retention at FreshBox, the meal-kit subscription, and wanted to catch customers who are about to cancel so a small win-back offer reaches only them. Back then we only framed her problem. Today we actually build her model: split, fit, predict, evaluate, and make every number reproducible.
By the end of this lesson you will be able to:
- Run a complete workflow on one dataset: split, fit, predict, evaluate
- Fit a classifier on the training rows and score customers it has never seen
- Judge the model with a confusion matrix and the metric that matches Priya's decision, not just accuracy
- Make the entire run reproducible with a fixed seed and one script
Prerequisites: you can run R and read its output, and you have the three earlier lessons: Framing a Problem as ML, The Bias-Variance Tradeoff, and Train, Validation, Test, and Data Leakage.
The five stages below are the whole journey. We walk through each one on Priya's real data.
One table, and a seed before anything else
Priya's raw data is one table: one row per FreshBox customer, holding the handful of facts she knows at decision time. Each lesson here runs in a fresh session, so we build that table inline. The very first line is set.seed, before a single random number is drawn, because reproducibility starts at the first random call, not at the end.
Nine hundred customers, of whom 257 churned. The four features are the classic customer-health signals: how recently someone ordered, how often, how much, and how long they have been a subscriber. churned (yes or no) is the target Priya wants to predict. That last column is decided by a rule we will never hand the model, exactly as real churn is decided by the world, not by us.