A Detailed Example
The following example shows how to use the model selection class RWLinRegModelSelector<F> on a linear regression problem. We begin with a double-precision predictor matrix called predictorData and a double-precision observation vector called observationData. The next three lines create a linear regression model and a model selector object set to use forward selection with the F statistic as the subset evaluation criterion.
 
// Save some typing.
typedef RWLinRegModelSelector<RWLinRegressFStatistic>
FStatModelSelector;
RWLinearRegression lr(predictorData,observationData);
FStatModelSelector selector(lr, rwForwardSelection);
The next few lines examine the results of forward selection and print out key diagnostics. These diagnostics include a bit vector showing which predictor variables were selected, the parameter values associated with the selected predictor variables, and the evaluation criterion given to the best subset found using forward selection.
 
if ( !selector.fail() ){
cout << "Selected variables: "
<< selector.selectedParamIndices();
cout << "Selected parameter values: "
<< selector.selectedParamValues();
cout << "Subset evaluation value: "
<< selector.evalFunctionForSelected();
}
Now we switch to stepwise selection and see if a better subset is found.
 
selector.setSearchMethod( rwStepwiseSelection );
cout << "New subset evaluation value: "
<< selector.evalFunctionForSelected();