Carefully explain the differences between the KNN classifier and KNN regression methods
In KNN - to predict y for a given value of x in the training data, We identify the neighborhood of x based on k neighbors.KNN classifier is used typically when the response is qualitative. Consider an example where k = 3, we search for three neighbors of x. Suppose, the three neighbors of x are: two circles and a square. In KNN classifier, the test observation belongs to most commonly occurring class. In the above example, the value of test observation is a circle.
KNN regression is similar to KNN classifier. However, here the response is quantitative. Here again, we identify the neighborhood of x based on k neighbors. To predict y for a given value of x, we take average of the responses of k neighbors of x. Consider an example: we have two variables height and weight in the training dataset. We would like to identify weight(response variable) of the testing data set. Suppose: k = 2 and the values of “weight”(response variable) close to the data point x is 100 and 120 pounds. Then, the predicted y value of the testing dataset takes the value of average of 100 and 120. i.e., 110 pounds.
This question involves the use of multiple linear regression on the Auto data set.
library(ISLR)
attach(Auto)
View(Auto)
(a) Produce a scatterplot matrix which includes all of the variables in the data set.
pairs(Auto,
main="Simple Scatterplot Matrix")
(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.
a <- Auto[1:8] #Creating a dataset without names
cor(a)
## 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:
fit <- lm(formula = mpg ~ . - name, data = Auto)
summary(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
i. Is there a relationship between the predictors and the response?
The p-values values and F-statistic clearly show that there is association between mpg and other predictors.
ii. Which predictors appear to have a statistically significant relationship to the response?
By checking on the p-values and t-statistic, “displacement”, “weight”, “year” and “origin” have a statistically significant relationship with miles per gallon (mpg). However, “cylinders”, “horsepower” and “acceleration” do not have statistically significant relationship with the response.
iii. What does the coefficient for the year variable suggest?
With each growing year, the miles per gallon increases by 0.75 (i.e., the cars become become more fuel efficient every year by 0.75 mpg/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(fit)
The residual plot shows that 326, 327 and 323 are highlighted as the outliers. The leverage plot suggest that there may be leverage points: 327, 394 and 14.To plot the leverage points - I plotted the hatvalues and found that point 14 has the largest leverage statistic.
plot(hatvalues (fit))
which.max(hatvalues (fit))
## 14
## 14
Let us plot the studentized residual.
plot(predict (fit), rstudent (fit))
In the studentized residuals plot, we find that there are several outliers (above the value of 3).
(e) Use the * and : symbols to fit linear regression models with interaction effects. Do any interactions appear to be statistically significant?
fit1 <- lm(mpg ~ cylinders+displacement+horsepower+weight+acceleration+year+origin+ displacement:cylinders + displacement *weight)
summary(fit1)
##
## Call:
## lm(formula = mpg ~ cylinders + displacement + horsepower + weight +
## acceleration + year + origin + displacement:cylinders + displacement *
## weight)
##
## Residuals:
## Min 1Q Median 3Q Max
## -10.0609 -1.7589 -0.0494 1.5790 12.1496
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -4.795e+00 4.515e+00 -1.062 0.28883
## cylinders -1.091e-01 5.965e-01 -0.183 0.85502
## displacement -7.186e-02 1.363e-02 -5.273 2.25e-07 ***
## horsepower -3.457e-02 1.304e-02 -2.651 0.00836 **
## weight -1.030e-02 1.064e-03 -9.680 < 2e-16 ***
## acceleration 6.618e-02 8.817e-02 0.751 0.45334
## year 7.840e-01 4.566e-02 17.171 < 2e-16 ***
## origin 5.475e-01 2.643e-01 2.071 0.03901 *
## cylinders:displacement 1.186e-03 2.715e-03 0.437 0.66251
## displacement:weight 2.141e-05 3.712e-06 5.768 1.66e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.967 on 382 degrees of freedom
## Multiple R-squared: 0.8588, Adjusted R-squared: 0.8555
## F-statistic: 258.2 on 9 and 382 DF, p-value: < 2.2e-16
From the correlation matrix, I obtained the two highest correlated pairs and used them in picking my interaction effects. From the p-values, we can see that the interaction between displacement and weight is statistically significant, while the interaction between cylinders and displacement is not.
(f) Try a few different transformations of the variables, such as log(X), √X, X2. Comment on your findings.
fit2 = lm(mpg~log(weight)+sqrt(horsepower)+acceleration+I(acceleration^2))
summary(fit2)
##
## Call:
## lm(formula = mpg ~ log(weight) + sqrt(horsepower) + acceleration +
## I(acceleration^2))
##
## Residuals:
## Min 1Q Median 3Q Max
## -11.2932 -2.5082 -0.2237 2.0237 15.7650
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 178.30303 10.80451 16.503 < 2e-16 ***
## log(weight) -14.74259 1.73994 -8.473 5.06e-16 ***
## sqrt(horsepower) -1.85192 0.36005 -5.144 4.29e-07 ***
## acceleration -2.19890 0.63903 -3.441 0.000643 ***
## I(acceleration^2) 0.06139 0.01857 3.305 0.001037 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.99 on 387 degrees of freedom
## Multiple R-squared: 0.7414, Adjusted R-squared: 0.7387
## F-statistic: 277.3 on 4 and 387 DF, p-value: < 2.2e-16
From the p-values, it is clear that the log(weight), sqrt(horsepower), and acceleration^2 are statistically significant. And they are associated with miles per gallon.
This question should be answered using the Carseats data set.
library(ISLR)
attach(Carseats)
(a) Fit a multiple regression model to predict ‘Sales’ using ‘Price’,‘Urban’, and ‘US’.
fit <- lm(Sales ~ Price + Urban + US)
summary(fit)
##
## Call:
## lm(formula = Sales ~ Price + Urban + US)
##
## 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!
From the table above, ‘Price’ and ‘US’ are significant predictors of ‘Sales’.For every $1 dollar increase in ‘Price’ the ‘Sales’ decreases by $54.Sales inside the US are $1,200 higher than sales outside the US.’Urban’ has no effect on ‘Sales’
(c) Write out the model in equation form, being careful to handle the qualitative variables properly. \(Sales = 13.043469 - 0.054459 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.
fit <- lm(Sales ~ Price + US)
summary(fit)
##
## Call:
## lm(formula = Sales ~ Price + US)
##
## 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?
Not well, each model explains around 23% of the variance in Sales.
(g) Using the model from (e), obtain 95 % confidence intervals for the coefficient(s)
confint(fit)
## 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(fit)
If we look closely at the residual plot, there are three outliers - 69,377 and 51.
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 regression of Y onto X is
\(\hat{ \beta} = \Sigma_ix_iy_i/\Sigma_jx_j^2\)
The coefficient estimate for the regression of X onto Y is
\(\hat{ \beta}^\prime = \Sigma_ix_iy_i/\Sigma_jy_j^2\)
The coefficients are same if : \(\Sigma_jx_j^2 = \Sigma_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(123)
x <- 1:100
sum(x^2)
## [1] 338350
y <- 3 * x + rnorm(100, sd = 0.1)
sum(y^2)
## [1] 3045550
fit.Y <- lm(y ~ x + 0)
fit.X <- lm(x ~ y + 0)
summary(fit.Y)
##
## Call:
## lm(formula = y ~ x + 0)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.245085 -0.056817 -0.003652 0.062934 0.208238
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## x 3.0001968 0.0001565 19176 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.09101 on 99 degrees of freedom
## Multiple R-squared: 1, Adjusted R-squared: 1
## F-statistic: 3.677e+08 on 1 and 99 DF, p-value: < 2.2e-16
summary(fit.X)
##
## Call:
## lm(formula = x ~ y + 0)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.069396 -0.020968 0.001234 0.018939 0.081709
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## y 3.333e-01 1.738e-05 19176 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.03033 on 99 degrees of freedom
## Multiple R-squared: 1, Adjusted R-squared: 1
## F-statistic: 3.677e+08 on 1 and 99 DF, p-value: < 2.2e-16
In the above example, the coefficient estimate for the regression of X onto Y is not same as the coefficient estimate for the regression of Y onto X.
(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
sum(x^2)
## [1] 338350
y <- 100:1
sum(y^2)
## [1] 338350
fit.Y <- lm(y ~ x + 0)
fit.X <- lm(x ~ y + 0)
summary(fit.Y)
##
## Call:
## lm(formula = y ~ x + 0)
##
## Residuals:
## Min 1Q Median 3Q Max
## -49.75 -12.44 24.87 62.18 99.49
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## x 0.5075 0.0866 5.86 6.09e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 50.37 on 99 degrees of freedom
## Multiple R-squared: 0.2575, Adjusted R-squared: 0.25
## F-statistic: 34.34 on 1 and 99 DF, p-value: 6.094e-08
summary(fit.X)
##
## Call:
## lm(formula = x ~ y + 0)
##
## Residuals:
## Min 1Q Median 3Q Max
## -49.75 -12.44 24.87 62.18 99.49
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## y 0.5075 0.0866 5.86 6.09e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 50.37 on 99 degrees of freedom
## Multiple R-squared: 0.2575, Adjusted R-squared: 0.25
## F-statistic: 34.34 on 1 and 99 DF, p-value: 6.094e-08
In the above example,the coefficient estimate for the regression of X onto Y is the same as the coefficient estimate for the regression of Y onto X.