-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shaoran Sun 5120 final project.Rmd
592 lines (461 loc) · 16.8 KB
/
Shaoran Sun 5120 final project.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
---
title: "STAT 5120 Final Project"
author: "Shaoran Sun"
date: "May 8, 2016"
output:
pdf_document:
keep_tex: yes
number_sections: yes
toc: yes
---
\newpage
#Introduction
This report is to build and select a model for the response y, using the generated data set "dat.cvs" given by the course instructor. Most of the tools studied in course STAT 5120 (Linear Regression) will be used to facilitate model selection, model estimation, model validation, and model diagnosis procedures. This report is written by Shaoran Sun individually.
#Materials and methods
Data is given by the instructor. Data contains one response variable, $y$, and 10 predictors, $x_1, x_2, \dots, x_{10}$.
There are 100 intances of data, in total of 100 rows. See the data on [Appendix I](#data)
The analysis method used are:
* Log Transformation
* Detecting Outliers in Predictors with leverages
* Detecting Infuential Observations with DFFITS and Cook's Distance
* Automated Forward Selection Procedure
* Diagnosing Multicollinearity with Variance Inflation Factor (VIF)
* General Linear F Test
The criteria for statistical significance is $P<0.05$.
#Results
With the given data set "dat.cvs",
* y is log-transformed
* Entry 10 is reomoved due to large influence to the overall data
* Predictors x1, x6, x7, x8, x9, and x10 are $removed$ due to insignificance.
* Predictors x2, x3, x4,and x5 are kept, and combined as a first order model.
The final model is: $$\mathbf{y=2.00 \cdot x_2+0.91\cdot x_3 +0.40 \cdot x_4 + 3.35\cdot x_5 +1.51}$$
\includegraphics[height=5cm]{S.png}
The intercept, x2, and x5 all have $p$-values of $(< 2e-16)$, x3 has $p$-value of $2.46e-14$, and x4 has $p$-value of $2.01e-05$. The overall $p$-value is $(< 2.2e-16)$. These $p$-values are all less than 0.05 and significant.
The $R^2$ is 0.955, which means the model represents 95.5% of the data.
#Detailed Procedures
## Read in data and fit the originial data to a model
I first plot in all 10 variables in responsing to y. All the $p$-values to the variables are greater than 0.05, which indicates that none of the variable is significant.
The 5 assumptions about linear regression are:
1. There exists a linear relation between the response and predictor variable(s).
2. The error terms have the constant variance
3. The error terms are independent, have mean 0.
4. Model fits all observations well (no outliers).
5. The errors follow a Normal distribution.
The Residuals vs Fitted graph suggested that residuals do not fall in a horizontal band around 0, and they have an apparent pattern. Assumption 1 and 3 are not met.
The residuals also do not have similar vertical variation across fits. Assumption 2 is not met.
Hence, we consider using a boxcox transformation to test for log-likelihood.
```{r,echo=FALSE}
dat <- read.csv("~/Desktop/5120 Final/dat.csv")
attach(dat)
dat.lm=lm(dat$y~.,data=dat)
summary(dat.lm)
par(mfrow=c(2,2))
plot(dat.lm)
```
##Boxcox transformation
As we can see from the boxcox graph, $\lambda$ is very close to 0. So we consider a log-transformation.
```{r,echo=FALSE}
par(mfrow=c(1,1))
library(MASS)
boxcox(dat.lm, plotit = TRUE)
```
##Fit the log-transformed y
After the transformation, some of the predictors are starting to become significant, namely, x3, x5, and intercept. The overall $p$-value also becomes very significant, comparing to before the transformaiton, where $p$-value = 0.36.
```{r,echo=FALSE,warning=FALSE, message=FALSE}
dat$y=log(dat$y)
dat.lm=lm(dat$y~.,data=dat)
summary(dat.lm)
par(mfrow=c(2,2))
plot(dat.lm)
attach(dat)
```
##Detect and remove outliers
Next, I will detect outliers.
From the above graphs, we can see that outliers definitely exist, possibly, data 10.
Obtain leverages and two measures that can be used to identify influential points, DFFITS (difference in fts) and Cook's Distance.
```{r,echo=FALSE, include=FALSE}
dat.lm$residuals
tmp<-lm.influence(dat.lm)
tmp$hat[tmp$hat>2*10/100]
DFFITS<-dffits(dat.lm)
DFFITS[DFFITS>1]
dat[DFFITS>1,]
COOKS<-cooks.distance(dat.lm)
round(COOKS,3)
COOKS>qf(0.5,10,90)
```
From the output of influence, DFFITS (difference in fts), and Cook's Distance, we can see that observation 10 is indeed an influential outlier. Hence we remove it.
After removing observation 10, the model fits the data better than before, as we can see in the following output table. Next, we will be choosing which predictors are actually significant.
```{r,echo=FALSE}
no10<- dat[-10, ]
no10.lm=lm(no10$y~.,data=no10)
summary(no10.lm)
```
##Check for multicolinearity issue
```{r,echo=FALSE}
round(cor(no10),3)
library(faraway)
vif(no10.lm)
```
We can see that x1 and x2 are highly correlated with 0.999 correlation, and 515 VIFs, which is way greater than 10, the threshold. We will next apply automated search to search for significant predictors. If x1 and x2 are both in the result, we will remove one of them in the final predictors.
##Use automated search
Now I use automated search, after removing data 10 from the data set.
```{r,echo=FALSE}
data=no10
start<-lm(y~1, data=data)
end<-lm(y~.,data=data)
result<-step(start, scope=list(lower=start, upper=end), direction="forward", trace=FALSE)
summary(result)
```
Using automated forward search, we get x2, x3, x4, and x5 are chosen, among which all are significant with $p$-values way less than 0.05. The over all fitting of the 4 predictors results in a $p$-value of < 2.2e-16. This proves that after log-transformation, removing data 10 and automated forward selection, the model fits better. x1 and x2 are not both in the result, so there is no multicolinearity issue.
##Model Comparison and Selection
Next, we will check use techniques in Model Comparison and Selection, by comparing R2.adj, PRESS, AIC, BIC, and Cp. We will be using data after removing outlier and log-transformation.
```{r,echo=FALSE,warning=FALSE,include=FALSE}
dat=data
attach(dat)
##################################################
##STEP 1: create design matrix for maximal model##
##################################################
x<-cbind(Const=1, x1, x2,x3,x4,x5,x6,x7,x8,x9,x10)
y<-y
###########################################################################################
##STEP 2: logical matrix to indicate for each possible model, which predictors are in/out##
###########################################################################################
models<-matrix(F, 2^10, 10) ##10 predictors, change accordingly
dimnames(models)<-list(NULL, c("x1","x2","x3","x4","x5","x6","x7","x8","x9","x10"))
row<-0
for (a in c(F,T)) { ##loop has 10 for statements, one for each predictor
for (b in c(F,T)) {
for (c in c(F,T)) {
for (d in c(F,T)) {
for (e in c(F,T)) {
for (f in c(F,T)) {
for (g in c(F,T)) {
for (h in c(F,T)) {
for (i in c(F,T)) {
for (j in c(F,T)) {
row<-row+1
models[row,]<-c(a,b,c,d,e,f,g,h,i,j)
}}}}}}}}}}
#################################################
##STEP 3: Matrix to store results from criteria##
#################################################
results<-matrix(NA, 2^10, 7) ##10 predictors, store 7 statistics
dimnames(results)<-list(NULL, c("p","R2","R2.adj","PRESS","AIC","BIC","Cp"))
#################################
##STEP 4: MSE for maximal model##
#################################
tmp<-lsfit(x,y,intercept=F)
n<-nrow(dat)
p<-ncol(x)
MSE.max<-sum(tmp$res^2)/(n-p)
###################################
##STEP 5: Fit all possible models##
###################################
time1<-Sys.time()
for (i in 1:(2^10)){ ##10 predictors
which<-c(T, models[i,]) ##pull out the row of model and append an intercept
tmp<-lsfit(x[,which], y, intercept=F) ##fit the model and compute the criteria
p<-sum(which) ##number of parameters for chosen model
SSTo<-(n-1)*var(y)
MSTo<-var(y)
SSE<-sum(tmp$res^2)
MSE<-SSE/(n-p)
R2<-1-(SSE/SSTo)
R2.adj<-1-(MSE/MSTo)
hi<-ls.diag(tmp)$hat ##leverages
res.PRESS<-tmp$res/(1-hi)
PRESS<-sum(res.PRESS^2)
AIC<-n*log(SSE/n)+2*p
BIC<-n*log(SSE/n)+p*log(n)
Cp<-(SSE/MSE.max)-n+2*p
##store the results
results[i,1]<-p
results[i,2]<-R2
results[i,3]<-R2.adj
results[i,4]<-PRESS
results[i,5]<-AIC
results[i,6]<-BIC
results[i,7]<-Cp
##have R print out the iteration we are at
print(paste("run",i))}
Sys.time()-time1 ##see how long it takes!
################
##Save results##
################
save(x,y,results,models,file="results.RData")
######################
##Give results names##
######################
p<-results[,1]
R2<-results[,2]
R2.adj<-results[,3]
PRESS<-results[,4]
AIC<-results[,5]
BIC<-results[,6]
Cp<-results[,7]
###############################
##Find model with best R2.adj##
###############################
i<-R2.adj == max(R2.adj) ##what index has the maximum R2.adj
(1:2^10)[i] ##2^10 because 10 potential predictors
round(results[i,],3)
models[i,]
############################################
##Find model with best PRESS, AIC, BIC, Cp##
############################################
i2<-PRESS == min(PRESS) ##what index has the min PRESS
(1:2^10)[i2] ##2^10 because 10 potential predictors
i3<-AIC == min(AIC) ##what index has the min AIC
(1:2^10)[i3] ##2^10 because 10 potential predictors
i4<-BIC == min(BIC) ##what index has the min BIC
(1:2^10)[i4] ##2^10 because 10 potential predictors
i5<-Cp == min(Cp) ##what index has the min Cp
(1:2^10)[i5] ##2^10 because 10 potential predictors
models[i2,]
models[i3,]
models[i4,]
models[i5,]
###########################################
##plot the residual against fitted values##
###########################################
dat489.lm=lm(y ~ x2+x3+x4+x5+x7)
summary(dat489.lm)
dat481.lm=lm(y ~ x2+x3+x4+x5)
summary(dat481.lm)
dat489.res=resid(dat489.lm)
par(mfrow=c(1,1))
plot(y, dat489.res, ylab="Residuals", xlab="Fitted Value", main="Residual Plot")
abline(0, 0) # the horizon
dat481.res=resid(dat481.lm)
par(mfrow=c(1,1))
plot(y, dat481.res, ylab="Residuals", xlab="Fitted Value", main="Residual Plot")
abline(0, 0) # the horizon
```
After comparing all permutation's R2.adj, PRESS, AIC, BIC, and Cp. We have the following results:
\begin{center}
\begin{tabular}{c|c|c|c|c}
\hline
Permutation Number & Criteria & Result Predictors & $p$-value&R-squared\\
\hline
489 & R2.adj & x2+x3+x4+x5+x7 & < 2.2e-16 & 0.9556\\
\hline
481 & PRESS, AIC, BIC, Cp & x2+x3+x4+x5 & < 2.2e-16 & 0.9550\\
\hline
\end{tabular}
\end{center}
##General F-test
Model 481 is a reduced model of 489, with an extra x7. We apply a general F-test to test if x7 is significant.
```{r,echo=FALSE}
anova(dat481.lm,dat489.lm)
```
$p$-value is greater than 0.05. Hence, x7 is not significant, we only need x2, x3, x4, and x5.
Finally, fit our final model again, and test for goodness of fit.
```{r,echo=FALSE}
par(mfrow=c(2,2))
result.lm=lm(y ~ x2+x3+x4+x5)
summary(result.lm)
plot(result.lm)
```
\newpage
#Conclusion
With the given data set "dat.cvs",
* y is log-transformed
* Entry 10 is reomoved due to large influence to the overall data
* Predictors x1, x6, x7, x8, x9, and x10 are $removed$ due to insignificance.
* Predictors x2, x3, x4,and x5 are kept, and combined as a first order model.
The final model is: $$\mathbf{y=2.00 \cdot x_2+0.91\cdot x_3 +0.40 \cdot x_4 + 3.35\cdot x_5 +1.51}$$
```{r,echo=FALSE}
summary(result.lm)
```
\begin{center}
\begin{tabular}{c|c}
\hline
Predictor & $P$-value \\
\hline
\hline
intercept & $(< 2e-16)$ \\
\hline
x2 & $(< 2e-16)$ \\
\hline
x3 & $2.46e-14$ \\
\hline
x4 & $2.01e-05$ \\
\hline
x5 & $(< 2e-16)$ \\
\hline
\hline
overall & $(< 2.2e-16)$ \\
\hline
\end{tabular}
\end{center}
The intercept, x2, and x5 all have $p$-values of $(< 2e-16)$, x3 has $p$-value of $2.46e-14$, and x4 has $p$-value of $2.01e-05$. The overall $p$-value is $(< 2.2e-16)$. These $p$-values are all less than 0.05 and significant.
The $R^2$ is 0.955, which means the model represents 95.5% of the data. The model fits the data very well.
\newpage
#Appendix
##Data
```{r,echo=FALSE}
read.csv("~/Desktop/5120 Final/dat.csv")
```
##R Code
```{r}
dat <- read.csv("~/Desktop/5120 Final/dat.csv")
attach(dat)
dat.lm=lm(dat$y~.,data=dat)
summary(dat.lm)
par(mfrow=c(2,2))
plot(dat.lm)
par(mfrow=c(1,1))
library(MASS)
boxcox(dat.lm, plotit = TRUE)
dat$y=log(dat$y)
dat.lm=lm(dat$y~.,data=dat)
summary(dat.lm)
par(mfrow=c(2,2))
plot(dat.lm)
attach(dat)
dat.lm$residuals
tmp<-lm.influence(dat.lm)
tmp$hat[tmp$hat>2*10/100]
DFFITS<-dffits(dat.lm)
DFFITS[DFFITS>1]
dat[DFFITS>1,]
COOKS<-cooks.distance(dat.lm)
round(COOKS,3)
COOKS>qf(0.5,10,90)
no10<- dat[-10, ]
no10.lm=lm(no10$y~.,data=no10)
summary(no10.lm)
round(cor(no10),3)
library(faraway)
vif(no10.lm)
data=no10
start<-lm(y~1, data=data)
end<-lm(y~.,data=data)
result<-step(start, scope=list(lower=start, upper=end), direction="forward", trace=FALSE)
summary(result)
dat=data
attach(dat)
##################################################
##STEP 1: create design matrix for maximal model##
##################################################
x<-cbind(Const=1, x1, x2,x3,x4,x5,x6,x7,x8,x9,x10)
y<-y
###########################################################################################
##STEP 2: logical matrix to indicate for each possible model, which predictors are in/out##
###########################################################################################
models<-matrix(F, 2^10, 10) ##10 predictors, change accordingly
dimnames(models)<-list(NULL, c("x1","x2","x3","x4","x5","x6","x7","x8","x9","x10"))
row<-0
for (a in c(F,T)) { ##loop has 10 for statements, one for each predictor
for (b in c(F,T)) {
for (c in c(F,T)) {
for (d in c(F,T)) {
for (e in c(F,T)) {
for (f in c(F,T)) {
for (g in c(F,T)) {
for (h in c(F,T)) {
for (i in c(F,T)) {
for (j in c(F,T)) {
row<-row+1
models[row,]<-c(a,b,c,d,e,f,g,h,i,j)
}}}}}}}}}}
#################################################
##STEP 3: Matrix to store results from criteria##
#################################################
results<-matrix(NA, 2^10, 7) ##10 predictors, store 7 statistics
dimnames(results)<-list(NULL, c("p","R2","R2.adj","PRESS","AIC","BIC","Cp"))
#################################
##STEP 4: MSE for maximal model##
#################################
tmp<-lsfit(x,y,intercept=F)
n<-nrow(dat)
p<-ncol(x)
MSE.max<-sum(tmp$res^2)/(n-p)
###################################
##STEP 5: Fit all possible models##
###################################
time1<-Sys.time()
for (i in 1:(2^10)){ ##10 predictors
which<-c(T, models[i,]) ##pull out the row of model and append an intercept
tmp<-lsfit(x[,which], y, intercept=F) ##fit the model and compute the criteria
p<-sum(which) ##number of parameters for chosen model
SSTo<-(n-1)*var(y)
MSTo<-var(y)
SSE<-sum(tmp$res^2)
MSE<-SSE/(n-p)
R2<-1-(SSE/SSTo)
R2.adj<-1-(MSE/MSTo)
hi<-ls.diag(tmp)$hat ##leverages
res.PRESS<-tmp$res/(1-hi)
PRESS<-sum(res.PRESS^2)
AIC<-n*log(SSE/n)+2*p
BIC<-n*log(SSE/n)+p*log(n)
Cp<-(SSE/MSE.max)-n+2*p
##store the results
results[i,1]<-p
results[i,2]<-R2
results[i,3]<-R2.adj
results[i,4]<-PRESS
results[i,5]<-AIC
results[i,6]<-BIC
results[i,7]<-Cp
##have R print out the iteration we are at
print(paste("run",i))}
Sys.time()-time1 ##see how long it takes!
################
##Save results##
################
save(x,y,results,models,file="results.RData")
######################
##Give results names##
######################
p<-results[,1]
R2<-results[,2]
R2.adj<-results[,3]
PRESS<-results[,4]
AIC<-results[,5]
BIC<-results[,6]
Cp<-results[,7]
###############################
##Find model with best R2.adj##
###############################
i<-R2.adj == max(R2.adj) ##what index has the maximum R2.adj
(1:2^10)[i] ##2^10 because 10 potential predictors
round(results[i,],3)
models[i,]
############################################
##Find model with best PRESS, AIC, BIC, Cp##
############################################
i2<-PRESS == min(PRESS) ##what index has the min PRESS
(1:2^10)[i2] ##2^10 because 10 potential predictors
i3<-AIC == min(AIC) ##what index has the min AIC
(1:2^10)[i3] ##2^10 because 10 potential predictors
i4<-BIC == min(BIC) ##what index has the min BIC
(1:2^10)[i4] ##2^10 because 10 potential predictors
i5<-Cp == min(Cp) ##what index has the min Cp
(1:2^10)[i5] ##2^10 because 10 potential predictors
models[i2,]
models[i3,]
models[i4,]
models[i5,]
###########################################
##plot the residual against fitted values##
###########################################
dat489.lm=lm(y ~ x2+x3+x4+x5+x7)
summary(dat489.lm)
dat481.lm=lm(y ~ x2+x3+x4+x5)
summary(dat481.lm)
dat489.res=resid(dat489.lm)
par(mfrow=c(1,1))
plot(y, dat489.res, ylab="Residuals", xlab="Fitted Value", main="Residual Plot")
abline(0, 0) # the horizon
dat481.res=resid(dat481.lm)
par(mfrow=c(1,1))
plot(y, dat481.res, ylab="Residuals", xlab="Fitted Value", main="Residual Plot")
abline(0, 0) # the horizon
anova(dat481.lm,dat489.lm)
result.lm=lm(y ~ x2+x3+x4+x5)
summary(result.lm)
plot(result.lm)
```