O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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)]) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required