Sub topic 9
R squared pitfalls: negative values, added features, and adjusted R squared
This chapter assumes you can compute R squared as 1 - SS_res / SS_tot, know the predict-the-mean baseline, and understand the train/test split. R squared looks simple, but it misleads in three common ways.
Pitfall 1: R squared can be negative
R squared scores a model against a baseline that always predicts the mean of y. That baseline scores exactly 0. A model whose squared errors are larger than the baseline's gets a negative score.
A negative value is not a bug. It means "worse than predicting the mean."
import numpy as np
from sklearn.metrics import r2_score
y_true = np.array([3.0, 5.0, 2.5, 7.0])
y_pred = np.array([7.0, 2.0, 6.0, 3.0]) # badly wrong on every point
print(f"{r2_score(y_true, y_pred):.4f}") # -3.1970Here SS_res is 53.25 and SS_tot is 12.6875. The ratio is about 4.2, so R squared is 1 - 4.2.
Negative values appear mostly on held-out data. On the training set, a linear model with an intercept fitted by least squares cannot do worse than the mean, so its training R squared is at least 0. On a test set, the model never saw those points and can easily do worse.
Pitfall 2: adding features never lowers training R squared
When you add a feature to a least-squares model, the fit can ignore it by giving it a coefficient near zero. So training R squared stays the same or rises, even if the feature is pure noise. A higher training R squared therefore does not show that the new feature helps.
Adjusted R squared
Adjusted R squared charges a penalty for each added feature:
adjusted R2 = 1 - (1 - R2) * (n - 1) / (n - p - 1)
Here n is the number of samples and p is the number of features. The adjusted score rises only if a new feature improves the fit by more than chance would.
The next example compares one real feature with the same feature plus 30 noise columns.
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
def adjusted_r2(r2, n, p):
return 1 - (1 - r2) * (n - 1) / (n - p - 1)
rng = np.random.default_rng(0)
n = 60
signal = rng.normal(size=(n, 1))
y = 3 * signal[:, 0] + rng.normal(scale=2.0, size=n)
noise = rng.normal(size=(n, 30))
train, test = slice(0, 40), slice(40, 60)
for name, X in [("1 feature", signal), ("1 + 30 noise", np.hstack([signal, noise]))]:
model = LinearRegression().fit(X[train], y[train])
r2_train = r2_score(y[train], model.predict(X[train]))
adj = adjusted_r2(r2_train, n=40, p=X.shape[1])
r2_test = r2_score(y[test], model.predict(X[test]))
print(f"{name:13s} train R2={r2_train:.3f} adjusted={adj:.3f} test R2={r2_test:.3f}")Training R squared climbs with the noise columns. Adjusted R squared climbs much less, and test R squared drops, often below zero. The model has memorized noise.
Pitfall 3: a high value is not proof of a good model
- No unit. R squared is a ratio, so it says nothing about error size in the target's units. A model with R squared 0.95 can still miss by more than your application tolerates. Report MAE or RMSE alongside it.
- Depends on the data.
SS_totcomes from the spread ofy. The same model scores higher on a test set with wide variation inyand lower on a narrow one. Do not compare R squared across different datasets. - Tests do not share a baseline. On a test set, scikit-learn uses the test mean as the baseline, not the training mean. That is the normal convention, but it differs slightly from the "predict the training mean" idea.
Summary of fixes
| Symptom | Cause | Response |
|---|---|---|
| R squared below 0 | Model is worse than the mean baseline | Inspect features, leakage, and the train/test split |
| Training R squared rises with every feature | Extra parameters absorb noise | Use adjusted R squared and test scores |
| High R squared, large errors | Ratio hides the error scale | Report RMSE or MAE too |
Exercise
A model scores training R squared 0.92 and test R squared -0.15. Name two conclusions you can draw, and one thing you should do next.
Show answer
The model fits the training data well but predicts the test data worse than the mean baseline, so it is overfitting and does not generalize. The training R squared is not a reliable guide to quality. Next, simplify the model by removing features or adding regularization, and re-evaluate on held-out data. Also check for a mismatch between the train and test distributions.