-
Notifications
You must be signed in to change notification settings - Fork 25
/
03-linear-regression.Rmd
960 lines (745 loc) · 30.4 KB
/
03-linear-regression.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
# Linear Regression
## Conceptual
### Question 1
> Describe the null hypotheses to which the p-values given in Table 3.4
> correspond. Explain what conclusions you can draw based on these p-values.
> Your explanation should be phrased in terms of `sales`, `TV`, `radio`, and
> `newspaper`, rather than in terms of the coefficients of the linear model.
For intercept, that $\beta_0 = 0$
For the others, that $\beta_n = 0$ (for $n = 1, 2, 3$)
We can conclude that that without any spending, there are still some sales (the
intercept is not 0). Furthermore, we can conclude that money spent on TV and
radio are significantly associated with increased sales, but the same cannot be
said of newspaper spending.
### Question 2
> Carefully explain the differences between the KNN classifier and KNN
> regression methods.
The KNN classifier is categorical and assigns a value based on the most
frequent observed category among $K$ nearest neighbors, whereas KNN regression
assigns a continuous variable, the average of the response variables for
the $K$ nearest neighbors.
### Question 3
> Suppose we have a data set with five predictors, $X_1$ = GPA, $X_2$ = IQ,
> $X_3$ = Level (1 for College and 0 for High School), $X_4$ = Interaction
> between GPA and IQ, and $X_5$ = Interaction between GPA and Level. The
> response is starting salary after graduation (in thousands of dollars).
> Suppose we use least squares to fit the model, and get $\hat\beta_0 = 50$,
> $\hat\beta_1 = 20$, $\hat\beta_2 = 0.07$, $\hat\beta_3 = 35$, $\hat\beta_4 =
> 0.01$, $\hat\beta_5 = -10$.
>
> a. Which answer is correct, and why?
> i. For a fixed value of IQ and GPA, high school graduates earn more on
> average than college graduates.
> ii. For a fixed value of IQ and GPA, college graduates earn more on
> average than high school graduates.
> iii. For a fixed value of IQ and GPA, high school graduates earn more on
> average than college graduates provided that the GPA is high enough.
> iv. For a fixed value of IQ and GPA, college graduates earn more on
> average than high school graduates provided that the GPA is high
> enough.
The model is:
$y = \beta_0 + \beta_1 \cdot \text{GPA} + \beta_2 \cdot \text{IQ} + \beta_3 \cdot \text{Level} + \beta_4 \cdot \text{GPA} \cdot \text{IQ} + \beta_5 \cdot \text{GPA} \cdot \text{Level}$
Fixing IQ and GPA, changing Level from 0 to 1 will change the outcome by:
$\Delta y = \beta_3 + \beta_5 \cdot \text{GPA}$
$\Delta y > 0 \Rightarrow \beta_3 + \beta_5 \cdot \text{GPA} > 0 \Rightarrow \text{GPA} < \dfrac{-\beta_3}{\beta_5} = \dfrac{-35}{-10} = 3.5$
From a graphical standpoint:
```{r, message = FALSE, warning = FALSE}
library(plotly)
```
```{r}
model <- function(gpa, iq, level) {
50 +
gpa * 20 +
iq * 0.07 +
level * 35 +
gpa * iq * 0.01 +
gpa * level * -10
}
x <- seq(1, 5, length = 10)
y <- seq(1, 200, length = 20)
college <- t(outer(x, y, model, level = 1))
high_school <- t(outer(x, y, model, level = 0))
plot_ly(x = x, y = y) |>
add_surface(
z = ~college,
colorscale = list(c(0, 1), c("rgb(107,184,214)", "rgb(0,90,124)")),
colorbar = list(title = "College")) |>
add_surface(
z = ~high_school,
colorscale = list(c(0, 1), c("rgb(255,112,184)", "rgb(128,0,64)")),
colorbar = list(title = "High school")) |>
layout(scene = list(
xaxis = list(title = "GPA"),
yaxis = list(title = "IQ"),
zaxis = list(title = "Salary")))
```
Option iii correct.
> b. Predict the salary of a college graduate with IQ of 110 and a GPA of 4.0.
```{r}
model(gpa = 4, iq = 110, level = 1)
```
> c. True or false: Since the coefficient for the GPA/IQ interaction term is
> very small, there is very little evidence of an interaction effect. Justify
> your answer.
This is false. It is important to remember that GPA and IQ vary over different
scales. It is better to explicitly test the significance of the interaction
effect, and/or visualize or quantify the effect on sales under realistic ranges
of GPA/IQ values.
### Question 4
> I collect a set of data ($n = 100$ observations) containing a single predictor
> and a quantitative response. I then fit a linear regression model to the data,
> as well as a separate cubic regression, i.e. $Y = \beta_0 + \beta_1X +
> \beta_2X^2 + \beta_3X^3 + \epsilon$.
>
> a. Suppose that the true relationship between $X$ and $Y$ is linear,
> i.e. $Y = \beta_0 + \beta_1X + \epsilon$. Consider the training residual
> sum of squares (RSS) for the linear regression, and also the training RSS
> for the cubic regression. Would we expect one to be lower than the other,
> would we expect them to be the same, or is there not enough information to
> tell? Justify your answer.
You would expect the cubic regression to have lower RSS since it is at least
as flexible as the linear regression.
> b. Answer (a) using test rather than training RSS.
Though we could not be certain, the test RSS would likely be higher due to
overfitting.
> c. Suppose that the true relationship between $X$ and $Y$ is not linear, but
> we don't know how far it is from linear. Consider the training RSS for the
> linear regression, and also the training RSS for the cubic regression.
> Would we expect one to be lower than the other, would we expect them to be
> the same, or is there not enough information to tell? Justify your answer.
You would expect the cubic regression to have lower RSS since it is at least
as flexible as the linear regression.
> d. Answer (c) using test rather than training RSS.
There is not enough information to tell, it depends on how non-linear the
true relationship is.
### Question 5
> Consider the fitted values that result from performing linear regression
> without an intercept. In this setting, the ith fitted value takes the form
> $$\hat{y}_i = x_i\hat\beta,$$
> where
> $$\hat{\beta} = \left(\sum_{i=1}^nx_iy_i\right) / \left(\sum_{i' = 1}^n x^2_{i'}\right).$$
> show that we can write
> $$\hat{y}_i = \sum_{i' = 1}^na_{i'}y_{i'}$$
> What is $a_{i'}$?
>
> _Note: We interpret this result by saying that the fitted
> values from linear regression are linear combinations of the response values._
\begin{align}
\hat{y}_i
& = x_i \frac{\sum_{i=1}^nx_iy_i}{\sum_{i' = 1}^n x^2_{i'}} \\
& = x_i \frac{\sum_{i'=1}^nx_{i'}y_{i'}}{\sum_{i'' = 1}^n x^2_{i''}} \\
& = \frac{\sum_{i'=1}^n x_i x_{i'}y_{i'}}{\sum_{i'' = 1}^n x^2_{i''}} \\
& = \sum_{i'=1}^n \frac{ x_i x_{i'}y_{i'}}{\sum_{i'' = 1}^n x^2_{i''}} \\
& = \sum_{i'=1}^n \frac{ x_i x_{i'}}{\sum_{i'' = 1}^n x^2_{i''}} y_{i'}
\end{align}
therefore,
$$a_{i'} = \frac{ x_i x_{i'}}{\sum x^2}$$
### Question 6
> Using (3.4), argue that in the case of simple linear regression, the
> least squares line always passes through the point $(\bar{x}, \bar{y})$.
when $x = \bar{x}$ what is $y$?
\begin{align}
y &= \hat\beta_0 + \hat\beta_1\bar{x} \\
&= \bar{y} - \hat\beta_1\bar{x} + \hat\beta_1\bar{x} \\
&= \bar{y}
\end{align}
### Question 7
> It is claimed in the text that in the case of simple linear regression
> of $Y$ onto $X$, the $R^2$ statistic (3.17) is equal to the square of the
> correlation between $X$ and $Y$ (3.18). Prove that this is the case. For
> simplicity, you may assume that $\bar{x} = \bar{y} = 0$.
We have the following equations:
$$ R^2 = \frac{\textit{TSS} - \textit{RSS}}{\textit{TSS}} $$
$$ Cor(x,y) = \frac{\sum_i (x_i-\bar{x})(y_i - \bar{y})}{\sqrt{\sum_i(x_i - \bar{x})^2}\sqrt{\sum_i(y_i - \bar{y})^2}} $$
As above, its important to remember $\sum_i x_i = \sum_j x_j$
when $\bar{x} = \bar{y} = 0$
$$ Cor(x,y)^2 = \frac{(\sum_ix_iy_i)^2}{\sum_ix_i^2 \sum_iy_i^2} $$
Also note that:
$$\hat{y}_i = \hat\beta_o + \hat\beta_1x_i = x_i\frac{\sum_j{x_jy_j}}{\sum_jx_j^2}$$
Therefore, given that $RSS = \sum_i(y_i - \hat{y}_i)^2$ and
$\textit{TSS} = \sum_i(y_i - \bar{y})^2 = \sum_iy_i^2$
\begin{align}
R^2 &= \frac{\sum_iy_i^2 - \sum_i(y_i - x_i\frac{\sum_j{x_jy_j}}{\sum_jx_j^2})^2}
{\sum_iy_i^2} \\
&= \frac{\sum_iy_i^2 - \sum_i(
y_i^2 -
2y_ix_i\frac{\sum_j{x_jy_j}}{\sum_jx_j^2} +
x_i^2 (\frac{\sum_j{x_jy_j}}{\sum_jx_j^2})^2
)}{\sum_iy_i^2} \\
&= \frac{
2\sum_i(y_ix_i\frac{\sum_j{x_jy_j}}{\sum_jx_j^2}) -
\sum_i(x_i^2 (\frac{\sum_j{x_jy_j}}{\sum_jx_j^2})^2)
}{\sum_iy_i^2} \\
&= \frac{
2\sum_i(y_ix_i) \frac{\sum_j{x_jy_j}}{\sum_jx_j^2} -
\sum_i(x_i^2) \frac{(\sum_j{x_jy_j})^2}{(\sum_jx_j^2)^2}
}{\sum_iy_i^2} \\
&= \frac{
2\frac{(\sum_i{x_iy_i})^2}{\sum_jx_j^2} -
\frac{(\sum_i{x_iy_i})^2}{\sum_jx_j^2}
}{\sum_iy_i^2} \\
&= \frac{(\sum_i{x_iy_i})^2}{\sum_ix_i^2 \sum_iy_i^2}
\end{align}
## Applied
### Question 8
> This question involves the use of simple linear regression on the Auto
> data set.
>
> a. Use the `lm()` function to perform a simple linear regression with `mpg` as
> the response and `horsepower` as the predictor. Use the `summary()` function
> to print the results. Comment on the output. For example:
> i. Is there a relationship between the predictor and the response?
> ii. How strong is the relationship between the predictor and the response?
> iii. Is the relationship between the predictor and the response positive or
> negative?
> iv. What is the predicted mpg associated with a horsepower of 98?
> v. What are the associated 95% confidence and prediction intervals?
```{r}
library(ISLR2)
fit <- lm(mpg ~ horsepower, data = Auto)
summary(fit)
```
Yes, there is a significant relationship between predictor and response. For
every unit increase in horsepower, mpg reduces by 0.16 (a negative
relationship).
```{r}
predict(fit, data.frame(horsepower = 98), interval = "confidence")
predict(fit, data.frame(horsepower = 98), interval = "prediction")
```
> b. Plot the response and the predictor. Use the `abline()` function to display
> the least squares regression line.
```{r}
plot(Auto$horsepower, Auto$mpg, xlab = "horsepower", ylab = "mpg")
abline(fit)
```
> c. Use the `plot()` function to produce diagnostic plots of the least squares
> regression fit. Comment on any problems you see with the fit.
```{r}
par(mfrow = c(2, 2))
plot(fit, cex = 0.2)
```
The residuals show a trend with respect to the fitted values suggesting a
non-linear relationship.
### Question 9
> This question involves the use of multiple linear regression on the `Auto`
> data set.
>
> a. Produce a scatterplot matrix which includes all of the variables in the
> data set.
```{r}
pairs(Auto, cex = 0.2)
```
> b. Compute the matrix of correlations between the variables using the function
> `cor()`. You will need to exclude the name variable, `name` which is
> qualitative.
```{r}
x <- subset(Auto, select = -name)
cor(x)
```
> 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:
> i. Is there a relationship between the predictors and the response?
> ii. Which predictors appear to have a statistically significant relationship
> to the response?
> iii. What does the coefficient for the `year` variable suggest?
```{r}
fit <- lm(mpg ~ ., data = x)
summary(fit)
```
Yes, there is a relationship between some predictors and response, notably
"displacement" (positive), "weight" (negative), "year" (positive) and
"origin" (positive).
The coefficient for year (which is positive $~0.75$) suggests that mpg
increases by about this amount every year on average.
> 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?
```{r}
par(mfrow = c(2, 2))
plot(fit, cex = 0.2)
```
One point has high leverage, the residuals also show a trend with fitted values.
> e. Use the `*` and `:` symbols to fit linear regression models with
> interaction effects. Do any interactions appear to be statistically
> significant?
```{r}
summary(lm(mpg ~ . + weight:horsepower, data = x))
summary(lm(mpg ~ . + acceleration:horsepower, data = x))
summary(lm(mpg ~ . + cylinders:weight, data = x))
```
There are at least three cases where the interactions appear to be highly
significant.
> f. Try a few different transformations of the variables, such as $log(X)$,
> $\sqrt{X}$, $X^2$. Comment on your findings.
Here I'll just consider transformations for `horsepower`.
```{r}
par(mfrow = c(2, 2))
plot(Auto$horsepower, Auto$mpg, cex = 0.2)
plot(log(Auto$horsepower), Auto$mpg, cex = 0.2)
plot(sqrt(Auto$horsepower), Auto$mpg, cex = 0.2)
plot(Auto$horsepower ^ 2, Auto$mpg, cex = 0.2)
x <- subset(Auto, select = -name)
x$horsepower <- log(x$horsepower)
fit <- lm(mpg ~ ., data = x)
summary(fit)
par(mfrow = c(2, 2))
plot(fit, cex = 0.2)
```
A log transformation of `horsepower` appears to give a more linear relationship
with mpg.
### Question 10
> This question should be answered using the `Carseats` data set.
>
> a. Fit a multiple regression model to predict `Sales` using `Price`, `Urban`,
> and `US`.
```{r}
fit <- lm(Sales ~ Price + Urban + US, data = Carseats)
```
> b. Provide an interpretation of each coefficient in the model. Be
> careful---some of the variables in the model are qualitative!
```{r}
summary(fit)
```
> c. Write out the model in equation form, being careful to handle the
> qualitative variables properly.
$$
\textit{Sales} = 13 + -0.054 \times \textit{Price} + \begin{cases}
-0.022, & \text{if $\textit{Urban}$ is Yes, $\textit{US}$ is No} \\
1.20, & \text{if $\textit{Urban}$ is No, $\textit{US}$ is Yes} \\
1.18, & \text{if $\textit{Urban}$ and $\textit{US}$ is Yes} \\
0, & \text{Otherwise}
\end{cases}
$$
> d. For which of the predictors can you reject the null hypothesis
> $H_0 : \beta_j = 0$?
Price and US (Urban shows no significant difference between "No" and "Yes")
> 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.
```{r}
fit2 <- lm(Sales ~ Price + US, data = Carseats)
```
> f. How well do the models in (a) and (e) fit the data?
```{r}
summary(fit)
summary(fit2)
anova(fit, fit2)
```
They have similar $R^2$ and the model containing the extra variable "Urban" is
non-significantly better.
> g. Using the model from (e), obtain 95% confidence intervals for the
> coefficient(s).
```{r}
confint(fit2)
```
> h. Is there evidence of outliers or high leverage observations in the model
> from (e)?
```{r}
par(mfrow = c(2, 2))
plot(fit2, cex = 0.2)
```
Yes, somewhat.
### Question 11
> In this problem we will investigate the _t_-statistic for the null hypothesis
> $H_0 : \beta = 0$ in simple linear regression without an intercept. To
> begin, we generate a predictor `x` and a response `y` as follows.
>
> ```r
> set.seed(1)
> x <- rnorm(100)
> y <- 2 * x + rnorm(100)
> ```
```{r}
set.seed(1)
x <- rnorm(100)
y <- 2 * x + rnorm(100)
```
> a. Perform a simple linear regression of `y` onto `x`, _without_ an intercept.
> Report the coefficient estimate $\hat{\beta}$, the standard error of this
> coefficient estimate, and the _t_-statistic and _p_-value associated with the
> null hypothesis $H_0 : \beta = 0$. Comment on these results. (You can
> perform regression without an intercept using the command `lm(y~x+0)`.)
```{r}
fit <- lm(y ~ x + 0)
coef(summary(fit))
```
There's a significant positive relationship between $y$ and $x$. $y$ values are
predicted to be (a little below) twice the $x$ values.
> b. Now perform a simple linear regression of `x` onto `y` without an intercept,
> and report the coefficient estimate, its standard error, and the
> corresponding _t_-statistic and _p_-values associated with the null hypothesis
> $H_0 : \beta = 0$. Comment on these results.
```{r}
fit <- lm(x ~ y + 0)
coef(summary(fit))
```
There's a significant positive relationship between $x$ and $y$. $x$ values are
predicted to be (a little below) half the $y$ values.
> c. What is the relationship between the results obtained in (a) and (b)?
Without error, the coefficients would be the inverse of each other (2 and 1/2).
The t-statistic and p-values are the same.
> d. For the regression of $Y$ onto $X$ without an intercept, the
> _t_-statistic for $H_0 : \beta = 0$ takes the form
> $\hat{\beta}/SE(\hat{\beta})$, where $\hat{\beta}$ is given by (3.38), and
> where
> $$
> SE(\hat\beta) = \sqrt{\frac{\sum_{i=1}^n(y_i - x_i\hat\beta)^2}{(n-1)\sum_{i'=1}^nx_{i'}^2}}.
> $$
> (These formulas are slightly different from those given in Sections 3.1.1 and
> 3.1.2, since here we are performing regression without an intercept.) Show
> algebraically, and confirm numerically in R, that the t-statistic can be
> written as
> $$
> \frac{(\sqrt{n-1}) \sum_{i-1}^nx_iy_i)}
> {\sqrt{(\sum_{i=1}^nx_i^2)(\sum_{i'=1}^ny_{i'}^2)-(\sum_{i'=1}^nx_{i'}y_{i'})^2}}
> $$
$$ \beta = \sum_i x_i y_i / \sum_{i'} x_{i'}^2 ,$$
therefore
\begin{align}
t &= \frac{\sum_i x_i y_i \sqrt{n-1} \sqrt{\sum_ix_i^2}}
{\sum_i x_i^2 \sqrt{\sum_i(y_i - x_i \beta)^2}} \\
&= \frac{\sum_i x_i y_i \sqrt{n-1}}
{\sqrt{\sum_ix_i^2 \sum_i(y_i - x_i \beta)^2}} \\
&= \frac{\sum_i x_i y_i \sqrt{n-1}}
{\sqrt{
\sum_ix_i^2
\sum_i(y_i^2 - 2 y_i x_i \beta + x_i^2 \beta^2)
}} \\
&= \frac{\sum_i x_i y_i \sqrt{n-1}}
{\sqrt{
\sum_ix_i^2 \sum_iy_i^2 -
\beta \sum_ix_i^2 (2 \sum_i y_i x_i -\beta \sum_i x_i^2)
}} \\
&= \frac{\sum_i x_i y_i \sqrt{n-1}}
{\sqrt{
\sum_ix_i^2 \sum_iy_i^2 -
\sum_i x_i y_i (2 \sum_i y_i x_i - \sum_i x_i y_i)
}} \\
&= \frac{\sum_i x_i y_i \sqrt{n-1}}
{\sqrt{\sum_ix_i^2 \sum_iy_i^2 - (\sum_i x_i y_i)^2}} \\
\end{align}
We can show this numerically in R by computing $t$ using the above equation.
```{r}
n <- length(x)
sqrt(n - 1) * sum(x * y) / sqrt(sum(x ^ 2) * sum(y ^ 2) - sum(x * y) ^ 2)
```
> e. Using the results from (d), argue that the _t_-statistic for the
> regression of y onto x is the same as the _t_-statistic for the regression
> of `x` onto `y`.
Swapping $x_i$ for $y_i$ in the formula for $t$ will give the same result.
> f. In `R`, show that when regression is performed _with_ an intercept, the
> _t_-statistic for $H_0 : \beta_1 = 0$ is the same for the regression of `y`
> onto `x` as it is for the regression of `x` onto `y`.
```{r}
coef(summary(lm(y ~ x)))
coef(summary(lm(x ~ y)))
```
### Question 12
> This problem involves simple linear regression without an intercept.
>
> a. Recall that the coefficient estimate $\hat{\beta}$ 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$?
$$ \hat\beta = \sum_i x_iy_i / \sum_{i'} x_{i'}^2 $$
The coefficient for the regression of X onto Y swaps the $x$ and $y$ variables:
$$ \hat\beta = \sum_i x_iy_i / \sum_{i'} y_{i'}^2 $$
So they are the same when $\sum_{i} x_{i}^2 = \sum_{i} y_{i}^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$.
```{r}
x <- rnorm(100)
y <- 2 * x + rnorm(100, 0, 0.1)
c(sum(x^2), sum(y^2))
c(coef(lm(y ~ x))[2], coef(lm(x ~ y))[2])
```
> 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$.
```{r}
x <- rnorm(100)
y <- x + rnorm(100, 0, 0.1)
c(sum(x^2), sum(y^2))
c(coef(lm(y ~ x))[2], coef(lm(x ~ y))[2])
```
### Question 13
> In this exercise you will create some simulated data and will fit simple
> linear regression models to it. Make sure to use `set.seed(1)` prior to
> starting part (a) to ensure consistent results.
```{r}
set.seed(1)
```
> a. Using the `rnorm()` function, create a vector, `x`, containing 100
> observations drawn from a $N(0, 1)$ distribution. This represents a
> feature, $X$.
```{r}
x <- rnorm(100, 0, 1)
```
> b. Using the `rnorm()` function, create a vector, `eps`, containing 100
> observations drawn from a $N(0, 0.25)$ distribution---a normal
> distribution with mean zero and variance 0.25.
```{r}
eps <- rnorm(100, 0, sqrt(0.25))
```
> c. Using x and `eps`, generate a vector y according to the model
> $$Y = -1 + 0.5X + \epsilon$$
> What is the length of the vector `y`? What are the values of
> $\beta_0$ and $\beta_1$ in this linear model?
```{r}
y <- -1 + 0.5 * x + eps
length(y)
```
$\beta_0 = -1$ and $\beta_1 = 0.5$
> d. Create a scatterplot displaying the relationship between `x` and `y`.
> Comment on what you observe.
```{r}
plot(x, y)
```
There is a linear relationship between $x$ and $y$ (with some error).
> e. Fit a least squares linear model to predict `y` using `x`. Comment on the
> model obtained. How do $\hat\beta_0$ and $\hat\beta_1$ compare to $\beta_0$
> and $\beta_1$?
```{r}
fit <- lm(y ~ x)
summary(fit)
```
$\beta_0$ and $\beta_1$ are close to their population values.
> f. Display the least squares line on the scatterplot obtained in (d). Draw the
> population regression line on the plot, in a different color. Use the
> `legend()` command to create an appropriate legend.
```{r}
plot(x, y)
abline(fit)
abline(-1, 0.5, col = "red", lty = 2)
legend("topleft",
c("model fit", "population regression"),
col = c("black", "red"),
lty = c(1, 2)
)
```
> g. Now fit a polynomial regression model that predicts `y` using `x` and `x^2`.
> Is there evidence that the quadratic term improves the model fit? Explain
> your answer.
```{r}
fit2 <- lm(y ~ poly(x, 2))
anova(fit2, fit)
```
There is no evidence for an improved fit, since the F-test is non-significant.
> h. Repeat (a)--(f) after modifying the data generation process in such a way
> that there is *less* noise in the data. The model (3.39) should remain the
> same. You can do this by decreasing the variance of the normal distribution
> used to generate the error term $\epsilon$ in (b). Describe your results.
```{r}
x <- rnorm(100, 0, 1)
y <- -1 + 0.5 * x + rnorm(100, 0, sqrt(0.05))
fit2 <- lm(y ~ x)
summary(fit2)
plot(x, y)
abline(fit2)
abline(-1, 0.5, col = "red", lty = 2)
legend("topleft",
c("model fit", "population regression"),
col = c("black", "red"),
lty = c(1, 2)
)
```
The data shows less variability and the $R^2$ is higher.
> i. Repeat (a)--(f) after modifying the data generation process in such a way
> that there is *more* noise in the data. The model (3.39) should remain the
> same. You can do this by increasing the variance of the normal distribution
> used to generate the error term $\epsilon$ in (b). Describe your results.
```{r}
x <- rnorm(100, 0, 1)
y <- -1 + 0.5 * x + rnorm(100, 0, 1)
fit3 <- lm(y ~ x)
summary(fit3)
plot(x, y)
abline(fit3)
abline(-1, 0.5, col = "red", lty = 2)
legend("topleft",
c("model fit", "population regression"),
col = c("black", "red"),
lty = c(1, 2)
)
```
The data shows more variability. The $R^2$ is lower.
> j. What are the confidence intervals for $\beta_0$ and $\beta_1$ based on the
> original data set, the noisier data set, and the less noisy data set?
> Comment on your results.
```{r}
confint(fit)
confint(fit2)
confint(fit3)
```
The confidence intervals for the coefficients are smaller when there is less
error.
### Question 14
> This problem focuses on the collinearity problem.
>
> a. Perform the following commands in R :
>
> ```r
> > set.seed(1)
> > x1 <- runif(100)
> > x2 <- 0.5 * x1 + rnorm(100) / 10
> > y <- 2 + 2 * x1 + 0.3 * x2 + rnorm(100)
> ```
>
> The last line corresponds to creating a linear model in which `y` is a
> function of `x1` and `x2`. Write out the form of the linear model. What are
> the regression coefficients?
```{r}
set.seed(1)
x1 <- runif(100)
x2 <- 0.5 * x1 + rnorm(100) / 10
y <- 2 + 2 * x1 + 0.3 * x2 + rnorm(100)
```
The model is of the form:
$$Y = \beta_0 + \beta_1X_1 + \beta_2X_2 + \epsilon$$
The coefficients are $\beta_0 = 2$, $\beta_1 = 2$, $\beta_3 = 0.3$.
> b. What is the correlation between `x1` and `x2`? Create a scatterplot
> displaying the relationship between the variables.
```{r}
cor(x1, x2)
plot(x1, x2)
```
> c. Using this data, fit a least squares regression to predict `y` using `x1`
> and `x2`. Describe the results obtained. What are $\hat\beta_0$,
> $\hat\beta_1$, and $\hat\beta_2$? How do these relate to the true $\beta_0$,
> $\beta_1$, and \beta_2$? Can you reject the null hypothesis $H_0 : \beta_1$
> = 0$? How about the null hypothesis $H_0 : \beta_2 = 0$?
```{r}
summary(lm(y ~ x1 + x2))
```
$\hat\beta_0 = 2.13$, $\hat\beta_1 = 1.43$, and $\hat\beta_2 = 1.01$. These are
relatively poor estimates of the true values. We can reject the hypothesis that
$H_0 : \beta_1$ at a p-value of 0.05 (just about). We cannot reject the
hypothesis that $H_0 : \beta_2 = 0$.
> d. Now fit a least squares regression to predict `y` using only `x1`. Comment
> on your results. Can you reject the null hypothesis $H 0 : \beta_1 = 0$?
```{r}
summary(lm(y ~ x1))
```
We can reject $H_0 : \beta_1 = 0$. The p-value is much more significant for
$\beta_1$ compared to when `x2` is included in the model.
> e. Now fit a least squares regression to predict `y` using only `x2`. Comment
> on your results. Can you reject the null hypothesis $H_0 : \beta_1 = 0$?
```{r}
summary(lm(y ~ x2))
```
Similarly, we can reject $H_0 : \beta_2 = 0$. The p-value is much more
significant for $\beta_2$ compared to when `x1` is included in the model.
> f. Do the results obtained in (c)--(e) contradict each other? Explain your
> answer.
No they do not contradict each other. Both `x1` and `x2` individually are
capable of explaining much of the variation observed in `y`, however since they
are correlated, it is very difficult to tease apart their separate
contributions.
> g. Now suppose we obtain one additional observation, which was unfortunately
> mismeasured.
> ```r
> > x1 <- c(x1, 0.1)
> > x2 <- c(x2, 0.8)
> > y <- c(y, 6)
> ```
> Re-fit the linear models from (c) to (e) using this new data. What effect
> does this new observation have on the each of the models? In each model, is
> this observation an outlier? A high-leverage point? Both? Explain your
> answers.
```{r}
x1 <- c(x1 , 0.1)
x2 <- c(x2 , 0.8)
y <- c(y ,6)
summary(lm(y ~ x1 + x2))
summary(lm(y ~ x1))
summary(lm(y ~ x2))
par(mfrow = c(2, 2))
plot(lm(y ~ x1 + x2), cex = 0.2)
par(mfrow = c(2, 2))
plot(lm(y ~ x1), cex = 0.2)
par(mfrow = c(2, 2))
plot(lm(y ~ x2), cex = 0.2)
```
In the first model (with both predictors), the new point has very high leverage
(since it is an outlier in terms of the joint `x1` and `x2` distribution),
however it is not an outlier. In the model that includes `x1`, it is an outlier
but does not have high leverage. In the model that includes `x2`, it has high
leverage but is not an outlier. It is useful to consider the scatterplot of
`x1` and `x2`.
```{r}
plot(x1, x2)
points(0.1, 0.8, col = "red", pch = 19)
```
### Question 15
> This problem involves the `Boston` data set, which we saw in the lab for this
> chapter. We will now try to predict per capita crime rate using the other
> variables in this data set. In other words, per capita crime rate is the
> response, and the other variables are the predictors.
We are trying to predict `crim`.
```{r}
pred <- subset(Boston, select = -crim)
```
> a. For each predictor, fit a simple linear regression model to predict the
> response. Describe your results. In which of the models is there a
> statistically significant association between the predictor and the
> response? Create some plots to back up your assertions.
```{r}
fits <- lapply(pred, function(x) lm(Boston$crim ~ x))
printCoefmat(do.call(rbind, lapply(fits, function(x) coef(summary(x))[2, ])))
```
There are significant associations for all predictors with the exception of
`chas` when fitting separate linear models. For example, consider the following
plot representing the third model
```
plot(Boston$rm, Boston$crim)
abline(fits[[5]])
```
> b. Fit a multiple regression model to predict the response using all of the
> predictors. Describe your results. For which predictors can we reject the
> null hypothesis $H_0 : \beta_j = 0$?
```{r}
mfit <- lm(crim ~ ., data = Boston)
summary(mfit)
```
There are now only significant associations for `zn`, `dis`, `rad`, `black` and
`medv`.
> c. How do your results from (a) compare to your results from (b)? Create a
> plot displaying the univariate regression coefficients from (a) on the
> $x$-axis, and the multiple regression coefficients from (b) on the
> $y$-axis. That is, each predictor is displayed as a single point in the
> plot. Its coefficient in a simple linear regression model is shown on the
> x-axis, and its coefficient estimate in the multiple linear regression
> model is shown on the y-axis.
The results from (b) show reduced significance compared to the models fit in
(a).
```{r}
plot(sapply(fits, function(x) coef(x)[2]), coef(mfit)[-1],
xlab = "Univariate regression",
ylab = "multiple regression")
```
The estimated coefficients differ (in particular the estimated coefficient for
`nox` is dramatically different) between the two modelling strategies.
> d. Is there evidence of non-linear association between any of the predictors
> and the response? To answer this question, for each predictor X, fit a
> model of the form
> $$
> Y = \beta_0 + \beta_1X + \beta_2X^2 + \beta_3X^3 + \epsilon
> $$
```{r}
pred <- subset(pred, select = -chas)
fits <- lapply(names(pred), function(p) {
f <- paste0("crim ~ poly(", p, ", 3)")
lm(as.formula(f), data = Boston)
})
for (fit in fits) printCoefmat(coef(summary(fit)))
```
Yes there is strong evidence for many variables having non-linear associations.
In many cases, the addition of a cubic term is significant (`indus`, `nox`,
`age`, `dis`, `ptratio` and `medv`). In other cases although the cubic terms
is not significant, the squared term is (`zn`, `rm`, `rad`, `tax`, `lstat`).
In only one case, `black` is there no evidence for a non-linear relationship.