The KNN regression method is used to find the neighborhood of X0, then estimating f(x0) and averaging the responses in the neighborhood that was given. While the KNN classifier is used by identifying the neighborhood of X0 and estimating the conditional probability for the class j as a fraction of the neighborhood whose values are equal to j.
(a) Produce a scatterplot matrix which includes all of the variables in the data set.
Auto <- read.csv(file="C:/Users/fds35/OneDrive/Documents/Auto.csv", header=TRUE, sep=",")
plot(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.
#apparently horsepower is being read in as a factor and I went and checked the dataset and saw there are missing values noted as "?".
sapply(Auto, class)
## mpg cylinders displacement horsepower weight
## "numeric" "integer" "numeric" "integer" "integer"
## acceleration year origin name
## "numeric" "integer" "integer" "factor"
cor(Auto[sapply(Auto, function(x) !is.factor(x))])
## mpg cylinders displacement horsepower weight
## mpg 1.0000000 -0.7762599 -0.8044430 NA -0.8317389
## cylinders -0.7762599 1.0000000 0.9509199 NA 0.8970169
## displacement -0.8044430 0.9509199 1.0000000 NA 0.9331044
## horsepower NA NA NA 1 NA
## weight -0.8317389 0.8970169 0.9331044 NA 1.0000000
## acceleration 0.4222974 -0.5040606 -0.5441618 NA -0.4195023
## year 0.5814695 -0.3467172 -0.3698041 NA -0.3079004
## origin 0.5636979 -0.5649716 -0.6106643 NA -0.5812652
## acceleration year origin
## mpg 0.4222974 0.5814695 0.5636979
## cylinders -0.5040606 -0.3467172 -0.5649716
## displacement -0.5441618 -0.3698041 -0.6106643
## horsepower NA NA NA
## weight -0.4195023 -0.3079004 -0.5812652
## acceleration 1.0000000 0.2829009 0.2100836
## year 0.2829009 1.0000000 0.1843141
## origin 0.2100836 0.1843141 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:
x <- lm(mpg ~ cylinders + displacement + horsepower + weight + year + origin,
data = Auto)
summary(x)
##
## Call:
## lm(formula = mpg ~ cylinders + displacement + horsepower + weight +
## year + origin, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -9.7604 -2.1791 -0.1535 1.8524 13.1209
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.556e+01 4.175e+00 -3.728 0.000222 ***
## cylinders -5.067e-01 3.227e-01 -1.570 0.117236
## displacement 1.927e-02 7.472e-03 2.579 0.010287 *
## horsepower -2.389e-02 1.084e-02 -2.205 0.028031 *
## weight -6.218e-03 5.714e-04 -10.883 < 2e-16 ***
## year 7.475e-01 5.079e-02 14.717 < 2e-16 ***
## origin 1.428e+00 2.780e-01 5.138 4.43e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.326 on 385 degrees of freedom
## (5 observations deleted due to missingness)
## Multiple R-squared: 0.8212, Adjusted R-squared: 0.8184
## F-statistic: 294.6 on 6 and 385 DF, p-value: < 2.2e-16
Is there a relationship between the predictors and the response?
Which predictors appear to have a statistically significant relationship to the response?
What does the coefficient for the year variable suggest?
(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?
plot(x)
(e) Use the * and : symbols to fit linear regression models with interaction effects. Do any interactions appear to be statistically significant?
y <- lm(mpg ~ cylinders + displacement + horsepower * weight + year + origin,
data = Auto)
summary(y)
##
## Call:
## lm(formula = mpg ~ cylinders + displacement + horsepower * weight +
## year + origin, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -8.4229 -1.7769 -0.1437 1.4934 11.9546
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 7.321e-01 3.990e+00 0.183 0.854504
## cylinders -2.329e-02 2.881e-01 -0.081 0.935601
## displacement 6.873e-03 6.689e-03 1.027 0.304872
## horsepower -2.200e-01 2.087e-02 -10.542 < 2e-16 ***
## weight -1.141e-02 7.034e-04 -16.219 < 2e-16 ***
## year 7.727e-01 4.483e-02 17.236 < 2e-16 ***
## origin 8.425e-01 2.512e-01 3.354 0.000876 ***
## horsepower:weight 5.432e-05 5.140e-06 10.568 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.931 on 384 degrees of freedom
## (5 observations deleted due to missingness)
## Multiple R-squared: 0.8615, Adjusted R-squared: 0.8589
## F-statistic: 341.1 on 7 and 384 DF, p-value: < 2.2e-16
(f) Try a few different transformations of the variables, such as log(X), ???X, X2. Comment on your findings.
#plot(log(Auto$horsepower), Auto$mpg)
#plot(sqrt(Auto$horsepower), Auto$mpg)
#plot((Auto$horsepower)^2, Auto$mpg)
Adding the transformations
library(ISLR)
## Warning: package 'ISLR' was built under R version 3.5.2
##
## Attaching package: 'ISLR'
## The following object is masked _by_ '.GlobalEnv':
##
## Auto
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 I increase tge price, my sales go down $0.05. Sales inside of the US are $1.20 higher than sales outside of 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 = -0.054459xPrice -0.021916xUrbanYes + 1.200573 * USYes
(d) For which of the predictors can you reject the null hypothesis H0 : ??j = 0? For the variables Price and US we will reject the null hypothesis
(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.
m <- lm(Sales ~ Price + US)
summary(m)
##
## 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? The R^2 are essentially the same for both models but the adjusted R^2 (e) is higher than (a). About 23.93% of the data is explained with these models.
(g) Using the model from (e), obtain 95 % confidence intervals for the coefficient(s).
confint(m)
## 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)?
plot(m)
Yes there are high leverage values present as noted in the plot Residuals vs. Leverage there are points that are 2 over from 0. ##Q12: 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? If and only if the expected value of x^2, is equal to y^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<- 1:100
sum(x^2)
## [1] 338350
y<- rnorm(100, sd=0.2)
sum(y^2)
## [1] 3.242204
y1<-lm(y~x+0)
x1<- lm(x~y +0)
summary(y1)
##
## Call:
## lm(formula = y ~ x + 0)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.44718 -0.12512 0.00885 0.11701 0.46185
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## x 0.0003028 0.0003096 0.978 0.33
##
## Residual standard error: 0.1801 on 99 degrees of freedom
## Multiple R-squared: 0.00957, Adjusted R-squared: -0.0004347
## F-statistic: 0.9566 on 1 and 99 DF, p-value: 0.3304
summary(x1)
##
## Call:
## lm(formula = x ~ y + 0)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.083 27.108 47.213 78.097 106.740
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## y 31.60 32.31 0.978 0.33
##
## Residual standard error: 58.18 on 99 degrees of freedom
## Multiple R-squared: 0.00957, Adjusted R-squared: -0.0004347
## F-statistic: 0.9566 on 1 and 99 DF, p-value: 0.3304
(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
y1<-lm(y~x+0)
x1<- lm(x~y +0)
summary(y1)
##
## 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(x1)
##
## 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