apaOLS.Stepwise(X, Y)
=apaOLS.Stepwise(X, Y)Result
Performs a forward stepwise linear regression with an intercept and returns the full selection path: a matrix with columns(X) rows and 2 x columns(X) + 4 columns, one row per step. Column 1 holds the adjusted R2, column 2 the R2, column 3 a flag that is 1 for the row with the highest adjusted R2 and 0 otherwise, the next columns(X) columns the numbers of the variables in their order of entry, followed by the intercept and the betas of all X variables in their original column order.
Syntax
X: rows x columns matrix of regressors, one variable per column; do not include a column of ones, the intercept is added internally
Y: rows x 1 vector of the dependent variable
Remarks
Each step adds the variable that yields the highest adjusted R2, which within a step is the same as the highest R2 or the lowest sum of squared residuals; ties go to the lowest column index. Variables are never removed, and the function does not stop on its own: all steps are reported, and the flag in column 3 marks the step with the highest adjusted R2 as the suggested model.
R2 = 1 - SSR/SST, with SST the sum of squared deviations of Y from its mean. The adjusted R2 is 1 - (1 - R2)(n - 1)/(n - p - 1), with n the number of observations and p the number of selected variables (excluding the intercept).
Cells without a result show #N/A: variable numbers not yet entered, betas of variables not yet selected, and all cells of steps that cannot be computed. A candidate is skipped if it is constant or (nearly) collinear with the intercept and the variables already selected, i.e. if 1 - R2 of its regression on them is below 1E-10 (variance inflation factor above 1E10); selection also ends when n <= p + 1. If Y is constant, or X and Y differ in their number of rows, an error message is returned.
