Conceptual

Question 2

Carefully explain the differences between the KNN classifier and KNN regression methods.


KNN classifier are used to label unknown points when the values are categorical. To use KNN classifiers, the K value needs to be an odd number. This is so that there ill always be a majority of a category when labeling a point on a graph. For example, if you are trying to figure out if a point is red or blue, you would set the k to an odd number (5 for this example). The KNN will then find the 5 closest points and label the unknown point as the majority. Continuing the example, if 3 of the 5 points near the unknown is blue then the unknown point will be labeled as blue.

KNN regression methods are used to label unknown points when the values are continuous. To use KNN regression methods, the K value can be set to any number (odd or even). The reason for the difference in what K needs to be set to is because, KNN regressions label the unknown value based on the average of the points around it. For example, if you are trying to determine the temperature of an unknown point, you would first set your K value. To continue this example, K will be set to 2. For the unknown point the model will look at the two nearest points and find the average which will then represent the unknown point, i.e. point 1: 97 and point 2: 80, the average is (97 + 80) / 2 = 88.5. Therefore the model will label the unknown point as 88.5.

Applied

Question 9


This question involves the use of multiple linear regression on the Auto data set.

# called in data set
auto <- read.csv("Auto.csv", header = TRUE)
# removed any rows where horsepower was ? 
clean_auto <- auto[auto$horsepower != "?",]

# converting horsepower to a numeric value not that all ? have been removed
clean_auto$horsepower <- as.numeric(clean_auto$horsepower)
  1. Produce a scatterplot matrix which includes all of the variables in the data set.
plot(clean_auto)

b. Computer the matrix of correlations between the variables using the function cor(). You will need to exclude the name variable, which is qualitative.

# removing name column
clean_auto_wout_name <- clean_auto[,-9]
cor(clean_auto_wout_name)
##                     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
  1. 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_lm <- lm(mpg ~ cylinders + displacement + horsepower + weight + acceleration + year + origin, data = clean_auto)
summary(auto_lm)
## 
## Call:
## lm(formula = mpg ~ cylinders + displacement + horsepower + weight + 
##     acceleration + year + origin, data = clean_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? Yes, there is a relationship between the predictors and the response. ii. Which predictors appear to have a statistically significant relationship to the response? The predictors that appear to have a statistically significant relationship are displacement, weight, year, and origin. iii. What does the coefficient for the year variable suggest? The coefficient for the year suggests that when the year increases by one unit, the mpg for the car will increase by about 0.75. 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_lm)

Based on the residual plots there are some visible problems. Based on the residuals vs fitted plot, the red line is not a straight line which means that there is an issue on non-linearity. Another issue is the residuals are not normally distributed based on the Q-Q residuals plots. Lastly the scale-location plots show heteroscedasticity. In the Q-Q plot, shows that points 387, 394, and 323 are outliers. In the Residuals vs Levarage plots, we can see unusually large outliers, some of these points are 387, and 394. Lastly, we can see from the graphs that point 333 is a high leverage point. e. Use the * and : symbols to fit linear regression models with interaction effects. Do any interactions appear to be statistically significant?

auto_lm_interaction <- lm(mpg ~ cylinders * displacement + horsepower + weight + acceleration + year + origin, data = clean_auto)
auto_lm_interaction2 <- lm(mpg ~ cylinders:horsepower + displacement + weight + acceleration + year + origin, data = clean_auto)
summary(auto_lm_interaction)
## 
## Call:
## lm(formula = mpg ~ cylinders * displacement + horsepower + weight + 
##     acceleration + year + origin, data = clean_auto)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -11.6081  -1.7833  -0.0465   1.6821  12.2617 
## 
## Coefficients:
##                          Estimate Std. Error t value Pr(>|t|)    
## (Intercept)            -2.7096590  4.6858582  -0.578 0.563426    
## cylinders              -2.6962123  0.4094916  -6.584 1.51e-10 ***
## displacement           -0.0774797  0.0141535  -5.474 7.96e-08 ***
## horsepower             -0.0476026  0.0133736  -3.559 0.000418 ***
## weight                 -0.0052339  0.0006253  -8.370 1.10e-15 ***
## acceleration            0.0597997  0.0918038   0.651 0.515188    
## year                    0.7594500  0.0473354  16.044  < 2e-16 ***
## origin                  0.7087399  0.2736917   2.590 0.009976 ** 
## cylinders:displacement  0.0136081  0.0017209   7.907 2.84e-14 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.089 on 383 degrees of freedom
## Multiple R-squared:  0.8465, Adjusted R-squared:  0.8433 
## F-statistic: 264.1 on 8 and 383 DF,  p-value: < 2.2e-16
summary(auto_lm_interaction2)
## 
## Call:
## lm(formula = mpg ~ cylinders:horsepower + displacement + weight + 
##     acceleration + year + origin, data = clean_auto)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -9.1247 -2.0841  0.0128  1.8373 12.8884 
## 
## Coefficients:
##                        Estimate Std. Error t value Pr(>|t|)    
## (Intercept)          -2.210e+01  4.201e+00  -5.261 2.38e-07 ***
## displacement          2.167e-03  6.980e-03   0.310  0.75636    
## weight               -7.223e-03  6.021e-04 -11.997  < 2e-16 ***
## acceleration          2.306e-01  8.790e-02   2.623  0.00905 ** 
## year                  7.809e-01  5.043e-02  15.486  < 2e-16 ***
## origin                1.161e+00  2.819e-01   4.118 4.68e-05 ***
## cylinders:horsepower  3.004e-03  1.585e-03   1.896  0.05875 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.323 on 385 degrees of freedom
## Multiple R-squared:  0.8215, Adjusted R-squared:  0.8188 
## F-statistic: 295.4 on 6 and 385 DF,  p-value: < 2.2e-16

Based on the two interactions I used, the first one cylinders * displacement was not statistically significant in modeling the mpg for a car. On the other hand, my interaction of cylinders:horsepower, various combinations of cylinders and horsepower are showing as slightly significant to the model. f. Try a few different transformations of the variables such as \(log(X)\), \(\sqrt{X}\), \(X^2\)

log_auto <- lm(mpg ~ cylinders + log(displacement) + horsepower + weight + acceleration + year + origin, data = clean_auto)
sqrt_auto <- lm(mpg ~ cylinders + displacement + horsepower + sqrt(weight) + acceleration + year + origin, data = clean_auto)
square_auto <- lm(mpg ~ cylinders + displacement + horsepower + weight + I(acceleration^2) + year + origin, data = clean_auto)
summary(log_auto)
## 
## Call:
## lm(formula = mpg ~ cylinders + log(displacement) + horsepower + 
##     weight + acceleration + year + origin, data = clean_auto)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -10.6594  -1.8712  -0.0741   1.6427  12.8462 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        2.3922996  7.1102490   0.336 0.736709    
## cylinders          0.8052759  0.3081112   2.614 0.009312 ** 
## log(displacement) -5.2475829  1.3910486  -3.772 0.000187 ***
## horsepower        -0.0048428  0.0130659  -0.371 0.711106    
## weight            -0.0044886  0.0006912  -6.494 2.58e-10 ***
## acceleration      -0.0047404  0.0986602  -0.048 0.961703    
## year               0.7437614  0.0503990  14.757  < 2e-16 ***
## origin             0.6282457  0.3011778   2.086 0.037642 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.297 on 384 degrees of freedom
## Multiple R-squared:  0.8247, Adjusted R-squared:  0.8215 
## F-statistic: 258.1 on 7 and 384 DF,  p-value: < 2.2e-16
summary(sqrt_auto)
## 
## Call:
## lm(formula = mpg ~ cylinders + displacement + horsepower + sqrt(weight) + 
##     acceleration + year + origin, data = clean_auto)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -9.4018 -2.0112  0.0246  1.7565 12.8943 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   2.840893   4.486253   0.633  0.52695    
## cylinders    -0.430040   0.310000  -1.387  0.16618    
## displacement  0.021846   0.007134   3.062  0.00235 ** 
## horsepower   -0.010706   0.013111  -0.817  0.41469    
## sqrt(weight) -0.794322   0.066906 -11.872  < 2e-16 ***
## acceleration  0.131710   0.094051   1.400  0.16220    
## year          0.773764   0.049030  15.781  < 2e-16 ***
## origin        1.210091   0.268519   4.507 8.76e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.191 on 384 degrees of freedom
## Multiple R-squared:  0.8359, Adjusted R-squared:  0.8329 
## F-statistic: 279.4 on 7 and 384 DF,  p-value: < 2.2e-16
summary(square_auto)
## 
## Call:
## lm(formula = mpg ~ cylinders + displacement + horsepower + weight + 
##     I(acceleration^2) + year + origin, data = clean_auto)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -9.456 -2.148 -0.060  1.899 13.017 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       -1.746e+01  4.336e+00  -4.027 6.80e-05 ***
## cylinders         -4.713e-01  3.229e-01  -1.460  0.14518    
## displacement       1.985e-02  7.467e-03   2.659  0.00817 ** 
## horsepower        -1.162e-02  1.330e-02  -0.873  0.38295    
## weight            -6.647e-03  6.312e-04 -10.531  < 2e-16 ***
## I(acceleration^2)  4.481e-03  2.828e-03   1.585  0.11383    
## year               7.539e-01  5.085e-02  14.825  < 2e-16 ***
## origin             1.419e+00  2.775e-01   5.112 5.05e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.32 on 384 degrees of freedom
## Multiple R-squared:  0.8223, Adjusted R-squared:  0.8191 
## F-statistic: 253.9 on 7 and 384 DF,  p-value: < 2.2e-16

Question 10


This question should be answered using the Carseats data set.

library(ISLR2)
carsets_data <- Carseats
  1. Fit a multiple regression model to predict Sales using Price, Urban, and US.
carset_lm <- lm(Sales ~ Price + Urban + US, data = carsets_data)
summary(carset_lm)
## 
## Call:
## lm(formula = Sales ~ Price + Urban + US, data = carsets_data)
## 
## 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
  1. Provide an interpretation of each coefficient in the model. Be careful some of the variables in the model are qualitative! When price increase by one unit, sales decrease by about 0.05. Whan a car seat is being sold in an urban store, sales decrease by about 0.02 compared to a car seat being sold in a rural store. When a car seat is being sold in a US based stored, sales increase by about 1.20 compared to a car seat not being sold in the US.
  2. Write out the model in equation form, being careful to handle the qualitative variables properly. Equation: \(f\)(carseat) = 13.04 + -0.05\(Price\) + -0.02\(UrbanYes\) + 1.20\(USYes\)
  3. For which of the predictors can you reject the null hypothesis \(H_0 : β_j = 0\)? The null hypothesis \(H_0 : β_j = 0\) can be rejected for predictors: Price and US: Yes.
  4. 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.
reduced_carseat_lm <- lm(Sales ~ Price + US, data = carsets_data)
summary(reduced_carseat_lm)
## 
## Call:
## lm(formula = Sales ~ Price + US, data = carsets_data)
## 
## 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? The initial model (a) has a significant p-value, has an adjusted \(R^2\) value of 0.2335, and the model explains about 23.93% of the car seat data. However in the reduced model (e), it also has a significant p-value, has an adjusted \(R^2\) value of 0.2354, but also explains about 23.93% of the car seat data. The biggest difference between the two is that coefficients for Price and US:Yes increase in the reduced model but the US:Yes p-value decreases.
  2. Using the model from (e), obtain 95 % confidence intervals for the coefficient(s).
carseat_confidence <- confint(reduced_carseat_lm)
carseat_confidence
##                   2.5 %      97.5 %
## (Intercept) 11.79032020 14.27126531
## Price       -0.06475984 -0.04419543
## USYes        0.69151957  1.70776632
  1. Is there evidence of outliers or high leverage observations in the model from (e)?
par(mfrow = c(2, 2))
plot(reduced_carseat_lm)

Based on the plots made from model e, the Scale-Location graph shows that points 69, 51, and 377 are outliers. While the Residuals vs Leverage plot, shows that points 26 and 50 are high leverage.

Question 12


This problem involves simple linear regression without an intercept.

  1. Recall that the coefficient estimate \(\hat{b}\) 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 only circumstances for the coefficient estimate for the regression of X onto Y being the same as the coefficient estimates for the regression of Y onto X is when the sum of squares between the two are equal or when the sum of the products adds up to zero.
  2. 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(10)

x_100 <- rnorm(100)
y_100 <- x_100 + rnorm(100)

reg_x_on_y <- lm(x_100 ~ y_100 - 1)
reg_y_on_x <- lm(y_100 ~ x_100 - 1)
summary(reg_x_on_y)
## 
## Call:
## lm(formula = x_100 ~ y_100 - 1)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.64677 -0.51001 -0.07035  0.46075  1.53455 
## 
## Coefficients:
##       Estimate Std. Error t value Pr(>|t|)    
## y_100  0.48754    0.05245   9.296 3.76e-15 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.695 on 99 degrees of freedom
## Multiple R-squared:  0.4661, Adjusted R-squared:  0.4607 
## F-statistic: 86.41 on 1 and 99 DF,  p-value: 3.758e-15
summary(reg_y_on_x)
## 
## Call:
## lm(formula = y_100 ~ x_100 - 1)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.28638 -0.86611  0.06209  0.63881  2.07521 
## 
## Coefficients:
##       Estimate Std. Error t value Pr(>|t|)    
## x_100   0.9559     0.1028   9.296 3.76e-15 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9733 on 99 degrees of freedom
## Multiple R-squared:  0.4661, Adjusted R-squared:  0.4607 
## F-statistic: 86.41 on 1 and 99 DF,  p-value: 3.758e-15
  1. 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(10)

x_100_same <- rnorm(100)
y_100_same <- sample(x_100_same)

reg_x_on_y_same <- lm(x_100_same ~ y_100_same - 1)
reg_y_on_x_same <- lm(y_100_same ~ x_100_same - 1)
summary(reg_x_on_y_same)
## 
## Call:
## lm(formula = x_100_same ~ y_100_same - 1)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.1977 -0.8562 -0.1878  0.5946  2.2649 
## 
## Coefficients:
##            Estimate Std. Error t value Pr(>|t|)
## y_100_same  0.04229    0.10041   0.421    0.675
## 
## Residual standard error: 0.9503 on 99 degrees of freedom
## Multiple R-squared:  0.001789,   Adjusted R-squared:  -0.008294 
## F-statistic: 0.1774 on 1 and 99 DF,  p-value: 0.6745
summary(reg_y_on_x_same)
## 
## Call:
## lm(formula = y_100_same ~ x_100_same - 1)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.1841 -0.8098 -0.1710  0.6060  2.2286 
## 
## Coefficients:
##            Estimate Std. Error t value Pr(>|t|)
## x_100_same  0.04229    0.10041   0.421    0.675
## 
## Residual standard error: 0.9503 on 99 degrees of freedom
## Multiple R-squared:  0.001789,   Adjusted R-squared:  -0.008294 
## F-statistic: 0.1774 on 1 and 99 DF,  p-value: 0.6745