Performing variable selection in linear regression

The MASS package has the functionality for variable selection and this recipe illustrates its use.

Getting ready

If you have not already done so, download the data files for this chapter and ensure that the auto-mpg.csv file is in your R working directory. We want to predict mpg based on cylinders, displacement, horsepower, weight, and acceleration.

How to do it...

To perform variable selection in linear regression, follow the steps below:

  1. Load the caret and MASS packages:
    > library(caret)
    > library(MASS)
  2. Read the data:
    > auto <- read.csv("auto-mpg.csv")
  3. Convert the categorical variable cylinders into a factor with appropriate renaming of the levels:
    > auto$cylinders <- factor(auto$cylinders, levels = c(3,4,5,6,8), ...

Get R: Recipes for Analysis, Visualization and Machine Learning now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.