Stacking and the Super Learner
In Lesson 4 you gave a regression model a conscience: a Gaussian process that predicts a value AND admits how sure it is. This lesson does something different with your growing toolbox. Instead of crowning one winner and discarding the rest, it keeps several models and teaches a second model to blend them.
Meet Nadia. She runs a coffee cart outside a train station, and she has logged cups sold in 160 fifteen-minute windows: the hour of day, and the cups she sold. Demand has a morning rush, an evening rush, and a sharp jump at noon when the office across the road breaks for lunch. Nadia has built three models to forecast demand for her ordering app: a straight line, a smooth curve, and a regression tree. Her app has room for one. Which does she ship?
The surprising answer this lesson defends: none of them alone. By the end you will be able to:
- Explain why picking a single best model leaves accuracy on the table, and why a plain average can even hurt
- Build a stacking ensemble in R: out-of-fold predictions, a meta-learner, and the blend weights it learns
- See why the meta-learner must be trained on out-of-fold predictions, not in-sample ones, and what goes wrong when it is not
- Turn a stack into a Super Learner with convex weights, and score the whole thing honestly with nested cross-validation
Prerequisites: several strong, different learners already in hand (decision trees, SVMs, Gaussian processes from this course), k-fold cross-validation as an honest accuracy estimate, linear regression with lm(), and the overfitting idea from the ML Workflow course.
The interactive below is the lesson's payoff in miniature: three base learners and the stack built on top of them. Toggle to Test error and the stacked bar sits lowest; toggle to Blend weights to see how much the meta-learner leans on each. We will build every piece of this from scratch.
Three good models, one slot
Each lesson runs in a fresh R session, so we start by typing Nadia's log into the machine. We build her 160 windows and, separately, a larger fresh fortnight of data to score models on honestly, so no model is ever judged on rows it trained on.
Now Nadia's three models. They are deliberately simple and very different in shape: a line that can only tilt, a degree-5 polynomial that can bend smoothly, and a tree that predicts a flat value inside step-like ranges of the hour. These three stand in for the richer learners you built earlier (the SVMs, GPs and forests); stacking works exactly the same whatever the learners are, and these three run live and give the same numbers every time.
The tree wins, just, at 4.867 cups of typical error; the smooth curve is close behind; the straight line is hopeless (it cannot represent two rush hours with one slope). The naive move is to ship the tree and bin the other two. But look at WHERE each model is right before you throw anything away.
Press Run. The smooth curve traces the two rushes gracefully but rounds off the sharp noon jump. The tree nails the noon jump (a clean step) but climbs the rushes in coarse stairs. The line just splits the difference everywhere. Each model is wrong exactly where another is right. Throwing two of them away throws away the corrections they could make. Keeping all three is the whole idea.