ISLR Home

# Question

In this problem, you will develop a model to predict whether a given car gets high or low gas mileage based on the Auto data set.

1. Create a binary variable, mpg01, that contains a 1 if mpg contains a value above its median, and a 0 if mpg contains a value below its median. You can compute the median using the median() function. Note you may find it helpful to use the data.frame() function to create a single data set containing both mpg01 and the other Auto variables.

2. Explore the data graphically in order to investigate the associ- ation between mpg01 and the other features. Which of the other features seem most likely to be useful in predicting mpg01? Scat- terplots and boxplots may be useful tools to answer this ques- tion. Describe your findings.

3. Split the data into a training set and a test set.

4. Perform LDA on the training data in order to predict mpg01 using the variables that seemed most associated with mpg01 in (b). What is the test error of the model obtained?

5. Perform QDA on the training data in order to predict mpg01 using the variables that seemed most associated with mpg01 in (b). What is the test error of the model obtained?

6. Perform logistic regression on the training data in order to pre- dict mpg01 using the variables that seemed most associated with mpg01 in (b). What is the test error of the model obtained?

7. Perform KNN on the training data, with several values of K, in order to predict mpg01. Use only the variables that seemed most associated with mpg01 in (b). What test errors do you obtain? Which value of K seems to perform the best on this data set?

library(ISLR)
library(MASS)
library(class) # knn

Column names

names(Auto)
##  "mpg"          "cylinders"    "displacement" "horsepower"   "weight"
##  "acceleration" "year"         "origin"       "name"

# 11a

1. Create a binary variable, mpg01, that contains a 1 if mpg contains a value above its median, and a 0 if mpg contains a value below its median. You can compute the median using the median() function. Note you may find it helpful to use the data.frame() function to create a single data set containing both mpg01 and the other Auto variables.

Creating a mpg01 predictor that returns 1 if greater than median of mpg

y.test = test.data$mpg01 # 11d 1. Perform LDA on the training data in order to predict mpg01 using the variables that seemed most associated with mpg01 in (b). What is the test error of the model obtained? ## LDA Model lda.fit = lda(mpg01 ~ displacement + horsepower + weight, data=new.auto, subset = train) ## Predicting mpg01 using the test data lda.pred = predict(lda.fit, test.data) ## Confusion Matrix lda.class = lda.pred$class
table(lda.class, y.test) 
##          y.test
## lda.class  0  1
##         0 33  1
##         1  9 36

## Calculating the test error rate

mean(lda.class != y.test) # test error rate: 12.7%
##  0.1265823

# 11e

1. Perform QDA on the training data in order to predict mpg01 using the variables that seemed most associated with mpg01 in (b). What is the test error of the model obtained?

## QDA Model

qda.fit = qda(mpg01 ~ displacement + horsepower + weight, data=new.auto, subset = train)

## Predicting mpg01 using the test data

qda.pred = predict(qda.fit, test.data)

## Scaling the data

scaled.train.X = scale(train.X)
scaled.test.X = scale(test.X)

## KNN, k=1

knn.pred.1 = knn(scaled.train.X, scaled.test.X, train.y, k=1)

### Calculating test error rate

mean(y.test != knn.pred.1) # 15.1% (changed from 13.9% after scaling)
##  0.1518987

## KNN, k=3

knn.pred.3 = knn(scaled.train.X, scaled.test.X, train.y, k=3)

### Calculating test error rate

mean(y.test != knn.pred.3) # 12.7% (changed from 13.9% after scaling)
##  0.1265823

### KNN, k=5

knn.pred.5 = knn(scaled.train.X, scaled.test.X, train.y, k=5)

### Calculating test error rate

mean(y.test != knn.pred.5) # 10.1%
##  0.1012658

### KNN, k=10

knn.pred.10 = knn(scaled.train.X, scaled.test.X, train.y, k=10)

### Calculating test error rate

mean(y.test != knn.pred.10) # 10.1%
##  0.1392405