How it works...

Step 1 loads the caret package, step 2 reads the data, and step 3 converts the categorical variable cylinders (which have numeric values that R treats as numbers by default) into factors.

Step 4 creates the partitions (refer to the Creating random data partitions recipe in Chapter 2, What's In There? - Exploratory Data Analysis for more details). We set the random seed to enable you to match your results with what we have displayed.

Step 5 prints the variable names in the file so that we can use the appropriate variables in the linear regression model.

Step 6 uses the lm function that builds the linear regression model. We specified data = auto[t.idx, -c(1,8,9)] because we want the model to use only the training data and because ...

Get R Data Analysis Cookbook - Second Edition 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.