Conceptual Questions

2.) Carefully explain the differences between the KNN classifier and KNN regression methods.

The KNN classifier is used on qualitative groups and attempts to identify the closest points to the test observations that belongs to the most commonly-occurring class. The KNN regression is used on quantitative groups that identifies the closest observations by averaging all training responses.

Applied Questions

9.) This question involves the use of multiple linear regression on the Auto data set.
(a) Produce a scatterplot matrix which includes all of the variables in the data set.

library(ISLR)
attach(Auto)
pairs(Auto)

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

Auto$name<-NULL
cor(Auto)
##                     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

(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:

i. Is there a relationship between the predictors and the response?

The results of the multiple linear regression below indicates that there is a relationship between the predictors and the response variables because most of the predictors are statistically significant and shows evidence that there is a relationship.

ii. Which predictors appear to have a statistically significant relationship to the response?

The predictors that are statistically significant at alpha = .05 are displacement, weight, year, and origin.

iii. What does the coefficient for the year variable suggest?

The positive coefficient of the year 0.750773 suggests that for every increase in year, the mpg increases by 0.750773.

lm.fit <- lm(mpg~., data = Auto)
summary(lm.fit)
## 
## Call:
## lm(formula = mpg ~ ., 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

(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?

By the appearance of the diagnostics plots below, the residuals vs fitted plots suggests that the data is nonlinear. The Normal Q-Q plot suggests that there are a few outliers on the right tail and it is slightly right skewed. The scale location seems to get wider and wider to the right and could be a sign of heteroscedasticity (unequal variance). The Residuals vs Leverage plot shows a case with a high leverage and is far beyond the cook’s distance line which is observation #14.

par(mfrow = c(2, 2))
plot(lm.fit)

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

Yes, the first model below shows statistically significant interactions between displacement and weight. The second model shows statistically significant interactions between cylinders and horsepower.

lm.fit.2 <- lm(mpg ~ . + weight*displacement, data=Auto)
summary(lm.fit.2)
## 
## Call:
## lm(formula = mpg ~ . + weight * displacement, data = Auto)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -9.9027 -1.8092 -0.0946  1.5549 12.1687 
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## (Intercept)         -5.389e+00  4.301e+00  -1.253   0.2109    
## cylinders            1.175e-01  2.943e-01   0.399   0.6899    
## displacement        -6.837e-02  1.104e-02  -6.193 1.52e-09 ***
## horsepower          -3.280e-02  1.238e-02  -2.649   0.0084 ** 
## weight              -1.064e-02  7.136e-04 -14.915  < 2e-16 ***
## acceleration         6.724e-02  8.805e-02   0.764   0.4455    
## year                 7.852e-01  4.553e-02  17.246  < 2e-16 ***
## origin               5.610e-01  2.622e-01   2.139   0.0331 *  
## displacement:weight  2.269e-05  2.257e-06  10.054  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.964 on 383 degrees of freedom
## Multiple R-squared:  0.8588, Adjusted R-squared:  0.8558 
## F-statistic: 291.1 on 8 and 383 DF,  p-value: < 2.2e-16
lm.fit.2 <- lm(mpg ~ . + cylinders:horsepower, data=Auto)
summary(lm.fit.2)
## 
## Call:
## lm(formula = mpg ~ . + cylinders:horsepower, data = Auto)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -9.2399 -1.6871 -0.0511  1.2858 11.9380 
## 
## Coefficients:
##                        Estimate Std. Error t value Pr(>|t|)    
## (Intercept)          11.7025260  4.9115648   2.383 0.017676 *  
## cylinders            -4.3060695  0.4580950  -9.400  < 2e-16 ***
## displacement         -0.0013925  0.0069110  -0.201 0.840426    
## horsepower           -0.3156601  0.0306339 -10.304  < 2e-16 ***
## weight               -0.0038948  0.0006231  -6.250 1.09e-09 ***
## acceleration         -0.1703028  0.0901427  -1.889 0.059612 .  
## year                  0.7393193  0.0448736  16.476  < 2e-16 ***
## origin                0.9031644  0.2496880   3.617 0.000338 ***
## cylinders:horsepower  0.0402008  0.0037856  10.619  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.929 on 383 degrees of freedom
## Multiple R-squared:  0.8621, Adjusted R-squared:  0.8592 
## F-statistic: 299.3 on 8 and 383 DF,  p-value: < 2.2e-16

(f) Try a few different transformations of the variables, such as log(X), √X, X2. Comment on your findings.

In comparison to the initial model in 9(c), displacements becomes insignificant and acceleration becomes significant.

lm.fit.3 = lm(mpg ~ . + log(horsepower) + sqrt(acceleration) + I(weight^2), data=Auto)
summary(lm.fit.3)
## 
## Call:
## lm(formula = mpg ~ . + log(horsepower) + sqrt(acceleration) + 
##     I(weight^2), data = Auto)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -9.2465 -1.5385 -0.0272  1.4602 12.3071 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)         9.509e+01  1.614e+01   5.892 8.41e-09 ***
## cylinders          -7.470e-03  2.827e-01  -0.026  0.97893    
## displacement       -3.513e-03  7.089e-03  -0.496  0.62050    
## horsepower          4.886e-02  3.285e-02   1.487  0.13777    
## weight             -1.425e-02  2.302e-03  -6.192 1.54e-09 ***
## acceleration        3.140e+00  1.030e+00   3.047  0.00247 ** 
## year                7.851e-01  4.466e-02  17.581  < 2e-16 ***
## origin              6.412e-01  2.499e-01   2.566  0.01067 *  
## log(horsepower)    -1.163e+01  3.865e+00  -3.008  0.00280 ** 
## sqrt(acceleration) -2.672e+01  8.215e+00  -3.252  0.00125 ** 
## I(weight^2)         1.585e-06  3.160e-07   5.017 8.05e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.864 on 381 degrees of freedom
## Multiple R-squared:  0.8688, Adjusted R-squared:  0.8653 
## F-statistic: 252.2 on 10 and 381 DF,  p-value: < 2.2e-16

10.) This question should be answered using the Carseats data set. (a) Fit a multiple regression model to predict Sales using Price, Urban, and US.

attach(Carseats)
lm.fit.4 <- lm(Sales ~ Price + Urban + US, data = Carseats)
summary(lm.fit.4)
## 
## 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

(b) Provide an interpretation of each coefficient in the model. Be careful—some of the variables in the model are qualitative!

The negative coefficient of the price, suggests that for every $1000 increase in price then the sales decreases by 54.459. The coefficient of the UrbanYes is statistically insignificant and there is not enough evidence whether this type of location affects the sales. The positive coefficient of the USYes, suggests that the sales increases by 1,200 if the store is located in US.

(c) Write out the model in equation form, being careful to handle the qualitative variables properly.

Sales = 13.04 - 0.054Price - 0.022UrbanYes + 1.20USYes

(d) For which of the predictors can you reject the null hypothesis H0 : βj = 0?

At alpha = 0.05, the predictors Price and USYes because the p-values is less than 0.05.

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

lm.fit.5 <- lm(Sales ~ Price + US, data = Carseats)
summary(lm.fit.5)
## 
## 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

(f) How well do the models in (a) and (e) fit the data?

The model doesn’t quite fit well because the R squared is fairly small, the Adjusted R-squared: 0.2335 on the first model explaining only 23.35% of the variance in sales. Although there is slight increase in the Adjusted R-squared: 0.2354 in the second model, it still only explains 23.54% of the variance in sales.

(g) Using the model from (e), obtain 95% confidence intervals for the coefficient(s).

confint(lm.fit.5)
##                   2.5 %      97.5 %
## (Intercept) 11.79032020 14.27126531
## Price       -0.06475984 -0.04419543
## USYes        0.69151957  1.70776632

(h) Is there evidence of outliers or high leverage observations in the model from (e)?

The Normal Q-Q plot seems to be normal and doesn’t indicate any outlier. However, on the Residuals vs Leverage, there seems to be a data point on the further right of the plot that could have high leverage that needs to be investigated.

par(mfrow=c(2,2))
plot(lm.fit.5)

12.) This problem involves simple linear regression without an intercept.

(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?

The estimate for the regression of X onto Y would be the same as the coefficient estimate for the regression of Y onto X if the coefficient if equal to 1, along with no indication of noise.

(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=0.5*x+rnorm(100)
coefficients(lm(x~y+0))
##         y 
## 0.3680156
coefficients(lm(y~x+0))
##         x 
## 0.5655502

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

x=rnorm(100)
y=1*x
coefficients(lm(x~y+0))
## y 
## 1
coefficients(lm(y~x+0))
## x 
## 1