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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.