-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCODE.R
510 lines (404 loc) · 12.1 KB
/
CODE.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
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
Sys.setenv(LANG = "en")
#load dataset
Z_Ali=read.csv(file=file.choose(),header=TRUE)
#quick overview about the data
ncol(Z_Ali) #number of colums
nrow(Z_Ali) #number of colums
head(Z_Ali) #Show first 6 observations in the data
#find the correlation between ecah variable
library(corrplot)
M=cor(Z_Ali)
RK=cor(Z_Ali[,-56],Z_Ali$Cath)
min10 <- which(RK<=sort(RK)[10], arr.ind = TRUE)
max10 <- which(-RK<=sort(-RK)[10], arr.ind = TRUE)
#plot Correlation Matrix
Z=cor(Z_Ali[,c(max10[,1],min10[,1],56)])
p.mat <- cor.mtest(Z_Ali[,c(max10[,1],min10[,1],56)])$p
col <- colorRampPalette(c("#BB4444", "#EE9988", "#FFFFFF", "#77AADD", "#4477AA"))
corrplot(Z, method = "color", col = col(200),
type = "upper", number.cex = .7,
addCoef.col = "black", # Add coefficient of correlation
tl.col = "red", tl.srt = 90, # Text label color and rotation
# Combine with significance
p.mat = p.mat, sig.level = 0.01, insig = "blank",
# hide correlation coefficient on the principal diagonal
diag = FALSE)
corrplot(Z, type = "upper",p.mat = p.mat, sig.level = 0.01)
#Clean out zero correlation
Z_Ali$Exertional.CP=NULL
Z_Ali.Refine=Z_Ali
#Get training and test datasets
set.seed(808) #Set seeds
train=sample(303,212) #Generate sample index
#Create training dataset named "Train.data"
Train.data=Z_Ali.Refine[train,]
dim(Train.data) #Find the dimension of the training data
#Create test dataset named "Test.data"
Test.data=Z_Ali.Refine[-train,]
dim(Test.data) #Find the dimension of the test data
#Fitting Logistic Regression
attach(Train.data)
#Train the model
glm.fit=glm(Cath˜., family=binomial, data=Train.data, control=list(
maxit=50))
"Warning messages:
1: glm.fit: fitted probabilities numerically 0 or 1 occurred"
#Summary statistics
summary(glm.fit)
#Making prediction
glm.probs=predict(glm.fit,Test.data,type="response")
glm.pred=rep(0,91)
glm.pred[glm.probs>0.5]=1
#Confusion matrix
table(glm.pred,Test.data$Cath)
glm.pred 0 1
0 26 8
1 8 49
#Misclassification rate
> mean(glm.pred!=Test.data$Cath)
[1] 0.1758242
#K-fold for Logistic Regression
#Create a function:
cv.log=function(data, model=Cath˜., yname="Cath", K=10, seed=123){
n=nrow(data)
set.seed(seed)
datay=data[,yname]#Response variable
library(MASS)#Load package
#Partition the data into K subsets
f=ceiling(n/K)
s=sample(rep(1:K, f), n)
#Generate indices 1:10 and sample n of them
#K fold cross-validated error
CV=NULL
for (i in 1:K) {#i=1
test.index=seq_len(n)[(s==i)] #Test data
train.index=seq_len(n)[(s!=i)] #Training data
#Model with training data
glm.fit=glm(model, data=data[train.index,],family=binomial)
#observed test set y
glm.y=data[test.index, yname]
#Predicted test set y
glm.predy=round(predict(glm.fit, data[test.index,],type="response"))
#Observed-predicted on test data
error=mean(glm.y!=glm.predy)
#Error rates
CV=c(CV,error)
}
#Output
list(call=model, K=K,
logistic_error_rate=mean(CV), seed=seed)
}
#Apply created function
cv.log(Z_Ali.Refine, model=as.factor(Cath)˜8., yname="Cath", K=10, seed
=808)
$Call
as.factor(Cath)˜.
$K
[1] 10
$logistic_error_rate
[1] 0.1878341
$seed
[1] 808
#Fitting LDA
#Load package MASS
library(MASS)
#Train the model
lda.fit=lda(Cath˜.,data=Train.data)
#Making prediction
lda.pred=predict(lda.fit,Test.data)
lda.class=lda.pred$class
#Confusion matrix
table(lda.class,Test.data$Cath)
#Misclassification rate
mean(lda.class!=Test.data$Cath)
[1] 0.1098901
#K-fold for lda
#Create function
cv.lda=function(data, model=Cath˜., yname="Cath", K=10, seed=123){
n=nrow(data)
set.seed(seed)
datay=data[,yname]#Response variable
library(MASS)
#Partition the data into K subsets
f=ceiling(n/K)
s=sample(rep(1:K, f), n)
#Generate indices 1:10 and sample n of them
#K fold cross-validated error
CV=NULL
for (i in 1:K) {#i=1
test.index=seq_len(n)[(s==i)] #test data
train.index=seq_len(n)[(s!=i)] #training data
#Model with training data
lda.fit=lda(model, data=data[train.index,])
#Observed test set
lda.y=data[test.index, yname]
#Predicted the test set
lda.predy=predict(lda.fit, data[test.index,])$class
#Observed-predicted on test data
error=mean(lda.y!=lda.predy)
#Error rates
CV=c(CV,error)
}
#Output
list(call=model, K = K, lda_error_rate=mean(CV), seed=seed)
}
cv.lda(Z_Ali.Refine, model=Cath˜.-CHF, yname="Cath", K=10, seed=808)
$call
Cath˜.-CHF
$K
[1] 10
$lda_error_rate
[1] 0.1481106
$seed
[1] 808
#Fitting QDA
qda.fit=qda(Cath~.-CHF,data=Train.data)
qda.fit
qda.pred=predict(qda.fit,Test.data)
qda.class=qda.pred$class
table(qda.class,Test.data$Cath)
mean(qda.class!=Test.data$Cath)
#Error rate=0.1648352 a higher than previous methods
#K-fold for Qda
#Sadly, we can't perform a QDA with the full 55 predictors. QDA
#requires a separate covariance matrix for each class, so with 55 predictors and 2 classes there
#would be 255(55+1)=2 = 3080 parameters, but we only have a total of 303 observations.
#by the fundamental theory of linear algebra, we can solve 3080 parameters with 303 observations.
#A quick try using KNN
#Using KNN
library(class)
Train.X=cbind(Z_Ali$Age,Z_Ali$DM,Z_Ali$HTN,Z_Ali$BP,Z_Ali$Typical.Chest.Pain,Z_Ali$Atypical,
Z_Ali$Nonanginal,Z_Ali$Tinversion,Z_Ali$FBS,Z_Ali$K,Z_Ali$EF.TTE,Z_Ali$Region.RWMA)[train,]
Test.X=cbind(Z_Ali$Age,Z_Ali$DM,Z_Ali$HTN,Z_Ali$BP,Z_Ali$Typical.Chest.Pain,Z_Ali$Atypical,
Z_Ali$Nonanginal,Z_Ali$Tinversion,Z_Ali$FBS,Z_Ali$K,Z_Ali$EF.TTE,Z_Ali$Region.RWMA)[-train,]
Train.Result=Cath[train]
set.seed(808)
#K=1
knn.pred=knn(Train.X,Test.X,Train.Result,k=1)
table(knn.pred,Test.data$Cath)
mean(knn.pred!=Test.data$Cath)
#error rate= 0.3736264
#K=2
knn.pred=knn(Train.X,Test.X,Train.Result,k=2)
table(knn.pred,Test.data$Cath)
mean(knn.pred!=Test.data$Cath)
#error rate= 0.428574
#K=3
knn.pred=knn(Train.X,Test.X,Train.Result,k=3)
table(knn.pred,Test.data$Cath)
mean(knn.pred!=Test.data$Cath)
#error rate= 0.373624 same as k=1]
#Decision Tree Methods.
#Load original data
Z_Ali.T=read.csv(file=file.choose(),header=TRUE)
#Clean the all zero column
Z_Ali.T$Exertional.CP=NULL
#Create training and test datasets using the same seed
set.seed(808)
train=sample(303,212)
TTrain.data=Z_Ali.T[train,]
TTest.data=Z_Ali.T[-train,]
#Load package
library(tree)
#Train the model
Tree.fit=tree(Cath˜., TTrain.data)
summary(Tree.fit)
#Plot the tree
plot(Tree.fit)
text(Tree.fit, pretty=0)
#Making prediction
tree.predict=predict(Tree.fit,TTest.data[,-55],type="class")
#Confusion matrix
table(tree.predict,TTest.data$Cath)
tree.predict Cad Normal
Cad 53 12
Normal 4 22
#Misclassification rate
mean(tree.predict!=TTest.data$Cath)
[1] 0.1758242
#K fold with Decision Tree
#load packages
library(plyr)
library(rpart)
#Set seed
set.seed(808)
#Set model form
form="Cath˜."
#Create folds and error object
folds=split(Z_Ali.T, cut(sample(1:nrow(Z_Ali.T)),10))
errs=rep(NA, length(folds))
#Loop
for (i in 1:length(folds))
{
test=ldply(folds[i], data.frame) #Assign test dataset
train=ldply(folds[-i], data.frame) #Assign test training dataset
tmp.model=tree(form, train) #Buid model
tmp.predict=predict(tmp.model, test, type="class") #Making prediction
conf.mat=table(test$Cath, tmp.predict)
errs[i]=1-sum(diag(conf.mat))/sum(conf.mat) #Assign errors
}
#Print out results
print(sprintf("Average Error Using K-fold Cross-Validation: %.3f
percent", 100*mean(errs)))
[1] "Average Error Using K-fold Cross-Validation: 22.462 percent"’
#SVM Approch
#Load package
library(e1071)
#Fit with radial kernel
svmfitR=svm(Cath˜., data=TTrain.data, kernel="radial", cost=1)
summary(svmfitR)
#Making prediction
svmpredictR=predict(svmfitR,TTest.data)
#Confusion matrix
table(svmpredictR,TTest.data$Cath)
svmpredictR Cad Normal
Cad 54 11
Normal 3 23
#Test error
mean(svmpredictR!=TTest.data$Cath)
[1] 0.1538462
#Fit with Linear Kernel
svmfitL=svm(Cath˜., data=TTrain.data, kernal="linear", cost=1)
summary(svmfitL)
#Making prediction
svmpredictL=predict(svmfitL,TTest.data)
#Confusion matrix
table(svmpredictL,TTest.data$Cath)
svmpredictL Cad Normal
Cad 51 8
Normal 6 26
#Test error
mean(svmpredictL!=TTest.data$Cath)
[1] 0.1538462
#10-fold cross validation for radial kernal
Rsvmfit=tune(svm, Cath˜., data=Z_Ali.T, kernel="radial", gamma
=0.01724138, cost=1, tunecontrol=tune.control(cross=10))
summary(Rsvmfit)
Error Estimation of SVM Using 10-fold Cross Validation: 0.13225
#10-fold cross validation for linear kernal
Lsvmfit=tune(svm, Cath˜., data=Z_Ali.T, kernel="linear", gamma
=0.01724138, cost=1, tunecontrol=tune.control(cross=10))
summary(Lsvmfit)
’Error Estimation of SVM Using 10-fold Cross Validation: 0.1647312’
#Random Forest
#Load package
library(randomForest)
#Set random seed
set.seed(808)
#Train the model
rf.fit=randomForest(Cath˜., data=Z_Ali.T,mty=7, importance=TRUE)
rf.fit
Call:
randomForest(formula=Cath˜., data=Z_Ali.T, mty=7, importance=TRUE)
Type of random forest: classification
Number of trees: 500
No. of variables tried at each split: 7
OOB estimate of error rate: 12.21%
Confusion matrix:
Cad Normal class.error
Cad 205 11 0.05092593
Normal 26 61 0.29885057
#Finding out valuable predictors
summary(rf.fit$importance)
importance(rf.fit)
varImpPlot(rf.fit,main="Important variables for Z-Ali")
#Create temporary data frame to hold results
rankingA=data.frame()
rankingG=data.frame()
#Looping
for (i in 1:100)
{
set.seed(100+i*3) #Change seed
#Run random forest model
rf.fit=randomForest(Cath˜., data=Z_Ali.T, mty=7, importance=TRUE)
#Get variable importance list and sort by ranking
newdata=data.frame(rf.fit$importance)
newDA=newdata[order(-newdata$MeanDecreaseAccuracy),]
newDG=newdata[order(-newdata$MeanDecreaseGini),]
#Generate top 8 variables and assign to temporary data frame
MDA=data.frame(row.names(newDA)[1:8])
MDG=data.frame(row.names(newDG)[1:8])
rankingA=rbind(rankingA,MDA)
rankingG=rbind(rankingG,MDG)
}
#Rename the column name and merge temporary data frame
colnames(rankingA)="Pick"
colnames(rankingG)="Pick"
Pick=rbind(rankingA,rankingG)
#Frequency table
table(Pick)
Pick
#Performances on Reduced Model
#Applied created function for Logistic Regression
cv.log(Z_Ali.Refine, model=as.factor(Cath)˜Typical.Chest.Pain+Age+
Atypical+
EF.TTE+Region.RWMA+TG+FBS+HTN, yname="Cath", K=10, seed=808)
$call
as.factor(Cath)˜Typical.Chest.Pain+Age+Atypical+EF.TTE+Region.RWMA+TG+
FBS+HTN
$K
[1] 10
$logistic_error_rate
[1] 0.1312289
$seed
[1] 808
#Applied created function for LDA
cv.lda(Z_Ali.Refine, model=as.factor(Cath)˜Typical.Chest.Pain+Age+
Atypical+
EF.TTE+Region.RWMA+TG+FBS+HTN, yname="Cath", K=10, seed=808)
#Results
$call
as.factor(Cath)˜Typical.Chest.Pain+Age+Atypical+EF.TTE+Region.RWMA+TG+
FBS+HTN
$K
[1] 10
$lda_error_rate
[1] 0.1445853
$seed
[1] 808
#Applied created function for QDA
cv.qda(Z_Ali.Refine, model=Cath˜Typical.Chest.Pain+Age+Atypical+
EF.TTE+Region.RWMA+TG+FBS+HTN, yname="Cath", K=10, seed=808)
#Results
$call
Cath˜Typical.Chest.Pain+Age+Atypical+EF.TTE+Region.RWMA+TG+FBS+HTN
$K
[1] 10
$qda_error_rate
[1] 0.1574885
$seed
[1] 808’
#Performance on reduced model for SVM
#Set random seed
set.seed(808)
#tune svm
tune.outL=tune(svm, Cath~Typical.Chest.Pain+Age+Atypical+EF.TTE+Region.RWMA+TG+FBS+HTN, data=Z_Ali.T, kernel="linear",
ranges=list(cost=seq(0.01,0.09,0.001), gamma=seq(0.001,0.01,0.001)),
tunecontrol=tune.control(cross=10))
summary(tune.outL)
#Set random seed
set.seed(808)
#tune svm
tune.outR=tune(svm, Cath~Typical.Chest.Pain+Age+Atypical+EF.TTE+Region.RWMA+TG+FBS+HTN, data=Z_Ali.T, kernel="radial",
ranges=list(cost=seq(0.15,0.16,0.001), gamma=seq(0.04,0.05,0.001)),
tunecontrol=tune.control(cross=10))
summary(tune.outR)
#Apply SVM with radial kernel
Rsvmfit=tune(svm, Cath˜Typical.Chest.Pain+Age+Atypical+EF.TTE+Region.
RWMA+
TG+FBS+HTN, data=Z_Ali.T, kernel="radial", gamma=0.048, cost=0.159,
CHAPTER 4. VARIABLE SELECTION AND MODEL IMPROVEMENT 39
tunecontrol=tune.control(cross=10))
#Error rate
summary(Rsvmfit)
Error estimation of svm using 10-fold cross validation: 0.1323656
#Set random seed
set.seed(808)
#Apply SVM with linear kernel
Lsvmfit=tune(svm, Cath˜Typical.Chest.Pain+Age+Atypical+EF.TTE+Region.
RWMA+
TG+FBS+HTN, data=Z_Ali.T, kernel="linear", gamma=0.001, cost=0.089,
tunecontrol=tune.control(cross=10))
#Error rate
summary(Lsvmfit)
Error estimation of svm using 10-fold cross validation: 0.1323656