-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchap3.Rmd
430 lines (234 loc) · 8.08 KB
/
chap3.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
---
title: 'Statistical Learning: Chapter 3 Applied Exercise'
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r, message=FALSE, warning=FALSE}
library(dplyr)
library(tidyverse)
library(ggplot2)
library(ggthemes)
```
## 8. This question involves the use of simple linear regression on the `Auto` data set.
```{r, message=FALSE, warning=FALSE}
setwd(dir = "~/Desktop/Statistical Learning/dataset")
## read the data set :
Auto = read.csv("Auto.csv",
stringsAsFactors = FALSE,
na.strings = "?")
str(Auto)
Auto = Auto[complete.cases(Auto),]
dim(Auto)
```
**Part Use the `lm()` to peform the simple linear regression with `mpg` response and horsepower as the predictor**
```{r}
str(Auto)
lm.fit = lm(formula = mpg~horsepower, data = Auto)
summary(lm.fit)
```
* There is a relationship between the predictor and the response.
```{r}
cor(x = Auto$horsepower, y = Auto$mpg, method = c("pearson"))
```
* The relationship is quite negatively strong.
* The predicted `mpg` associated with a `horsepower` of 98. The associated 95% confidence and prediction intervals.
```{r}
## 95% Confidence interval:
predict(object = lm.fit, data.frame(horsepower = c(98)),
interval = "confidence")
## 95% prediction interval:
predict(object = lm.fit, data.frame(horsepower = c(98)),
interval = "prediction")
```
* So the predicted `mpg` is $24.46708$ associated with `horsepower` of 98.
* The 95% confidence interval is $(23.97308,24.96108)$. The 95% prediction interval is $(14.8094, 34.12476)$.
**Part(b)**
```{r}
intercept.lmfit = as.numeric(lm.fit$coefficients[1])
slope.lmfit = as.numeric(lm.fit$coefficients[2])
scatterplot = ggplot(data = Auto, aes(x = horsepower, y = mpg))+
geom_point(color = "blue") +
xlab(label = "Horsepower") +
ylab(label = "mpg") +
ggtitle(label = "Horsepower vs. Gas Mileage") +
theme_base() + geom_abline(slope = slope.lmfit,
intercept = intercept.lmfit,
color = "red",
size = 1.5) +
geom_smooth(color = "green",
size = 1,
linetype = "dashed",
method = 'loess')
print(scatterplot)
```
```{r, fig.height=10, fig.width=12}
par(mfrow = c(2,2))
plot(lm.fit, pch = "x", col = "navy")
```
* There is a problem of non-constance variance assumption. The residual vs. fitted values suggest the heteroscadascity of variance.
## 11. To begin we generate the predictor `x` and a response `y` as follows:
```{r}
set.seed(1)
x = rnorm(n = 100)
y = 2*x + rnorm(100)
```
**Part (a)**
```{r}
### without interception:
lm.fit = lm(y ~ x + 0)
summary(lm.fit)
```
* The coefficient estimate $\hat \beta = 1.9939$, the standard error is 0.1065, t-value is 18.73 and p-value is extremely small as it is less than $2 \times 10^{-16}$.
**Part (b)**
* We perfom the regression of x onto y without an intercept, and report the estimated coefficient, SE, t-statistic and p-values.
```{r}
lm.fit2 = lm(x ~ y + 0)
summary(lm.fit2)
```
* The estimated coefficient is $0.39111$, $SE = 0.02089$, $t-value = 18.73$ and the p-value is extremely small less than $2\times 10^{-16}$.
## Exercise 13: Simulating the data
**Part (a)**
* Using `rnorm()` create the vector `x` containing 100 observations from a Normal(0,1) distribution. This represents feature X.
```{r}
set.seed(1)
X = rnorm(n = 100, mean = 0, sd = 1)
```
**Part (b)**
* Create the vector `eps` containing 100 observations from a Normal(0,0.25) with mean zero and variance 0.25.
```{r}
set.seed(1)
eps = rnorm(n = 100,
mean = 0,
sd = sqrt(0.25))
```
**Part (c)**
* Generate the vector `y` according to the model:
$$
Y = -1 + 0.5X + \epsilon
$$
```{r}
set.seed(1)
Y = -1 + 0.5*X + eps
Dataset = cbind(X, Y, eps)
Dataset = as.data.frame(Dataset)
```
* The length of vector `y` is 100. The value of $\beta_0$ is $-1$, and $\beta_1$ is $0.5$.
**Part (d)**
* The scatterplot displays the relationship between `x` and `y`.
```{r}
plot1 = ggplot(data = Dataset, aes(x = X, y = Y)) +
geom_point(pch = 4, color = "navy") +
ggtitle(label = "Scatterplot of X vs. Y") +
xlab(label = "Feature X") +
ylab(label = "Feature Y") +
theme_bw()
print(plot1)
```
**Part (e)**
* We fit a least squares linear model to predict y using x:
```{r, message=FALSE, warning=FALSE}
lm.fit = lm(Y ~ X , data = Dataset)
summary(lm.fit)
```
**Part (f)**
```{r}
plot2 = ggplot(data = Dataset, aes(x = X, y = Y)) +
geom_point(pch = 4, color = "navy") +
ggtitle(label = "Scatterplot of X vs. Y") +
xlab(label = "Feature X") +
ylab(label = "Feature Y") +
theme_bw() +
geom_smooth(method = "lm", color = "red",
size = 0.25, lty = 1)
print(plot2)
```
**Part (g)**
* We fit a polynomial regression predicting $y$ using $x$ and $x^2$:
```{r}
poly.fit = lm(Y ~ X + I(X^2), data = Dataset)
summary(poly.fit)
```
* There is no evidence that the quadratic term improves the model fit because the p-value associated with quadratic term is large as $p = 0.898$.
## Exercise 14: This problem involves on `collinearity` problem:
**Part (a)**
```{r}
set.seed(1)
x1 = runif(n = 100)
x2 = 0.5*x1 + rnorm(100)/10
y = 2 + 2*x1 + 0.3*x2 + rnorm(100)
DataSet = cbind(x1,x2,y)
DataSet = as.data.frame(DataSet)
```
* The form of the linear model:
$$
Y = 2 + 2X_{1} + 0.3X_{2} + \epsilon
$$
The regression coefficients:
$$
\beta_0 = 2; \quad
\beta_{1,1} = 2; \quad
\beta_{1,2} = 0.3
$$
**Part(b)**
* The correlation between `x1` and `x2`:
```{r}
Cor.X1.X2 = cor(x = DataSet$x1, y = DataSet$x2)
print(list(
Cor.X1.X2 = Cor.X1.X2
))
```
* The scatterplot between the variables:
```{r, warning=FALSE, message=FALSE, fig.align='left', fig.width=10, fig.height=8}
library(gridExtra)
Plot1 = ggplot(data = DataSet,
aes(x = x1, y = x2)) +
geom_point(pch = 2, color = "black", size = 2) +
ggtitle(label = "Scatteprlot X1 vs. X2") +
xlab(label = "Feature X1") +
ylab(label = "Feature X2") +
theme_bw()
Plot2 = ggplot(data = DataSet,
aes(x = x1, y = y)) +
geom_point(pch = 13, color = "orange", size = 2) +
ggtitle(label = "Scatteprlot X1 vs. Y") +
xlab(label = "Feature X1") +
ylab(label = "Feature Y") +
theme_bw()
Plot3 = ggplot(data = DataSet,
aes(x = x2, y = y)) +
geom_point(pch = 10, color = "red", size = 2) +
ggtitle(label = "Scatteprlot X2 vs. Y") +
xlab(label = "Feature X2") +
ylab(label = "Feature Y") +
theme_bw()
grid.arrange(Plot1,
Plot2,
Plot3,ncol = 2)
```
**Part (c)**
* We fit a least squares regression to predict y using x1 and x2:
```{r}
lm.fit = lm(y ~ x1 + x2, data = DataSet)
summary(lm.fit)
```
* The estimated coefficients are:
* $\hat \beta_0 = 2.1305$.
* $\hat \beta_1 = 1.4396$.
* $\hat \beta_2 = 1.0097$
* We can reject the null hypothesis for $\beta_1 = 0$ because the p-value is $0.0487$ which shows there is some evidence to accept the alternative hypothesis. We cannot reject the null hypothesis for $\beta_2 = 0$ because the p-value is $0.3754$ which is a large p value.
**Part (d)**
* We fit a linear regression to predict y using x1 only
```{r}
lm.fit2 = lm(y ~ x1, data = DataSet)
summary(lm.fit2)
```
* We have strong evidence to reject the null hypothesis for $\beta_1 =0$ because the p-value is very small.
**Part (e)**
* We fit a linear regression to predict y using x2 only
```{r}
lm.fit3 = lm(y ~ x2, data = DataSet)
summary(lm.fit3)
```
* We have strong evidence to reject the null hypothesis for $\beta_2 =0$ because the p-value is very small.