4.2 - R Scripts

Printer-friendly versionPrinter-friendly version

Continuation from Section 3.5 ...

3) Subset selection

To perform forward stepwise addition and backward stepwise deletion, the R function step is used for subset selection. For forward stepwise selection, baseModel indicates an initial model in the stepwise search and scope defines the range of models examined in the stepwise search. In the example below, the model starts from the base model and expands to the full model.

step(baseModel,scope=list(upper=fullModel,lower=~1),direction="forward")

The result shows the details for the selected predictor variable in each step. In this case Y ~ X2 + X6 + X1 + X7 + X3 + X8 is the best sequence of variables added using forward selection.

R output

R output

Similarly, backward stepwise deletion of variables can be executed by the following code:

step(fullModel, direction="backward")

It is also possible to use the regsubsets function in the leaps library to perform best subset selection and stepwise selection (this is covered in the R Lab for this lesson).

After completing the reading for this lesson, please finish the Quiz and R Lab on ANGEL (check the course schedule for due dates).