Performing variable selection in linear regression
MASS package has the functionality for variable selection and this recipe illustrates its use.
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
How to do it...
To perform variable selection in linear regression, follow the steps below:
- Load the
> library(caret) > library(MASS)
- Read the data:
> auto <- read.csv("auto-mpg.csv")
- 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), ...