Understand the idea
When you have many candidate predictors and are not sure which ones belong in a model, stepwise selection automates the search. Forward selection starts with nothing and adds whichever predictor helps the most at each step until nothing helps anymore. Backward selection starts with everything and removes whichever predictor hurts the least at each step. Bidirectional selection does both at every step, considering an addition and a removal together and taking whichever single move helps most.
Here is the single most important thing to understand about the RESULT of this search: the p-values and confidence intervals reported on the FINAL selected model are computed as if you had chosen those exact predictors in advance, before looking at any data. But you didn't: an algorithm picked them specifically BECAUSE they looked strong in this particular sample. That double-use of the same data to both choose the model AND test it makes the reported p-values misleadingly small and the confidence intervals misleadingly narrow. Treat every number in the final model as DESCRIPTIVE, a starting point for further validation, never as proof any retained predictor genuinely matters.
A striking, concrete demonstration of this instability: running forward and backward selection on the exact SAME data can land on genuinely DIFFERENT final models. Neither is 'the' true model; that disagreement is itself the lesson: the result each search produces is an artifact of which direction it happened to search in, not a discovered truth about the world.
Stepwise regression adds or removes predictors one at a time; backward elimination starts with all of them and drops the weakest at each step. Post-selection inference is the problem that p-values and intervals from the final model are too optimistic, because the same data chose the model.
Practitioner explanation
KernelStats offers forward, backward, and both (bidirectional, the SPSS 'stepwise' convention) directions optimizing either AIC (n*ln(RSS/n) + 2k + C) or BIC (n*ln(RSS/n) + k*ln(n) + C), reporting statsmodels' AIC/BIC INCLUDING the constant C; the product states this explicitly because R's step() drops the constant, so the printed numbers differ between the two tools even though the constant cancels in comparisons and the CHOSEN model matches. BIC's penalty per parameter (ln(n)) exceeds AIC's (2) once n > 7, so BIC systematically tends to select SMALLER models than AIC on the same data, stated as a direct practical consequence, not left implicit.
The post-selection-inference warning is the card's headline caution, stated with concrete consequences rather than as generic boilerplate: selecting from many pure-noise predictors ROUTINELY yields a final model full of 'significant' variables even when NONE are truly related to the outcome, a retained variable is not proven important and a dropped one is not proven irrelevant, and selection is UNSTABLE (a handful of additional rows can flip which predictors survive). The product's own concrete demonstration on the classic mtcars dataset is named directly: forward-AIC selects weight+cylinders+horsepower while backward-AIC selects weight+quarter-mile-time+transmission on the SAME data, disagreement that is the expected signature of this instability, not a bug.
For honest inference after a stepwise search, the product names three specific remedies rather than leaving the analyst to guess: pre-specify the model before ever looking at the data, validate the selected model on a held-out sample or via cross-validation (naming the cross-validation function directly), or use a penalized method that treats selection as part of a cross-validated prediction problem (lasso regression; note that lasso coefficients carry no valid p-values either) or a dedicated selective-inference procedure, which KernelStats does not implement; treating the raw stepwise output's own p-values as a substitute for any of these is explicitly the mistake this warning exists to prevent.
When to use it
Consider it when
You have many candidate predictors for a numeric outcome and want an automated search to suggest a SMALLER STARTING-POINT subset by an information criterion, to be validated further, not to be treated as a hypothesis test of which predictors 'matter'.
Use caution when
You intend to report the final model's p-values, standard errors, or confidence intervals as valid inference (they are invalid after this kind of data-dependent selection), predictors are highly correlated (the search becomes especially unstable, arbitrarily keeping one of a correlated pair or the other), or you specifically need a formal statistical test of predictor importance rather than a predictive-model starting point.
Formulas and symbols
Akaike Information Criterion (search objective)
k the number of estimated parameters; lower is better; C a constant that cancels when comparing models.
Bayesian Information Criterion (search objective)
Penalizes extra parameters more heavily than AIC once n > 7 (since ln(n) > 2), tending to select smaller models.
Assumptions and checks
- Stepwise selection uses the same data to both CHOOSE which predictors enter the model and to compute their p-values, standard errors, and confidence intervals; because the selection itself was data-driven, those reported p-values are systematically too small and the intervals too narrow.
In KernelStats: Not correctable by KernelStats: the card states this directly as a standing 'Post-selection inference is invalid' warning and recommends Cross-Validated Model Comparison on held-out data for honest inference.
- A predictor's presence in (or absence from) the final selected model is not proof that it matters (or does not matter) for the outcome; forward and backward searches, or a slightly different sample, can select a visibly different final model, especially among correlated candidate predictors.
In KernelStats: Not checked by KernelStats: stated directly as a standing 'Selection is not causal importance' reminder.
- Normality (of residuals, not of the raw data). Non-normal residuals in the final selected model make its standard errors, t-tests, and confidence intervals approximate rather than exact, on top of the post-selection bias already present.
In KernelStats: Shapiro-Wilk on the final model's residuals (for 3 to 5000 residuals), reported directly on the card.
- Homogeneity of variance (homoscedasticity). Heteroscedasticity in the final selected model makes its classical standard errors wrong, compounding the post-selection bias already present in the reported p-values.
In KernelStats: Breusch-Pagan test on the final model's residuals, reported directly on the card.
Alternatives
- Lasso Regression. You want automatic feature selection with a principled path (cross-validated penalty strength) built for the selection problem, rather than a stepwise search whose final p-values cannot be trusted.
- Cross-Validated Model Comparison. You want to validate a selected model's genuine predictive performance on held-out data, rather than trust the in-sample p-values of the final stepwise model.
- Linear Regression. The candidate predictor set was decided in advance (not searched), so ordinary regression's p-values and confidence intervals are valid without the post-selection caveat.
Worked example
Scenario
Selecting among all ten candidate predictors in the mtcars data (cylinders, displacement, horsepower, rear-axle ratio, weight, quarter-mile time, engine shape, transmission, gears, carburetors) for fuel economy (mpg), comparing forward-AIC and backward-AIC on the same data.
Result
Forward-AIC selects weight, cylinders, and horsepower; backward-AIC selects weight, quarter-mile time, and transmission type: two different three-predictor models from the same starting data.
Interpretation
This disagreement is the expected signature of stepwise selection's instability, not an error in either search; both models are plausible predictive starting points, but neither the specific predictors chosen nor their p-values in either final model should be reported as proof of which variables genuinely drive fuel economy; cross-validation or a held-out sample is needed to compare the two candidates honestly.
Common misconceptions
Misconception
A predictor retained by stepwise selection, with a significant p-value in the final model, has been shown to matter for the outcome.
Correction
The p-values in the final model are computed as though the predictors had been chosen BEFORE looking at the data, but a search algorithm chose them BECAUSE they looked strong in this specific sample: that double-use of the data makes the reported p-values too small and the intervals too narrow. Selecting from many pure-noise predictors routinely produces a final model full of 'significant' variables even when none are genuinely related to the outcome; treat the final p-values as descriptive, not as proof of importance.
Read more: Misconception: the p-value is the probability the null hypothesis is true
Misconception
Forward and backward stepwise selection should generally agree on the same final model, since they're searching for the same optimal subset.
Correction
Forward and backward searches routinely land on GENUINELY DIFFERENT final models on the exact same data, because each explores the space of possible subsets along a different path and can get stuck at a different local optimum. Neither result is 'the' true model: the disagreement itself demonstrates that the outcome is an artifact of the search path, not a discovered fact about which predictors matter.
References
- The Elements of Statistical Learning, Section 3.3, Hastie, T., Tibshirani, R. and Friedman, J. (2009).
- Regression Modeling Strategies, Harrell, F. E. (2015).
