The KNN classifier tries to predict the class to which the variable belongs through by calculating the probability. The KNN regression attempts to predict the output variable/Dependent variable’s value.
Auto data set.library(ISLR2)
attach(Auto)
Auto <- Auto
Q9(a) Produce a scatterplot matrix which includes all of the variables in the data set.
pairs(Auto)
Q9(b) Compute the matrix of correlations between the variables using the function cor(). You will need to exclude the name variable, cor() which is qualitative.
cor(Auto[,-9])
## mpg cylinders displacement horsepower weight
## mpg 1.0000000 -0.7776175 -0.8051269 -0.7784268 -0.8322442
## cylinders -0.7776175 1.0000000 0.9508233 0.8429834 0.8975273
## displacement -0.8051269 0.9508233 1.0000000 0.8972570 0.9329944
## horsepower -0.7784268 0.8429834 0.8972570 1.0000000 0.8645377
## weight -0.8322442 0.8975273 0.9329944 0.8645377 1.0000000
## acceleration 0.4233285 -0.5046834 -0.5438005 -0.6891955 -0.4168392
## year 0.5805410 -0.3456474 -0.3698552 -0.4163615 -0.3091199
## origin 0.5652088 -0.5689316 -0.6145351 -0.4551715 -0.5850054
## acceleration year origin
## mpg 0.4233285 0.5805410 0.5652088
## cylinders -0.5046834 -0.3456474 -0.5689316
## displacement -0.5438005 -0.3698552 -0.6145351
## horsepower -0.6891955 -0.4163615 -0.4551715
## weight -0.4168392 -0.3091199 -0.5850054
## acceleration 1.0000000 0.2903161 0.2127458
## year 0.2903161 1.0000000 0.1815277
## origin 0.2127458 0.1815277 1.0000000
Q9(c) Use the lm() function to perform a multiple linear regression with mpg as the response and all other variables except name as the predictors. Use the summary() function to print the results. Comment on the output. For instance:
MPGLM <- lm(mpg~.-name, data=Auto)
summary(MPGLM)
##
## Call:
## lm(formula = mpg ~ . - name, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -9.5903 -2.1565 -0.1169 1.8690 13.0604
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -17.218435 4.644294 -3.707 0.00024 ***
## cylinders -0.493376 0.323282 -1.526 0.12780
## displacement 0.019896 0.007515 2.647 0.00844 **
## horsepower -0.016951 0.013787 -1.230 0.21963
## weight -0.006474 0.000652 -9.929 < 2e-16 ***
## acceleration 0.080576 0.098845 0.815 0.41548
## year 0.750773 0.050973 14.729 < 2e-16 ***
## origin 1.426141 0.278136 5.127 4.67e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.328 on 384 degrees of freedom
## Multiple R-squared: 0.8215, Adjusted R-squared: 0.8182
## F-statistic: 252.4 on 7 and 384 DF, p-value: < 2.2e-16
Q9(c)i. Is there a relationship between the
predictors and the response?
A9(c)i. There is a strong relationship between the
predictor and the response. The models R-squared of 0.8215 suggests that
82.15% of the variance in mpg can be explained by the
predictors. In addition, the F-statistic is large at 252.7 and p value
is significant. Q9(c)ii. Which predictors appear to
have a statistically significant relationship to the response?
A9(c)ii. Based on the p-values for each varialbe in the
table above, displacement, weight,
year, and origin are statistically significant
predictor of mpg.
Q9(c)iii. What does the coefficient for the year
variable suggest?
A9(c)iii. The coefficient of .7507727 for the
year variable suggests for every 1 increase in
year (1 year newer), mpg increases by .75.
Q9(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? A9(d) The residual plot shows a non-linearity and the shape of the spread is funneled, showing that the variance is not finite (not homoscedastic). The leverage plot does shows that observation 14 has high leverage.
par(mfrow=c(2,2))
plot(MPGLM)
Q9(e) Use the * and : symbols to fit linear
regression models with interaction effects. Do any interactions appear
to be statistically significant?
A9(e) Most of the interactions appear to be
statistically significant, with models explaining anywhere from 56%
(mpg ~ displacement:horsepower)
to 83% (mpg ~ year * weight) of
variation in the response variable.
summary(lm(mpg ~ displacement*cylinders, data = Auto))
##
## Call:
## lm(formula = mpg ~ displacement * cylinders, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -16.0432 -2.4308 -0.2263 2.2048 20.9051
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 48.22040 2.34712 20.545 < 2e-16 ***
## displacement -0.13436 0.01615 -8.321 1.50e-15 ***
## cylinders -2.41838 0.53456 -4.524 8.08e-06 ***
## displacement:cylinders 0.01182 0.00207 5.711 2.24e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.454 on 388 degrees of freedom
## Multiple R-squared: 0.6769, Adjusted R-squared: 0.6744
## F-statistic: 271 on 3 and 388 DF, p-value: < 2.2e-16
summary(lm(mpg ~ horsepower*cylinders, data = Auto))
##
## Call:
## lm(formula = mpg ~ horsepower * cylinders, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -11.5862 -2.1945 -0.5617 1.9541 16.3329
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 72.815097 3.071314 23.708 <2e-16 ***
## horsepower -0.416007 0.034521 -12.051 <2e-16 ***
## cylinders -6.492462 0.510560 -12.716 <2e-16 ***
## horsepower:cylinders 0.047247 0.004732 9.984 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.094 on 388 degrees of freedom
## Multiple R-squared: 0.727, Adjusted R-squared: 0.7249
## F-statistic: 344.4 on 3 and 388 DF, p-value: < 2.2e-16
summary(lm(mpg ~ displacement*weight, data = Auto))
##
## Call:
## lm(formula = mpg ~ displacement * weight, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -13.8664 -2.4801 -0.3355 1.8071 17.9429
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.372e+01 1.940e+00 27.697 < 2e-16 ***
## displacement -7.831e-02 1.131e-02 -6.922 1.85e-11 ***
## weight -8.931e-03 8.474e-04 -10.539 < 2e-16 ***
## displacement:weight 1.744e-05 2.789e-06 6.253 1.06e-09 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.097 on 388 degrees of freedom
## Multiple R-squared: 0.7265, Adjusted R-squared: 0.7244
## F-statistic: 343.6 on 3 and 388 DF, p-value: < 2.2e-16
summary(lm(mpg ~ displacement*horsepower, data = Auto))
##
## Call:
## lm(formula = mpg ~ displacement * horsepower, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -10.9391 -2.3373 -0.5816 2.1698 17.5771
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.305e+01 1.526e+00 34.77 <2e-16 ***
## displacement -9.805e-02 6.682e-03 -14.67 <2e-16 ***
## horsepower -2.343e-01 1.959e-02 -11.96 <2e-16 ***
## displacement:horsepower 5.828e-04 5.193e-05 11.22 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.944 on 388 degrees of freedom
## Multiple R-squared: 0.7466, Adjusted R-squared: 0.7446
## F-statistic: 381 on 3 and 388 DF, p-value: < 2.2e-16
summary(lm(mpg ~ horsepower*weight, data = Auto))
##
## Call:
## lm(formula = mpg ~ horsepower * weight, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -10.7725 -2.2074 -0.2708 1.9973 14.7314
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.356e+01 2.343e+00 27.127 < 2e-16 ***
## horsepower -2.508e-01 2.728e-02 -9.195 < 2e-16 ***
## weight -1.077e-02 7.738e-04 -13.921 < 2e-16 ***
## horsepower:weight 5.355e-05 6.649e-06 8.054 9.93e-15 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.93 on 388 degrees of freedom
## Multiple R-squared: 0.7484, Adjusted R-squared: 0.7465
## F-statistic: 384.8 on 3 and 388 DF, p-value: < 2.2e-16
summary(lm(mpg ~ cylinders*weight, data = Auto))
##
## Call:
## lm(formula = mpg ~ cylinders * weight, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -14.4916 -2.6225 -0.3927 1.7794 16.7087
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 65.3864559 3.7333137 17.514 < 2e-16 ***
## cylinders -4.2097950 0.7238315 -5.816 1.26e-08 ***
## weight -0.0128348 0.0013628 -9.418 < 2e-16 ***
## cylinders:weight 0.0010979 0.0002101 5.226 2.83e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.165 on 388 degrees of freedom
## Multiple R-squared: 0.7174, Adjusted R-squared: 0.7152
## F-statistic: 328.3 on 3 and 388 DF, p-value: < 2.2e-16
summary(lm(mpg ~ displacement:cylinders, data = Auto))
##
## Call:
## lm(formula = mpg ~ displacement:cylinders, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -11.705 -3.426 -0.450 2.704 17.715
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 30.9896203 0.3905111 79.36 <2e-16 ***
## displacement:cylinders -0.0061177 0.0002462 -24.85 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.863 on 390 degrees of freedom
## Multiple R-squared: 0.6128, Adjusted R-squared: 0.6119
## F-statistic: 617.4 on 1 and 390 DF, p-value: < 2.2e-16
summary(lm(mpg ~ horsepower:cylinders, data = Auto))
##
## Call:
## lm(formula = mpg ~ horsepower:cylinders, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -11.2598 -3.4728 -0.4374 2.7793 17.8564
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 32.4981341 0.4473037 72.65 <2e-16 ***
## horsepower:cylinders -0.0144406 0.0005931 -24.35 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.923 on 390 degrees of freedom
## Multiple R-squared: 0.6031, Adjusted R-squared: 0.6021
## F-statistic: 592.7 on 1 and 390 DF, p-value: < 2.2e-16
summary(lm(mpg ~ displacement:weight, data = Auto))
##
## Call:
## lm(formula = mpg ~ displacement:weight, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -11.5066 -3.3499 -0.6626 2.7207 17.4808
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.126e+01 3.879e-01 80.59 <2e-16 ***
## displacement:weight -1.182e-05 4.600e-07 -25.69 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.763 on 390 degrees of freedom
## Multiple R-squared: 0.6285, Adjusted R-squared: 0.6276
## F-statistic: 659.8 on 1 and 390 DF, p-value: < 2.2e-16
summary(lm(mpg ~ displacement:horsepower, data = Auto))
##
## Call:
## lm(formula = mpg ~ displacement:horsepower, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -10.1917 -3.9460 -0.9919 3.0108 18.2170
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.989e+01 3.901e-01 76.62 <2e-16 ***
## displacement:horsepower -2.694e-04 1.209e-05 -22.28 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 5.184 on 390 degrees of freedom
## Multiple R-squared: 0.56, Adjusted R-squared: 0.5589
## F-statistic: 496.4 on 1 and 390 DF, p-value: < 2.2e-16
summary(lm(mpg ~ horsepower:weight, data = Auto))
##
## Call:
## lm(formula = mpg ~ horsepower:weight, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -11.5691 -3.3660 -0.6786 2.6173 17.5124
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.292e+01 4.471e-01 73.63 <2e-16 ***
## horsepower:weight -2.791e-05 1.106e-06 -25.25 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.815 on 390 degrees of freedom
## Multiple R-squared: 0.6204, Adjusted R-squared: 0.6194
## F-statistic: 637.3 on 1 and 390 DF, p-value: < 2.2e-16
summary(lm(mpg ~ cylinders:weight, data = Auto))
##
## Call:
## lm(formula = mpg ~ cylinders:weight, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -12.3648 -3.1295 -0.3412 2.3604 17.5106
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.429e+01 4.640e-01 73.91 <2e-16 ***
## cylinders:weight -6.168e-04 2.282e-05 -27.03 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.61 on 390 degrees of freedom
## Multiple R-squared: 0.652, Adjusted R-squared: 0.6511
## F-statistic: 730.6 on 1 and 390 DF, p-value: < 2.2e-16
summary(lm(mpg ~ year*weight, data = Auto))
##
## Call:
## lm(formula = mpg ~ year * weight, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -8.0397 -1.9956 -0.0983 1.6525 12.9896
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.105e+02 1.295e+01 -8.531 3.30e-16 ***
## year 2.040e+00 1.718e-01 11.876 < 2e-16 ***
## weight 2.755e-02 4.413e-03 6.242 1.14e-09 ***
## year:weight -4.579e-04 5.907e-05 -7.752 8.02e-14 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.193 on 388 degrees of freedom
## Multiple R-squared: 0.8339, Adjusted R-squared: 0.8326
## F-statistic: 649.3 on 3 and 388 DF, p-value: < 2.2e-16
Q9(f) Try a few different transformations of the
variables, such as log(X), √ X, X2. Comment on your findings.
A9(f) When comparing the models tested, the
transformation of the horsepower variable showed the
biggest increase in how much the mpg variable can be
explained by horsepower(R-squared). Additionally, the
transformation of horsepower compared to the other
transformed variables showed largest decrease in the residual standard
error.
summary(lm(mpg ~ displacement, data = Auto))
##
## Call:
## lm(formula = mpg ~ displacement, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -12.9170 -3.0243 -0.5021 2.3512 18.6128
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 35.12064 0.49443 71.03 <2e-16 ***
## displacement -0.06005 0.00224 -26.81 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.635 on 390 degrees of freedom
## Multiple R-squared: 0.6482, Adjusted R-squared: 0.6473
## F-statistic: 718.7 on 1 and 390 DF, p-value: < 2.2e-16
summary(lm(mpg ~ log(displacement), data = Auto))
##
## Call:
## lm(formula = mpg ~ log(displacement), data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -16.1204 -2.5843 -0.4217 2.1979 19.9005
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 85.6906 2.1422 40.00 <2e-16 ***
## log(displacement) -12.1385 0.4155 -29.21 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.377 on 390 degrees of freedom
## Multiple R-squared: 0.6863, Adjusted R-squared: 0.6855
## F-statistic: 853.4 on 1 and 390 DF, p-value: < 2.2e-16
summary(lm(mpg ~ sqrt(displacement), data = Auto))
##
## Call:
## lm(formula = mpg ~ sqrt(displacement), data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -14.4034 -2.7367 -0.4956 2.3207 19.3499
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 47.11839 0.86246 54.63 <2e-16 ***
## sqrt(displacement) -1.75878 0.06186 -28.43 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.458 on 390 degrees of freedom
## Multiple R-squared: 0.6746, Adjusted R-squared: 0.6738
## F-statistic: 808.5 on 1 and 390 DF, p-value: < 2.2e-16
summary(lm(mpg ~ weight, data = Auto))
##
## Call:
## lm(formula = mpg ~ weight, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -11.9736 -2.7556 -0.3358 2.1379 16.5194
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 46.216524 0.798673 57.87 <2e-16 ***
## weight -0.007647 0.000258 -29.64 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.333 on 390 degrees of freedom
## Multiple R-squared: 0.6926, Adjusted R-squared: 0.6918
## F-statistic: 878.8 on 1 and 390 DF, p-value: < 2.2e-16
summary(lm(mpg ~ log(weight), data = Auto))
##
## Call:
## lm(formula = mpg ~ log(weight), data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -12.4315 -2.6752 -0.2888 1.9429 16.0136
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 209.9433 6.0002 34.99 <2e-16 ***
## log(weight) -23.4317 0.7534 -31.10 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.189 on 390 degrees of freedom
## Multiple R-squared: 0.7127, Adjusted R-squared: 0.7119
## F-statistic: 967.3 on 1 and 390 DF, p-value: < 2.2e-16
summary(lm(mpg ~ poly(weight,3), data = Auto))
##
## Call:
## lm(formula = mpg ~ poly(weight, 3), data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -12.6259 -2.7080 -0.3552 1.8385 16.0816
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 23.4459 0.2112 111.008 < 2e-16 ***
## poly(weight, 3)1 -128.4436 4.1817 -30.716 < 2e-16 ***
## poly(weight, 3)2 23.1589 4.1817 5.538 5.65e-08 ***
## poly(weight, 3)3 0.2204 4.1817 0.053 0.958
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.182 on 388 degrees of freedom
## Multiple R-squared: 0.7151, Adjusted R-squared: 0.7129
## F-statistic: 324.7 on 3 and 388 DF, p-value: < 2.2e-16
summary(lm(mpg ~ horsepower, data = Auto))
##
## Call:
## lm(formula = mpg ~ horsepower, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -13.5710 -3.2592 -0.3435 2.7630 16.9240
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 39.935861 0.717499 55.66 <2e-16 ***
## horsepower -0.157845 0.006446 -24.49 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.906 on 390 degrees of freedom
## Multiple R-squared: 0.6059, Adjusted R-squared: 0.6049
## F-statistic: 599.7 on 1 and 390 DF, p-value: < 2.2e-16
summary(lm(mpg ~ log(horsepower), data = Auto))
##
## Call:
## lm(formula = mpg ~ log(horsepower), data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -14.2299 -2.7818 -0.2322 2.6661 15.4695
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 108.6997 3.0496 35.64 <2e-16 ***
## log(horsepower) -18.5822 0.6629 -28.03 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.501 on 390 degrees of freedom
## Multiple R-squared: 0.6683, Adjusted R-squared: 0.6675
## F-statistic: 785.9 on 1 and 390 DF, p-value: < 2.2e-16
summary(lm(mpg ~ poly(horsepower,2), data = Auto))
##
## Call:
## lm(formula = mpg ~ poly(horsepower, 2), data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -14.7135 -2.5943 -0.0859 2.2868 15.8961
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 23.4459 0.2209 106.13 <2e-16 ***
## poly(horsepower, 2)1 -120.1377 4.3739 -27.47 <2e-16 ***
## poly(horsepower, 2)2 44.0895 4.3739 10.08 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.374 on 389 degrees of freedom
## Multiple R-squared: 0.6876, Adjusted R-squared: 0.686
## F-statistic: 428 on 2 and 389 DF, p-value: < 2.2e-16
summary(lm(mpg ~ weight, data = Auto))
##
## Call:
## lm(formula = mpg ~ weight, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -11.9736 -2.7556 -0.3358 2.1379 16.5194
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 46.216524 0.798673 57.87 <2e-16 ***
## weight -0.007647 0.000258 -29.64 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.333 on 390 degrees of freedom
## Multiple R-squared: 0.6926, Adjusted R-squared: 0.6918
## F-statistic: 878.8 on 1 and 390 DF, p-value: < 2.2e-16
summary(lm(mpg ~ log(weight), data = Auto))
##
## Call:
## lm(formula = mpg ~ log(weight), data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -12.4315 -2.6752 -0.2888 1.9429 16.0136
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 209.9433 6.0002 34.99 <2e-16 ***
## log(weight) -23.4317 0.7534 -31.10 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.189 on 390 degrees of freedom
## Multiple R-squared: 0.7127, Adjusted R-squared: 0.7119
## F-statistic: 967.3 on 1 and 390 DF, p-value: < 2.2e-16
summary(lm(mpg ~ poly(weight,3), data = Auto))
##
## Call:
## lm(formula = mpg ~ poly(weight, 3), data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -12.6259 -2.7080 -0.3552 1.8385 16.0816
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 23.4459 0.2112 111.008 < 2e-16 ***
## poly(weight, 3)1 -128.4436 4.1817 -30.716 < 2e-16 ***
## poly(weight, 3)2 23.1589 4.1817 5.538 5.65e-08 ***
## poly(weight, 3)3 0.2204 4.1817 0.053 0.958
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.182 on 388 degrees of freedom
## Multiple R-squared: 0.7151, Adjusted R-squared: 0.7129
## F-statistic: 324.7 on 3 and 388 DF, p-value: < 2.2e-16
summary(lm(mpg ~ horsepower, data = Auto))
##
## Call:
## lm(formula = mpg ~ horsepower, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -13.5710 -3.2592 -0.3435 2.7630 16.9240
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 39.935861 0.717499 55.66 <2e-16 ***
## horsepower -0.157845 0.006446 -24.49 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.906 on 390 degrees of freedom
## Multiple R-squared: 0.6059, Adjusted R-squared: 0.6049
## F-statistic: 599.7 on 1 and 390 DF, p-value: < 2.2e-16
summary(lm(mpg ~ log(horsepower), data = Auto))
##
## Call:
## lm(formula = mpg ~ log(horsepower), data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -14.2299 -2.7818 -0.2322 2.6661 15.4695
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 108.6997 3.0496 35.64 <2e-16 ***
## log(horsepower) -18.5822 0.6629 -28.03 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.501 on 390 degrees of freedom
## Multiple R-squared: 0.6683, Adjusted R-squared: 0.6675
## F-statistic: 785.9 on 1 and 390 DF, p-value: < 2.2e-16
summary(lm(mpg ~ poly(horsepower,2), data = Auto))
##
## Call:
## lm(formula = mpg ~ poly(horsepower, 2), data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -14.7135 -2.5943 -0.0859 2.2868 15.8961
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 23.4459 0.2209 106.13 <2e-16 ***
## poly(horsepower, 2)1 -120.1377 4.3739 -27.47 <2e-16 ***
## poly(horsepower, 2)2 44.0895 4.3739 10.08 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.374 on 389 degrees of freedom
## Multiple R-squared: 0.6876, Adjusted R-squared: 0.686
## F-statistic: 428 on 2 and 389 DF, p-value: < 2.2e-16
par(mfrow= c(2,2))
plot(lm(mpg ~ displacement, data = Auto))
plot(lm(mpg ~ log(displacement), data = Auto))
plot(lm(mpg ~ sqrt(displacement), data = Auto))
par(mfrow= c(2,2))
plot(lm(mpg ~ weight, data = Auto))
plot(lm(mpg ~ log(weight), data = Auto))
plot(lm(mpg ~ poly(weight,3), data = Auto))
par(mfrow= c(2,2))
plot(lm(mpg ~ horsepower, data = Auto))
plot(lm(mpg ~ log(horsepower), data = Auto))
plot(lm(mpg ~ poly(horsepower,2), data = Auto))
Note: Problem 10 was accomplished during the Reproducible Research
Lesson.
This question should be answered using the Carseats data
set located in the ISLR2 library
library(ISLR)
attach(Carseats)
Q10(a) Fit a multiple regression model to predict
Sales using Price, Urban, and
US. A10(a) Fitted model below using
provided predictors.
fit<-lm(Sales ~ Price + Urban + US, data=Carseats)
summary(fit)
##
## Call:
## lm(formula = Sales ~ Price + Urban + US, data = Carseats)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.9206 -1.6220 -0.0564 1.5786 7.0581
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 13.043469 0.651012 20.036 < 2e-16 ***
## Price -0.054459 0.005242 -10.389 < 2e-16 ***
## UrbanYes -0.021916 0.271650 -0.081 0.936
## USYes 1.200573 0.259042 4.635 4.86e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.472 on 396 degrees of freedom
## Multiple R-squared: 0.2393, Adjusted R-squared: 0.2335
## F-statistic: 41.52 on 3 and 396 DF, p-value: < 2.2e-16
Q10(b) Provide an interpretation of each coefficient in the model. Be careful—some of the variables in the model are qualitative!
A10(b) From the table above, Price and
US are significant predictor of Sales, for
every $1 increase in my price, Sales decreases
by $54. Sales inside the US are $1,200 higher than sales outside of the
US. Urban has no significant affect on
Sales.
Q10(c) Write out the model in equation form, being
careful to handle the qualitative variables properly.
A10(c) \(Sales=13.043469 -
.054459Price - .021916Urban_{Yes} + 1.200573US_{Yes}\)
Q10(d) For which of the predictors can you reject
the null hypothesis** \(H_0 : \beta_j =
0\)?
A10(d) Price and US.
Q10(e) On the basis of your response to the previous
question, fit a smaller model that only uses the predictors for which
there is evidence of association with the outcome.
A10(e) Fitted model below only using Price
and US.
fit<-lm(Sales ~ Price + US, data=Carseats)
summary(fit)
##
## Call:
## lm(formula = Sales ~ Price + US, data = Carseats)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.9269 -1.6286 -0.0574 1.5766 7.0515
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 13.03079 0.63098 20.652 < 2e-16 ***
## Price -0.05448 0.00523 -10.416 < 2e-16 ***
## USYes 1.19964 0.25846 4.641 4.71e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.469 on 397 degrees of freedom
## Multiple R-squared: 0.2393, Adjusted R-squared: 0.2354
## F-statistic: 62.43 on 2 and 397 DF, p-value: < 2.2e-16
Q10(f) How well do the models in (a) and (e) fit the
data?
A10(f) The models don’t fit well, each model explains
around 23% of the variance in sales.
Q10(g) Using the model from (e), obtain 95 % confidence intervals for the coefficient(s). A10(g) Price is -0.06475984 and -0.04419543. USYes is 0.69151957 and 1.70776632.
confint(fit)
## 2.5 % 97.5 %
## (Intercept) 11.79032020 14.27126531
## Price -0.06475984 -0.04419543
## USYes 0.69151957 1.70776632
Q10(h) Is there evidence of outliers or high
leverage observations in the model from (e)?
A10(h) R has built in functions that can help us
identify influential points using various statistics with one simple
command. Researchers have suggested several cutoff levels or upper
limits to what is the acceptable influence an observation should have
before being considered an outlier. For example, the average leverage
\(\frac{(p+1)}{n}\) which for us is
\(\frac{(2+1)}{400} = 0.0075\).
par(mfrow=c(2,2))
plot(fit)
summary(influence.measures(fit))
## Potentially influential observations of
## lm(formula = Sales ~ Price + US, data = Carseats) :
##
## dfb.1_ dfb.Pric dfb.USYs dffit cov.r cook.d hat
## 26 0.24 -0.18 -0.17 0.28_* 0.97_* 0.03 0.01
## 29 -0.10 0.10 -0.10 -0.18 0.97_* 0.01 0.01
## 43 -0.11 0.10 0.03 -0.11 1.05_* 0.00 0.04_*
## 50 -0.10 0.17 -0.17 0.26_* 0.98 0.02 0.01
## 51 -0.05 0.05 -0.11 -0.18 0.95_* 0.01 0.00
## 58 -0.05 -0.02 0.16 -0.20 0.97_* 0.01 0.01
## 69 -0.09 0.10 0.09 0.19 0.96_* 0.01 0.01
## 126 -0.07 0.06 0.03 -0.07 1.03_* 0.00 0.03_*
## 160 0.00 0.00 0.00 0.01 1.02_* 0.00 0.02
## 166 0.21 -0.23 -0.04 -0.24 1.02 0.02 0.03_*
## 172 0.06 -0.07 0.02 0.08 1.03_* 0.00 0.02
## 175 0.14 -0.19 0.09 -0.21 1.03_* 0.02 0.03_*
## 210 -0.14 0.15 -0.10 -0.22 0.97_* 0.02 0.01
## 270 -0.03 0.05 -0.03 0.06 1.03_* 0.00 0.02
## 298 -0.06 0.06 -0.09 -0.15 0.97_* 0.01 0.00
## 314 -0.05 0.04 0.02 -0.05 1.03_* 0.00 0.02_*
## 353 -0.02 0.03 0.09 0.15 0.97_* 0.01 0.00
## 357 0.02 -0.02 0.02 -0.03 1.03_* 0.00 0.02
## 368 0.26 -0.23 -0.11 0.27_* 1.01 0.02 0.02_*
## 377 0.14 -0.15 0.12 0.24 0.95_* 0.02 0.01
## 384 0.00 0.00 0.00 0.00 1.02_* 0.00 0.02
## 387 -0.03 0.04 -0.03 0.05 1.02_* 0.00 0.02
## 396 -0.05 0.05 0.08 0.14 0.98_* 0.01 0.00
R points out a few observations that violate various rules for each influence measure. Typically, one can demonstrate these statistics and report both a regression with all data included and one with the outliers removed and compare.
outyling.obs<-c(26,29,43,50,51,58,69,126,160,166,172,175,210,270,298,314,353,357,368,377,384,387,396)
Carseats.small<-Carseats[-outyling.obs,]
fit2<-lm(Sales~Price+US,data=Carseats.small)
summary(fit2)
##
## Call:
## lm(formula = Sales ~ Price + US, data = Carseats.small)
##
## Residuals:
## Min 1Q Median 3Q Max
## -5.263 -1.605 -0.039 1.590 5.428
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 12.925232 0.665259 19.429 < 2e-16 ***
## Price -0.053973 0.005511 -9.794 < 2e-16 ***
## USYes 1.255018 0.248856 5.043 7.15e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.29 on 374 degrees of freedom
## Multiple R-squared: 0.2387, Adjusted R-squared: 0.2347
## F-statistic: 58.64 on 2 and 374 DF, p-value: < 2.2e-16
With these potential outliers or influential observations removed, very little changes from the linear model fit to the full data set. The confidence interval for the coefficient estimates produced by the linear model fit to the full data set contain the estimates of the coefficients for the estimates of the model with the outliers removed. It’s safe to include all of the data points in our model.
This problem involves simple linear regression without an
intercept.
Q12(a) Recall that the coefficient estimate βˆ for the
linear regression of Y onto X without an intercept is given by (3.38).
Under what circumstance is the coefficient estimate for the regression
of X onto Y the same as the coefficient estimate for the regression of Y
onto X?
A12(a) The coefficient estimate for the regression of X
onto Y would be the same as the coefficient estimate for the regression
of Y onto X when the coefficient for X is equal to 1 and there is no
irreducible error resulting in a perfect linear relationship.
Q12(b) Generate an example in R with n = 100 observations in which the coefficient estimate for the regression of X onto Y is different from the coefficient estimate for the regression of Y onto X.
X <- rnorm(100)
Y <- X^2 + rnorm(100)
RegX <- lm(X~Y+0)
RegY <- lm(Y~X+0)
coef(RegX)
## Y
## 0.03589451
coef(RegY)
## X
## 0.1757132
Q12(c) Generate an example in R with n = 100 observations in which the coefficient estimate for the regression of X onto Y is the same as the coefficient estimate for the regression of Y onto X.
X2 <- rnorm(100)
Y2 <- X2
RegX.same <- lm(X2~Y2+0)
RegY.same <- lm(Y2~X2+0)
coef(RegX.same)
## Y2
## 1
coef(RegY.same)
## X2
## 1