Docsity

ISLR Chapter 3 Linear Regression Lab Manual, Exercises of Statistics

Toggle sidebar
bg1
Chapter 3 - Linear Regression
Lab Solution
1 Problem 9
First we will read the “Auto" data. Note that most datasets referred to in the text are in the R package the authors developed.
So we just need to use the data() function to extract it into R’s memory.
library(ISLR)
data("Auto")
Each of the 392 rows of this dataset corresponds to a make/model of car, and columns represent various characteristics
of that make/model.
names(Auto)
## [1] "mpg" "cylinders" "displacement" "horsepower" "weight"
## [6] "acceleration" "year" "origin" "name"
dim(Auto)
## [1] 392 9
head(Auto)
## mpg cylinders displacement horsepower weight acceleration year origin
## 1 18 8 307 130 3504 12.0 70 1
## 2 15 8 350 165 3693 11.5 70 1
## 3 18 8 318 150 3436 11.0 70 1
## 4 16 8 304 150 3433 12.0 70 1
## 5 17 8 302 140 3449 10.5 70 1
## 6 15 8 429 198 4341 10.0 70 1
## name
## 1 chevrolet chevelle malibu
## 2 buick skylark 320
## 3 plymouth satellite
## 4 amc rebel sst
## 5 ford torino
## 6 ford galaxie 500
Eventually, we will try to predict the fuel efficiency of a car given its various characteristics.
(a). Produce a scatterplot matrix which includes all of the variables in the data set.
A scatterplot matrix can be constructed with the pairs() function.
pairs(Auto)
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download ISLR Chapter 3 Linear Regression Lab Manual and more Exercises Statistics in PDF only on Docsity!

Chapter 3 - Linear Regression

Lab Solution

1 Problem 9

First we will read the “Auto" data. Note that most datasets referred to in the text are in the R package the authors developed. So we just need to use the data() function to extract it into R’s memory.

library (ISLR) data ("Auto")

Each of the 392 rows of this dataset corresponds to a make/model of car, and columns represent various characteristics of that make/model.

names (Auto)

[1] "mpg" "cylinders" "displacement" "horsepower" "weight"

[6] "acceleration" "year" "origin" "name"

dim (Auto)

[1] 392 9

head (Auto)

mpg cylinders displacement horsepower weight acceleration year origin

1 18 8 307 130 3504 12.0 70 1

2 15 8 350 165 3693 11.5 70 1

3 18 8 318 150 3436 11.0 70 1

4 16 8 304 150 3433 12.0 70 1

5 17 8 302 140 3449 10.5 70 1

6 15 8 429 198 4341 10.0 70 1

name

1 chevrolet chevelle malibu

2 buick skylark 320

3 plymouth satellite

4 amc rebel sst

5 ford torino

6 ford galaxie 500

Eventually, we will try to predict the fuel efficiency of a car given its various characteristics.

(a). Produce a scatterplot matrix which includes all of the variables in the data set. A scatterplot matrix can be constructed with the pairs() function.

pairs (Auto)

mpg

3 5 7 50 150 10 20 1.0 2.0 3.

10

40

3

5

7 cylinders

displacement 100

400

50

(^200) horsepower

weight

1500

5000

10

(^20) acceleration

year 70

78

2.5 origin

10 30 100 300 1500 4000 70 76 82 0 150 300

0

name 200

Note some of the trends in the data. For example, as the number of cylinders increases is appears that fuel efficiency increases and decreases. There are numerous curvilinear relationships with mpg as well.

(b). Compute the matrix of correlations between the variables using the function cor(). You will need to exclude the name variable, which is qualitative. As the problem mentions, we need to remove the name column because it is not numeric. Recall the output of names(Auto) above, and note that “name" is the 9th entry in that output, so cor(Auto[,-9]) would do the job for us. However it would be inefficient to lookup this number each time, so instead we can use the which() function to find the column for us to remove in the same way. Additionally, the subset() function can be used to remove the names of columns. So that

cor (Auto[,-9]) cor (Auto[,- which ( names (Auto) %in% c ("name"))]) cor ( subset (Auto, select=-name))

will all produce

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.

year origin

iii. What does the coefficient for the year variable suggest? The regression coefficient for year is 0.750773, which suggests that for every one year increase of a make/model car, mpg increases by 0.750773. i.e. newer cars are more fuel efficient.

(d). Use the plot() function to produce diagnostic plots of the linear regression fit. Comment on any problems you see with the fit. Do the residual plots suggest any unusually large outliers? Does the leverage plot identify any observations with unusually high leverage? In R, plot() is what is called a "generic" function. This means that you can pass it many different types of objects and it will do different things depending on what is passed. In this problem, my_lm is an lm object. So when we call plot(my_lm), R will immeditly call the subroutine plot.lm(), which is the plotting function for lm objects.

par (mfrow= c (2,2)) #creates a 2 x 2 panel display plot (my_lm)

Fitted values

Residuals

Residuals vs Fitted

323 326327

Theoretical Quantiles

Standardized residuals

Normal Q−Q

323 326327

Fitted values

Standardized residuals

Scale−Location

(^323327) 326

Leverage

Standardized residuals Cook's distance

Residuals vs Leverage

14

394

327

From the plots, it seems:

  • The residual plot has a strong curve, indicating issues with the fit.
  • The assumption of normality does not seem to be violated.
  • Observation 14 has high leverage.

(e). Use the * and : symbols to fit linear regression models with interaction effects. Do any interactions appear to be statistically significant?

Note that when specifying formulas, the * operator gives the multiplicative term as well as the first order terms, and the : operator just gives the multiplicative term. This means that

lm (Y ~ a*b) lm (Y ~ a + b + a:b)

would give the same result.

There are many choices for possible interaction effects. Typically, one needs to have knowledge of the problem in order to choose meaningful combinations of predictor variables. For example, in this problem, it seems logical that the effect of weight and acceleration may have some kind of joint impact on mpg, hence we can modify the model in part (c) to add this in.

my_lm_e = lm (mpg ~. - name + weight:acceleration,data=Auto) summary (my_lm_e)

Call:

lm(formula = mpg ~. - name + weight:acceleration, data = Auto)

Residuals:

Min 1Q Median 3Q Max

-8.247 -2.048 -0.045 1.619 12.

Coefficients:

Estimate Std. Error t value Pr(>|t|)

(Intercept) -4.364e+01 5.811e+00 -7.511 4.18e-13 ***

cylinders -2.141e-01 3.078e-01 -0.696 0.

displacement 3.138e-03 7.495e-03 0.419 0.

horsepower -4.141e-02 1.348e-02 -3.071 0.002287 **

weight 4.027e-03 1.636e-03 2.462 0.014268 *

acceleration 1.629e+00 2.422e-01 6.726 6.36e-11 ***

year 7.821e-01 4.833e-02 16.184 < 2e-16 ***

origin 1.033e+00 2.686e-01 3.846 0.000141 ***

weight:acceleration -5.826e-04 8.408e-05 -6.928 1.81e-11 ***

---

Signif. codes: 0 '' 0.001 '' 0.01 '' 0.05 '.' 0.1 ' ' 1

Residual standard error: 3.141 on 383 degrees of freedom

Multiple R-squared: 0.8414,Adjusted R-squared: 0.

F-statistic: 253.9 on 8 and 383 DF, p-value: < 2.2e-

Note that our interaction effect is significant and the R^2 a value increased, indicating a better fit. There are many different possibilities here, so your answers will likely vary.

(f). Try a few different transformations of the variables, such as log ( X ) ,

X , X^2 , etc. Comment on your findings.

my_lm_f = lm (mpg ~. - name + log (weight) + sqrt (horsepower) + I (displacement^2) + I (cylinders^2),data=Auto) summary (my_lm_f)

plot (y,x)

y

x

(e). Fit a least squares linear model to predict y using x. Comment on the model obtained. How do β ˆ 0 and β^ ˆ 1 compare to β 0 and β 1?

my_lm = lm (y~x) summary (my_lm)

Call:

lm(formula = y ~ x)

Residuals:

Min 1Q Median 3Q Max

-0.93842 -0.30688 -0.06975 0.26970 1.

Coefficients:

Estimate Std. Error t value Pr(>|t|)

(Intercept) -1.01885 0.04849 -21.010 < 2e-16 ***

x 0.49947 0.05386 9.273 4.58e-15 ***

---

Signif. codes: 0 '' 0.001 '' 0.01 '' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.4814 on 98 degrees of freedom

Multiple R-squared: 0.4674,Adjusted R-squared: 0.

F-statistic: 85.99 on 1 and 98 DF, p-value: 4.583e-

(f). Display the least squares line on the scatterplot obtained in (d). Draw the population regression line on the plot, in a different color. Use the legend() command to create an appropriate legend.

plot (x, y) abline (my_lm, lwd=3, col=2) abline (-1, 0.5, lwd=3, col=3) legend ("bottomright", legend = c ("model fit", "pop. regression"), col=2:3, lwd=3)

x

y

model fit

pop. regression

(g). Now fit a polynomial regression model that predicts y using x and x^2. Is there evidence that the quadratic term improves the model fit? Explain your answer.

lm_fit_sq = lm (y ~ x + I (x^2)) summary (lm_fit_sq)

x

y

model fit

pop. regression

(i). Repeat (a)-(f) after modifying the data generation process in such a way that there is more noise in the data. The model (3.39) should remain the same. You can do this by increasing the variance of the normal distribution used to generate the error term in (b). Describe your results.

set.seed (1) x = rnorm (100) eps = rnorm (100, 0,.75)

y = -1 + 0.5*x + eps lm.fit = lm (y~x) summary (lm.fit)

Call:

lm(formula = y ~ x)

Residuals:

Min 1Q Median 3Q Max

-1.4076 -0.4603 -0.1046 0.4046 1.

Coefficients:

Estimate Std. Error t value Pr(>|t|)

(Intercept) -1.02827 0.07274 -14.136 < 2e-16 ***

x 0.49920 0.08080 6.179 1.48e-08 ***

---

Signif. codes: 0 '' 0.001 '' 0.01 '' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.7221 on 98 degrees of freedom

Multiple R-squared: 0.2803,Adjusted R-squared: 0.

F-statistic: 38.18 on 1 and 98 DF, p-value: 1.479e-

plot (x, y) abline (lm.fit, lwd=3, col=2) abline (-1, 0.5, lwd=3, col=3) legend ("bottomright", legend = c ("model fit", "pop. regression"), col=2:3, lwd=3)