Class 11/Lab: Averaging and Stacking Models on Bike-Share Demand

Live in-browser R — edit and re-run any cell

Published

November 20, 2026

Introduction

A city health department runs a bike-share program. It wants to forecast how many bikes people will ride each hour. Cycling is physical activity, so the forecast helps plan bike lanes, staff and safety messages.

You are the biostatistician. You can fit many models. Which one do you hand over? Lecture 11 gave a different answer: hand over a combination. In this lab we fit five different models, then combine them in three ways:

  1. Unweighted and weighted averages of the predictions.
  2. Stacking (the SuperLearner idea): train a new model on the predictions of the other models.
  3. We check the identity from class: ensemble risk = average risk − diversity.

The lab asks one question again and again: does the combination beat the best single model on data it has never seen? Sometimes it does not. We will see when it does and why.

Every cell runs live in your browser via webR. Click Run Code on each cell, in order. The cell that fits five models takes about a minute, so be patient.

The data: hourly bike rentals

The data come from a bike-share system in Washington, DC (the Bikeshare data in the ISLR2 package). Each row is one hour. We keep 3,000 randomly chosen hours as our “study”.

Variable Meaning
bikers Number of bikes rented in that hour (the outcome)
hour Hour of the day, 0 to 23
month Month of the year, 1 to 12
workingday Is the day a working day? (no/yes)
temp_c Temperature in °C
humidity Relative humidity, %
wind_kmh Wind speed, km/h
weather clear, cloudy or rain_snow

What does demand look like?

Before we fit anything, look at the shape of the outcome. Average rentals by hour, on working days and on other days:

On working days there are two peaks: the morning and evening commute (around 8:00 and 17:00). On other days there is one broad midday hump. So the effect of hour is not a straight line, and it depends on workingday. A model must learn a curve, and a different curve for each kind of day. Keep this in mind. It decides which models do well.

Train and test

We keep one third of the hours as a test set. The test set is never used for fitting or for choosing weights. Every comparison below uses it.

Part 1: Five different models

We fit five models through caret::train() with one shared setup. Two details matter for stacking later:

  • All five models use the same five cross-validation folds. We create the folds once and pass them in with index.
  • savePredictions = "final" keeps the out-of-fold predictions. Each training hour is predicted by a model that did not see that hour.

These are cross-validated RMSEs (in bikes per hour). The five models fall into two groups.

  • Poor: lm (107.5) and knn (86.6). The linear model fits one straight-line effect of hour. It cannot make two commute peaks, and it cannot make a different shape for working days. kNN with these features is also poor, because most features are noise for the hour-by-hour pattern.
  • Better: tree (68.1), then rf (47.1) and gbm (45.7). Trees split on hour and workingday together, so they find the interaction by themselves. A random forest and boosting are ensembles of trees (Lecture 10), so we are about to build an ensemble of ensembles.

Here is the same story as a picture. Predicted average rentals by hour on working days, from the linear model and from boosting, against the truth:

The linear model gets the level right and the shape wrong. Boosting follows both peaks.

Part 2: Do the models make different mistakes?

Lecture 11 said: averaging only helps if the models make different predictions. A direct check is the correlation between the models’ errors. We use the out-of-fold predictions, so these are honest errors on hours each model did not see.

The errors are only moderately correlated (0.45 to 0.77). Two patterns are clear.

  • Models from the same family err together: rf and gbm (0.77), tree and rf (0.75), and, among the weak models, lm and knn (0.75).
  • Models from different families disagree more. For example lm and rf have a correlation of 0.49.

So there is real diversity to use. The question is whether we can use it without paying for the weak models.

Part 3: Ensemble risk = average risk − diversity

Now the identity from class, with numbers. On the test set, take the unweighted average of the five predictions. For squared error:

\[\text{risk of the average} \;=\; \text{average risk of the five models} \;-\; V,\]

where \(V\) is the average squared distance of each model’s prediction from the ensemble prediction (the diversity).

The last two numbers match. The identity holds exactly.

Look at the size of \(V\). The diversity (2,277) is about 41% of the average risk (5,546). That is a lot of diversity. It is also because we included two weak and different models.

Now look at what the identity does not say. Compare the ensemble with the single models on the test set (RMSE, in bikes per hour):

The unweighted ensemble (57.2) is much better than the average model, as the identity promises. But it is worse than the best single model (gbm, 44.0). The identity compares the ensemble with the average member. When a few members are much worse than the others, the average member is a low bar.

Part 4: Weighted averages

A natural fix: give good models more weight. Weight each model by the inverse of its cross-validated RMSE, so higher-error models count less. These weights come from the training folds only.

Inverse-RMSE weights help (57.2 down to 50.5). But the weights are too even. lm (RMSE 107) still gets 12% of the weight, because 1/107 is only about twice as small as 1/46. So the weighted average is still worse than the best single model.

Lecture 11 warned about this. Including weak models raises the average risk more than it raises the diversity.

Try it yourself: average only the good models

What if we average only the best models? Fill in the blanks to average the predictions of the two models with the lowest cross-validated RMSE. Then try three models, and all five.

With two models (gbm and rf) the average has a test RMSE of about 41.7. It beats the best single model (44.0). Adding the third model (tree) makes it worse again. Averaging helps when the members are accurate and different.

Part 5: Stacking by hand

Choosing “the top two” by looking at the results is a manual decision, and we made it after seeing the test performance. Stacking makes the choice automatically and fairly. We follow the five steps from the lecture.

  1. Split the training data into folds. (done: folds)
  2. Train each base model on all but one fold. (done inside train())
  3. Predict the held-out fold with each model. (done: savePredictions)
  4. Repeat until every training hour has a cross-validated prediction from each model. (this is oof)
  5. Train a meta-learner that predicts the outcome from these cross-validated predictions.

Steps 1 to 4 are already done. Our meta-learner is a non-negative least squares regression (method = "nnls"): the outcome is regressed on the five columns of oof, and the coefficients are forced to be zero or positive. Non-negative weights cannot cancel each other, so they are easy to read.

Read the weights. rf gets 0.44, gbm gets 0.57, and the other three get 0. The meta-learner dropped lm, knn and tree by itself. The weights add to about 1, so this is a weighted average that was learned from data.

Now the test set. To predict a new hour, we take each base model’s prediction and combine them with the stacking weights:

The stack has the lowest test RMSE of all (41.6, against 44.0 for the best single model). This is a gain of about 5%.

NoteIs a 5% gain a lot?

It depends on the cost of an error. For one test set it is a clear gain, but it is not dramatic, and most of it came from averaging rf and gbm. The simple average of those two (Part 4 exercise) reached 41.7. What stacking adds is that it found the two models by itself, on the training data, with no peeking at the test set.

Part 6: Why out-of-fold predictions?

Why not stack the models’ predictions on the data they were trained on? That is simpler: no folds are needed. Let’s try, and compare.

The in-sample stack puts all the weight on rf (1.01), and its test RMSE is 45.3. That is worse than the correct stack (41.6), and no better than gbm alone.

The reason is the random forest. On its own training data it is almost perfect: each tree has seen those hours, so the forest’s in-sample predictions are much too good. The meta-learner sees this and trusts the forest completely. On new hours, the forest is no better than boosting. The out-of-fold predictions are honest, so the meta-learner sees the real quality of each model.

Rule: the meta-learner must learn from predictions the base models made on data they did not train on. This is the point of steps 1 to 4 in the lecture.

Part 7: Is one split enough?

All the numbers above come from one sample of 3,000 hours and one train/test split. Could we be lucky? We ran the code in this lab eight times offline, each time with a different random sample of 3,000 hours (the seeds 42 and 1 to 7 for the sample). Average test RMSE over the eight runs:

Method Mean test RMSE Beats the best single model in
Best single model (usually gbm) 44.2 —
Equal-weight average of all five 57.1 0 of 8 runs
Inverse-RMSE weighted average 50.8 0 of 8 runs
Average of rf and gbm only 42.0 8 of 8 runs
Stack (non-negative weights) 42.1 8 of 8 runs

The pattern repeats in every run. Averaging all the models never wins. Averaging the good and different models, or stacking, always wins by about 5%. The stack gave lm a weight of exactly 0 in all eight runs and put its weight on rf (average 0.45) and gbm (average 0.57).

Try it yourself: add a bad model

Lecture 11 said that including a very bad model to “increase diversity” does not help. Test it. A very bad model is one that ignores the features and always predicts the training mean. Add it to the stack and to the equal-weight average of rf and gbm:

The equal-weight average gets much worse: the test RMSE of rf and gbm goes from 41.7 to 61.8 when we add the bad model. The stack gives the bad model weight exactly 0 and keeps the weights on rf (0.44) and gbm (0.57). It sees, from the out-of-fold predictions, that the bad model carries no information. A fixed average cannot do that.

Things worth remembering

  • Diversity is necessary, not sufficient. Ensemble risk equals the average member’s risk minus diversity. So the ensemble is never worse than the average member. It can be worse than the best member.
  • An unweighted average pays for weak members. Here, averaging all five models was much worse than the best single model.
  • Stacking chooses the weights from data. It found that rf and gbm carry the signal and put weight 0 on the rest.
  • Train the meta-learner on out-of-fold predictions. In-sample predictions reward models that overfit, such as a random forest.
  • The gain is real and modest. About 5% lower RMSE here. The result held in all eight repeated runs, but we should not expect big gains when one family of models already fits well.
  • Judge every ensemble on data it never saw. A comparison of an ensemble’s in-sample error with other models’ cross-validated error is not fair.

References

Phillips, R. V., van der Laan, M. J., Lee, H., and Gruber, S. (2023). Practical considerations for specifying a super learner. International Journal of Epidemiology, 52(4), 1276–1285.

Krogh, A., and Vedelsby, J. (1995). Neural network ensembles, cross validation, and active learning. Advances in Neural Information Processing Systems, 7.

James, G., Witten, D., Hastie, T., and Tibshirani, R. (2021). An Introduction to Statistical Learning (2nd ed.), §8.2.

Fanaee-T, H., and Gama, J. (2014). Event labeling combining ensemble detectors and background knowledge. Progress in Artificial Intelligence, 2, 113–127. (Source of the bike-share data, via the ISLR2 package.)