Carefully explain the differences between the KNN classifier
and KNN regression methods.
The KNN classifier method is used when the response variable is
qualitative or categorical. In this method we are trying to predict the
category of the input. The KNN regression method is used when the
response variable is quantitative. This method will be used to obtain a
numerical value.
This question involves the use of multiple linear regression on the Auto data set.
library(ISLR)
attach(Auto)
(a) Produce a scatterplot matrix which includes all of the variables in the data set.
Auto$horsepower <- as.factor(Auto$horsepower)
Auto$horsepower <- as.numeric(Auto$horsepower)
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.
Auto_new<-Auto
Auto_new$name=NULL
cor(Auto_new)
## mpg cylinders displacement horsepower weight
## mpg 1.0000000 -0.7776175 -0.8051269 -0.8291518 -0.8322442
## cylinders -0.7776175 1.0000000 0.9508233 0.8511437 0.8975273
## displacement -0.8051269 0.9508233 1.0000000 0.8874240 0.9329944
## horsepower -0.8291518 0.8511437 0.8874240 1.0000000 0.8824651
## weight -0.8322442 0.8975273 0.9329944 0.8824651 1.0000000
## acceleration 0.4233285 -0.5046834 -0.5438005 -0.6844978 -0.4168392
## year 0.5805410 -0.3456474 -0.3698552 -0.4038337 -0.3091199
## origin 0.5652088 -0.5689316 -0.6145351 -0.4875914 -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.6844978 -0.4038337 -0.4875914
## 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:
y1<-lm(mpg~ .-name, data=Auto)
summary(y1)
##
## Call:
## lm(formula = mpg ~ . - name, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -9.2278 -1.8702 -0.2198 1.7414 12.3558
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -9.460988 4.209932 -2.247 0.02519 *
## cylinders -0.342945 0.303611 -1.130 0.25937
## displacement 0.016687 0.006791 2.457 0.01444 *
## horsepower -0.154056 0.022341 -6.896 2.22e-11 ***
## weight -0.003910 0.000694 -5.634 3.42e-08 ***
## acceleration -0.314601 0.100236 -3.139 0.00183 **
## year 0.688889 0.048240 14.280 < 2e-16 ***
## origin 1.471640 0.256216 5.744 1.89e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.145 on 384 degrees of freedom
## Multiple R-squared: 0.8405, Adjusted R-squared: 0.8376
## F-statistic: 289.1 on 7 and 384 DF, p-value: < 2.2e-16
i. Is there a relationship between the predictors and the
response?
There is a relationship with at least one predictor as indicated by a p
value for the f statistic of less than 0.05.
ii. Which predictors appear to have a statistically
significant relationship to the response?
The predictors that appear to have a statistically significant
relationship to the response are displacement,
weight, year, and origin.
iii. What does the coefficient for the year variable
suggest?
The coefficient for the year variable suggests that there is a positive
correlation between mpg and year. A one unit
shift in year will increase the mean of mpg by 0.7508 while
all other variables are held constant.
(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(y1)
summary(influence.measures(y1))
## Potentially influential observations of
## lm(formula = mpg ~ . - name, data = Auto) :
##
## dfb.1_ dfb.cyln dfb.dspl dfb.hrsp dfb.wght dfb.accl dfb.year dfb.orgn
## 14 0.01 0.08 -0.21 -0.12 0.23 -0.12 -0.02 -0.03
## 112 -0.20 0.17 0.04 -0.06 -0.08 0.12 0.16 -0.15
## 155 -0.05 0.08 -0.18 0.27 -0.10 -0.08 0.10 -0.03
## 156 -0.08 0.08 -0.26 0.31 -0.02 -0.06 0.10 -0.01
## 167 0.02 -0.23 -0.01 -0.03 0.22 0.02 -0.05 0.05
## 245 -0.07 0.05 0.14 -0.06 -0.15 0.24 0.00 0.05
## 248 -0.08 0.04 0.04 0.04 -0.07 0.11 0.03 0.19
## 271 0.08 0.15 -0.10 -0.11 0.05 -0.04 -0.09 -0.21
## 300 -0.04 -0.04 0.03 0.01 0.02 0.07 0.01 0.03
## 301 -0.08 0.09 -0.01 0.03 -0.06 0.10 0.03 0.00
## 310 -0.02 0.02 -0.05 -0.09 0.04 -0.17 0.11 -0.02
## 323 -0.16 0.06 0.05 -0.13 0.02 0.00 0.13 0.32
## 325 -0.13 0.03 0.05 -0.01 -0.04 0.09 0.08 0.19
## 326 -0.16 0.05 0.13 -0.07 -0.11 0.24 0.10 0.06
## 327 -0.23 0.03 0.10 -0.04 -0.05 0.38 0.09 0.09
## 328 -0.08 0.14 -0.18 -0.22 0.25 -0.06 0.03 0.06
## 330 -0.01 0.05 0.04 -0.20 0.03 -0.23 0.10 0.19
## 367 0.09 -0.02 0.04 0.27 -0.23 0.16 -0.13 0.05
## 387 -0.24 -0.13 0.37 -0.08 -0.20 0.10 0.29 0.00
## 394 -0.40 -0.01 0.27 0.21 -0.34 0.60 0.25 0.10
## dffit cov.r cook.d hat
## 14 -0.27 1.22_* 0.01 0.17_*
## 112 -0.47_* 0.87_* 0.03 0.02
## 155 -0.49_* 0.93_* 0.03 0.04
## 156 -0.54_* 0.87_* 0.04 0.03
## 167 -0.40 0.92_* 0.02 0.03
## 245 0.49_* 0.82_* 0.03 0.02
## 248 0.28 0.92_* 0.01 0.01
## 271 -0.33 0.92_* 0.01 0.02
## 300 0.11 1.07_* 0.00 0.05
## 301 0.16 1.08_* 0.00 0.06
## 310 0.30 0.86_* 0.01 0.01
## 323 0.49_* 0.74_* 0.03 0.01
## 325 0.29 0.93_* 0.01 0.02
## 326 0.50_* 0.81_* 0.03 0.02
## 327 0.64_* 0.78_* 0.05 0.03
## 328 0.40 0.92_* 0.02 0.03
## 330 0.42 0.90_* 0.02 0.02
## 367 -0.39 0.90_* 0.02 0.02
## 387 0.53_* 0.87_* 0.03 0.03
## 394 0.76_* 0.84_* 0.07 0.05
outyling.obs<-c(14,112,155,156,167,245,248,271,300,301,310,323,325,326,327,328,330,367,387,394)
Auto.small<-Auto[-outyling.obs,]
y2<-lm(mpg~ .-name, data=Auto.small)
summary(y2)
##
## Call:
## lm(formula = mpg ~ . - name, data = Auto.small)
##
## Residuals:
## Min 1Q Median 3Q Max
## -9.1802 -1.9007 -0.1046 1.6312 12.8381
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -7.6451388 4.1268496 -1.853 0.06476 .
## cylinders -0.4253142 0.3053753 -1.393 0.16454
## displacement 0.0188770 0.0070340 2.684 0.00761 **
## horsepower -0.1323373 0.0227749 -5.811 1.36e-08 ***
## weight -0.0045442 0.0007381 -6.156 1.96e-09 ***
## acceleration -0.2836625 0.1022957 -2.773 0.00584 **
## year 0.6733323 0.0472404 14.253 < 2e-16 ***
## origin 1.3348623 0.2565224 5.204 3.27e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.041 on 365 degrees of freedom
## Multiple R-squared: 0.8445, Adjusted R-squared: 0.8415
## F-statistic: 283.1 on 7 and 365 DF, p-value: < 2.2e-16
After removing all potential outliers or influential observations,
the model still explains around 84% of the variance in mpg.
Very little changes in the model with all data points included, so they
are safe to be kept in the model.
(e) Use the * and : symbols to fit linear regression models with interaction effects. Do any interactions appear to be statistically significant?
y2<-lm(mpg~.:.,Auto_new)
summary(y2)
##
## Call:
## lm(formula = mpg ~ .:., data = Auto_new)
##
## Residuals:
## Min 1Q Median 3Q Max
## -7.5833 -1.4368 0.0459 1.2984 10.9683
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.351e+01 4.743e+01 0.707 0.48032
## cylinders 9.771e+00 7.959e+00 1.228 0.22035
## displacement -5.123e-01 1.836e-01 -2.790 0.00556 **
## horsepower 1.086e+00 5.102e-01 2.129 0.03396 *
## weight -1.637e-04 1.809e-02 -0.009 0.99279
## acceleration -5.004e+00 2.246e+00 -2.228 0.02651 *
## year 6.209e-01 5.656e-01 1.098 0.27301
## origin -1.846e+01 6.718e+00 -2.747 0.00631 **
## cylinders:displacement -3.750e-03 6.373e-03 -0.588 0.55662
## cylinders:horsepower -2.375e-02 4.337e-02 -0.548 0.58429
## cylinders:weight 9.750e-04 9.751e-04 1.000 0.31801
## cylinders:acceleration 1.634e-01 1.771e-01 0.923 0.35680
## cylinders:year -1.811e-01 9.515e-02 -1.904 0.05772 .
## cylinders:origin 4.013e-01 4.806e-01 0.835 0.40427
## displacement:horsepower 5.495e-04 7.387e-04 0.744 0.45745
## displacement:weight 1.420e-05 1.356e-05 1.048 0.29548
## displacement:acceleration -7.109e-04 3.671e-03 -0.194 0.84654
## displacement:year 5.744e-03 2.321e-03 2.475 0.01379 *
## displacement:origin 2.753e-02 1.960e-02 1.405 0.16090
## horsepower:weight -4.747e-05 5.311e-05 -0.894 0.37203
## horsepower:acceleration -1.114e-02 5.441e-03 -2.047 0.04134 *
## horsepower:year -1.074e-02 6.100e-03 -1.761 0.07902 .
## horsepower:origin -6.123e-03 3.561e-02 -0.172 0.86356
## weight:acceleration 1.420e-04 2.308e-04 0.615 0.53873
## weight:year -1.621e-04 2.271e-04 -0.714 0.47590
## weight:origin -7.521e-04 1.594e-03 -0.472 0.63726
## acceleration:year 4.628e-02 2.700e-02 1.714 0.08732 .
## acceleration:origin 4.308e-01 1.590e-01 2.709 0.00707 **
## year:origin 1.178e-01 7.582e-02 1.554 0.12116
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.693 on 363 degrees of freedom
## Multiple R-squared: 0.8895, Adjusted R-squared: 0.881
## F-statistic: 104.3 on 28 and 363 DF, p-value: < 2.2e-16
y3<-lm(mpg~.*.,Auto_new)
summary(y3)
##
## Call:
## lm(formula = mpg ~ . * ., data = Auto_new)
##
## Residuals:
## Min 1Q Median 3Q Max
## -7.5833 -1.4368 0.0459 1.2984 10.9683
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.351e+01 4.743e+01 0.707 0.48032
## cylinders 9.771e+00 7.959e+00 1.228 0.22035
## displacement -5.123e-01 1.836e-01 -2.790 0.00556 **
## horsepower 1.086e+00 5.102e-01 2.129 0.03396 *
## weight -1.637e-04 1.809e-02 -0.009 0.99279
## acceleration -5.004e+00 2.246e+00 -2.228 0.02651 *
## year 6.209e-01 5.656e-01 1.098 0.27301
## origin -1.846e+01 6.718e+00 -2.747 0.00631 **
## cylinders:displacement -3.750e-03 6.373e-03 -0.588 0.55662
## cylinders:horsepower -2.375e-02 4.337e-02 -0.548 0.58429
## cylinders:weight 9.750e-04 9.751e-04 1.000 0.31801
## cylinders:acceleration 1.634e-01 1.771e-01 0.923 0.35680
## cylinders:year -1.811e-01 9.515e-02 -1.904 0.05772 .
## cylinders:origin 4.013e-01 4.806e-01 0.835 0.40427
## displacement:horsepower 5.495e-04 7.387e-04 0.744 0.45745
## displacement:weight 1.420e-05 1.356e-05 1.048 0.29548
## displacement:acceleration -7.109e-04 3.671e-03 -0.194 0.84654
## displacement:year 5.744e-03 2.321e-03 2.475 0.01379 *
## displacement:origin 2.753e-02 1.960e-02 1.405 0.16090
## horsepower:weight -4.747e-05 5.311e-05 -0.894 0.37203
## horsepower:acceleration -1.114e-02 5.441e-03 -2.047 0.04134 *
## horsepower:year -1.074e-02 6.100e-03 -1.761 0.07902 .
## horsepower:origin -6.123e-03 3.561e-02 -0.172 0.86356
## weight:acceleration 1.420e-04 2.308e-04 0.615 0.53873
## weight:year -1.621e-04 2.271e-04 -0.714 0.47590
## weight:origin -7.521e-04 1.594e-03 -0.472 0.63726
## acceleration:year 4.628e-02 2.700e-02 1.714 0.08732 .
## acceleration:origin 4.308e-01 1.590e-01 2.709 0.00707 **
## year:origin 1.178e-01 7.582e-02 1.554 0.12116
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.693 on 363 degrees of freedom
## Multiple R-squared: 0.8895, Adjusted R-squared: 0.881
## F-statistic: 104.3 on 28 and 363 DF, p-value: < 2.2e-16
The interactions between acceleration:origin,
horsepower:acceleration, and displacement:year
are all significant.
(f) Try a few different transformations of the variables, such as log(X), √X, X2. Comment on your findings.
y4 <- lm(mpg~sqrt(weight)+log(horsepower)+acceleration+I(acceleration^2),Auto_new)
summary(y4)
##
## Call:
## lm(formula = mpg ~ sqrt(weight) + log(horsepower) + acceleration +
## I(acceleration^2), data = Auto_new)
##
## Residuals:
## Min 1Q Median 3Q Max
## -15.2799 -2.2872 -0.2241 1.8562 15.1486
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 69.2436011 5.5503808 12.475 < 2e-16 ***
## sqrt(weight) -0.6256849 0.0468385 -13.358 < 2e-16 ***
## log(horsepower) -3.1441346 0.5513133 -5.703 2.34e-08 ***
## acceleration -0.0447541 0.6091750 -0.073 0.941
## I(acceleration^2) -0.0005801 0.0192360 -0.030 0.976
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.013 on 387 degrees of freedom
## Multiple R-squared: 0.7383, Adjusted R-squared: 0.7356
## F-statistic: 273 on 4 and 387 DF, p-value: < 2.2e-16
The square root of weight and the log of
horsepower are both significant in the model.
Acceleration^2 is not significant in the model.
This question should be answered using the Carseats data set.
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 increase in
my price, sales decrease by $54. Sales inside the US are $1200 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 = 13.043469 - 0.054459Price - 0.021916UrbanYes + 1.200573USYes\)
(d) For which of the predictors can you reject the null
hypothesis \(H0 : \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?
The R squared for each model is around 24% which does not explain much
of the variance in the dependent Sales variable, therefore
the models are not a good fit.
(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)
summary(influence.measures(fit))
## Potentially influential observations of
## lm(formula = Sales ~ Price + US) :
##
## dfb.1_ dfb.Pric dfb.USYs dffit cov.r cook.d hat
## 26 0.24 -0.18 -0.17 0.28_* 0.97_* 0.03 0.01
## 29 -0.10 0.10 -0.10 -0.18 0.97_* 0.01 0.01
## 43 -0.11 0.10 0.03 -0.11 1.05_* 0.00 0.04_*
## 50 -0.10 0.17 -0.17 0.26_* 0.98 0.02 0.01
## 51 -0.05 0.05 -0.11 -0.18 0.95_* 0.01 0.00
## 58 -0.05 -0.02 0.16 -0.20 0.97_* 0.01 0.01
## 69 -0.09 0.10 0.09 0.19 0.96_* 0.01 0.01
## 126 -0.07 0.06 0.03 -0.07 1.03_* 0.00 0.03_*
## 160 0.00 0.00 0.00 0.01 1.02_* 0.00 0.02
## 166 0.21 -0.23 -0.04 -0.24 1.02 0.02 0.03_*
## 172 0.06 -0.07 0.02 0.08 1.03_* 0.00 0.02
## 175 0.14 -0.19 0.09 -0.21 1.03_* 0.02 0.03_*
## 210 -0.14 0.15 -0.10 -0.22 0.97_* 0.02 0.01
## 270 -0.03 0.05 -0.03 0.06 1.03_* 0.00 0.02
## 298 -0.06 0.06 -0.09 -0.15 0.97_* 0.01 0.00
## 314 -0.05 0.04 0.02 -0.05 1.03_* 0.00 0.02_*
## 353 -0.02 0.03 0.09 0.15 0.97_* 0.01 0.00
## 357 0.02 -0.02 0.02 -0.03 1.03_* 0.00 0.02
## 368 0.26 -0.23 -0.11 0.27_* 1.01 0.02 0.02_*
## 377 0.14 -0.15 0.12 0.24 0.95_* 0.02 0.01
## 384 0.00 0.00 0.00 0.00 1.02_* 0.00 0.02
## 387 -0.03 0.04 -0.03 0.05 1.02_* 0.00 0.02
## 396 -0.05 0.05 0.08 0.14 0.98_* 0.01 0.00
outyling.obs<-c(26,29,43,50,51,58,69,126,160,166,172,175,210,270,298,314,353,357,368,377,384,387,396)
Carseats.small<-Carseats[-outyling.obs,]
fit2<-lm(Sales~Price+US,data=Carseats.small)
summary(fit2)
##
## Call:
## lm(formula = Sales ~ Price + US, data = Carseats.small)
##
## Residuals:
## Min 1Q Median 3Q Max
## -5.263 -1.605 -0.039 1.590 5.428
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 12.925232 0.665259 19.429 < 2e-16 ***
## Price -0.053973 0.005511 -9.794 < 2e-16 ***
## USYes 1.255018 0.248856 5.043 7.15e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.29 on 374 degrees of freedom
## Multiple R-squared: 0.2387, Adjusted R-squared: 0.2347
## F-statistic: 58.64 on 2 and 374 DF, p-value: < 2.2e-16
Even after removing all potential outliers or influential
observations, the model still only explains around 24% of the variance
in Sales. The confidence interval for the coefficient
estimates produced by the linear model fit to the full data set contain
the estimates of the coefficients for the estimates of the model with
the outliers removed. Very little changes in the model with all data
points included, so they are safe to be kept in the model.
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 X onto Y will be the same
as the coefficient estimate for the regression of Y onto X if the sum of
xi^2 is equal to the sum of yi^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.
x=rnorm(100)
y=rbinom(100,2,0.3)
ex1<-lm(y~x+0)
summary(ex1)
##
## Call:
## lm(formula = y ~ x + 0)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.43358 -0.01389 0.17382 1.00233 1.94032
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## x 0.18219 0.07933 2.297 0.0237 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.7522 on 99 degrees of freedom
## Multiple R-squared: 0.05059, Adjusted R-squared: 0.041
## F-statistic: 5.275 on 1 and 99 DF, p-value: 0.02374
ex2<-lm(x~y+0)
summary(ex2)
##
## Call:
## lm(formula = x ~ y + 0)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.57035 -0.59943 -0.04551 0.58326 2.37984
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## y 0.2777 0.1209 2.297 0.0237 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.9286 on 99 degrees of freedom
## Multiple R-squared: 0.05059, Adjusted R-squared: 0.041
## F-statistic: 5.275 on 1 and 99 DF, p-value: 0.02374
(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
y=100:1
ex3<-lm(y~x+0)
summary(ex3)
##
## 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
ex4<-lm(x~y+0)
summary(ex4)
##
## 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