---
title: "Stepwise Regression Demonstration"
output: html_notebook
author: Paul A. Rubin (rubin@msu.edu)
date: November 9, 2018
---
# 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](https://orinanobworld.blogspot.com/2011/02/stepwise-regression-in-r.html).
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 specify 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.
### Update (November 2018)
Besides correcting a few spelling errors (oops), I have made a few minor modifications to the function, and added demonstration code below for forward and backward stepwise regression.
The changes to the function are as follows:
* The `alpha.to.enter` parameter now has a default value of 0.0 (which will prevent any variables from entering the model if you fail to specify a value for the parameter).
* The `alpha.to.leave` parameter now has a default value of 1.0 (which will prevent any variables from leaving the model if you fail to specify a value for the parameter).
* You can now use the "." shortcut to specify a model (for instance, "Fertility ~ ."), but only if you explicitly specify the data source (via the `data` argument to the function).
# Function definition
The following defines the stepwise function.
```{r}
#'
#' 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 = 0.0, alpha.to.leave = 1.0, 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)
}
# 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)))
}
# Deal with a missing data argument.
if (is.null(data)) {
# Catch the use of "." in a formula when the data argument is null.
if ("." %in% all.vars(fm) | "." %in% all.vars(im)) {
warning("In order to use the shortcut '.' in a formula, you must explicitly specify the data source via the 'data' argument.\n")
return(NA)
} else {
# Use the parent environment.
data <- parent.frame()
}
}
# 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, full, 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` data set (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.
```{r}
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.
```{r}
result <- stepwise(Fertility ~ 1 + Agriculture + Examination + Education + Catholic + Infant.Mortality, Fertility ~ 1, aToEnter, aToLeave)
```
The return value is an instance of a linear model.
```{r}
class(result)
```
It can be summarized, used for predictions etc. just like any other linear model.
```{r}
summary(result)
```
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).
```{r}
stepwise(Fertility ~ 1 + Agriculture + Examination + Education + Catholic + Infant.Mortality, Fertility ~ 1 + Agriculture + Examination + Education + Catholic + Infant.Mortality, aToEnter, aToLeave)
```
Finally, we start with Education and Examination as the two predictors. The same final model wins out.
```{r}
stepwise(Fertility ~ Agriculture + Examination + Education + Catholic + Infant.Mortality, Fertility ~ Examination + Education, aToEnter, aToLeave)
```
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 initial model.
```{r}
stepwise(Fertility ~ 1 + Agriculture + Examination + Education + Catholic + Infant.Mortality, Fertility ~ 0 + Examination + Education, aToEnter, aToLeave)
```
None of the models, including the last one, has an intercept. Now we reverse that, including it only in the initial model.
```{r}
stepwise(Fertility ~ 0 + Agriculture + Examination + Education + Catholic + Infant.Mortality, Fertility ~ 1 + Examination + Education, aToEnter, aToLeave)
```
Every model has an intercept.
Similarly, if the initial model contains variables not included in the full model, they will automatically be added to the full model. To demonstrate this, we run stepwise with a full model consisting of just a constant term and a bigger initial model.
```{r}
stepwise(Fertility ~ 1, Fertility ~ 1 + Examination + Catholic + Infant.Mortality, aToEnter, aToLeave)
```
Notice that Examination and Infant.Mortality (not included in the "full" model) are retained but that Education (which is in neither the "full" nor the initial model) is never added.
Next, we run "forward" stepwise regression (in which variables may enter but may not leave the model under construction) and "backward" stepwise regression (in which variables may leave but may not enter).
To demonstrate forward regression, we begin with a model containing only a constant term and the Examination variable, and observe that Examination (which has been consistently dropped above) remains in the final model. The demonstration code omits the alpha.to.leave value, but setting it explicitly to something sufficiently large would produce the same result.
```{r}
stepwise(Fertility ~ 1 + Agriculture + Examination + Education + Catholic + Infant.Mortality, Fertility ~ 1 + Examination, alpha.to.enter = aToEnter)
```
Notice that Examination ends up with a pretty high p-value (~0.3) but remains in the model.
To demonstrate backward regression, we start with a model containing all variables except Education (which in previous models tended to be the strongest predictor in terms of p-value), and use the default value for alpha.to.enter. The demonstration code omits the alpha.to.enter value, but setting it explicitly to something sufficiently small would produce the same result.
```{r}
stepwise(Fertility ~ 1 + Agriculture + Examination + Education + Catholic + Infant.Mortality, Fertility ~ 1 + Agriculture + Examination + Catholic + Infant.Mortality, alpha.to.leave = aToLeave)
```
Notice this time that Education never made it into the model.
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.
```{r}
result <- stepwise(Fertility ~ 1 + Agriculture + Examination + Education + Catholic + Infant.Mortality, Fertility ~ 1, 0.2, 0.1)
```
The result of this is a missing model.
```{r}
result
```
The "." shortcut can be used to specify that the right-hand side of a model contain all terms except the dependent variable. *It requires that the data source be specified explicitly.* So the following works.
```{r}
stepwise(Fertility ~ ., Fertility ~ 1, aToEnter, aToLeave, data = swiss)
```
If we omit the `data` argument and rely on the `swiss` data set being attached, we get an error.
```{r}
stepwise(Fertility ~ ., Fertility ~ 1, aToEnter, aToLeave)
```
We are done with the `swiss` data set.
```{r}
detach(swiss)
rm(swiss)
```