library(ISLR)
## Warning: package 'ISLR' was built under R version 4.0.5

Question 2

Carefully explain the differences between between the KNN classifier and KNN regression methods. KNN classifier : The goal of the KNN classifier is classification, where the response variable is categorical. Given a positive integer K and a test observation x0, the KNN classifier first identifies the K points in the training data that are closest to x0, represented by N0. It then estimates the conditional probability for class j as a fraction of the points in N0 whose response values equals j. Then, it applies the Bayes rule and classifies the test observation x0 to the class with the largest probability.

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.

`

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

Auto=read.csv("C:/Auto.csv",header=T,na.strings="?", stringsAsFactors=FALSE)
Auto=na.omit(Auto)
fix(Auto)
qualitative_columns = c(2,8,9)
pairs( Auto[,-qualitative_columns] )

(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.num= Auto[-9] #Exclude name variable
auto.cor = cor(auto.num)
auto.cor
##                     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.

lm.auto = lm(mpg~cylinders+displacement+horsepower+weight+acceleration+year+origin, 
             data = Auto)
summary(lm.auto)
## 
## Call:
## lm(formula = mpg ~ cylinders + displacement + horsepower + weight + 
##     acceleration + year + origin, 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

Comment on the output. For instance:

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

The p-value is very small (<2.2e-16), Also, the goodness-of-fit measured by R-squared value is 82.15% which means that 82% of the variance can be explained by the model. Therefore, there is a relationship between the predictors and the response.

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

displacement, weight, year and origin have a statistically significant relationship to the response since their p-values are less than 0.05

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

The year variable is statistically significant to the response since it has a p-value of < 2.2e-16. The coefficient is 0.750773 which suggests that for each year that progresses, the miles per gallon increases by 0.750773.

(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(lm.auto)

Based on the normal Q-Q plot, the data follows normality because most of the points are very close to the line. The curve shape in the residuals plot shows some non-linearity in the data. The leverage plot shows the influential point (observation 14) with unusually high leverage between -2 and +2.

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

lm.inter1 = lm(mpg~-name+cylinders*displacement+origin+horsepower*weight*acceleration+year,data = Auto)
summary(lm.inter1)
## 
## Call:
## lm(formula = mpg ~ -name + cylinders * displacement + origin + 
##     horsepower * weight * acceleration + year, data = Auto)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -9.3693 -1.5354  0.0358  1.3874 11.9225 
## 
## Coefficients:
##                                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                    -7.480e+00  1.117e+01  -0.670  0.50338    
## cylinders                      -4.100e-01  5.011e-01  -0.818  0.41367    
## displacement                   -2.079e-02  1.645e-02  -1.264  0.20692    
## origin                          6.491e-01  2.560e-01   2.536  0.01161 *  
## horsepower                      3.592e-02  1.040e-01   0.345  0.73007    
## weight                         -9.580e-03  3.445e-03  -2.781  0.00569 ** 
## acceleration                    6.872e-01  6.227e-01   1.104  0.27042    
## year                            7.631e-01  4.449e-02  17.154  < 2e-16 ***
## cylinders:displacement          2.521e-03  2.239e-03   1.126  0.26094    
## horsepower:weight               1.115e-05  2.721e-05   0.410  0.68220    
## horsepower:acceleration        -1.832e-02  7.397e-03  -2.476  0.01371 *  
## weight:acceleration             1.205e-05  2.099e-04   0.057  0.95425    
## horsepower:weight:acceleration  2.700e-06  1.922e-06   1.404  0.16109    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.877 on 379 degrees of freedom
## Multiple R-squared:  0.8683, Adjusted R-squared:  0.8642 
## F-statistic: 208.3 on 12 and 379 DF,  p-value: < 2.2e-16

P-values of origin, weight, year and horsepower:acceleration are less than 0.05, hense these variables are statistically significant.

lm.inter2 = lm(mpg~-name+displacement+origin+horsepower:weight+acceleration:cylinders+year,data = Auto)
summary(lm.inter2)
## 
## Call:
## lm(formula = mpg ~ -name + displacement + origin + horsepower:weight + 
##     acceleration:cylinders + year, data = Auto)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -11.2393  -2.1992  -0.2751   2.0135  13.7888 
## 
## Coefficients:
##                          Estimate Std. Error t value Pr(>|t|)    
## (Intercept)            -2.149e+01  4.464e+00  -4.815 2.12e-06 ***
## displacement           -9.433e-03  6.433e-03  -1.466    0.143    
## origin                  1.545e+00  3.092e-01   4.997 8.85e-07 ***
## year                    7.088e-01  5.581e-02  12.700  < 2e-16 ***
## horsepower:weight      -1.289e-05  2.557e-06  -5.040 7.16e-07 ***
## acceleration:cylinders -6.221e-02  1.234e-02  -5.041 7.14e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.718 on 386 degrees of freedom
## Multiple R-squared:  0.776,  Adjusted R-squared:  0.7731 
## F-statistic: 267.5 on 5 and 386 DF,  p-value: < 2.2e-16

P-values of origin, year, horsepower:weight and acceleration:cylinders are less than 0.05, hense these variables are statistically significant.

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

par(mfrow=c(2,2))
attach(Auto)

plot(log(horsepower), mpg)

plot(sqrt(horsepower), mpg)

plot(I(horsepower^2), mpg)

Looking at the different transformations of horsepower, we see that the log transformation gives the most linear looking plot.

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

#detach(Auto)
#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 ...
lm.car = lm(Sales ~ Price + Urban + US, data = Carseats)
summary(lm.car)
## 
## 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 p-value of Price is very small (<2e-16), hence it is statistically significant to Sales. For 1 unit increase in Price, the Sales decreases by 0.0544 Since the coefficient is negative.

The p-value of Urban is 0.936, larger than 0.05, so it is not significant to the response variable.

The p-value of US is smaller than 0.05 (4.86e-06), So it is statistically significant. Since the coefficient is positive, we can say that car seat stores in the US have more sales than stors that are out of US.

str(data.frame(Carseats$Price, Carseats$Urban, Carseats$US))
## 'data.frame':    400 obs. of  3 variables:
##  $ Carseats.Price: num  120 83 80 97 128 72 108 120 124 124 ...
##  $ Carseats.Urban: Factor w/ 2 levels "No","Yes": 2 2 2 2 2 1 2 2 1 1 ...
##  $ Carseats.US   : Factor w/ 2 levels "No","Yes": 2 2 2 2 1 2 1 2 1 2 ...

Urban and US are qualitative varialbles.

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

contrasts(Carseats$Urban)
##     Yes
## No    0
## Yes   1
contrasts(Carseats$US)
##     Yes
## No    0
## Yes   1

Sales=13.043469−0.054459∗Price−0.021916∗UrbanYes+1.200573∗USYes.

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

The null hypothesis can be rejected for Price and US since their p-values are 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.sigvar = lm(Sales ~ Price+US, data = Carseats)
summary(lm.sigvar)
## 
## 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
  1. How well do the models in (a) and (e) fit the data?
anova(lm.car, lm.sigvar)

Based on lm.car and lm.sigvar, there is a small increase in adjusted R-squared after removing the Urban. But the anova results show that there is not a big difference between both models.

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

confint(lm.sigvar, level = 0.95)
##                   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)?

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

which.max(hatvalues(lm.sigvar))
## 43 
## 43

From the normal Q-Q plot, we can see that most of the data falls on the normal Q-Q line. Hence, normality assumption is not violated.

Based on the Residuals vs Leverage plot there are a few outliers scattered beyond -2 and +2. Observation 43 is an outlier by the hatvalues function.

Question 12

This problem involves simple linear regression without an intercept.

(a) Recall that the coefficient estimate β-hat 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 β=∑ix_iy_i/∑jx2_j

The coefficient estimate for the linear regression of X onto Y is β′=∑ix_iy_i/∑jy2_j

The coefficients for both will be the same when ∑jx2_j=∑jy2_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.

x=rnorm(100)
y=2+x+3
Exm_1<-lm(y~x+0)
summary(Exm_1)
## 
## Call:
## lm(formula = y ~ x + 0)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
##  4.463  4.845  4.998  5.153  5.605 
## 
## Coefficients:
##   Estimate Std. Error t value Pr(>|t|)
## x   0.7637     0.5368   1.423    0.158
## 
## Residual standard error: 5.02 on 99 degrees of freedom
## Multiple R-squared:  0.02003,    Adjusted R-squared:  0.01013 
## F-statistic: 2.024 on 1 and 99 DF,  p-value: 0.158
Exm_2<-lm(x~y+0)
summary(Exm_2)
## 
## Call:
## lm(formula = x ~ y + 0)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.3418 -0.7695 -0.1377  0.5003  2.3609 
## 
## Coefficients:
##   Estimate Std. Error t value Pr(>|t|)
## y  0.02623    0.01844   1.423    0.158
## 
## Residual standard error: 0.9304 on 99 degrees of freedom
## Multiple R-squared:  0.02003,    Adjusted R-squared:  0.01013 
## F-statistic: 2.024 on 1 and 99 DF,  p-value: 0.158

(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+rnorm(100)/1000
y=1:100+rnorm(100)/1000
Exm_3<-lm(y~x+0)
summary(Exm_3)
## 
## Call:
## lm(formula = y ~ x + 0)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -0.0032797 -0.0010422  0.0000452  0.0009913  0.0033028 
## 
## Coefficients:
##    Estimate Std. Error t value Pr(>|t|)    
## x 1.000e+00  2.484e-06  402501   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.001445 on 99 degrees of freedom
## Multiple R-squared:      1,  Adjusted R-squared:      1 
## F-statistic: 1.62e+11 on 1 and 99 DF,  p-value: < 2.2e-16
Exm_4<-lm(x~y+0)
summary(Exm_4)
## 
## Call:
## lm(formula = x ~ y + 0)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -0.0033027 -0.0009913 -0.0000452  0.0010422  0.0032797 
## 
## Coefficients:
##    Estimate Std. Error t value Pr(>|t|)    
## y 1.000e+00  2.484e-06  402501   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.001445 on 99 degrees of freedom
## Multiple R-squared:      1,  Adjusted R-squared:      1 
## F-statistic: 1.62e+11 on 1 and 99 DF,  p-value: < 2.2e-16