(2.0) Carefully explain the differences between the KNN classifier and KNN regression methods.
(2.0) Answer: KNN classifier attempts to estimate a new observation’s class target as the majority class target of the k nearest neighbors. KNN regression attempts the same but on a continuous target. KNN regression attempts to estimate a new observation’s numeric target as the average of the k nearest neighbor’s targets.
(9.0) This question involves the use of multiple linear regression on the Auto data set.
(9.a) Produce a scatter-plot matrix which includes all of the variables in the data set.
(9.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..
kable(round(cor(Auto[,-9]), 2))
mpg | cylinders | displacement | horsepower | weight | acceleration | year | origin | |
---|---|---|---|---|---|---|---|---|
mpg | 1.00 | -0.78 | -0.81 | -0.78 | -0.83 | 0.42 | 0.58 | 0.57 |
cylinders | -0.78 | 1.00 | 0.95 | 0.84 | 0.90 | -0.50 | -0.35 | -0.57 |
displacement | -0.81 | 0.95 | 1.00 | 0.90 | 0.93 | -0.54 | -0.37 | -0.61 |
horsepower | -0.78 | 0.84 | 0.90 | 1.00 | 0.86 | -0.69 | -0.42 | -0.46 |
weight | -0.83 | 0.90 | 0.93 | 0.86 | 1.00 | -0.42 | -0.31 | -0.59 |
acceleration | 0.42 | -0.50 | -0.54 | -0.69 | -0.42 | 1.00 | 0.29 | 0.21 |
year | 0.58 | -0.35 | -0.37 | -0.42 | -0.31 | 0.29 | 1.00 | 0.18 |
origin | 0.57 | -0.57 | -0.61 | -0.46 | -0.59 | 0.21 | 0.18 | 1.00 |
(9.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.
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
(9.c.i) Is there a relationship between the predictors and the response? (9.c.i) Answer: The F-statistic p-value is < 2.2e-16 => reject the F-statistic null hypothesis => at least one beta is not zero, model is useful. This is confirmed by a few term t-test p-values < 0.05 => reject the t-test null hypothesis => terms have a relationship.
(9.c.ii) Which predictors appear to have a statistically significant relationship to the response? (9.c.ii) Answer: The predictors that appear to have a statistically significant relationship to the response are: displacement, weight, year, and origin.
(9.c.iii) What does the coefficient for the year variable suggest?
(9.c.ii) Answer: The coefficient for the year variable suggest that with a 1 unit increase in model year, there is expected to be an increase of 0.750773 in miles per gallon, when all other predictors are fixed.
(9.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); abline(h = c(2,-2), col="red")
(9.d) Answer: Looking at the residuals plot (top left) there seems to be an increasing spread pattern suggesting some non-linearity behavior is not being captured. The standardized residual vs leverage plot (lower right) does show a few outliers outside the (-2,2) boundary along with point 14 being high leverage.
(9.e) Use the * and : symbols to fit linear regression models with interaction effects. Do any interactions appear to be statistically significant?
##
## Call:
## lm(formula = mpg ~ cylinders * displacement + cylinders:horsepower +
## cylinders:weight + cylinders:acceleration + cylinders:year +
## cylinders:origin + displacement * horsepower + displacement:weight +
## displacement:acceleration + displacement:year + displacement:origin +
## horsepower * weight + horsepower:acceleration + horsepower:year +
## horsepower:origin + weight * acceleration + weight:year +
## weight:origin + acceleration * year + acceleration:origin -
## name, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -7.5050 -1.3936 -0.0591 1.2698 11.4043
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -2.651e+01 4.997e+01 -0.530 0.5961
## cylinders 5.788e+00 8.317e+00 0.696 0.4869
## displacement -3.741e-01 1.843e-01 -2.030 0.0431 *
## horsepower 6.070e-01 3.462e-01 1.753 0.0804 .
## weight 5.763e-03 1.781e-02 0.324 0.7465
## acceleration -4.071e+00 2.134e+00 -1.908 0.0572 .
## year 1.181e+00 5.860e-01 2.015 0.0447 *
## cylinders:displacement -5.663e-03 6.498e-03 -0.872 0.3840
## cylinders:horsepower 1.836e-02 2.435e-02 0.754 0.4514
## cylinders:weight 1.062e-04 8.893e-04 0.119 0.9050
## cylinders:acceleration 3.083e-01 1.668e-01 1.849 0.0653 .
## cylinders:year -1.501e-01 9.767e-02 -1.537 0.1252
## cylinders:origin 3.275e-02 4.831e-01 0.068 0.9460
## displacement:horsepower -2.300e-04 2.880e-04 -0.798 0.4251
## displacement:weight 3.215e-05 1.458e-05 2.205 0.0281 *
## displacement:acceleration -5.056e-03 3.257e-03 -1.552 0.1214
## displacement:year 4.823e-03 2.324e-03 2.076 0.0386 *
## displacement:origin 2.976e-02 1.956e-02 1.522 0.1290
## horsepower:weight -3.170e-05 2.933e-05 -1.081 0.2806
## horsepower:acceleration -7.017e-03 3.761e-03 -1.866 0.0629 .
## horsepower:year -5.859e-03 3.916e-03 -1.496 0.1355
## horsepower:origin -5.365e-02 2.432e-02 -2.206 0.0280 *
## weight:acceleration 1.602e-04 2.305e-04 0.695 0.4877
## weight:year -2.293e-04 2.145e-04 -1.069 0.2857
## weight:origin 2.903e-04 1.536e-03 0.189 0.8502
## acceleration:year 4.413e-02 2.567e-02 1.719 0.0865 .
## acceleration:origin 5.244e-02 9.533e-02 0.550 0.5826
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.729 on 365 degrees of freedom
## Multiple R-squared: 0.8859, Adjusted R-squared: 0.8778
## F-statistic: 109 on 26 and 365 DF, p-value: < 2.2e-16
(9.e) Answer: The interactions that appear to be statistically significant are: horsepower:origin, displacement:weight, and displacement:year
(9.f) Try a few different transformations of the variables, such as log(X), √X, X2. Comment on your findings.
(9.f) Answer: the log, square root, or square transforms did not affect the patterns in the plots of mpg vs acceleration very much. The log transform of displacement, horsepower, and weight provided the most linear looking patterns
(10.0) This question should be answered using the Carseats data set.
(10.a) Fit a multiple regression model to predict Sales using Price, Urban, and US.
carseats.fit <- lm(Sales ~ Price + Urban + US, data = Carseats)
(10.b) Provide an interpretation of each coefficient in the model. Be careful—some of the variables in the model are qualitative!
##
## 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
(10.b) Answer: With 1 unit increase in the Price the company charges for car seats at each site, on average unit Sales at each location is expected to decrease by 54.459 when all other predictors are fixed. On average US located store unit Sales at each location is 1200.573 higher than non US located stores, when all other predictors are fixed.
(10.c) Write out the model in equation form, being careful to handle the qualitative variables properly.
(10.c) Answer: The model may be written as
\(\hat{Sales}= {13.0434689 + (−0.0544588)(Price) + (−0.0219162)(Urban) + (1.2005727)(US) + ε,}\)
where if
Urban = 1 indicates if the store is in an urban location
Urban = 0 indicates if the store is in an rural location
US = 1 indicates the store is in the US
US = 0 indicates the store is not in the US
(10.d) For which of the predictors can you reject the null hypothesis H0 : βj = 0?
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 13.04346894 0.651012245 20.03567373 3.626602e-62
## Price -0.05445885 0.005241855 -10.38923205 1.609917e-22
## UrbanYes -0.02191615 0.271650277 -0.08067781 9.357389e-01
## USYes 1.20057270 0.259041508 4.63467306 4.860245e-06
(10.d) Answer: the predictors for which you can reject the t-test null hypothesis (H0 : βj = 0) are: Price and US
(10.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.fit2 <- lm(Sales ~ Price + US, data = Carseats); #summary(carseats.fit2)
(10.f) How well do the models in (a) and (e) fit the data?
summary(carseats.fit)$adj.r.squared
## [1] 0.2335123
summary(carseats.fit2)$adj.r.squared
## [1] 0.2354305
(10.f) Answer: The adjusted R^2 for the smaller model explains 23.54305% of the variability in the data while the first model explains 23.35123%.
(10.g) Using the model from (e), obtain 95 % confidence intervals for the coefficient(s).
round(confint(carseats.fit2), 2)
## 2.5 % 97.5 %
## (Intercept) 11.79 14.27
## Price -0.06 -0.04
## USYes 0.69 1.71
(10.h) Is there evidence of outliers or high leverage observations in the model from (e)?
(10.h) Answer: Yes, the plot above show evidence of outliers and a high leverage observation in the model from (e).
(12.0) This problem involves simple linear regression without an intercept.
(12.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?
(12.a) Answer: The coefficient estimate for the regression of Y onto X is
\(\hat{\beta}= \frac{\sum_{i=1}^{n}x_{i}y_{i}} {\sum_{k=1}^{n}x_{k}^2}\)
The coefficient estimate for the regression of X onto Y is
\(\hat{\beta}^T= \frac{\sum_{i=1}^{n}y_{i}x_{i}} {\sum_{k=1}^{n}y_{k}^2}\)
The coefficients are the same iff
\({\sum_{k=1}^{n}x_{k}^2} = {\sum_{k=1}^{n}y_{k}^2}\)
(12.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 <- 1:100; sum(x^2)
## [1] 338350
y <- 101:200; sum(y^2)
## [1] 2348350
fit.Y <- lm(y ~ x + 0); summary(fit.Y)$coefficient
## Estimate Std. Error t value Pr(>|t|)
## x 2.492537 0.08574403 29.06951 2.809136e-50
fit.X <- lm(x ~ y + 0); summary(fit.X)$coefficient
## Estimate Std. Error t value Pr(>|t|)
## y 0.3591245 0.01235399 29.06951 2.809136e-50
(12.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.
sum(x^2)
## [1] 338350
y2 <- 100:1; sum(y2^2)
## [1] 338350
fit.Y2 <- lm(y2 ~ x + 0); summary(fit.Y2)$coefficient
## Estimate Std. Error t value Pr(>|t|)
## x 0.5074627 0.08660147 5.859747 6.094418e-08
fit.X <- lm(x ~ y2 + 0); summary(fit.X)$coefficient
## Estimate Std. Error t value Pr(>|t|)
## y2 0.5074627 0.08660147 5.859747 6.094418e-08