# Preamble

This notebook supplies and demonstrates some code I cobbled together to do “classical” stepwise linear regression. Both the code and the rationale for writing it are discussed in a blog post I wrote in 2011 (when I used the code in a course I taught): Stepwise Regression in R.

The function “stepwise” defined below performs stepwise regression based on a “nested model” F test for inclusion/exclusion of a predictor. In keeping with my intent to use it as an instructional tool, it spews progress reports to output (as a side effect) while returning the final model chosen (an lm object).

To keep it simple, I made no provision for forcing certain variables to be included in all models. The current version has the following properties.

• You can specificy a data frame, using the optional data argument (as with lm).
• The code does some consistency checks (such as whether your sample size exceeds the number of variables, and whether your alpha-to-enter is less than your alpha-to-leave, but not whether the initial model is a subset of the full model). If one of the checks fails, the function will nag you and return NA.
• A constant term is optional. Whether or not a constant term is included is controlled by its presence/absence in the initial model specified, regardless of whether the full model has one.
• Both the full and initial models can be specified as formulas or as character vectors (strings). In other words, y ~ x and "y ~ x" should work equally well.

One other note: since the code uses R’s drop1 and add1 functions, it respects hierarchy in models. That is, regardless of p values, it will not attempt to drop a term while retaining a higher order interaction involving that term, nor will it add an interaction term if the lower order components are not all present. (You can of course defeat this by putting interactions into new variables and feeding it what looks like a first-order model.)

Consider this to be “beta” code (and feel free to improve it). I’ve done somewhat limited testing on it, beyond what you see in this notebook.

# Function definition

The following defines the stepwise function.

#'
#' Perform a stepwise linear regression using F tests of significance.
#'
#' @param full.model the model containing all possible terms
#' @param initial.model the first model to consider
#' @param alpha.to.enter the significance level above which a variable may enter the model
#' @param alpha.to.leave the significance level below which a variable may be deleted from the model
#' @param data the data frame to use (optional, as with lm)
#'
#' @return the final model
#'
stepwise <-
function(full.model, initial.model, alpha.to.enter, alpha.to.leave, data = NULL) {
# Sanity check: alpha.to.enter should not be greater than alpha.to.leave.
if (alpha.to.enter > alpha.to.leave) {
warning("Your alpha-to-enter is greater than your alpha-to-leave, which could throw the function into an infinite loop.\n")
return(NA)
}
# Deal with a missing data argument.
if (is.null(data)) {
data <- parent.frame()
}
# Warning: horrible kludge coming!
# Acquire the full and initial models as formulas. If they are
# entered as formulas, convert them to get their environments
# squared away.
# Note: "showEnv = F" is necessary to avoid having an
# environment identifier break things if the model is
# defined inside a function.
if (is.character(full.model)) {
fm <- as.formula(full.model)
} else {
fm <- as.formula(capture.output(print(full.model, showEnv = F)))
}
if (is.character(initial.model)) {
im <- as.formula(initial.model)
} else {
im <- as.formula(capture.output(print(initial.model, showEnv = F)))
}
# Fit the full model.
full <- lm(fm, data);
# Sanity check: do not allow an overspecified full model.
if (full$df.residual < 1) { warning("Your full model does not have enough observations to properly estimate it.\n") return(NA) } msef <- (summary(full)$sigma)^2;  # MSE of full model
n <- length(full$residuals); # sample size # Fit the initial model. current <- lm(im, data); # Process consecutive models until we break out of the loop. while (TRUE) { # Summarize the current model. temp <- summary(current); # Print the model description. print(temp$coefficients);
# Get the size, MSE and Mallow's cp of the current model.
p <- dim(temp$coefficients)[1]; # size mse <- (temp$sigma)^2; # MSE
cp <- (n - p)*mse/msef - (n - 2*p);  # Mallow's cp
# Show the fit statistics.
fit <- sprintf("\nS = %f, R-sq = %f, R-sq(adj) = %f, C-p = %f",
temp$sigma, temp$r.squared, temp\$adj.r.squared, cp);
# Show the fit itself.
write(fit, file = "");
write("=====", file = "");
# Try to drop a term (but only if more than one is left).
if (p > 1) {
# Look for terms that can be dropped based on F tests.
d <- drop1(current, test = "F");
# Find the term with largest p-value.
pmax <- suppressWarnings(max(d[, 6], na.rm = TRUE));
# If the term qualifies, drop the variable.
if (pmax > alpha.to.leave) {
# We have a candidate for deletion.
# Get the name of the variable to delete.
var <- rownames(d)[d[,6] == pmax];
# If an intercept is present, it will be the first name in the list.
# There also could be ties for worst p-value.
# Taking the second entry if there is more than one is a safe solution to both issues.
if (length(var) > 1) {
var <- var[2];
}
# Print out the variable to be dropped.
write(paste("--- Dropping", var, "\n"), file = "");
# Modify the formulat to drop the chosen variable (by subtracting it from the current formula).
f <- formula(current);
f <- as.formula(paste(f[2], "~", paste(f[3], var, sep = " - ")), env = environment(f));
# Fit the modified model and loop.
current <- lm(f, data);
next;
}
}
# If we get here, we failed to drop a term; try adding one.
# Note: add1 throws an error if nothing can be added (current == full), which we trap with tryCatch.
a <- tryCatch(
add1(current, fm, test = "F"),
error = function(e) NULL
);
if (is.null(a)) {
# There are no unused variables (or something went splat), so we bail out.
break;
}
# Find the minimum p-value of any term (skipping the terms with no p-value). In case none of the remaining terms have a p-value (true of the intercept and any linearly dependent predictors), suppress warnings about an empty list. The test for a suitable candidate to drop will fail since pmin will be set to infinity.
pmin <- suppressWarnings(min(a[, 6], na.rm = TRUE));
if (pmin < alpha.to.enter) {
# We have a candidate for addition to the model. Get the variable's name.
var <- rownames(a)[a[,6] == pmin];
# We have the same issue with ties and the presence of an intercept term, and the same solution, as above.
if (length(var) > 1) {
var <- var[2];
}
# Print the variable being added.
write(paste("+++ Adding", var, "\n"), file = "");
# Add it to the current formula.
f <- formula(current);
f <- as.formula(paste(f[2], "~", paste(f[3], var, sep = " + ")), env = environment(f));
# Fit the modified model and loop.
current <- lm(f, data = data);
next;
}
# If we get here, we failed to make any changes to the model; time to declare victory and exit.
break;
}
current
}

# Demonstrations

The rest of the notebook demonstrates the function in operation.

The first tests of the function will be done using the swiss dataset (47 observations of 6 variables) from the datasets package. We will (arbitrarily) use alpha = 0.05 to add a variable and alpha = 0.10 to remove one.

data(swiss)
attach(swiss) # to save typing
aToEnter <- 0.05
aToLeave <- 0.10

The first invocation will start with just a constant term. Everything except Examination ends up used.

result <- stepwise(Fertility ~ 1 + Agriculture + Examination + Education + Catholic + Infant.Mortality, Fertility ~ 1, aToEnter, aToLeave)
            Estimate Std. Error  t value     Pr(>|t|)
(Intercept) 70.14255   1.822101 38.49542 1.212895e-36

S = 12.491697, R-sq = 0.000000, R-sq(adj) = 0.000000, C-p = 94.805296
=====

Estimate Std. Error   t value
(Intercept) 79.6100585  2.1040971 37.835734
Education   -0.8623503  0.1448447 -5.953619
Pr(>|t|)
(Intercept) 9.302464e-36
Education   3.658617e-07

S = 9.446029, R-sq = 0.440616, R-sq(adj) = 0.428185, C-p = 35.204895
=====

Estimate Std. Error   t value
(Intercept) 74.2336892 2.35197061 31.562337
Education   -0.7883293 0.12929324 -6.097219
Catholic     0.1109210 0.02980965  3.720974
Pr(>|t|)
(Intercept) 7.349828e-32
Education   2.428340e-07
Catholic    5.598332e-04

S = 8.331442, R-sq = 0.574507, R-sq(adj) = 0.555167, C-p = 18.486158
=====

Estimate Std. Error   t value
(Intercept)      48.67707330 7.91908348  6.146806
Education        -0.75924577 0.11679763 -6.500524
Catholic          0.09606607 0.02721795  3.529511
Infant.Mortality  1.29614813 0.38698777  3.349326
Pr(>|t|)
(Intercept)      2.235983e-07
Education        6.833658e-08
Catholic         1.006201e-03
Infant.Mortality 1.693753e-03

S = 7.505417, R-sq = 0.662544, R-sq(adj) = 0.639000, C-p = 8.178162
=====

Estimate Std. Error   t value
(Intercept)      62.1013116 9.60488611  6.465596
Education        -0.9802638 0.14813668 -6.617293
Catholic          0.1246664 0.02889350  4.314686
Infant.Mortality  1.0784422 0.38186621  2.824136
Agriculture      -0.1546175 0.06818992 -2.267454
Pr(>|t|)
(Intercept)      8.491981e-08
Education        5.139985e-08
Catholic         9.503030e-05
Infant.Mortality 7.220378e-03
Agriculture      2.856968e-02

S = 7.168166, R-sq = 0.699348, R-sq(adj) = 0.670714, C-p = 5.032800
=====

The return value is an instance of a linear model.

class(result)
[1] "lm"

It can be summarized, used for predictions etc. just like any other linear model.

summary(result)

Call:
lm(formula = f, data = data)

Residuals:
Min       1Q   Median       3Q      Max
-14.6765  -6.0522   0.7514   3.1664  16.1422

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept)      62.10131    9.60489   6.466 8.49e-08
Education        -0.98026    0.14814  -6.617 5.14e-08
Catholic          0.12467    0.02889   4.315 9.50e-05
Infant.Mortality  1.07844    0.38187   2.824  0.00722
Agriculture      -0.15462    0.06819  -2.267  0.02857

(Intercept)      ***
Education        ***
Catholic         ***
Infant.Mortality **
Agriculture      *
---
Signif. codes:
0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 7.168 on 42 degrees of freedom
Multiple R-squared:  0.6993,    Adjusted R-squared:  0.6707
F-statistic: 24.42 on 4 and 42 DF,  p-value: 1.717e-10

The second invocation starts with the complete model and initially winnows it. We end up with the same model as the previous attempt (albeit with the variables listed in a different order).

stepwise(Fertility ~ 1 + Agriculture + Examination + Education + Catholic + Infant.Mortality, Fertility ~ 1 + Agriculture + Examination + Education + Catholic + Infant.Mortality, aToEnter, aToLeave)
                   Estimate  Std. Error   t value
(Intercept)      66.9151817 10.70603759  6.250229
Agriculture      -0.1721140  0.07030392 -2.448142
Examination      -0.2580082  0.25387820 -1.016268
Education        -0.8709401  0.18302860 -4.758492
Catholic          0.1041153  0.03525785  2.952969
Infant.Mortality  1.0770481  0.38171965  2.821568
Pr(>|t|)
(Intercept)      1.906051e-07
Agriculture      1.872715e-02
Examination      3.154617e-01
Education        2.430605e-05
Catholic         5.190079e-03
Infant.Mortality 7.335715e-03

S = 7.165369, R-sq = 0.706735, R-sq(adj) = 0.670971, C-p = 6.000000
=====
--- Dropping Examination

Estimate Std. Error   t value
(Intercept)      62.1013116 9.60488611  6.465596
Agriculture      -0.1546175 0.06818992 -2.267454
Education        -0.9802638 0.14813668 -6.617293
Catholic          0.1246664 0.02889350  4.314686
Infant.Mortality  1.0784422 0.38186621  2.824136
Pr(>|t|)
(Intercept)      8.491981e-08
Agriculture      2.856968e-02
Education        5.139985e-08
Catholic         9.503030e-05
Infant.Mortality 7.220378e-03

S = 7.168166, R-sq = 0.699348, R-sq(adj) = 0.670714, C-p = 5.032800
=====

Call:
lm(formula = f, data = data)

Coefficients:
(Intercept)       Agriculture         Education
62.1013           -0.1546           -0.9803
Catholic  Infant.Mortality
0.1247            1.0784  

Finally, we start with Education and Examination as the two predictors. The same final model wins out.

stepwise(Fertility ~ Agriculture + Examination + Education + Catholic + Infant.Mortality, Fertility ~ Examination + Education, aToEnter, aToLeave)
              Estimate Std. Error   t value
(Intercept) 85.2532753  3.0854981 27.630312
Examination -0.5572183  0.2319374 -2.402451
Education   -0.5394570  0.1924380 -2.803277
Pr(>|t|)
(Intercept) 1.945244e-29
Examination 2.057160e-02
Education   7.497224e-03

S = 8.981812, R-sq = 0.505485, R-sq(adj) = 0.483007, C-p = 28.135883
=====

Estimate Std. Error   t value
(Intercept)      55.2746618  8.8077340  6.275696
Examination      -0.5108888  0.2063175 -2.476226
Education        -0.5225093  0.1709099 -3.057221
Infant.Mortality  1.4556114  0.4064507  3.581274
Pr(>|t|)
(Intercept)      1.451652e-07
Examination      1.729132e-02
Education        3.832793e-03
Infant.Mortality 8.644778e-04

S = 7.973957, R-sq = 0.619096, R-sq(adj) = 0.592521, C-p = 14.252399
=====

Estimate Std. Error    t value
(Intercept)      50.02820666 8.66076269  5.7764204
Examination      -0.10580461 0.26036962 -0.4063631
Education        -0.70415772 0.17969218 -3.9186887
Infant.Mortality  1.30567908 0.39150335  3.3350393
Catholic          0.08631125 0.03649293  2.3651501
Pr(>|t|)
(Intercept)      8.325568e-07
Examination      6.865390e-01
Education        3.221868e-04
Infant.Mortality 1.790664e-03
Catholic         2.271709e-02

S = 7.579356, R-sq = 0.663865, R-sq(adj) = 0.631853, C-p = 9.993398
=====
--- Dropping Examination

Estimate Std. Error   t value
(Intercept)      48.67707330 7.91908348  6.146806
Education        -0.75924577 0.11679763 -6.500524
Infant.Mortality  1.29614813 0.38698777  3.349326
Catholic          0.09606607 0.02721795  3.529511
Pr(>|t|)
(Intercept)      2.235983e-07
Education        6.833658e-08
Infant.Mortality 1.693753e-03
Catholic         1.006201e-03

S = 7.505417, R-sq = 0.662544, R-sq(adj) = 0.639000, C-p = 8.178162
=====

Estimate Std. Error   t value
(Intercept)      62.1013116 9.60488611  6.465596
Education        -0.9802638 0.14813668 -6.617293
Infant.Mortality  1.0784422 0.38186621  2.824136
Catholic          0.1246664 0.02889350  4.314686
Agriculture      -0.1546175 0.06818992 -2.267454
Pr(>|t|)
(Intercept)      8.491981e-08
Education        5.139985e-08
Infant.Mortality 7.220378e-03
Catholic         9.503030e-05
Agriculture      2.856968e-02

S = 7.168166, R-sq = 0.699348, R-sq(adj) = 0.670714, C-p = 5.032800
=====

Call:
lm(formula = f, data = data)

Coefficients:
(Intercept)         Education  Infant.Mortality
62.1013           -0.9803            1.0784
Catholic       Agriculture
0.1247           -0.1546  

Whether the final model contains a constant term or not depends on how the initial model is specified (with or without one), irrespective of whether the full model contains a constant term.

First, we include the constant in the full model but not in the intial model.

stepwise(Fertility ~ 1 + Agriculture + Examination + Education + Catholic + Infant.Mortality, Fertility ~ 0 + Examination + Education, aToEnter, aToLeave)
             Estimate Std. Error   t value
Examination  4.321474  0.6370496  6.783575
Education   -1.501667  0.8016925 -1.873121
Pr(>|t|)
Examination 2.135175e-08
Education   6.755593e-02

S = 38.046199, R-sq = 0.726789, R-sq(adj) = 0.714646, C-p = 1225.697117
=====

Estimate Std. Error    t value
Examination      -0.1277718  0.2696722 -0.4738042
Education        -0.5546268  0.2337591 -2.3726428
Infant.Mortality  3.8798737  0.1729717 22.4306888
Pr(>|t|)
Examination      6.379822e-01
Education        2.209730e-02
Infant.Mortality 1.030375e-25

S = 10.911136, R-sq = 0.978029, R-sq(adj) = 0.976531, C-p = 61.027090
=====
--- Dropping Examination

Estimate Std. Error  t value
Education        -0.6342234  0.1611389 -3.93588
Infant.Mortality  3.8195926  0.1161705 32.87919
Pr(>|t|)
Education        2.846018e-04
Infant.Mortality 4.156264e-33

S = 10.816708, R-sq = 0.977917, R-sq(adj) = 0.976935, C-p = 59.547638
=====

Estimate Std. Error   t value
Education        -0.57683975 0.15306577 -3.768574
Infant.Mortality  3.58816498 0.14029530 25.575802
Catholic          0.09692921 0.03687941  2.628275
Pr(>|t|)
Education        4.850099e-04
Infant.Mortality 4.794287e-28
Catholic         1.177271e-02

S = 10.169722, R-sq = 0.980913, R-sq(adj) = 0.979612, C-p = 47.632657
=====

Call:
lm(formula = f, data = data)

Coefficients:
Education  Infant.Mortality          Catholic
-0.57684           3.58816           0.09693  

None of the models, including the last one, has an intercept. Now we reverse that, including it only in the initial model.

stepwise(Fertility ~ 0 + Agriculture + Examination + Education + Catholic + Infant.Mortality, Fertility ~ 1 + Examination + Education, aToEnter, aToLeave)
              Estimate Std. Error   t value
(Intercept) 85.2532753  3.0854981 27.630312
Examination -0.5572183  0.2319374 -2.402451
Education   -0.5394570  0.1924380 -2.803277
Pr(>|t|)
(Intercept) 1.945244e-29
Examination 2.057160e-02
Education   7.497224e-03

S = 8.981812, R-sq = 0.505485, R-sq(adj) = 0.483007, C-p = -4.733290
=====

Estimate Std. Error   t value
(Intercept)      55.2746618  8.8077340  6.275696
Examination      -0.5108888  0.2063175 -2.476226
Education        -0.5225093  0.1709099 -3.057221
Infant.Mortality  1.4556114  0.4064507  3.581274
Pr(>|t|)
(Intercept)      1.451652e-07
Examination      1.729132e-02
Education        3.832793e-03
Infant.Mortality 8.644778e-04

S = 7.973957, R-sq = 0.619096, R-sq(adj) = 0.592521, C-p = -11.065312
=====

Estimate Std. Error    t value
(Intercept)      50.02820666 8.66076269  5.7764204
Examination      -0.10580461 0.26036962 -0.4063631
Education        -0.70415772 0.17969218 -3.9186887
Infant.Mortality  1.30567908 0.39150335  3.3350393
Catholic          0.08631125 0.03649293  2.3651501
Pr(>|t|)
(Intercept)      8.325568e-07
Examination      6.865390e-01
Education        3.221868e-04
Infant.Mortality 1.790664e-03
Catholic         2.271709e-02

S = 7.579356, R-sq = 0.663865, R-sq(adj) = 0.631853, C-p = -12.348605
=====
--- Dropping Examination

Estimate Std. Error   t value
(Intercept)      48.67707330 7.91908348  6.146806
Education        -0.75924577 0.11679763 -6.500524
Infant.Mortality  1.29614813 0.38698777  3.349326
Catholic          0.09606607 0.02721795  3.529511
Pr(>|t|)
(Intercept)      2.235983e-07
Education        6.833658e-08
Infant.Mortality 1.693753e-03
Catholic         1.006201e-03

S = 7.505417, R-sq = 0.662544, R-sq(adj) = 0.639000, C-p = -14.251684
=====

Estimate Std. Error   t value
(Intercept)      62.1013116 9.60488611  6.465596
Education        -0.9802638 0.14813668 -6.617293
Infant.Mortality  1.0784422 0.38186621  2.824136
Catholic          0.1246664 0.02889350  4.314686
Agriculture      -0.1546175 0.06818992 -2.267454
Pr(>|t|)
(Intercept)      8.491981e-08
Education        5.139985e-08
Infant.Mortality 7.220378e-03
Catholic         9.503030e-05
Agriculture      2.856968e-02

S = 7.168166, R-sq = 0.699348, R-sq(adj) = 0.670714, C-p = -14.950793
=====

Call:
lm(formula = f, data = data)

Coefficients:
(Intercept)         Education  Infant.Mortality
62.1013           -0.9803            1.0784
Catholic       Agriculture
0.1247           -0.1546  

Every model has an intercept.

Before we abandon this data set, there is one other thing worth noting. Your alpha-to-enter must not be larger than your alpha-to-leave. Although you can get away with that sometimes, it carries the potential to put stepwise regression into an infinite loop (adding and dropping the same variable repeatedly), so the function disallows it.

result <- stepwise(Fertility ~ 1 + Agriculture + Examination + Education + Catholic + Infant.Mortality, Fertility ~ 1, 0.2, 0.1)
Your alpha-to-enter is greater than your alpha-to-leave, which could throw the function into an infinite loop.

The result of this is a missing model.

result
[1] NA

We are done with the swiss dataset.

detach(swiss)
rm(swiss)