There are a couple of libraries that support forward and backward selection such as Caret and MASS. But I find leaps to be the most useful albeit its incompatibility with predict() . However, with a couple more lines, we could bypass the incompatibility and yield the same result.

Let’s load the library, create train and test sets, and create theme for visualization.

Next, we train the models.

As usual, summary()  will give an overview of the models

If it were an lm()  function, we could just use predict() . But as predict() doesn’t support regsubsets class. We need a workaround as follows:

Next, we use a for loop()  to perform matrix multiplication %*%  then manually calculate $$RMSE$$.

Then, we visualize.

It seems like model 9th has the lowest test $$RMSE$$. With the loop, we can calculate $$RMSE$$ on the test set with Best Subset Selection, Backward, or Stepwise if we wish.