-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassification_model.R
596 lines (463 loc) · 15.3 KB
/
classification_model.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
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
## AMI22T Home Exercise 2, Problem 1
## Patient with Parkinson's Disease or Healthy?
## Saumya Gupta, DS
# set path of all-data export import directory
setwd('C:/Users/gupta/OneDrive/Documents/MS-DS/AMI22T/HomeExercises/HomeExercise2/')
# load required packages
library(prettyR)
library(data.table)
library(dplyr)
library(tree)
library(MLmetrics)
library(glmnet)
library(e1071)
library(randomForest)
library(gbm)
library(ggplot2)
# Utility Function ----
mode_aggregate <- function(class, id) {
return (aggregate(list(class = class),
by = list(id = id),
FUN = Mode)$class %>%
as.factor())
}
# Data Read ----
# load data and store in data table
pd.speech.features <-
fread("pd_speech_features.csv", skip = 1, header = T)
pd.speech.features <- data.frame(pd.speech.features)
# set seed
set.seed(9899)
# check for columns with missing data
names(which(colSums(is.na(pd.speech.features)) > 0))
# check for any non-numeric variables
sapply(pd.speech.features[c(names(pd.speech.features)[sapply(pd.speech.features,
class) != 'numeric'])],
class)
# check gender distribution
ggplot(pd.speech.features,
aes(x = as.factor(class),
fill = as.factor(gender))) +
geom_bar(position = "dodge")
# Normalization ----
# exclude id, gender and class variables from scaling process
# exclude app_entropy_shannon_10_coef (integer64) and scale it separately
# (otherwise transforms latter to NA!)
pd.speech.features[,-c(1, 2, 201, 755)] <-
scale(pd.speech.features[,-c(1, 2, 201, 755)])
pd.speech.features$app_entropy_shannon_10_coef <-
scale(pd.speech.features$app_entropy_shannon_10_coef)
# check for presence of highly correlated variables
correlations <- cor(as.matrix(pd.speech.features))
correlations <- as.data.frame(correlations)
correlations[correlations < 0.8 | correlations == 1] <- ""
correlations <-
correlations[!sapply(correlations, function(x)
all(is.na(x) | x == ""))]
# Train-Test Split ----
# 80% of humans go for training
train <- as.vector(sample(0:251, floor(252 * 0.8)))
features.train <-
pd.speech.features[pd.speech.features$id %in% train, ]
features.test <-
pd.speech.features[!(pd.speech.features$id %in% train), ]
# Task 1 ----
## Modelling ----
data.train <- features.train %>%
select(-c("id"))
data.train$class <- as.factor(data.train$class)
true.class <- as.vector(features.test$class)
# get mode aggregates for majority voting
true.class <- mode_aggregate(true.class, features.test$id)
### Decision Tree ----
# fit
tree.features <- tree(class ~ ., data.train)
summary(tree.features)
plot(tree.features)
text(tree.features, pretty = 0)
# predict for test
tree.pred <- predict(tree.features, features.test, type = "class")
# get mode aggregates
tree.pred <- mode_aggregate(tree.pred, features.test$id)
# evaluate prediction
prop.table(table(tree.pred, true.class), margin = 2)
acc.dt <-
(table(tree.pred,
true.class)[1] + table(tree.pred,
true.class)[4]) / (nrow(features.test) /
3)
f1score.dt <- F1_Score(true.class, tree.pred)
# perform pruning with cross-validation and let it make feature selection
cv.features <- cv.tree(tree.features)
# find best number of terminal nodes
plot(cv.features$size, cv.features$dev, type = "b")
# fit with best number of terminal nodes
prune.features <- prune.tree(tree.features, best = 4)
summary(prune.features)
plot(prune.features)
text(prune.features, pretty = 0)
# predict for test
prune.tree.pred <-
predict(prune.features, features.test, type = "class")
# get mode aggregates
prune.tree.pred <- mode_aggregate(prune.tree.pred, features.test$id)
# evaluate prediction
prop.table(table(prune.tree.pred, true.class), margin = 2)
acc.dt.cv <-
(table(prune.tree.pred,
true.class)[1] + table(prune.tree.pred,
true.class)[4]) / (nrow(features.test) /
3)
f1score.dt.cv <- F1_Score(true.class, prune.tree.pred)
### Support-Vector Machine ----
# perform feature selection using lasso regression
glm.fit <-
cv.glmnet(
x = model.matrix(class ~ ., data = data.train)[,-1],
y = as.numeric(data.train$class),
type.measure = 'deviance',
nfolds = 10,
alpha = 0.5
)
# get all coefficients
coefficients <- coef(glm.fit, s = 'lambda.1se', exact = TRUE)
# filter out coefficients equal to 0
important.variables.lasso <-
row.names(coefficients)[which(coefficients != 0)]
# get significant variables
important.variables.lasso <-
important.variables.lasso[!(important.variables.lasso %in%
'(Intercept)')]
# linear kernel
# tune cost hyperparameter
tune.out <- tune(
svm,
class ~ .,
data = data.train %>% select(c(
all_of(important.variables.lasso), "class"
)),
kernel = "linear",
ranges = list(cost = c(0.0001, 0.001, 0.01, 0.1, 1, 5, 10, 100))
)
summary(tune.out)
# use linear kernel with optimal cost
svm.l.features <- svm(
class ~ .,
data = data.train %>% select(c(
all_of(important.variables.lasso), "class"
)),
kernel = "linear",
cost = 0.1,
scale = FALSE
)
summary(svm.l.features)
# predict for test
svm.l.pred <- predict(svm.l.features, features.test)
# get mode aggregates
svm.l.pred <- mode_aggregate(svm.l.pred, features.test$id)
# evaluate prediction
prop.table(table(predict = svm.l.pred, truth = true.class), margin = 2)
acc.svm.l <-
(table(svm.l.pred,
true.class)[1] + table(svm.l.pred,
true.class)[4]) / (nrow(features.test) /
3)
f1score.svm.l <- F1_Score(true.class, svm.l.pred)
# radial kernel
# tune cost and gamma hyperparameter
tune.out <- tune(
svm,
class ~ .,
data = data.train %>% select(c(
all_of(important.variables.lasso), "class"
)),
kernel = "radial",
ranges = list(
cost = c(1, 5, 10, 70, 80, 90, 100),
gamma = c(0.01, 0.1)
)
)
summary(tune.out)
# use radial kernel with optimal cost and gamma
svm.r.features <- svm(
class ~ .,
data = data.train %>% select(c(
all_of(important.variables.lasso), "class"
)),
kernel = "radial",
cost = 5,
gamma = 0.01,
scale = FALSE
)
summary(svm.r.features)
# predict for test
svm.r.pred <- predict(svm.r.features, features.test)
# get mode aggregates
svm.r.pred <- mode_aggregate(svm.r.pred, features.test$id)
# evaluate prediction
prop.table(table(predict = svm.r.pred, truth = true.class), margin = 2)
acc.svm.r <-
(table(svm.r.pred,
true.class)[1] + table(svm.r.pred,
true.class)[4]) / (nrow(features.test) /
3)
f1score.svm.r <- F1_Score(true.class, svm.r.pred)
### Bagging ----
# perform feature selection by finding variables with high mean drop in accuracy
rf.features.selection <-
randomForest(class ~ ., data = data.train, importance = TRUE)
varImpPlot(rf.features.selection)
# get RF filter variables
important.variables.rf <-
data.frame(importance(rf.features.selection)) %>%
slice_max(order_by = MeanDecreaseAccuracy, n = 25) %>%
rownames()
# find optimal ntree value using OOB error estimates
plot(rf.features.selection)
# fit with optimal ntree value and selected variables
bag.features <- randomForest(
class ~ .,
data = data.train %>% select(c(all_of(
important.variables.rf
), "class")),
mtry = 25,
ntree = 50
)
# predict for test
bag.pred <- predict(bag.features, newdata = features.test)
# get mode aggregates
bag.pred <- mode_aggregate(bag.pred, features.test$id)
# evaluate prediction
prop.table(table(bag.pred, true.class), margin = 2)
acc.bag <-
(table(bag.pred, true.class)[1] + table(bag.pred,
true.class)[4]) / (nrow(features.test) /
3)
f1score.bag <- F1_Score(true.class, bag.pred)
### Random Forest ----
# find optimal mtry value using 500 trees for tuning and previously selected features
mtry <- tuneRF(
data.train %>% select(c(all_of(
important.variables.rf
))),
data.train$class,
ntreeTry = 500,
stepFactor = 1.5,
improve = 0.01,
trace = TRUE,
plot = TRUE
)
print(mtry)
# fit with optimal mtry value and previously selected features
# use optimal ntree value found during bagging
rf.features <- randomForest(
class ~ .,
data = data.train %>% select(c(all_of(
important.variables.rf
), "class")),
mtry = 7,
ntree = 50
)
# predict for test
rf.pred <- predict(rf.features, newdata = features.test)
# get mode aggregates
rf.pred <- mode_aggregate(rf.pred, features.test$id)
# evaluate prediction
prop.table(table(rf.pred, true.class), margin = 2)
acc.rf <-
(table(rf.pred, true.class)[1] + table(rf.pred,
true.class)[4]) / (nrow(features.test) /
3)
f1score.rf <- F1_Score(true.class, rf.pred)
### Boosting ----
# perform feature selection by finding variables with high relative influence
# boosting using 1000 trees, 0.01 shrinkage and default depth
boost.features.selection <- gbm(
as.integer(class) - 1 ~ .,
data = data.train,
n.trees = 1000,
shrinkage = 0.01,
distribution = "bernoulli"
)
# get Boosting filter variables
important.variables.boost <-
summary(boost.features.selection) %>%
slice_max(order_by = rel.inf, n = 25) %>%
rownames()
# use the selected variables
boost.features <- gbm(
as.integer(class) - 1 ~ .,
data = data.train %>% select(c(
all_of(important.variables.boost), "class"
)),
n.trees = 1000,
shrinkage = 0.01,
distribution = "bernoulli"
)
# predict for test
boost.pred <- predict(boost.features,
newdata = features.test,
n.trees = 1000,
type = "response")
boost.binary.pred <- as.factor(ifelse(boost.pred > 0.5, 1, 0))
# get mode aggregates
boost.binary.pred <-
mode_aggregate(boost.binary.pred, features.test$id)
# evaluate prediction
prop.table(table(boost.binary.pred, true.class), margin = 1)
acc.boost <-
(table(boost.binary.pred,
true.class)[1] + table(boost.binary.pred,
true.class)[4]) / (nrow(features.test) /
3)
f1score.boost <- F1_Score(true.class, boost.binary.pred)
## Comparison Results ----
# create a data frame for comparison
accuracies <-
data.frame(
Model = c(
"Decision Tree",
"Decision Tree (Pruned)",
"Support-Vector Machine (Linear)",
"Support-Vector Machine (Radial)",
"Bagging",
"Random Forest",
"Boosting"
),
Accuracy = c(
acc.dt,
acc.dt.cv,
acc.svm.l,
acc.svm.r,
acc.bag,
acc.rf,
acc.boost
),
F1Score = c(
f1score.dt,
f1score.dt.cv,
f1score.svm.l,
f1score.svm.r,
f1score.bag,
f1score.rf,
f1score.boost
)
)
accuracies <-
accuracies %>% mutate_if(is.numeric, round, digits = 2)
# plot all model results
ggplot(accuracies, aes(Model, Accuracy, fill = Model)) +
geom_bar(stat = "identity")
ggplot(accuracies, aes(Model, F1Score, fill = Model)) +
geom_bar(stat = "identity")
# Task 2 ----
## Dimensionality Reduction (Using PCA) ----
# find principal components for train
principal.components <-
prcomp(features.train %>% select(-c("id", "class")))
# calculate variances using standard deviation of the projected points
pc.variances <- (principal.components$sdev) ^ 2
# calculate proportion explained
pc.proportion.variances <- pc.variances / sum(pc.variances)
# get components collectively explaining 90% of input variance
number.of.components <-
length(pc.proportion.variances[cumsum(pc.proportion.variances) < .90]) + 1
## Modelling ----
# create train with selected components and target
data.train.pca <-
data.frame(class = as.factor(features.train$class),
principal.components$x[, 1:number.of.components])
# perform PCA for test too and get desired number of components
data.test.pca <-
data.frame(predict(principal.components, newdata = features.test %>%
select(-c("id", "class"))))[, 1:number.of.components]
# perform classification again with select algorithms from set
### Support-Vector Machine ----
# linear kernel
tune.out <- tune(
svm,
class ~ .,
data = data.train.pca,
kernel = "linear",
ranges = list(cost = c(0.001, 0.005, 0.007, 0.009, 0.01, 0.12))
)
summary(tune.out)
svm.l.features.pca <- svm(
class ~ .,
data = data.train.pca,
kernel = "linear",
cost = 0.009,
scale = FALSE
)
summary(svm.l.features.pca)
svm.l.pred.pca <- predict(svm.l.features.pca, data.test.pca)
svm.l.pred.pca <- mode_aggregate(svm.l.pred.pca, features.test$id)
prop.table(table(predict = svm.l.pred.pca, truth = true.class), margin = 2)
acc.svm.l.pca <-
(table(svm.l.pred.pca,
true.class)[1] + table(svm.l.pred.pca,
true.class)[4]) / (nrow(data.test.pca) /
3)
f1score.svm.l.pca <- F1_Score(true.class, svm.l.pred.pca)
# radial kernel
tune.out <- tune(
svm,
class ~ .,
data = data.train.pca,
kernel = "radial",
ranges = list(cost = c(0.01, 1, 2, 3, 5),
gamma = c(0.01, 0.1))
)
summary(tune.out)
svm.r.features.pca <- svm(
class ~ .,
data = data.train.pca,
kernel = "radial",
cost = 3,
gamma = 0.01,
scale = FALSE
)
summary(svm.r.features.pca)
svm.r.pred.pca <- predict(svm.r.features.pca, data.test.pca)
svm.r.pred.pca <- mode_aggregate(svm.r.pred.pca, features.test$id)
prop.table(table(predict = svm.r.pred.pca, truth = true.class), margin = 2)
acc.svm.r.pca <-
(table(svm.r.pred.pca,
true.class)[1] + table(svm.r.pred.pca,
true.class)[4]) / (nrow(data.test.pca) /
3)
f1score.svm.r.pca <- F1_Score(true.class, svm.r.pred.pca)
## Comparison Results ----
accuracies.pca <-
data.frame(
Kernel = rep(c("Linear",
"Radial"), 2),
Method = rep(c(rep(
"Feature Selection", 2
),
rep("PCA", 2)), 2),
Metric = c(rep("Overall Accuracy", 4), rep("F1Score", 4)),
Value = c(
acc.svm.l,
acc.svm.r,
acc.svm.l.pca,
acc.svm.r.pca,
f1score.svm.l,
f1score.svm.r,
f1score.svm.l.pca,
f1score.svm.r.pca
)
)
# plot difference in performance
ggplot(accuracies.pca, aes(Kernel, Value, fill = Method, label = Value)) +
geom_bar(stat = "identity", position = "dodge") +
geom_text(
position = position_dodge(width = 1),
aes(
y = Value + 0.25,
label = round(Value, 2),
hjust = 1.5
),
angle = 90
) +
facet_wrap( ~ Metric)