# 4.2 - R Scripts

4.2 - R Scripts

## R

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 ~ X_2 + X_6 + X_1 + X_7 + X_3 + X_8$$ is the best sequence of variables added using forward selection.

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

 [1] Link ↥ Has Tooltip/Popover Toggleable Visibility