Question 2

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

KNN Classifier : KNN Classifier is used to predict the qualitative responses where as KNN Regression is used to estimate quantitative responses. KNN Classifier uses the approach to estimate the conditional distribution of Y given X, and then classify a given observation to the class with highest estimated probability. Given a positive integer K and a test observation x0, the KNN classifier first identifies the neighbors K points in the training data that are closest to x0, represented by N0. It then estimates the conditional probability for class j as the fraction of points in N0.

KNN Regression : The goal of the KNN regression is where the response variable is quantitative and we try to find a relationship between the response variable and the predictors. Given a value for K and a prediction point x0, KNN regression first identifies the K training observations that are closest to x0, represented by N0. It then estimates the f(x0) using the average of all the training responses in N0.

Applied

Question 9

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.

data("Auto")
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, which is qualitative

auto.mod = Auto[,-9]
cor(auto.mod)
##                     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.

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

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

We can observe that the p value for the model is very low, hence at least one of the predictors have significant relationship with the response.

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

By observing the p-value for the individual predictors, displacement, weight, year and origin have low p-values than 0.05(significance level). hence these predictors have statistically significant relationship with the response.

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

The coefficient year is 0.75. which suggest that for every year there is an average increase of 0.75 units in miles per gallon(mpg)

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

par(mfrow = c(2,2))
plot(auto.m1)

  • From the Q-Q plot we observe most of the data is along the normal line but few point on the right tail seem to deviate from the normal line.
  • Residual plot suggests there is a slight trend in the residuals.
  • From the leverage plots, we can observe that some observations have high leverage

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

auto.m2  = lm(data = Auto, formula = mpg ~ -name + cylinders*displacement + weight*horsepower + year + origin)
summary(auto.m2)
## 
## Call:
## lm(formula = mpg ~ -name + cylinders * displacement + weight * 
##     horsepower + year + origin, data = Auto)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -9.2332 -1.6317 -0.0646  1.5501 11.8896 
## 
## Coefficients:
##                          Estimate Std. Error t value Pr(>|t|)    
## (Intercept)             2.463e+00  4.076e+00   0.604  0.54604    
## cylinders              -7.683e-01  4.814e-01  -1.596  0.11129    
## displacement           -2.077e-02  1.581e-02  -1.314  0.18978    
## weight                 -1.024e-02  9.274e-04 -11.039  < 2e-16 ***
## horsepower             -1.981e-01  2.372e-02  -8.352 1.24e-15 ***
## year                    7.716e-01  4.467e-02  17.272  < 2e-16 ***
## origin                  7.159e-01  2.588e-01   2.766  0.00595 ** 
## cylinders:displacement  4.141e-03  2.148e-03   1.928  0.05459 .  
## weight:horsepower       4.580e-05  6.763e-06   6.773 4.77e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.921 on 383 degrees of freedom
## Multiple R-squared:  0.8628, Adjusted R-squared:  0.8599 
## F-statistic: 301.1 on 8 and 383 DF,  p-value: < 2.2e-16

The interaction between weight and horsepower is statistically significant. The other significant rems are weight, horsepower, year and origin.

auto.m3 = lm(data = Auto, formula = mpg ~ -name + origin+ year+ horsepower + displacement+ weight :acceleration+ cylinders : displacement)
summary(auto.m3)
## 
## Call:
## lm(formula = mpg ~ -name + origin + year + horsepower + displacement + 
##     weight:acceleration + cylinders:displacement, data = Auto)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -10.4959  -1.8649  -0.1428   1.5649  12.7422 
## 
## Coefficients:
##                          Estimate Std. Error t value Pr(>|t|)    
## (Intercept)            -1.199e+01  4.235e+00  -2.831 0.004886 ** 
## origin                  9.849e-01  2.924e-01   3.369 0.000831 ***
## year                    7.260e-01  5.075e-02  14.305  < 2e-16 ***
## horsepower             -8.340e-02  1.102e-02  -7.566 2.87e-13 ***
## displacement           -5.769e-02  1.404e-02  -4.109 4.86e-05 ***
## weight:acceleration    -1.762e-04  1.854e-05  -9.502  < 2e-16 ***
## displacement:cylinders  5.379e-03  1.374e-03   3.914 0.000107 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.34 on 385 degrees of freedom
## Multiple R-squared:  0.8197, Adjusted R-squared:  0.8169 
## F-statistic: 291.6 on 6 and 385 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.

par(mfrow = c(3,2))
plot(log(displacement), mpg)
plot(sqrt(displacement), mpg)
plot(I(displacement^2), mpg)

plot(log(horsepower), mpg)
plot(sqrt(horsepower), mpg)
plot(I(horsepower^2), mpg)

From the above plots, log transformation of horsepower seems to be most linear

detach(Auto)

Question 10

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)
str(Carseats)
## 'data.frame':    400 obs. of  11 variables:
##  $ Sales      : num  9.5 11.22 10.06 7.4 4.15 ...
##  $ CompPrice  : num  138 111 113 117 141 124 115 136 132 132 ...
##  $ Income     : num  73 48 35 100 64 113 105 81 110 113 ...
##  $ Advertising: num  11 16 10 4 3 13 0 15 0 0 ...
##  $ Population : num  276 260 269 466 340 501 45 425 108 131 ...
##  $ Price      : num  120 83 80 97 128 72 108 120 124 124 ...
##  $ ShelveLoc  : Factor w/ 3 levels "Bad","Good","Medium": 1 2 3 3 1 1 3 2 3 3 ...
##  $ Age        : num  42 65 59 55 38 78 71 67 76 76 ...
##  $ Education  : num  17 10 12 14 13 16 15 10 10 17 ...
##  $ Urban      : Factor w/ 2 levels "No","Yes": 2 2 2 2 2 1 2 2 1 1 ...
##  $ US         : Factor w/ 2 levels "No","Yes": 2 2 2 2 1 2 1 2 1 2 ...
carseats.m1 =  lm(data = Carseats, formula = Sales ~ Price+ Urban+ US)
summary(carseats.m1)
## 
## 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

The summary of the model suggests that Urban has no significant effect on Sales.Price has a negative effect with $1 increase in price. US is significant.The sales are reduced by $54.There as $1200 more sales in United states when compared to outside of United states.The

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

The model equation for Urban stores in US:

\(Sales = 13.043469 - 0.054459*Price_{Yes} - 0.021916 * Urban_{Yes}\)

(d) For which of the predictors can you reject the null hypothesis

We can reject the null hypothesis if the p-value is less than 0.05, and thus the predictors are significant. In this case, for Price and US we can reject the null hypothesis.

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

carseats.m2 = lm(data = Carseats, formula = Sales ~ Price + US)
summary(carseats.m2)
## 
## 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?

Both the models are not good as they can explain of around 23% of variation in sales.

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

confint(carseats.m2)
##                   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)?

Leverage measures how far is the data point from the mean value. In general \(1/n<=h_i<=1\), there are k independent variables in the model, the men value of leverage id \(\frac{(k+1)}{n}\). The average leverage in our model is \(\frac{(2+1)}{400} = 0.0075\)

par(mfrow = c(2,2))
plot(carseats.m2)

summary(influence.measures(carseats.m2))
## 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

These are some observations that violate rules for each influence measure. We can compare regression models with full data and with out;iers removed.

outlier.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.out.rem = Carseats[-outlier.obs,]
carseats.m3 = lm(Sales~Price+US, data = carseats.out.rem)
summary(carseats.m3)
## 
## Call:
## lm(formula = Sales ~ Price + US, data = carseats.out.rem)
## 
## 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
confint(carseats.m3)
##                   2.5 %      97.5 %
## (Intercept) 11.61711554 14.23334897
## Price       -0.06480885 -0.04313656
## USYes        0.76568499  1.74435086

There is little change in the model by removing outliers as compared to the full model. The confidence intervals for the coefficient estimated from the full model contains those with outliers removed. hence it is safe to include all observations in the model.

Question 12

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 coefficient estimate for the linear regression of Y onto X is \(\hat\beta= \frac{\sum_{i}x_{i}y_{i}}{\sum_{j}x^2_{j}}\)

The coefficient estimate for the linear regression of X onto Y is \(\hat\beta= \frac{\sum_{i}x_{i}y_{i}}{\sum_{j}y^2_{j}}\)

The coefficients for both will be the same when \(\sum_{j}x^2_{j} = \sum_{j}y^2_{j}\)

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

set.seed(40)
x = rnorm(100)
y = rbinom(100, 3, 0.4)
example.1 = lm(y~x)
summary(example.1)
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.3534 -0.3432 -0.2830  0.6835  1.7471 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  1.30745    0.08545  15.301   <2e-16 ***
## x            0.03838    0.08496   0.452    0.652    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.8526 on 98 degrees of freedom
## Multiple R-squared:  0.002078,   Adjusted R-squared:  -0.008105 
## F-statistic: 0.2041 on 1 and 98 DF,  p-value: 0.6525
example.2 = lm(x~y)
summary(example.2)
## 
## Call:
## lm(formula = x ~ y)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.11899 -0.77235  0.07035  0.77262  2.16459 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.004579   0.186838  -0.025    0.980
## y            0.054142   0.119857   0.452    0.652
## 
## Residual standard error: 1.013 on 98 degrees of freedom
## Multiple R-squared:  0.002078,   Adjusted R-squared:  -0.008105 
## F-statistic: 0.2041 on 1 and 98 DF,  p-value: 0.6525

(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 = 1:100
y = 100:1
example.3 = lm(y~x)
summary(example.3)
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -3.575e-14 -5.302e-15 -2.850e-15  4.300e-16  2.680e-13 
## 
## Coefficients:
##               Estimate Std. Error    t value Pr(>|t|)    
## (Intercept)  1.010e+02  5.598e-15  1.804e+16   <2e-16 ***
## x           -1.000e+00  9.624e-17 -1.039e+16   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.778e-14 on 98 degrees of freedom
## Multiple R-squared:      1,  Adjusted R-squared:      1 
## F-statistic: 1.08e+32 on 1 and 98 DF,  p-value: < 2.2e-16
example.4 = lm(x~y)
summary(example.4)
## 
## Call:
## lm(formula = x ~ y)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -2.680e-13 -4.300e-16  2.850e-15  5.302e-15  3.575e-14 
## 
## Coefficients:
##               Estimate Std. Error    t value Pr(>|t|)    
## (Intercept)  1.010e+02  5.598e-15  1.804e+16   <2e-16 ***
## y           -1.000e+00  9.624e-17 -1.039e+16   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.778e-14 on 98 degrees of freedom
## Multiple R-squared:      1,  Adjusted R-squared:      1 
## F-statistic: 1.08e+32 on 1 and 98 DF,  p-value: < 2.2e-16