Choosing Variables (Over- vs Under-fitting)
Info sheet · Statistics for Psychology & Neuroscience
Warning✎ Editing notes — to do / to check
Working notes for the author — not shown to students once collapsed; remove before publishing.
- [ ]
What you’ll get from this sheet
How many predictors should a model have? By the end you should be able to:
- Explain the trade‑off between too many and too few predictors.
- State the law of parsimony (Occam’s razor) and why it guides variable selection.
Too few predictors → underfitting (high bias, misses real structure). Too many → overfitting (high variance, chases noise, won’t generalise). The sweet spot is parsimony (Occam’s razor): the fewest predictors that do the job.
Too many vs too few
Say we’re modelling median house value from 13 possible predictors. Should we use all 13? It’s not obvious, because there are costs on both sides. Adding predictors always improves the fit to this sample — but at a price: overfitting, collinearity, and a hunger for more data (statistical power). Stripping predictors too far risks underfitting — a model so simple it explains almost nothing and misses real structure.
This is the bias–variance trade‑off: too‑simple models are biased (systematically wrong), too‑complex models have high variance (they swing around to fit noise, so they change wildly from sample to sample). Somewhere between sits the sweet spot.
Watch a model over‑fit
Below, the black points are the training data. Slide the model’s flexibility (polynomial degree) up and it fits those points ever more tightly — but watch the test error (on new data it hasn’t seen). Training error only ever falls; test error falls, bottoms out, then climbs as the model starts memorising noise.
At degree 1 the line is too stiff to follow the gentle curve — it underfits, and both errors are high (that’s bias). Around degree 2–3 it traces the true relationship and the test error is lowest. Push it higher and the curve starts snaking through individual training points: the training error keeps shrinking, but the test error climbs, because the model is now fitting noise it will never see again. That’s overfitting — and it’s why more predictors isn’t automatically better.
The law of parsimony
The guide is Occam’s razor: the best model is as complex as it needs to be, and no more. In regression that means neither throwing in every predictor you can find, nor stripping it down until it explains nothing — but finding the minimum set of predictors that meets the goal. Variable selection isn’t arbitrary; it’s this balance between simplicity and explanatory power. The next sheet gives you the metrics (adjusted R², AIC) that put numbers on it.
See it in code
x = linspace(-1,1,12)'; y = 0.8*x.^2 + 0.5*x + 0.2 + 0.28*randn(12,1);
for d = [1 2 6]
c = polyfit(x, y, d);
% evaluate polyval(c, xnew) against held-out ynew ...
endThe R and Python tabs run live; MATLAB is a static reference.
TipCheck your understanding
Adding a predictor makes your R² go up. Does that mean you should keep it?
Not necessarily. R² (and the training fit generally) always improves — or at least never worsens — when you add a predictor, even a useless one, because the model has more freedom to chase the sample. The real question is whether it improves fit on new data. Use a complexity‑aware measure (adjusted R², AIC) or held‑out/cross‑validated error, and Occam’s razor: keep the predictor only if it earns its place.
“Fit to the sample” and “quality of the model” are not the same thing — a model can fit the training data perfectly and still predict terribly. Beware automated stepwise selection run without thought (it capitalises on chance and inflates significance), and remember that adding predictors also demands more data and can introduce multicollinearity. Simpler models are also easier to interpret and replicate.
Where this shows up next
Next: the metrics that quantify this balance — MSE, R², adjusted R², and AIC. See Chapter (Model Building & Evaluation).