-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chapter3_Exercise.R
348 lines (257 loc) · 6.5 KB
/
Chapter3_Exercise.R
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
library(MASS)
library(ISLR)
Auto = read.csv('Data/Auto.csv' ,header=T, na.strings="?")
Auto = na.omit(Auto)
Auto = na.omit(Auto)
View(Auto)
attach(Auto)
# Exercise 8 (a)
lm.fit=lm(mpg~horsepower)
summary(lm.fit)
# Has an R squared of 60.59 % Both Intercept and horsepower is significant
# Also we see negative relationship b/w horsepower and mpg
# (iv) mpg = 39.93 - 0.157845 * 98 = 24.46119
predict(lm.fit, data.frame(horsepower=c(98)), interval="confidence")
predict(lm.fit, data.frame(horsepower=c(98)), interval="prediction")
# 8 (c)
plot(horsepower,mpg)
abline(lm.fit, lw = 3 , col = "red")
# 8 (d)
par(mfrow=c(2,2))
plot(lm.fit)
plot(predict(lm.fit), residuals(lm.fit))
plot(predict(lm.fit), rstudent(lm.fit))
# 9 (a)
par(mfrow=c(1,1))
pairs(Auto)
names(Auto)
cor(subset(Auto, select = -name))
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.
#
par(mfrow=c(2,2))
plot(lm.fit1)
plot(predict(lm.fit1), residuals(lm.fit1))
plot(predict(lm.fit1), rstudent(lm.fit1))
# Lot of possible outliers
lm.fit2 = lm(mpg ~ displacement + horsepower * displacement)
summary(lm.fit2)
lm.fit3 = lm(mpg~log(weight)+sqrt(horsepower)+acceleration+I(acceleration^2))
summary(lm.fit3)
par(mfrow = c(2,2))
plot(lm.fit3)
par(mfrow = c(1,1))
plot(predict(lm.fit3), rstudent(lm.fit3))
lm.fit3 = lm(log(mpg)~log(weight)+log(horsepower)+ log(acceleration) + log(weight))
summary(lm.fit3)
par(mfrow = c(2,2))
plot(lm.fit3)
plot(hatvalues(lm.fit3))
which.max(hatvalues(lm.fit3))
lm.fit4 = lm(log(mpg)~log(weight)+sqrt(horsepower)+acceleration+I(acceleration^2))
summary(lm.fit4)
par(mfrow = c(2,2))
plot(lm.fit4)
par(mfrow = c(1,1))
plot(predict(lm.fit4), rstudent(lm.fit4))
# log model follows a good regression model
# Exercise 10
summary(Carseats)
attach(Carseats)
lm.fit = lm(Sales~ Price + Urban + US)
summary(lm.fit)
# price is significant
# UrbanYes is not significant
# US yes is significant
# R^2 is very less
# Exercise 10 (e)
lm.fit2 = lm(Sales ~ Price + US)
summary(lm.fit2)
# Still R^2 is very less
confint(lm.fit2)
par(mfrow = c(2,2))
plot(lm.fit2)
plot(predict(lm.fit2), rstudent(lm.fit2))
# Exercise 11 (a)
set.seed(1)
x = rnorm(100)
y = 2 *x + rnorm(100)
lm.fit = lm(y~x + 0)
summary(lm.fit)
par(mfrow = c(2,2))
plot(lm.fit)
# Exercise 11 (b)
lm.fit2 = lm(x~y + 0)
summary(lm.fit2)
coef(lm.fit2)
# 11 (c)
# the t stat are same
# 11(c) --> numerically
t_stat = sqrt(99) * sum(x*y)/sqrt(sum(x*x)*sum(y*y) - sum(x*y)^2) # = 18.73
names(lm.fit2)
#11(f) -> can be shown both have t value = 18.73
# 12(a) --> When the sum of squares is same
# 12(b)
set.seed(1)
x = rnorm(100)
y = 3*x
lm.fit = lm( y ~ x)
summary(lm.fit)
lm.fit2 = lm( x ~ y)
summary(lm.fit2)
# 12 (c)
set.seed(1)
x = rbeta(100, shape1 = 1, shape2 = 1) # using beta distribution
y = -sample(x,100)
lm.fit = lm( x ~ y + 0)
lm.fit2 = lm(y ~ x + 0)
names(lm.fit)
coef(lm.fit)
coef(lm.fit2)
# we can see they have the same coefficient
# Exerccise 13(a)
set.seed(1)
x = rnorm(100)
eps = rnorm(100, sd =0.25)
# 13(c)
y = -1 + 0.5 *x + eps
# beta_0 = -1 , beta_1 = 0.5
# Exercise 13(d)
par(mfrow = c(1,1))
plot(x,y) # Linear relationship
lm.fit = lm( y ~x)
summary(lm.fit)
coef(lm.fit)
# (Intercept) x
# -1.0094232 0.4997349
plot(x,y)
abline(lm.fit,lwd=3)
abline(lm.fit,lwd=3,col="red")
abline(-1, 0.5, lwd=3, col="green")
legend(-1, legend = c("model fit", "pop. regression"), col=2:3, lwd=3)
lm.fit_sq = lm( y~x + I(x^2))
summary(lm.fit_sq) # More R squared p value suggests no relationship
# Repat with less variance
# 13(h)
set.seed(1)
x = rnorm(100)
eps = rnorm(100, sd =0.05)
y = -1 + 0.5 *x + eps
# beta_0 = -1 , beta_1 = 0.5
par(mfrow = c(1,1))
plot(x,y) # Linear relationship
lm.fit1 = lm( y ~x)
summary(lm.fit1)
coef(lm.fit1)
# (Intercept) x
# -1.0094232 0.4997349
plot(x,y)
abline(lm.fit1,lwd=3)
abline(lm.fit1,lwd=3,col="red")
abline(-1, 0.5, lwd=3, col="green")
legend(-1, legend = c("model fit", "pop. regression"), col=2:3, lwd=3)
# 13(i)
set.seed(1)
x = rnorm(100)
eps = rnorm(100, sd =0.5)
y = -1 + 0.5 *x + eps
# beta_0 = -1 , beta_1 = 0.5
par(mfrow = c(1,1))
plot(x,y) # Linear relationship
lm.fit2 = lm( y ~x)
summary(lm.fit2)
coef(lm.fit2)
# (Intercept) x
# -1.0094232 0.4997349
plot(x,y)
abline(lm.fit1,lwd=3)
abline(lm.fit1,lwd=3,col="red")
abline(-1, 0.5, lwd=3, col="green")
legend(-1, legend = c("model fit", "pop. regression"), col=2:3, lwd=3)
# clearly R squared decreases
confint(lm.fit)
confint(lm.fit1)
confint(lm.fit2)
# Narrower confidence interval less variance
# Exercise (14)
set.seed(1)
x1 = runif(100)
x2 = 0.5 * x1 + rnorm(100)/10
y = 2+ 2* x1 + 0.3 * x2 + rnorm(100)
# y = 2.5 * x1 + 2 + eps
cor(x1,x2)
par(mfrow = c(1,1))
plot(x1,x2)
lm.fit = lm(y ~ x1 + x2)
summary(lm.fit)
# cannot reject beta_2 = 0 high p value
lm.fit1 = lm(y ~ x1)
summary(lm.fit1)
library(car)
vif(lm.fit)
lm.fit2 = lm( y ~ x2)
summary(lm.fit2)
# 14(f) --> No
# 14(e)
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)
par(mfrow=c(2,2))
plot(lm.fit1)
par(mfrow=c(2,2))
plot(lm.fit2)
par(mfrow=c(2,2))
plot(lm.fit3)
plot(predict(lm.fit1), rstudent(lm.fit1))
plot(predict(lm.fit2), rstudent(lm.fit2))
plot(predict(lm.fit3), rstudent(lm.fit3))
# Exercise 15
library(MASS)
Boston$chas <- factor(Boston$chas, labels = c("N","Y"))
View(Boston)
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
# 15 (b)
lm.all = lm(crim~., data=Boston)
summary(lm.all)