Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

ISLR Chapter 4 Classification Lab Manual, Exercises of Statistics

Introduction to Statistical Learning (James/Witten/Hastie/Tibshirani)

Typology: Exercises

2020/2021

Uploaded on 05/26/2021

eekanath
eekanath 🇺🇸

4.7

(18)

271 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 4 - Classification
Lab Solution
1 Problem 11
library(ISLR)
data("Auto")
attach(Auto)
(a). 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.
set.seed(1)
mpg01 =rep(0,nrow(Auto))
mpg01[mpg>median(mpg)]=1
Auto =data.frame(Auto, mpg01)
(b). Explore the data graphically in order to investigate the association between mpg01 and the other fea-
tures. Which of the other features seem most likely to be useful in predicting mpg01? Scatterplots and
boxplots may be useful tools to answer this question. Describe your findings.
cor(Auto[,-9])
## mpg cylinders displacement horsepower weight acceleration
## mpg 1.0000000 -0.7776175 -0.8051269 -0.7784268 -0.8322442 0.4233285
## cylinders -0.7776175 1.0000000 0.9508233 0.8429834 0.8975273 -0.5046834
## displacement -0.8051269 0.9508233 1.0000000 0.8972570 0.9329944 -0.5438005
## horsepower -0.7784268 0.8429834 0.8972570 1.0000000 0.8645377 -0.6891955
## weight -0.8322442 0.8975273 0.9329944 0.8645377 1.0000000 -0.4168392
## acceleration 0.4233285 -0.5046834 -0.5438005 -0.6891955 -0.4168392 1.0000000
## year 0.5805410 -0.3456474 -0.3698552 -0.4163615 -0.3091199 0.2903161
## origin 0.5652088 -0.5689316 -0.6145351 -0.4551715 -0.5850054 0.2127458
## mpg01 0.8369392 -0.7591939 -0.7534766 -0.6670526 -0.7577566 0.3468215
## year origin mpg01
## mpg 0.5805410 0.5652088 0.8369392
## cylinders -0.3456474 -0.5689316 -0.7591939
## displacement -0.3698552 -0.6145351 -0.7534766
## horsepower -0.4163615 -0.4551715 -0.6670526
## weight -0.3091199 -0.5850054 -0.7577566
## acceleration 0.2903161 0.2127458 0.3468215
## year 1.0000000 0.1815277 0.4299042
## origin 0.1815277 1.0000000 0.5136984
## mpg01 0.4299042 0.5136984 1.0000000
pairs(Auto)
1
pf3

Partial preview of the text

Download ISLR Chapter 4 Classification Lab Manual and more Exercises Statistics in PDF only on Docsity!

Chapter 4 - Classification

Lab Solution

1 Problem 11

library (ISLR) data ("Auto") attach (Auto)

(a). 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.

set.seed (1) mpg01 = rep (0, nrow (Auto)) mpg01[mpg> median (mpg)]= Auto = data.frame (Auto, mpg01)

(b). Explore the data graphically in order to investigate the association between mpg01 and the other fea- tures. Which of the other features seem most likely to be useful in predicting mpg01? Scatterplots and boxplots may be useful tools to answer this question. Describe your findings.

cor (Auto[,-9])

mpg cylinders displacement horsepower weight acceleration

mpg 1.0000000 -0.7776175 -0.8051269 -0.7784268 -0.8322442 0.

cylinders -0.7776175 1.0000000 0.9508233 0.8429834 0.8975273 -0.

displacement -0.8051269 0.9508233 1.0000000 0.8972570 0.9329944 -0.

horsepower -0.7784268 0.8429834 0.8972570 1.0000000 0.8645377 -0.

weight -0.8322442 0.8975273 0.9329944 0.8645377 1.0000000 -0.

acceleration 0.4233285 -0.5046834 -0.5438005 -0.6891955 -0.4168392 1.

year 0.5805410 -0.3456474 -0.3698552 -0.4163615 -0.3091199 0.

origin 0.5652088 -0.5689316 -0.6145351 -0.4551715 -0.5850054 0.

mpg01 0.8369392 -0.7591939 -0.7534766 -0.6670526 -0.7577566 0.

year origin mpg

mpg 0.5805410 0.5652088 0.

cylinders -0.3456474 -0.5689316 -0.

displacement -0.3698552 -0.6145351 -0.

horsepower -0.4163615 -0.4551715 -0.

weight -0.3091199 -0.5850054 -0.

acceleration 0.2903161 0.2127458 0.

year 1.0000000 0.1815277 0.

origin 0.1815277 1.0000000 0.

mpg01 0.4299042 0.5136984 1.

pairs (Auto)

mpg

3

5

7

50

200

10

20

10 30

3 5 7

cylinders

displacement

100 400

50 200

horsepower

weight

1500 4500

10 20

acceleration

year

70 78

1.0 2.

origin

name

0 200

0.0 0.

10

30

100

400

1500

4500

70

78

0

200

mpg

mpg01 seems to have a strong inverse relationship with cylinders, displacement, horsepower, and weight. Year is positively corellated, indicating that newer cars are more fuel efficient.

(c). Split the data into a training set and a test set.

train.OBS= sample (1: nrow (Auto),.5* nrow (Auto),replace=FALSE) Auto.train = Auto[train.OBS,] Auto.test = Auto[-train.OBS,] mpg01.test = Auto.test$mpg

(d). 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?

library (MASS) lda.fit = lda (mpg01 ~ cylinders + weight + displacement + horsepower,data=Auto,subset = train.OBS)