Carefully explain the differences between the KNN classifier and KNN regression methods.
The KNN classifier method is typically used to solve classification problems with a qualitative response. The KNN class is solved by identifying the neighborhood of \(x0\) and then estimating the conditional probability \(P(Y=j|X=x0)\) for class \(j\) as the fraction of points in the neighborhood whose response values equal \(j\).
The KNN regression method is used to solve regression problems with a quantitative response by again identifying the neighborhood of \(x0\) and then estimating \(f(x0)\) as the average of all the training responses in the neighborhood.
This question involves the use of multiple linear regression on the Auto data set.
library(ISLR)
data(Auto)
summary(Auto)
## mpg cylinders displacement horsepower weight
## Min. : 9.00 Min. :3.000 Min. : 68.0 Min. : 46.0 Min. :1613
## 1st Qu.:17.00 1st Qu.:4.000 1st Qu.:105.0 1st Qu.: 75.0 1st Qu.:2225
## Median :22.75 Median :4.000 Median :151.0 Median : 93.5 Median :2804
## Mean :23.45 Mean :5.472 Mean :194.4 Mean :104.5 Mean :2978
## 3rd Qu.:29.00 3rd Qu.:8.000 3rd Qu.:275.8 3rd Qu.:126.0 3rd Qu.:3615
## Max. :46.60 Max. :8.000 Max. :455.0 Max. :230.0 Max. :5140
##
## acceleration year origin name
## Min. : 8.00 Min. :70.00 Min. :1.000 amc matador : 5
## 1st Qu.:13.78 1st Qu.:73.00 1st Qu.:1.000 ford pinto : 5
## Median :15.50 Median :76.00 Median :1.000 toyota corolla : 5
## Mean :15.54 Mean :75.98 Mean :1.577 amc gremlin : 4
## 3rd Qu.:17.02 3rd Qu.:79.00 3rd Qu.:2.000 amc hornet : 4
## Max. :24.80 Max. :82.00 Max. :3.000 chevrolet chevette: 4
## (Other) :365
str(Auto)
## 'data.frame': 392 obs. of 9 variables:
## $ mpg : num 18 15 18 16 17 15 14 14 14 15 ...
## $ cylinders : num 8 8 8 8 8 8 8 8 8 8 ...
## $ displacement: num 307 350 318 304 302 429 454 440 455 390 ...
## $ horsepower : num 130 165 150 150 140 198 220 215 225 190 ...
## $ weight : num 3504 3693 3436 3433 3449 ...
## $ acceleration: num 12 11.5 11 12 10.5 10 9 8.5 10 8.5 ...
## $ year : num 70 70 70 70 70 70 70 70 70 70 ...
## $ origin : num 1 1 1 1 1 1 1 1 1 1 ...
## $ name : Factor w/ 304 levels "amc ambassador brougham",..: 49 36 231 14 161 141 54 223 241 2 ...
(a) Produce a scatterplot matrix which includes all of the variables in the data set.
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.
cor(Auto[1:8])
## 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_fit<-lm(mpg~.-name, data = Auto)
summary(Auto_fit)
##
## 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
For instance:
i. Is there a relationship between the predictors and the response?
Based on the p-value of the f-statistic (<2.2e-16), there is a relationship between mpg and at least one other variable.
ii. Which predictors appear to have a statistically significant relationship to the response?
Based on the results of the model, the significant predictors of mpg are displacement, weight, year, and origin with p-values below .05.
iii. What does the coefficient for the year variable suggest?
For every unit increase in year results in 0.750773 increase in mpg, meaning vehicles are more fuel efficient with each year.
(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_fit)
The plots are indicating the presence of outliers. The Residuals vs. Fitted plot has a slight dip in the line, which suggests non-linearity. Each plot is identifying outliers for observations 323, 327, and 326. In Cook’s D plot, the bottom right-hand point is showing observation 14 with high leverage.
(e) Use the * and : symbols to fit linear regression models with interaction effects. Do any interactions appear to be statistically significant?
Based on the correlation plot in (b) above, there appears to be a strong correlation (\(>=90%\)) between:
cylinders * displacement 0.9508233
cylinders * weight 0.8975273
displacement * weight 0.9329944
horsepower * displacement 0.8972570
Therefore, I will re-fit the model to these predictors including their interactions.
Auto_fit_int<-lm(mpg~cylinders*displacement + cylinders*weight + displacement*weight + horsepower*displacement, data = Auto[,1:8])
summary(Auto_fit_int)
##
## Call:
## lm(formula = mpg ~ cylinders * displacement + cylinders * weight +
## displacement * weight + horsepower * displacement, data = Auto[,
## 1:8])
##
## Residuals:
## Min 1Q Median 3Q Max
## -11.1599 -2.1702 -0.3695 1.8505 16.9403
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.888e+01 6.574e+00 8.956 < 2e-16 ***
## cylinders -5.920e-01 2.013e+00 -0.294 0.7689
## displacement -7.692e-02 3.980e-02 -1.933 0.0540 .
## weight -5.043e-03 2.934e-03 -1.719 0.0865 .
## horsepower -1.890e-01 3.002e-02 -6.297 8.32e-10 ***
## cylinders:displacement 4.489e-04 4.220e-03 0.106 0.9153
## cylinders:weight 3.482e-04 6.924e-04 0.503 0.6153
## displacement:weight 3.078e-07 9.623e-06 0.032 0.9745
## displacement:horsepower 4.448e-04 1.065e-04 4.178 3.65e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.869 on 383 degrees of freedom
## Multiple R-squared: 0.7593, Adjusted R-squared: 0.7543
## F-statistic: 151 on 8 and 383 DF, p-value: < 2.2e-16
Based on the model, the interaction of displacement and horsepower appears to be statistically significant with a low p-value of 3.65e-05.
(f) Try a few different transformations of the variables, such as \(log(X), √ X, X2\). Comment on your findings.
Since horsepower was significant in my second model Auto_fit_int and not the original, I will conduct the transformations on this predictor.
par(mfrow = c(2,2))
plot((Auto$horsepower), Auto$mpg, xlab = "horsepower", ylab ="MPG")
plot(log(Auto$horsepower), Auto$mpg, xlab = "log(horsepower)", ylab ="MPG")
plot(sqrt(Auto$horsepower), Auto$mpg, xlab = "sqrt(horsepower)", ylab ="MPG")
plot((Auto$horsepower)^2, Auto$mpg, xlab = "horsepower^2", ylab ="MPG")
Auto_fit_log<-lm(mpg~displacement + weight + year + origin + log(horsepower), data=Auto)
summary(Auto_fit_log)
##
## Call:
## lm(formula = mpg ~ displacement + weight + year + origin + log(horsepower),
## data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -8.8091 -1.9147 -0.1579 1.8765 12.5231
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 9.2041460 6.1865858 1.488 0.13763
## displacement 0.0166532 0.0049675 3.352 0.00088 ***
## weight -0.0054967 0.0005671 -9.693 < 2e-16 ***
## year 0.7163068 0.0488094 14.676 < 2e-16 ***
## origin 1.5005416 0.2608891 5.752 1.8e-08 ***
## log(horsepower) -6.4123276 1.1128868 -5.762 1.7e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.215 on 386 degrees of freedom
## Multiple R-squared: 0.8325, Adjusted R-squared: 0.8303
## F-statistic: 383.7 on 5 and 386 DF, p-value: < 2.2e-16
Based on the results of the Auto_fit_log model, the originally significant predictors remain significant along with the log(horsepower).
This question should be answered using the Carseats data set.
library(ISLR)
summary(Carseats)
## Sales CompPrice Income Advertising
## Min. : 0.000 Min. : 77 Min. : 21.00 Min. : 0.000
## 1st Qu.: 5.390 1st Qu.:115 1st Qu.: 42.75 1st Qu.: 0.000
## Median : 7.490 Median :125 Median : 69.00 Median : 5.000
## Mean : 7.496 Mean :125 Mean : 68.66 Mean : 6.635
## 3rd Qu.: 9.320 3rd Qu.:135 3rd Qu.: 91.00 3rd Qu.:12.000
## Max. :16.270 Max. :175 Max. :120.00 Max. :29.000
## Population Price ShelveLoc Age Education
## Min. : 10.0 Min. : 24.0 Bad : 96 Min. :25.00 Min. :10.0
## 1st Qu.:139.0 1st Qu.:100.0 Good : 85 1st Qu.:39.75 1st Qu.:12.0
## Median :272.0 Median :117.0 Medium:219 Median :54.50 Median :14.0
## Mean :264.8 Mean :115.8 Mean :53.32 Mean :13.9
## 3rd Qu.:398.5 3rd Qu.:131.0 3rd Qu.:66.00 3rd Qu.:16.0
## Max. :509.0 Max. :191.0 Max. :80.00 Max. :18.0
## Urban US
## No :118 No :142
## Yes:282 Yes:258
##
##
##
##
(a) Fit a multiple regression model to predict Sales using Price , Urban, and US.
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
(b) Provide an interpretation of each coefficient in the model. Be careful—some of the variables in the model are qualitative!
Based on the results of the model, the significant predictors of Sales are Price and US. For every unit ($1) increase in Price, Sales decrease by $54.Sales within the US are $1,200 higher than those outside the US. Urban is not a significant predictor of Sales.
(c) Write out the model in equation form, being careful to handle the qualitative variables properly.
\(Sales = 13.043469 - (0.0544588)Price - (0.021916)Urban_{Yes} + (1.200573)US_{Yes} + ε\)
(d) For which of the predictors can you reject the null hypothesis \(H_0 : \beta_j = 0\)?
Price and US
(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.
fit2<-lm(Sales~Price+US, data = Carseats)
summary(fit2)
##
## 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 is explained by approximately 23% of the variance in Sales for both models in (a) and (e), which is not a strong fit.
(g) Using the model from (e), obtain 95% confidence intervals for the coefficient(s).
confint(fit2)
## 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(fit2)
It does not appear there are significant outliers in fit2, but there are a few observations that appear to show a higher leverage, though the standardized residual is not significantly different than the rest.
This problem involves simple linear regression without an intercept.
(a) Recall that the coefficient estimate \(\hat{beta}\) 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 of the regression of Y onto X is:
\(\hat{\beta} = \frac{\sum_ix_iy_i}{\sum_jx_j^2}\)
The coefficient estimate of the regression of X onto Y is:
\(\hat{\beta}' = \frac{\sum_ix_iy_i}{\sum_jy_j^2}\)
The coefficients are the same if \(\sum_jx_j^2 = \sum_jy_j^2\)
(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(1)
x = rnorm(100)
y = 2*x
y_lm_fit = lm(y~ x + 0)
x_lm_fit2 = lm(x~ y + 0)
summary(y_lm_fit)
##
## Call:
## lm(formula = y ~ x + 0)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.776e-16 -3.378e-17 2.680e-18 6.113e-17 5.105e-16
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## x 2.000e+00 1.296e-17 1.543e+17 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.167e-16 on 99 degrees of freedom
## Multiple R-squared: 1, Adjusted R-squared: 1
## F-statistic: 2.382e+34 on 1 and 99 DF, p-value: < 2.2e-16
summary(x_lm_fit2)
##
## Call:
## lm(formula = x ~ y + 0)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.888e-16 -1.689e-17 1.339e-18 3.057e-17 2.552e-16
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## y 5.00e-01 3.24e-18 1.543e+17 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 5.833e-17 on 99 degrees of freedom
## Multiple R-squared: 1, Adjusted R-squared: 1
## F-statistic: 2.382e+34 on 1 and 99 DF, p-value: < 2.2e-16
The regression coefficients are different for each linear regression, x 2.000e+00 and y 5.00e-01.
(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.
set.seed(1)
x <- rnorm(100)
y <- -sample(x, 100)
sum(x^2)
## [1] 81.05509
sum(y^2)
## [1] 81.05509
y_lm_fit <- lm(y~ x + 0)
x_lm_fit2 <- lm(x~ y + 0)
summary(y_lm_fit)
##
## Call:
## lm(formula = y ~ x + 0)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.2833 -0.6945 -0.1140 0.4995 2.1665
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## x 0.07768 0.10020 0.775 0.44
##
## Residual standard error: 0.9021 on 99 degrees of freedom
## Multiple R-squared: 0.006034, Adjusted R-squared: -0.004006
## F-statistic: 0.601 on 1 and 99 DF, p-value: 0.4401
summary(x_lm_fit2)
##
## Call:
## lm(formula = x ~ y + 0)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.2182 -0.4969 0.1595 0.6782 2.4017
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## y 0.07768 0.10020 0.775 0.44
##
## Residual standard error: 0.9021 on 99 degrees of freedom
## Multiple R-squared: 0.006034, Adjusted R-squared: -0.004006
## F-statistic: 0.601 on 1 and 99 DF, p-value: 0.4401
In these results, the regression coefficients are the same for each linear regression, x 0.07768 and y 0.07768. Based on the response in (a), as long as the sum(x^2) = sum(y^2) we can expect the coefficient estimates to be the same as well for X onto Y and Y onto X.