-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
applied.Rmd
648 lines (567 loc) · 15 KB
/
applied.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
Chapter 3: Linear Regression: Exercises: Applied
================================================
8a.
---
```{r}
Auto = read.csv("../data/Auto.csv", header=T, na.strings="?")
Auto = na.omit(Auto)
summary(Auto)
attach(Auto)
lm.fit = lm(mpg ~ horsepower)
summary(lm.fit)
```
### i.
Yes, there is a relationship between horsepower and mpg as deterined by
testing the null hypothesis of all regression coefficients equal to zero.
Since the F-statistic is far larger than 1 and the p-value of the F-statistic
is close to zero we can reject the null hypothesis and state there is a
statistically significant relationship between horsepower and mpg.
### ii.
To calculate the residual error relative to the response we use the mean
of the response and the RSE. The mean of mpg is ``r mean(mpg, na.rm=T)``.
The RSE of the lm.fit was ``r 4.906`` which indicates a percentage error of
``r 4.906/mean(mpg, na.rm=T) * 100.0``%. The $R^2$ of the lm.fit was
about ``r summary(lm.fit)$r.sq``, meaning ``r summary(lm.fit)$r.sq * 100.0``%
of the variance in mpg is explained by horsepower.
### iii.
The relationship between mpg and horsepower is negative. The more horsepower
an automobile has the linear regression indicates the less mpg fuel efficiency
the automobile will have.
### iv.
```{r}
predict(lm.fit, data.frame(horsepower=c(98)), interval="confidence")
predict(lm.fit, data.frame(horsepower=c(98)), interval="prediction")
```
8b.
---
```{r}
plot(horsepower, mpg)
abline(lm.fit)
```
8c.
---
```{r}
par(mfrow=c(2,2))
plot(lm.fit)
```
Based on the residuals plots, there is some evidence of non-linearity.
9a.
---
```{r}
pairs(Auto)
```
9b.
---
```{r}
cor(subset(Auto, select=-name))
```
9c.
---
```{r}
lm.fit1 = lm(mpg~.-name, data=Auto)
summary(lm.fit1)
```
### i.
Yes, there is a relatioship between the predictors and the response by testing
the null hypothesis of whether all the regression coefficients are zero. The F
-statistic is far from 1 (with a small p-value), indicating evidence against
the null hypothesis.
### ii.
Looking at the p-values associated with each predictor's t-statistic, we see
that displacement, weight, year, and origin have a statistically significant relationship, while cylinders, horsepower, and acceleration do not.
### iii.
The regression coefficient for year, ``r coefficients(lm.fit1)["year"]``,
suggests that for every one year, mpg increases by the coefficient. In other
words, cars become more fuel efficient every year by almost 1 mpg / year.
9d.
---
```{r}
par(mfrow=c(2,2))
plot(lm.fit1)
```
The fit does not appear to be accurate because there is a discernible curve
pattern to the residuals plots. From the leverage plot, point 14 appears to have
high leverage, although not a high magnitude residual.
```{r}
plot(predict(lm.fit1), rstudent(lm.fit1))
```
There are possible outliers as seen in the plot of studentized residuals
because there are data with a value greater than 3.
9e.
---
```{r}
lm.fit2 = lm(mpg~cylinders*displacement+displacement*weight)
summary(lm.fit2)
```
From the correlation matrix, I obtained the two highest correlated pairs and
used them in picking my interaction effects. From the p-values, we can see
that the interaction between displacement and weight is statistically
signifcant, while the interactiion between cylinders and displacement is not.
9f.
---
```{r}
lm.fit3 = lm(mpg~log(weight)+sqrt(horsepower)+acceleration+I(acceleration^2))
summary(lm.fit3)
par(mfrow=c(2,2))
plot(lm.fit3)
```
```{r}
plot(predict(lm.fit3), rstudent(lm.fit3))
```
Apparently, from the p-values, the log(weight), sqrt(horsepower), and
acceleration^2 all have statistical significance of some sort. The residuals
plot has less of a discernible pattern than the plot of all linear regression
terms. The studentized residuals displays potential outliers (>3). The
leverage plot indicates more than three points with high leverage.
However, 2 problems are observed from the above plots:
1) the residuals vs fitted plot indicates heteroskedasticity (unconstant
variance over mean) in the model.
2) The Q-Q plot indicates somewhat unnormality of the residuals.
So, a better transformation need to be applied to our model.
From the correlation matrix in 9a., displacement, horsepower and weight show a
similar nonlinear pattern against our response mpg.
This nonlinear pattern is very close to a log form. So in the next attempt, we
use `log(mpg)` as our response variable.
The outputs show that log transform of mpg yield better model fitting
(better R^2, normality of residuals).
```{r}
lm.fit2<-lm(log(mpg)~cylinders+displacement+horsepower+weight+acceleration+year+origin,data=Auto)
summary(lm.fit2)
```
```{r}
par(mfrow=c(2,2))
plot(lm.fit2)
plot(predict(lm.fit2),rstudent(lm.fit2))
```
10a.
----
```{r}
library(ISLR)
summary(Carseats)
attach(Carseats)
lm.fit = lm(Sales~Price+Urban+US)
summary(lm.fit)
```
10b.
----
### Price
The linear regression suggests a relationship between price and sales given the
low p-value of the t-statistic. The coefficient states a negative
relationship between Price and Sales: as Price increases, Sales decreases.
### UrbanYes
The linear regression suggests that there isn't a relationship between the
location of the store and the number of sales based on the high p-value of the
t-statistic.
### USYes
The linear regression suggests there is a relationship between whether the store
is in the US or not and the amount of sales. The coefficient states a positive relationship between USYes and Sales: if the store is in the US, the sales will
increase by approximately 1201 units.
10c.
----
Sales = 13.04 + -0.05 Price + -0.02 UrbanYes + 1.20 USYes
10d.
----
Price and USYes, based on the p-values, F-statistic, and p-value of the
F-statistic.
10e.
----
```{r}
lm.fit2 = lm(Sales ~ Price + US)
summary(lm.fit2)
```
10f.
----
Based on the RSE and R^2 of the linear regressions, they both fit the data
similarly, with linear regression from (e) fitting the data slightly better.
10g.
----
```{r}
confint(lm.fit2)
```
10h.
----
```{r}
plot(predict(lm.fit2), rstudent(lm.fit2))
```
All studentized residuals appear to be bounded by -3 to 3, so not potential
outliers are suggested from the linear regression.
```{r}
par(mfrow=c(2,2))
plot(lm.fit2)
```
There are a few observations that greatly exceed $(p+1)/n$ (``r 3/397``) on the leverage-statistic plot that suggest that the corresponding points have high leverage.
11.
---
```{r}
set.seed(1)
x = rnorm(100)
y = 2*x + rnorm(100)
```
11a.
----
```{r}
lm.fit = lm(y~x+0)
summary(lm.fit)
```
The p-value of the t-statistic is near zero so the null hypothesis is rejected.
11b.
----
```{r}
lm.fit = lm(x~y+0)
summary(lm.fit)
```
The p-value of the t-statistic is near zero so the null hypothesis is rejected.
11c.
----
Both results in (a) and (b) reflect the same line created in 11a. In other
words, $y = 2x + \epsilon$ could also be written $x = 0.5 * (y - \epsilon)$.
11d.
----
$$
\begin{array}{cc}
t = \beta / SE(\beta) &
\beta = \frac {\sum{x_i y_i}} {\sum{x_i^2}} &
SE(\beta) = \sqrt{\frac {\sum{(y_i - x_i \beta)^2}} {(n-1) \sum{x_i^2}}}
\end{array}
\\
t = {\frac {\sum{x_i y_i}} {\sum{x_i^2}}}
{\sqrt{\frac {(n-1) \sum{x_i^2}} {\sum{(y_i - x_i \beta)^2}}}}
\\
\frac {\sqrt{n-1} \sum{x_i y_i}}
{\sqrt{\sum{x_i^2} \sum{(y_i - x_i \beta)^2}}}
\\
\frac {\sqrt{n-1} \sum{x_i y_i}}
{\sqrt{\sum{x_i^2} \sum{(y_i^2 - 2 \beta x_i y_i + x_i^2 \beta^2)}}}
\\
\frac {\sqrt{n-1} \sum{x_i y_i}}
{\sqrt{\sum{x_i^2} \sum{y_i^2} -
\sum{x_i^2} \beta (2 \sum{x_i y_i} - \beta \sum{x_i^2})}}
\\
\frac {\sqrt{n-1} \sum{x_i y_i}}
{\sqrt{\sum{x_i^2} \sum{y_i^2} -
\sum{x_i y_i} (2 \sum{x_i y_i} - \sum{x_i y_i})}}
\\
t = \frac {\sqrt{n-1} \sum{x_i y_i}}
{\sqrt{\sum{x_i^2} \sum{y_i^2} - (\sum{x_i y_i})^2 }}
$$
```{r}
(sqrt(length(x)-1) * sum(x*y)) / (sqrt(sum(x*x) * sum(y*y) - (sum(x*y))^2))
```
This is same as the t-statistic shown above.
11e.
----
If you swap t(x,y) as t(y,x), then you will find t(x,y) = t(y,x).
11f.
----
```{r}
lm.fit = lm(y~x)
lm.fit2 = lm(x~y)
summary(lm.fit)
summary(lm.fit2)
```
You can see the t-statistic is the same for the two linear regressions.
12a.
----
When the sum of the squares of the observed y-values are equal to the sum of the
squares of the observed x-values.
12b.
----
```{r}
set.seed(1)
x = rnorm(100)
y = 2*x
lm.fit = lm(y~x+0)
lm.fit2 = lm(x~y+0)
summary(lm.fit)
summary(lm.fit2)
```
The regression coefficients are different for each linear regression.
12c.
----
```{r}
set.seed(1)
x <- rnorm(100)
y <- -sample(x, 100)
sum(x^2)
sum(y^2)
lm.fit <- lm(y~x+0)
lm.fit2 <- lm(x~y+0)
summary(lm.fit)
summary(lm.fit2)
```
The regression coefficients are the same for each linear regression.
So long as sum sum(x^2) = sum(y^2) the condition in 12a. will be satisfied.
Here we have simply taken all the $x_i$ in a different order and made them
negative.
13a.
----
```{r}
set.seed(1)
x = rnorm(100)
```
13b.
----
```{r}
eps = rnorm(100, 0, sqrt(0.25))
```
13c.
----
```{r}
y = -1 + 0.5*x + eps
```
y is of length 100. $\beta_0$ is -1, $\beta_1$ is 0.5.
13d.
----
```{r}
plot(x, y)
```
I observe a linear relationship between x and y with a positive slope, with a
variance as is to be expected.
13e.
----
```{r}
lm.fit = lm(y~x)
summary(lm.fit)
```
The linear regression fits a model close to the true value of the coefficients
as was constructed. The model has a large F-statistic with a near-zero p-value
so the null hypothesis can be rejected.
13f.
----
```{r}
plot(x, y)
abline(lm.fit, lwd=3, col=2)
abline(-1, 0.5, lwd=3, col=3)
legend(-1, legend = c("model fit", "pop. regression"), col=2:3, lwd=3)
```
13g.
----
```{r}
lm.fit_sq = lm(y~x+I(x^2))
summary(lm.fit_sq)
```
There is evidence that model fit has increased over the training data given the
slight increase in $R^2$ and $RSE$. Although, the p-value of the t-statistic
suggests that there isn't a relationship between y and $x^2$.
13h.
----
```{r}
set.seed(1)
eps1 = rnorm(100, 0, 0.125)
x1 = rnorm(100)
y1 = -1 + 0.5*x1 + eps1
plot(x1, y1)
lm.fit1 = lm(y1~x1)
summary(lm.fit1)
abline(lm.fit1, lwd=3, col=2)
abline(-1, 0.5, lwd=3, col=3)
legend(-1, legend = c("model fit", "pop. regression"), col=2:3, lwd=3)
```
As expected, the error observed in $R^2$ and $RSE$ decreases considerably.
13i.
----
```{r}
set.seed(1)
eps2 = rnorm(100, 0, 0.5)
x2 = rnorm(100)
y2 = -1 + 0.5*x2 + eps2
plot(x2, y2)
lm.fit2 = lm(y2~x2)
summary(lm.fit2)
abline(lm.fit2, lwd=3, col=2)
abline(-1, 0.5, lwd=3, col=3)
legend(-1, legend = c("model fit", "pop. regression"), col=2:3, lwd=3)
```
As expected, the error observed in $R^2$ and $RSE$ increases considerably.
13j.
----
```{r}
confint(lm.fit)
confint(lm.fit1)
confint(lm.fit2)
```
All intervals seem to be centered on approximately 0.5, with the second fit's
interval being narrower than the first fit's interval and the last fit's
interval being wider than the first fit's interval.
14a.
----
```{r}
set.seed(1)
x1 = runif(100)
x2 = 0.5 * x1 + rnorm(100)/10
y = 2 + 2*x1 + 0.3*x2 + rnorm(100)
```
$$
Y = 2 + 2 X_1 + 0.3 X_2 + \epsilon \\
\beta_0 = 2, \beta_1 = 2, \beta_3 = 0.3
$$
14b.
----
```{r}
cor(x1, x2)
plot(x1, x2)
```
14c.
----
```{r}
lm.fit = lm(y~x1+x2)
summary(lm.fit)
```
$$\beta_0 = 2.0533, \beta_1 = 1.6336, \beta_3 = 0.5588$$
The regression coefficients are close to the true coefficients, although with
high standard error. We can reject the null hypothesis for $\beta_1$ because
its p-value is below 5%. We cannot reject the null hypothesis for $\beta_2$
because its p-value is much above the 5% typical cutoff, over 60%.
14d.
----
```{r}
lm.fit = lm(y~x1)
summary(lm.fit)
```
Yes, we can reject the null hypothesis for the regression coefficient given the
p-value for its t-statistic is near zero.
14e.
----
```{r}
lm.fit = lm(y~x2)
summary(lm.fit)
```
Yes, we can reject the null hypothesis for the regression coefficient given the
p-value for its t-statistic is near zero.
14f.
----
No, because x1 and x2 have collinearity, it is hard to distinguish their effects
when regressed upon together. When they are regressed upon separately, the
linear relationship between y and each predictor is indicated more clearly.
14g.
----
```{r}
x1 = c(x1, 0.1)
x2 = c(x2, 0.8)
y = c(y, 6)
lm.fit1 = lm(y~x1+x2)
summary(lm.fit1)
lm.fit2 = lm(y~x1)
summary(lm.fit2)
lm.fit3 = lm(y~x2)
summary(lm.fit3)
```
In the first model, it shifts x1 to statistically insignificance and shifts
x2 to statistiscal significance from the change in p-values between the two
linear regressions.
```{r}
par(mfrow=c(2,2))
plot(lm.fit1)
```
```{r}
par(mfrow=c(2,2))
plot(lm.fit2)
```
```{r}
par(mfrow=c(2,2))
plot(lm.fit3)
```
In the first and third models, the point becomes a high leverage point.
```{r}
plot(predict(lm.fit1), rstudent(lm.fit1))
plot(predict(lm.fit2), rstudent(lm.fit2))
plot(predict(lm.fit3), rstudent(lm.fit3))
```
Looking at the studentized residuals, we don't observe points too far from the
|3| value cutoff, except for the second linear regression: y ~ x1.
15a.
----
```{r}
library(MASS)
summary(Boston)
Boston$chas <- factor(Boston$chas, labels = c("N","Y"))
summary(Boston)
attach(Boston)
lm.zn = lm(crim~zn)
summary(lm.zn) # yes
lm.indus = lm(crim~indus)
summary(lm.indus) # yes
lm.chas = lm(crim~chas)
summary(lm.chas) # no
lm.nox = lm(crim~nox)
summary(lm.nox) # yes
lm.rm = lm(crim~rm)
summary(lm.rm) # yes
lm.age = lm(crim~age)
summary(lm.age) # yes
lm.dis = lm(crim~dis)
summary(lm.dis) # yes
lm.rad = lm(crim~rad)
summary(lm.rad) # yes
lm.tax = lm(crim~tax)
summary(lm.tax) # yes
lm.ptratio = lm(crim~ptratio)
summary(lm.ptratio) # yes
lm.black = lm(crim~black)
summary(lm.black) # yes
lm.lstat = lm(crim~lstat)
summary(lm.lstat) # yes
lm.medv = lm(crim~medv)
summary(lm.medv) # yes
```
All, except chas. Plot each linear regression using "plot(lm)" to see
residuals.
15b.
----
```{r}
lm.all = lm(crim~., data=Boston)
summary(lm.all)
```
zn, dis, rad, black, medv
15c.
----
```{r}
x = c(coefficients(lm.zn)[2],
coefficients(lm.indus)[2],
coefficients(lm.chas)[2],
coefficients(lm.nox)[2],
coefficients(lm.rm)[2],
coefficients(lm.age)[2],
coefficients(lm.dis)[2],
coefficients(lm.rad)[2],
coefficients(lm.tax)[2],
coefficients(lm.ptratio)[2],
coefficients(lm.black)[2],
coefficients(lm.lstat)[2],
coefficients(lm.medv)[2])
y = coefficients(lm.all)[2:14]
plot(x, y)
```
Coefficient for nox is approximately -10 in univariate model and 31 in
multiple regression model.
15d.
----
```{r}
lm.zn = lm(crim~poly(zn,3))
summary(lm.zn) # 1, 2
lm.indus = lm(crim~poly(indus,3))
summary(lm.indus) # 1, 2, 3
# lm.chas = lm(crim~poly(chas,3)) : qualitative predictor
lm.nox = lm(crim~poly(nox,3))
summary(lm.nox) # 1, 2, 3
lm.rm = lm(crim~poly(rm,3))
summary(lm.rm) # 1, 2
lm.age = lm(crim~poly(age,3))
summary(lm.age) # 1, 2, 3
lm.dis = lm(crim~poly(dis,3))
summary(lm.dis) # 1, 2, 3
lm.rad = lm(crim~poly(rad,3))
summary(lm.rad) # 1, 2
lm.tax = lm(crim~poly(tax,3))
summary(lm.tax) # 1, 2
lm.ptratio = lm(crim~poly(ptratio,3))
summary(lm.ptratio) # 1, 2, 3
lm.black = lm(crim~poly(black,3))
summary(lm.black) # 1
lm.lstat = lm(crim~poly(lstat,3))
summary(lm.lstat) # 1, 2
lm.medv = lm(crim~poly(medv,3))
summary(lm.medv) # 1, 2, 3
```
See inline comments above, the answer is yes for most, except for black and
chas.