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.

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
=====
+++ Adding Education 

              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
=====
+++ Adding Catholic 

              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
=====
+++ Adding Infant.Mortality 

                    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
=====
+++ Adding Agriculture 

                   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
=====
+++ Adding Infant.Mortality 

                   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
=====
+++ Adding Catholic 

                    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
=====
+++ Adding Agriculture 

                   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
=====
+++ Adding Infant.Mortality 

                   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
=====
+++ Adding Catholic 

                    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
=====
+++ Adding Infant.Mortality 

                   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
=====
+++ Adding Catholic 

                    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
=====
+++ Adding Agriculture 

                   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)
LS0tCnRpdGxlOiAiU3RlcHdpc2UgUmVncmVzc2lvbiBEZW1vbnN0cmF0aW9uIgpvdXRwdXQ6IGh0bWxfbm90ZWJvb2sKYXV0aG9yOiBQYXVsIEEuIFJ1YmluIChydWJpbkBtc3UuZWR1KQpkYXRlOiBBdWd1c3QgMjAsIDIwMTcKLS0tCgojIFByZWFtYmxlCgpUaGlzIG5vdGVib29rIHN1cHBsaWVzIGFuZCBkZW1vb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