Carefully explain the differences between the KNN classifier and KNN regression methods.
KNN classifier: identifies nearest K points of the input data and outputs a class label via majority vote of neighbors.
KNN regression: identifies nearest K points of the input data and outputs a continuous value via average of neighbors.
This question involves the use of multiple linear regression on the Auto data set.
pairs(Auto, cex = 0.2)
auto_noname <- subset(Auto, select = -name)
cor(auto_noname)
## 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
auto_reg <- lm(mpg ~ ., data = auto_noname)
summary(auto_reg)
##
## Call:
## lm(formula = mpg ~ ., data = auto_noname)
##
## 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
Is there a relationship between the predictors and the response? Yes, there is a relationship
Which predictors appear to have a statistically significant relationship to the response? displacement(+), weight(-), year(+), and origin(+)
What does the coefficient for the year variable suggest? Coefficient for year is 0.751. It suggests mpg increases on average by 0.751 for every year increase
par(mfrow = c(2,2))
plot(auto_reg, cex = 0.5)
According to the diagnostic plots, there may be some non-linearity with the data. There could be some outliers and observation 14 has high leverage.
summary(lm(mpg~.+year:cylinders, data = auto_noname))
##
## Call:
## lm(formula = mpg ~ . + year:cylinders, data = auto_noname)
##
## Residuals:
## Min 1Q Median 3Q Max
## -8.3346 -2.0533 -0.1177 1.6257 12.0879
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -9.290e+01 1.256e+01 -7.399 8.75e-13 ***
## cylinders 1.469e+01 2.378e+00 6.178 1.66e-09 ***
## displacement 1.175e-02 7.259e-03 1.619 0.1063
## horsepower -2.765e-02 1.322e-02 -2.092 0.0371 *
## weight -6.242e-03 6.212e-04 -10.049 < 2e-16 ***
## acceleration 1.213e-01 9.423e-02 1.287 0.1988
## year 1.734e+00 1.601e-01 10.826 < 2e-16 ***
## origin 1.375e+00 2.647e-01 5.197 3.30e-07 ***
## cylinders:year -1.949e-01 3.027e-02 -6.439 3.60e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.165 on 383 degrees of freedom
## Multiple R-squared: 0.8389, Adjusted R-squared: 0.8356
## F-statistic: 249.3 on 8 and 383 DF, p-value: < 2.2e-16
summary(lm(mpg~.+acceleration:horsepower, data = auto_noname))
##
## Call:
## lm(formula = mpg ~ . + acceleration:horsepower, data = auto_noname)
##
## Residuals:
## Min 1Q Median 3Q Max
## -9.0329 -1.8177 -0.1183 1.7247 12.4870
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -32.499820 4.923380 -6.601 1.36e-10 ***
## cylinders 0.083489 0.316913 0.263 0.792350
## displacement -0.007649 0.008161 -0.937 0.349244
## horsepower 0.127188 0.024746 5.140 4.40e-07 ***
## weight -0.003976 0.000716 -5.552 5.27e-08 ***
## acceleration 0.983282 0.161513 6.088 2.78e-09 ***
## year 0.755919 0.048179 15.690 < 2e-16 ***
## origin 1.035733 0.268962 3.851 0.000138 ***
## horsepower:acceleration -0.012139 0.001772 -6.851 2.93e-11 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.145 on 383 degrees of freedom
## Multiple R-squared: 0.841, Adjusted R-squared: 0.8376
## F-statistic: 253.2 on 8 and 383 DF, p-value: < 2.2e-16
There are interaction effects between year:cylinders and also between acceleration:horsepower. (statistically significant p-values)
par(mfrow = c(2, 2))
plot(Auto$weight, Auto$mpg, cex = 0.5)
plot(sqrt(Auto$weight), Auto$mpg, cex = 0.5)
plot(Auto$weight ^ 2, Auto$mpg, cex = 0.5)
plot(log(Auto$weight), Auto$mpg, cex = 0.5)
Out of the three other transformations, log(weight) appears to have the most linear relationship with mpg. It will be fitted to our model below:
auto_noname2 <- auto_noname
auto_noname2$weight <- log(auto_noname2$weight)
auto_reg_log <- lm(mpg ~ ., data = auto_noname)
summary(auto_reg_log)
##
## Call:
## lm(formula = mpg ~ ., data = auto_noname)
##
## 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
Unlike before, the significant variables are: displacement(+), weight(-), year(+), and origin(+). However, the R^2 value was actually lower than the initial fit. (0.841 > 0.8215)
This question should be answered using the Carseats data set.
carseat_reg <- lm(Sales~ Price + Urban + US, data=Carseats)
summary(carseat_reg)
##
## 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
Price(-): Sales will fall by 54.46 for every dollar increase in price. UrbanYes: Not considered statistically significant. USYes: If the store is in the US, Sales will increase by 1,200.
Sales = 13.04 - 0.54(Price) - 0.02(Urban:Yes = 1, No = 0) + 1.20(US:Yes = 1, No = 0)
Price and US. Urban was not statistically significant.
carseat_reg_sigonly <- lm(Sales~ Price + US, data=Carseats)
summary(carseat_reg_sigonly)
##
## 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
All variables from model (e) were considered significant. However, the R^2 values from both models were similar (0.2393) suggesting they have similar goodness-of-fit.
confint(carseat_reg_sigonly, level = 0.95)
## 2.5 % 97.5 %
## (Intercept) 11.79032020 14.27126531
## Price -0.06475984 -0.04419543
## USYes 0.69151957 1.70776632
par(mfrow = c(2, 2))
plot(carseat_reg_sigonly, cex = 0.5)
There is evidence of some outliers.
In this problem we will investigate the t-statistic for the null hypothesis H0 : β = 0 in simple linear regression without an intercept. To begin, we generate a predictor x and a response y as follows.
set.seed(1)
x <- rnorm(100)
y <- 2 * x + rnorm(100)
lm.yx = lm(y ~ x + 0)
summary(lm.yx)
##
## Call:
## lm(formula = y ~ x + 0)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.9154 -0.6472 -0.1771 0.5056 2.3109
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## x 1.9939 0.1065 18.73 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.9586 on 99 degrees of freedom
## Multiple R-squared: 0.7798, Adjusted R-squared: 0.7776
## F-statistic: 350.7 on 1 and 99 DF, p-value: < 2.2e-16
Variable x is statistically significant. For every unit increase in x, y is expected to increase by 1.99 on average. The standard error for x is 0.11, t-stat: 18.73, and p-value: <2e-16.
lm.xy = lm(x ~ y + 0)
summary(lm.xy)
##
## Call:
## lm(formula = x ~ y + 0)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.8699 -0.2368 0.1030 0.2858 0.8938
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## y 0.39111 0.02089 18.73 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4246 on 99 degrees of freedom
## Multiple R-squared: 0.7798, Adjusted R-squared: 0.7776
## F-statistic: 350.7 on 1 and 99 DF, p-value: < 2.2e-16
Variable y is statistically significant. For every unit increase in y, x is expected to increase by 0.39 on average. The standard error for y is 0.02, t-stat: 18.73, and p-value: <2e-16.
The t-stats and p-values from both models are the same. The axis has changed, causing inversion.
Show algebraically, and confirm numerically in R, that the t-statistic can be written as: [equation in book]
n = length(x)
t = sqrt(n - 1) * (x %*% y)/sqrt(sum(x^2) * sum(y^2) - (x %*% y)^2)
as.numeric(t)
## [1] 18.72593
Replacing x with y, or y with x, will give the same t-statistic.
coef(summary(lm(y ~ x)))
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.03769261 0.09698729 -0.3886346 6.983896e-01
## x 1.99893961 0.10772703 18.5555993 7.723851e-34
coef(summary(lm(x ~ y)))
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.03880394 0.04266144 0.9095787 3.652764e-01
## y 0.38942451 0.02098690 18.5555993 7.723851e-34
Same t-stat values are observed in both cases.