4.2 - R Scripts

4.2 - R Scripts


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.


The result shows the details for the selected predictor variable in each step. In this case \(Y ~ X_2 + X_6 + X_1 + X_7 + X_3 + X_8\) 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 the best subset selection and stepwise selection (this is covered in the R Lab for this lesson).

Has Tooltip/Popover
 Toggleable Visibility