






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







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)
dim (Auto)
head (Auto)
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
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
323 326327
Theoretical Quantiles
Standardized residuals
323 326327
Fitted values
Standardized residuals
(^323327) 326
Leverage
Standardized residuals Cook's distance
14
394
327
From the plots, it seems:
(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)
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)
(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)
(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)
(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)
(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)
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)