Interpreting R-Squared in Model Evaluation
A model can fit its own training data closely while failing completely on unseen data.
Why an Easy Model Can Still Fail
A common first move when starting a regression problem is to reach for a random forest. The reasoning is easy to follow: random forests require very little preprocessing, so you can often hand over raw feature values and get a model that makes sensible predictions without much tuning. That convenience is real, but it rests on a hidden assumption, namely that a model which is easy to set up will also be reliable on new data. This article tests that assumption and shows how to read the evidence when it fails.
What do you think happens?
You train a random forest on a time-based feature whose training timestamps all come from earlier in a dataset. You then predict targets for a test set whose timestamps all fall after the last training timestamp. If the model had learned a smooth trend over time, what should the predictions look like?
Reveal answer
A flat, constant line with no trend
The source describes the test-set predictions forming a constant line, even though the training-set predictions were quite good. The reason is that the test timestamps lie outside the range the forest was trained on, so the forest has no trained region to route them into.
The rest of this article follows that surprise to its cause. You will trace how a single input moves through a tree, see why inputs beyond the training range inherit a boundary value, learn how to read a train-versus-test gap through R2, and practice diagnosing a comparison by locating the exact point where a prediction departs from what you expected.
How One Tree Routes an Input
A random forest is built from many decision trees. Each tree makes its prediction by routing an input down a set of splits until it lands in a region that was populated during training. The forest's prediction is built from those individual tree predictions, so understanding one tree's routing is the first step to understanding the whole forest.
The routing depends entirely on whether the input falls inside the range of values the training data covered. When a feature value lands inside that range, the tree has a region to send it to. When the value falls outside the range, no trained region covers it. The tree cannot invent a value for an input it has no region for, so it has nowhere principled to go.
Read the diagram as a question you ask at every split. While the answer keeps coming back yes, the prediction is grounded in training examples. The first time the input leaves the training range, the path no longer corresponds to any region the model learned, and the output is taken from the edge of the training data instead.
Why Predictions Stop at the Training Boundary
Extrapolation means predicting beyond the range of inputs the model has seen. Trees and random forests do not extrapolate. For a feature value beyond the training range, the model predicts the target of the closest training point. Across many such inputs, the predictions stop following any trend and settle at a boundary value.
Predicting a time trend beyond training
A forest is trained on timestamps from days 1 through 10, where the target rises steadily over those days. Test timestamps cover days 11 through 15. What pattern should you expect in the test predictions, and how do you check it?
- 1Find the training range
The training timestamps span days 1 to 10, so the upper boundary is day 10.
- 2Locate each test input
Days 11 through 15 all lie above day 10, so none of them falls inside a region the forest was trained on.
- 3Trace the route
Each test input leaves the training range at the first split that compares against the time value, and it takes the output of the closest training point.
- 4Predict the shape
Because every test input inherits a boundary target, the predictions form a flat line rather than continuing the rising trend.
- 5Check against the data
Compare the test predictions with the test targets directly. A flat prediction line against a rising target signals that the trend was not learned beyond the range.
The test predictions form a constant line near the boundary target, which means the forest cannot reproduce a trend that continues past its training range.
Reading a Train-Versus-Test Gap
The source describes two results that look contradictory at first. The predictions on the training set are quite good, which is typical for random forests. The predictions on the test set, however, form a constant line, and the R2 on the test set is -0.03. R2 measures how much of the variation in the target the predictions explain, so a value near zero means the model explains almost none of that variation on the data it was scored on.
The source reads the test-set R2 of -0.03 as meaning the model learned nothing useful for that test set. A value near zero indicates that the predictions do no better than the simplest baseline of always predicting the average target. A value below zero indicates the predictions are worse than that baseline. In both cases the signal the model captured does not carry over to the new data.
The lesson in this case is about where you look. The failure was not caused by poor training. It was caused by the combination of the feature and the model: the test timestamps lie outside the range of the training timestamps. A gap between training and test results tells you that something about the data's coverage or the feature choice needs investigating.
Tracing the Two-Model Comparison
The source builds its comparison from two calls to the same evaluation helper, eval_on_features, which receives a feature matrix, a target, and a model. In the first call, the feature is the POSIX time and the model is a RandomForestRegressor with n_estimators set to 100 and random_state set to 0. The fixed random state makes the forest reproducible, so the same run gives the same result each time.
- The forest uses a single time feature; its test behavior depends on where test timestamps fall.
- The linear model uses a different feature set, so the two results differ in model and features at once.
- Read each result's test-set numbers directly instead of comparing the two models' overall quality.
The source then changes two things at once: the model and the features. It uses a LinearRegression model, imported from the linear model module, and passes it to the same helper with different input features, namely day of week and hour of day. Linear regression serves here as a comparison point, which is the purpose of the comparison rather than a claim that one model is always better.
Because the features changed along with the model, you cannot attribute the difference in behavior to the model type alone. The random forest struggled when its only feature was a time value that ran past the training data. The linear model's result gives a reference for judging whether the trouble lay with the forest, the feature, or both. Its caption describes predictions made from day of week and hour of day, and you should read that output directly rather than assume its quality from the forest's result.
Mistakes When Reading Model Comparisons
Common mistake Assuming a random forest can extrapolate to feature values it never saw
What it looks likeA forest trained on timestamps from earlier dates is asked to predict for later dates and is expected to continue the trend.
Why it failsTrees route inputs into regions built from training data. Values beyond the training range have no region, so the prediction inherits a boundary target.
Do this insteadCompare each test feature value against the training range before expecting a trend in the predictions.
Common mistake Judging a model by its training-set fit
What it looks likeTraining predictions look close to the targets, so the model is treated as reliable.
Why it failsA close training fit shows the model matches the data it was built from. It says nothing about unseen inputs, which is where the source's test R2 of -0.03 appeared.
Do this insteadRead the test-set results directly and treat the train-test gap as the main diagnostic.
Common mistake Reading a negative R2 as a small error instead of a failure to capture signal
What it looks likeAn R2 of -0.03 is dismissed as a rounding issue near zero.
Why it failsAn R2 near or below zero means the predictions explain almost none of the target's variation, and below zero means they do worse than predicting the average.
Do this insteadInterpret R2 near zero or below zero as evidence that the model has learned little useful signal for that data.
Common mistake Attributing a comparison result to the model type alone
What it looks likeA linear model with different features outperforms the forest, so linear models are declared better.
Why it failsThe model and the features changed together, so the difference cannot be assigned to one of them.
Do this insteadSeparate the effect of the model from the effect of the features before drawing a conclusion.
Practice: Diagnosing a Forecast Beyond the Data
A random forest is trained on a time feature covering January through June. Its test set covers July and August. The test predictions form a flat line, and the test R2 is close to zero. Trace the test timestamps against the training range. At which step does the prediction stop following any trend, and what would you check next to decide whether the trouble lies in the feature, the model, or both?
Hints
- Start by finding the largest training timestamp, which here is the end of June.
- Ask whether July and August values can be routed to any region the forest was trained on.
- Consider a second model on a different feature set to act as a reference point, as the source's comparison does.
Key Takeaways
- A random forest can fit training data closely and still fail on a test set, so training-set accuracy alone is not evidence of a reliable model.
- Trees and random forests cannot extrapolate: for feature values beyond the training range, the prediction is the target of the closest training point, which produces a flat line instead of a trend.
- A test R2 near zero, such as -0.03, indicates the model captured little useful signal, and a negative value means it does worse than predicting the average target.
- When comparing models, separate the effect of the model from the effect of the features, and read the test-set results directly before concluding anything.