How to do it...

To perform linear regression, follow these steps:

  1. Load the caret package:
> library(caret) 
  1. Read the data:
> auto <- read.csv("auto-mpg.csv") 
  1. 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), labels = c("3cyl", "4cyl", "5cyl",  "6cyl", "8cyl"))
  1. Create partitions:
> set.seed(1000) 
> t.idx <- createDataPartition(auto$mpg, p = 0.7,  list = FALSE) 
  1. See names of the variables in the data frame:
> names(auto) 
 
[1] "No"           "mpg"          
[3] "cylinders"    "displacement" 
[5] "horsepower"   "weight"       
[7] "acceleration" "model_year"   
[9] "car_name"     
  1. Build the linear regression model:
> mod <- lm(mpg ~ ., data = auto[t.idx, -c(1,8,9)]) ...

Get R Data Analysis Cookbook - Second Edition now with O’Reilly online learning.

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