Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

✅ Model Selection

In neurocognitive psychology, we often collect huge amounts of data. Sometimes we can have thousands of measurements from different brain regions or many behavioral scores. Having more information can, in theory, help us make better predictions, but it also brings risks: too many variables can make analyses infeasible, lead to false discoveries, or cause models to learn noise instead of real effects.

The large p issue

Big data refers to large data sets with many predictors which often cannot be processed or analyzed using traditional data processing techniques. For our prediction models, this brings some issues:

This is where linear model selection becomes essential, offering techniques to refine our models and extract meaningful insights from high-dimensional data.

Today’s data: Hitters

For practical demonstration, we will use the Hitters dataset. This dataset provides Major League Baseball Data from the 1986 and 1987 seasons. It contains 322 observations of major league players on 20 variables (so it’s not big data, but we can pretend it is). The Research aim is to predict a baseball player’s salary on the basis of various predictors associated with the performance in the previous year. You can check its contents here: https://islp.readthedocs.io/en/latest/datasets/Hitters.html

For computational reasons, we will not include all predictors but only a smaller subset:

Loading...

Let’s also take a look at the correlation matrix to check for potential multicollinearity, which can affect the stability of linear regression models.

<Figure size 640x480 with 2 Axes>

The heatmap reveals strong correlations between several predictors:

We thus remove two of the correlated features:

Handling big data in linear models

Subset Selection

In subset selection we identify a subset of the pp predictors that are truly related to the outcome. The model is then fitted using least squares on the reduced set of variables.

How do we determine which variables are relevant?

Best Subset Selection

We will start with performing Best Subset Selection (also called exhaustive search) as implemented in the mlxtend package. It has great documentation, e.g. for the exhaustive search. In short, this approach is a brute-force evaluation of feature subsets. A specific performance metric (e.g. MSE, R², or accuracy) is optimized given an arbitrary regressor or classifier. For example, if we have 4 features, the algorithm will evaluate all 15 possible combinations of features.

Loading...
Loading...

Before we start the subset selection, we first define the target and the features:

We first split our data into training and test dataset. Although the selection function uses cross-validation to identify the best subset of predictors (Step 3), this evaluation is done during the selection process and can still overfit to the data. To fairly assess how the final model performs on new data, we split off a test set and use it only after feature selection is complete.

PurposeWhat is it for?When?
Cross Validation in selection functionHelps choose the best subset of featuresDuring selection
Test Set EvaluationChecks how well the final model performsAfter selection

We then run Best Subset Selection on the training data:

Best R²: 0.41
Best subset (indices): (1, 3, 4)
Best subset (corresponding names): ('CWalks', 'Hits', 'HmRun')

If you are interested in the details, they are stored in the metric dictionary:

Loading...

Forward Stepwise Selection

Forward Stepwise Selection is a greedy feature‐selection method that starts with no features and, at each step, adds the single feature whose inclusion most improves the model’s performance. This “add the best remaining feature” is repeated until a desired number of features is reached, at which point the algorithm stops and returns the best subset.

An important parameter is k_features, which determines the number of features to select. We can, pass an integer (must be less than the total number of available features), a tuple (the algorithm will then evaluate all subset sizes between the min and max value), or one of two string options ("best", or "parsimonious"). Please refer to the documentation for further details.

>> Forward SFS:
   Best CV R²      : 0.413
   Optimal # feats : 3
   Feature indices : (1, 3, 4)
   Feature names   : ('CWalks', 'Hits', 'HmRun')

You can see we ended up with the same three predictors as in best subset selection: CWalks, Hits, HmRun. However, this is not necessarily always the cas! Best subset and stepwise selection can, and often do, lead to different results. In our case we only had a small number of predictors, which makes it more likely to end up with the same subset.

Backward Stepwise Selection

Backward Stepwise Selection begins with the full feature set and, at each step, removes the single feature whose exclusion most improves (or least harms) model performance. We keep repeating this “remove the worst feature” step until only the desired number of features remains, and then the algorithm returns that reduced subset:

<< Backward SFS:
   Best CV R²      : 0.413
   Optimal # feats : 3
   Feature indices : (1, 3, 4)
   Feature names   : ('CWalks', 'Hits', 'HmRun')

Scikit-learn Alternative

Scikit-learn provides SequentialFeatureSelector as an alternative to mlxtend’s stepwise selection. It supports both forward and backward selection via the direction parameter:

   Selected features: ['CWalks', 'Hits', 'HmRun']

Note that you can here either specify n_features_to_select directly, or you can use "auto" with a tol parameter that stops adding features once the score improvement falls below the threshold.

Summary

Selection MethodFinds best model?Works for large p?Works for p>n?Computational cost
Best Subset+ Yes- No- No- Very high
Forward Stepwise- No+ Moderate/large po If model size < n+ Efficient
Backward Stepwise- No+ Yes (only if p < n)- Noo Relatively Efficient
Loading...
Loading...

What next?

All three procedures converged on the same three predictors here, so let’s evaluate the model performance and estimate the true test error using that subset.

Test MSE:  182321.00
Test RMSE: 426.99
Test R²:   0.2492

So in sum:

Regularization and Dimensionality Reduction

As mentioned before, regularization and dimensionality reduction are two other measures of dealing with large numbers of predictors. Regularization techniques will be introduced in the next session, and dimensionality reduction will be introduced in the Principal Component Analysis session.