-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExperiment2.R
547 lines (471 loc) · 24.6 KB
/
Experiment2.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
# Experiment 2
# Ensemble Machine Learning Model Trained on a New Synthesized Dataset Generalizes Well for Stress Prediction Using Wearable Device
# Gideon Vos, Master of Philosophy, James Cook University, 2022
# Citations:
# WESAD (Wearable Stress and Affect Detection)
# Philip Schmidt, Attila Reiss, Robert Duerichen, Claus Marberger, and Kristof Van Laerhoven. 2018.
# Introducing WESAD, a Multimodal Dataset for Wearable Stress and Affect Detection.
# In Proceedings of the 20th ACM International Conference on Multimodal Interaction (ICMI '18).
# Association for Computing Machinery, New York, NY, USA, 400–408. DOI:https://doi.org/10.1145/3242969.3242985
# The SWELL Knowledge Work Dataset for Stress and User Modeling Research
# Koldijk, S., Sappelli, M., Verberne, S., Neerincx, M., & Kraaij, W. (2014).
# The SWELL Knowledge Work Dataset for Stress and User Modeling Research.
# To appear in: Proceedings of the 16th ACM International Conference on Multimodal Interaction (ICMI 2014) (Istanbul, Turkey, 12-16 November 2014).
# The dataset can be accessed medio 2015 here: http://persistent-identifier.nl/?identifier=urn:nbn:nl:ui:13-kwrv-3e.
# Non-EEG Dataset for Assessment of Neurological Status
# Birjandtalab, Javad, Diana Cogan, Maziyar Baran Pouyan, and Mehrdad Nourani,
# A Non-EEG Biosignals Dataset for Assessment and Visualization of Neurological Status,
# 2016 IEEE International Workshop on Signal Processing Systems (SiPS), Dallas, TX, 2016, pp. 110-114. doi: 10.1109/SiPS.2016.27
# Toadstool: A Dataset for Training Emotional Intelligent Machines Playing Super Mario Bros
# Svoren, H., Thambawita, V., Halvorsen, P., Jakobsen, P., Garcia-Ceja, E., Noori, F. M., … Hicks, S. (2020, February 28).
# https://doi.org/10.31219/osf.io/4v9mp
# The UBFC-Phys dataset is a public multimodal dataset dedicated to psychophysiological studies
# Meziati Sabour, Y. Benezeth, P. De Oliveira, J. Chappé, F. Yang. "UBFC-Phys: A Multimodal Database For Psychophysiological Studies Of Social Stress",
# IEEE Transactions on Affective Computing, 2021.
# A Wearable Exam Stress Dataset for Predicting Cognitive Performance in Real-World Settings
# Amin, M. R., Wickramasuriya, D., & Faghih, R. T. (2022). A Wearable Exam Stress Dataset for Predicting Cognitive Performance in Real-World Settings (version 1.0.0).
# PhysioNet. https://doi.org/10.13026/kvkb-aj90.
library(dplyr)
library(caret)
library(xgboost)
library(zoo)
library(stresshelpers)
library("ggsci")
library(e1071)
library(randomForest)
options(scipen=999)
set.seed(123)
prettyConfusion<-function(Actual,Predict,colors=c("white","red4","dodgerblue3"),text.scl=5){
actual = as.data.frame(table(Actual))
names(actual) = c("Actual","ActualFreq")
confusion = as.data.frame(table(Actual, Predict))
names(confusion) = c("Actual","Predicted","Freq")
confusion = merge(confusion, actual, by=c('Actual','Actual'))
confusion$Percent = confusion$Freq/confusion$ActualFreq*100
confusion$ColorScale<-confusion$Percent*-1
confusion[which(confusion$Actual==confusion$Predicted),]$ColorScale<-confusion[which(confusion$Actual==confusion$Predicted),]$ColorScale*-1
confusion$Label<-paste(round(confusion$Percent,0),"%, n=",confusion$Freq,sep="")
tile <- ggplot() +
geom_tile(aes(x=Actual, y=Predicted,fill=ColorScale),data=confusion, color="black",size=0.1) +
labs(x="Actual",y="Predicted")
tile = tile +
geom_text(aes(x=Actual,y=Predicted, label=Label),data=confusion, size=text.scl, colour="black") +
scale_fill_gradient2(low=colors[2],high=colors[3],mid=colors[1],midpoint = 0,guide='none')
}
#########################################################################################################################################################
# Load data, with feature-engineering
#########################################################################################################################################################
data_neuro <- stresshelpers::make_neuro_data('NEURO', feature_engineering = TRUE)
data_swell <- stresshelpers::make_swell_data('SWELL', feature_engineering = TRUE)
data_wesad <- stresshelpers::make_wesad_data('WESAD', feature_engineering = TRUE)
data_neuro <- data_neuro %>% select(hrrange, hrvar, hrstd, hrmin, edarange, edastd, edavar, hrkurt, edamin, hrmax, Subject, metric)
data_swell <- data_swell %>% select(hrrange, hrvar, hrstd, hrmin, edarange, edastd, edavar, hrkurt, edamin, hrmax, Subject, metric)
data_wesad <- data_wesad %>% select(hrrange, hrvar, hrstd, hrmin, edarange, edastd, edavar, hrkurt, edamin, hrmax, Subject, metric)
#########################################################################################################################################################
# Model training - SVM. LOSO on SWELL, NEURO, WESAD
#########################################################################################################################################################
# SWELL
subjects <- unique(data_swell$Subject)
index <- 1
results <- NULL
for (subject in subjects)
{
print(subject)
val <- data_swell[data_swell$Subject == subject,]
temp <- data_swell[!(data_swell$Subject == subject),]
val$metric <- as.factor(val$metric)
val$Subject <- NULL
temp$metric <- as.factor(temp$metric)
temp$Subject <- NULL
model_svm = svm(x = temp[,1:10], y = temp$metric, kernel = "radial", cost = 5, scale = FALSE)
y_hat = predict(model_svm, newdata = val[,1:10])
acc <- sum(val$metric == y_hat)/nrow(val)
precision <- posPredValue(y_hat, val$metric, positive="1")
recall <- sensitivity(y_hat, val$metric, positive="1")
F1 <- (2 * precision * recall) / (precision + recall)
res <- cbind(subject, acc, precision, recall, F1)
res <- as.data.frame(res)
names(res) <- c("SUBJECT","ACC", "PRECISION", "RECALL", "F1")
results <- rbind(results, res)
}
print(mean(as.numeric(results$ACC))) # 0.60
write.csv(results, "Ex2_SVM_Swell.csv", row.names = FALSE)
# NEURO
subjects <- unique(data_neuro$Subject)
index <- 1
results <- NULL
for (subject in subjects)
{
print(subject)
val <- data_neuro[data_neuro$Subject == subject,]
temp <- data_neuro[!(data_neuro$Subject == subject),]
val$metric <- as.factor(val$metric)
val$Subject <- NULL
temp$metric <- as.factor(temp$metric)
temp$Subject <- NULL
model_svm = svm(x = temp[,1:10], y = temp$metric, kernel = "radial", cost = 5, scale = FALSE)
y_hat = predict(model_svm, newdata = val[,1:10])
acc <- sum(val$metric == y_hat)/nrow(val)
precision <- posPredValue(y_hat, val$metric, positive="1")
recall <- sensitivity(y_hat, val$metric, positive="1")
F1 <- (2 * precision * recall) / (precision + recall)
res <- cbind(subject, acc, precision, recall, F1)
res <- as.data.frame(res)
names(res) <- c("SUBJECT","ACC", "PRECISION", "RECALL", "F1")
results <- rbind(results, res)
}
print(mean(as.numeric(results$ACC))) # 0.5688387
write.csv(results, "Ex2_SVM_Neuro.csv", row.names = FALSE)
# WESAD
subjects <- unique(data_wesad$Subject)
index <- 1
results <- NULL
for (subject in subjects)
{
print(subject)
val <- data_wesad[data_wesad$Subject == subject,]
temp <- data_wesad[!(data_wesad$Subject == subject),]
val$metric <- as.factor(val$metric)
val$Subject <- NULL
temp$metric <- as.factor(temp$metric)
temp$Subject <- NULL
model_svm = svm(x = temp[,1:10], y = temp$metric, kernel = "radial", cost = 5, scale = FALSE)
y_hat = predict(model_svm, newdata = val[,1:10])
acc <- sum(val$metric == y_hat)/nrow(val)
precision <- posPredValue(y_hat, val$metric, positive="1")
recall <- sensitivity(y_hat, val$metric, positive="1")
F1 <- (2 * precision * recall) / (precision + recall)
res <- cbind(subject, acc, precision, recall, F1)
res <- as.data.frame(res)
names(res) <- c("SUBJECT","ACC", "PRECISION", "RECALL", "F1")
results <- rbind(results, res)
}
print(mean(as.numeric(results$ACC))) # 0.5833315
write.csv(results, "Ex2_SVM_Wesad.csv", row.names = FALSE)
#########################################################################################################################################################
# Model training - Random Forest LOSO on SWELL, NEURO, WESAD
#########################################################################################################################################################
# SWELL
subjects <- unique(data_swell$Subject)
index <- 1
results <- NULL
for (subject in subjects)
{
print(subject)
val <- data_swell[data_swell$Subject == subject,]
temp <- data_swell[!(data_swell$Subject == subject),]
val$metric <- as.factor(val$metric)
val$Subject <- NULL
temp$metric <- as.factor(temp$metric)
temp$Subject <- NULL
model_rf = randomForest(x = temp[,1:10], y = temp$metric, ntree = 200, mtry=3, random_state = 123)
y_hat = predict(model_rf, newdata = val[,1:10])
acc <- sum(val$metric == y_hat)/nrow(val)
precision <- posPredValue(y_hat, val$metric, positive="1")
recall <- sensitivity(y_hat, val$metric, positive="1")
F1 <- (2 * precision * recall) / (precision + recall)
res <- cbind(subject, acc, precision, recall, F1)
res <- as.data.frame(res)
names(res) <- c("SUBJECT","ACC", "PRECISION", "RECALL", "F1")
results <- rbind(results, res)
}
print(mean(as.numeric(results$ACC))) # 0.6025758
write.csv(results, "Ex2_RF_Swell.csv", row.names = FALSE)
# NEURO
subjects <- unique(data_neuro$Subject)
index <- 1
results <- NULL
for (subject in subjects)
{
print(subject)
val <- data_neuro[data_neuro$Subject == subject,]
temp <- data_neuro[!(data_neuro$Subject == subject),]
val$metric <- as.factor(val$metric)
val$Subject <- NULL
temp$metric <- as.factor(temp$metric)
temp$Subject <- NULL
model_rf = randomForest(x = temp[,1:10], y = temp$metric, ntree = 200, mtry=3, random_state = 123)
y_hat = predict(model_rf, newdata = val[,1:10])
acc <- sum(val$metric == y_hat)/nrow(val)
precision <- posPredValue(y_hat, val$metric, positive="1")
recall <- sensitivity(y_hat, val$metric, positive="1")
F1 <- (2 * precision * recall) / (precision + recall)
res <- cbind(subject, acc, precision, recall, F1)
res <- as.data.frame(res)
names(res) <- c("SUBJECT","ACC", "PRECISION", "RECALL", "F1")
results <- rbind(results, res)
}
print(mean(as.numeric(results$ACC))) # 0.5715223
write.csv(results, "Ex2_RF_Neuro.csv", row.names = FALSE)
# WESAD
subjects <- unique(data_wesad$Subject)
index <- 1
results <- NULL
for (subject in subjects)
{
print(subject)
val <- data_wesad[data_wesad$Subject == subject,]
temp <- data_wesad[!(data_wesad$Subject == subject),]
val$metric <- as.factor(val$metric)
val$Subject <- NULL
temp$metric <- as.factor(temp$metric)
temp$Subject <- NULL
model_rf = randomForest(x = temp[,1:10], y = temp$metric, ntree = 200, mtry=3, random_state = 123)
y_hat = predict(model_rf, newdata = val[,1:10])
acc <- sum(val$metric == y_hat)/nrow(val)
precision <- posPredValue(y_hat, val$metric, positive="1")
recall <- sensitivity(y_hat, val$metric, positive="1")
F1 <- (2 * precision * recall) / (precision + recall)
res <- cbind(subject, acc, precision, recall, F1)
res <- as.data.frame(res)
names(res) <- c("SUBJECT","ACC", "PRECISION", "RECALL", "F1")
results <- rbind(results, res)
}
print(mean(as.numeric(results$ACC))) # 0.6353137
write.csv(results, "Ex2_RF_Wesad.csv", row.names = FALSE)
#########################################################################################################################################################
# Model training - SVM. Train on swell, predict on NEURO and WESAD
#########################################################################################################################################################
data_swell$y <- as.factor(data_swell$metric)
data_neuro$y <- as.factor(data_neuro$metric)
data_wesad$y <- as.factor(data_wesad$metric)
model_svm = svm(x = data_swell[,1:10], y = data_swell$y, kernel = "radial", cost = 5, scale = FALSE)
y_hat = predict(model_svm, newdata = data_neuro[,1:10])
print(sum(as.numeric(data_neuro$y == y_hat))/nrow(data_neuro)) # 41.48%
# precision, recall, F1 score
precision <- posPredValue(y_hat, data_neuro$y, positive="1") # 34.5%
recall <- sensitivity(y_hat, data_neuro$y, positive="1") # 0.075%
F1 <- (2 * precision * recall) / (precision + recall) # 12.40%
colors<-c("white","lightsteelblue1","dodgerblue3")
text.scl<-5
actual = as.data.frame(table(data_neuro$y))
names(actual) = c("Actual","ActualFreq")
confusion = as.data.frame(table(data_neuro$y, y_hat))
names(confusion) = c("Actual","Predicted","Freq")
confusion = merge(confusion, actual, by=c('Actual','Actual'))
confusion$Percent = confusion$Freq/confusion$ActualFreq*100
confusion$ColorScale<-confusion$Percent*-1
confusion[which(confusion$Actual==confusion$Predicted),]$ColorScale<-confusion[which(confusion$Actual==confusion$Predicted),]$ColorScale*-1
confusion$Label<-paste(round(confusion$Percent,0),"%",sep="")
ggplot() +
geom_tile(aes(x=Actual, y=Predicted,fill=ColorScale),data=confusion, color="black",size=0.1) +
labs(x="Actual",y="Predicted") +
geom_text(aes(x=Actual,y=Predicted, label=Label),data=confusion, size=text.scl, colour="black") +
scale_fill_gradient2(low=colors[2],high=colors[3],mid=colors[1],midpoint = 0,guide='none') +
theme_classic() +
theme(axis.title = element_text(size = 20, family="Times New Roman",face="bold")) +
theme(axis.text=element_text(size=12, family="Times New Roman",face="bold"))
y_hat = predict(model_svm, newdata = data_wesad[,1:10])
print(sum(as.numeric(data_wesad$y == y_hat))/nrow(data_wesad)) # 63.4%
# precision, recall, F1 score
precision <- posPredValue(y_hat, data_wesad$y, positive="1") # 35.5
recall <- sensitivity(y_hat, data_wesad$y, positive="1") # 25.89
F1 <- (2 * precision * recall) / (precision + recall) # 29,95
colors<-c("white","lightsteelblue1","dodgerblue3")
text.scl<-5
actual = as.data.frame(table(data_wesad$y))
names(actual) = c("Actual","ActualFreq")
confusion = as.data.frame(table(data_wesad$y, y_hat))
names(confusion) = c("Actual","Predicted","Freq")
confusion = merge(confusion, actual, by=c('Actual','Actual'))
confusion$Percent = confusion$Freq/confusion$ActualFreq*100
confusion$ColorScale<-confusion$Percent*-1
confusion[which(confusion$Actual==confusion$Predicted),]$ColorScale<-confusion[which(confusion$Actual==confusion$Predicted),]$ColorScale*-1
confusion$Label<-paste(round(confusion$Percent,0),"%",sep="")
ggplot() +
geom_tile(aes(x=Actual, y=Predicted,fill=ColorScale),data=confusion, color="black",size=0.1) +
labs(x="Actual",y="Predicted") +
geom_text(aes(x=Actual,y=Predicted, label=Label),data=confusion, size=text.scl, colour="black") +
scale_fill_gradient2(low=colors[2],high=colors[3],mid=colors[1],midpoint = 0,guide='none') +
theme_classic() +
theme(axis.title = element_text(size = 20, family="Times New Roman",face="bold")) +
theme(axis.text=element_text(size=12, family="Times New Roman",face="bold"))
#########################################################################################################################################################
# Model training - Random Forest. Train on swell, predict on NEURO and WESAD
#########################################################################################################################################################
model_rf = randomForest(x = data_swell[,1:10], y = data_swell$y, ntree = 200, random_state = 123)
y_hat = predict(model_rf, newdata = data_neuro[,1:10])
print(sum(as.numeric(data_neuro$y == y_hat))/nrow(data_neuro)) # 46.18%
# precision, recall, F1 score
precision <- posPredValue(y_hat, data_neuro$y, positive="1") # 51.89
recall <- sensitivity(y_hat, data_neuro$y, positive="1") # 24.37
F1 <- (2 * precision * recall) / (precision + recall) # 33.16
colors<-c("white","lightsteelblue1","dodgerblue3")
text.scl<-5
actual = as.data.frame(table(data_neuro$y))
names(actual) = c("Actual","ActualFreq")
confusion = as.data.frame(table(data_neuro$y, y_hat))
names(confusion) = c("Actual","Predicted","Freq")
confusion = merge(confusion, actual, by=c('Actual','Actual'))
confusion$Percent = confusion$Freq/confusion$ActualFreq*100
confusion$ColorScale<-confusion$Percent*-1
confusion[which(confusion$Actual==confusion$Predicted),]$ColorScale<-confusion[which(confusion$Actual==confusion$Predicted),]$ColorScale*-1
confusion$Label<-paste(round(confusion$Percent,0),"%",sep="")
ggplot() +
geom_tile(aes(x=Actual, y=Predicted,fill=ColorScale),data=confusion, color="black",size=0.1) +
labs(x="Actual",y="Predicted") +
geom_text(aes(x=Actual,y=Predicted, label=Label),data=confusion, size=text.scl, colour="black") +
scale_fill_gradient2(low=colors[2],high=colors[3],mid=colors[1],midpoint = 0,guide='none') +
theme_classic() +
theme(axis.title = element_text(size = 20, family="Times New Roman",face="bold")) +
theme(axis.text=element_text(size=12, family="Times New Roman",face="bold"))
y_hat = predict(model_rf, newdata = data_wesad[,1:10])
print(sum(as.numeric(data_wesad$y == y_hat))/nrow(data_wesad)) # 62.0%
# precision, recall, F1 score
precision <- posPredValue(y_hat, data_wesad$y, positive="1") # 31.55
recall <- sensitivity(y_hat, data_wesad$y, positive="1") # 21.86
F1 <- (2 * precision * recall) / (precision + recall) # 25.8
colors<-c("white","lightsteelblue1","dodgerblue3")
text.scl<-5
actual = as.data.frame(table(data_wesad$y))
names(actual) = c("Actual","ActualFreq")
confusion = as.data.frame(table(data_wesad$y, y_hat))
names(confusion) = c("Actual","Predicted","Freq")
confusion = merge(confusion, actual, by=c('Actual','Actual'))
confusion$Percent = confusion$Freq/confusion$ActualFreq*100
confusion$ColorScale<-confusion$Percent*-1
confusion[which(confusion$Actual==confusion$Predicted),]$ColorScale<-confusion[which(confusion$Actual==confusion$Predicted),]$ColorScale*-1
confusion$Label<-paste(round(confusion$Percent,0),"%",sep="")
ggplot() +
geom_tile(aes(x=Actual, y=Predicted,fill=ColorScale),data=confusion, color="black",size=0.1) +
labs(x="Actual",y="Predicted") +
geom_text(aes(x=Actual,y=Predicted, label=Label),data=confusion, size=text.scl, colour="black") +
scale_fill_gradient2(low=colors[2],high=colors[3],mid=colors[1],midpoint = 0,guide='none') +
theme_classic() +
theme(axis.title = element_text(size = 20, family="Times New Roman",face="bold")) +
theme(axis.text=element_text(size=12, family="Times New Roman",face="bold"))
data_swell$y <- NULL
data_neuro$y <- NULL
data_wesad$y <- NULL
#########################################################################################################################################################
# Parameter Search
#########################################################################################################################################################
hyper_grid <- expand.grid(
max_depth = c(8),
eta = c(0.08, 0.1,0.5),
subsample = c(0.35, 0.5, 0.7),
colsample_bytree = c(0.4, 0.6, 0.8),
rmse = 0,
trees = 0
)
# grid-search with 10 folds
for (i in seq_len(nrow(hyper_grid))) {
set.seed(123)
m <- xgb.cv(
data = as.matrix(data_swell[,1:10]),
label = data_swell$metric,
nrounds = 100,
early_stopping_rounds = 3,
objective = "reg:logistic",
nfold = 10,
verbose = 0,
params = list(
eta = hyper_grid$eta[i],
max_depth = hyper_grid$max_depth[i],
subsample = hyper_grid$subsample[i],
colsample_bytree = hyper_grid$colsample_bytree[i]
)
)
hyper_grid$rmse[i] <- min(m$evaluation_log$test_rmse_mean)
hyper_grid$trees[i] <- m$best_iteration
}
# display best parameters
hyper_grid %>%
filter(rmse > 0) %>%
arrange(rmse) %>%
glimpse()
#########################################################################################################################################################
# Model Training
#########################################################################################################################################################
train.index <- createDataPartition(data_swell$Subject, p = .7, list = FALSE) # 70/30 split along subjects
train <- data_swell[train.index,]
test <- data_swell[-train.index,]
# class balancing
scale_pos_weight = nrow(train[train$metric==0,])/nrow(train[train$metric==1,])
# found using hyper parameter search
params <- list(
eta = 0.5,
max_depth = 8,
subsample = 0.7,
colsample_bytree = 0.4
)
dtrain <- xgb.DMatrix(data = as.matrix(train[,1:10]), label = train$metric)
dtest <- xgb.DMatrix(data = as.matrix(test[,1:10]), label = test$metric)
watchlist <- list(train = dtrain, test = dtest)
model <- xgb.train(
params = params,
data = dtrain,
watchlist = watchlist,
objective = "reg:logistic",
nrounds = 5000,
early_stopping_rounds = 3,
verbose = 1,
scale_pos_weight = scale_pos_weight
)
# Best iteration:
# [73] train-rmse:0.023707 test-rmse:0.024838
importance_matrix <- xgb.importance(model = model)
importance_matrix$Feature <- toupper(importance_matrix$Feature)
xgb.ggplt <- xgb.ggplot.importance(importance_matrix = importance_matrix, top_n = 10)
xgb.ggplt + theme(text = element_text(size = 14),
axis.text.x = element_text(size = 14, angle = 45, hjust = 1)) +
theme_classic() + scale_color_lancet() + scale_fill_lancet() +
theme(axis.title = element_text(size = 20, family="Times New Roman",face="bold")) +
theme(axis.text=element_text(size=16, family="Times New Roman",face="bold")) +
theme(plot.title = element_text(family="Times New Roman",face="bold")) +
theme(legend.text = element_text(family="Times New Roman",face="bold", size=20)) +
theme(legend.title = element_text(family="Times New Roman",face="bold", size=20))
# now validate against unseen neuro
pred <- predict(model, as.matrix(data_neuro[,1:10]))
pred <- round(pred) # round to 0/1 for binary classification (no stress vs. stress)
print(sum(as.numeric(data_neuro$metric == pred))/nrow(data_neuro)) # 50%
# precision, recall, F1 score
precision <- posPredValue(as.factor(pred), as.factor(data_neuro$metric), positive="1") # 0.53
recall <- sensitivity(as.factor(pred), as.factor(data_neuro$metric), positive="1") # 0.24
F1 <- (2 * precision * recall) / (precision + recall) # 0.33
colors<-c("white","lightsteelblue1","dodgerblue3")
text.scl<-5
actual = as.data.frame(table(as.factor(data_neuro$metric)))
names(actual) = c("Actual","ActualFreq")
confusion = as.data.frame(table(as.factor(data_neuro$metric), as.factor(pred)))
names(confusion) = c("Actual","Predicted","Freq")
confusion = merge(confusion, actual, by=c('Actual','Actual'))
confusion$Percent = confusion$Freq/confusion$ActualFreq*100
confusion$ColorScale<-confusion$Percent*-1
confusion[which(confusion$Actual==confusion$Predicted),]$ColorScale<-confusion[which(confusion$Actual==confusion$Predicted),]$ColorScale*-1
confusion$Label<-paste(round(confusion$Percent,0),"%",sep="")
ggplot() +
geom_tile(aes(x=Actual, y=Predicted,fill=ColorScale),data=confusion, color="black",size=0.1) +
labs(x="Actual",y="Predicted") +
geom_text(aes(x=Actual,y=Predicted, label=Label),data=confusion, size=text.scl, colour="black") +
scale_fill_gradient2(low=colors[2],high=colors[3],mid=colors[1],midpoint = 0,guide='none') +
theme_classic() +
theme(axis.title = element_text(size = 20, family="Times New Roman",face="bold")) +
theme(axis.text=element_text(size=12, family="Times New Roman",face="bold"))
# validate against unseen wesad
pred <- predict(model, as.matrix(data_wesad[,1:10]))
pred <- round(pred) # round to 0/1 for binary classification (no stress vs. stress)
print(sum(as.numeric(data_wesad$metric == pred))/nrow(data_wesad)) # 68%
# precision, recall, F1 score
precision <- posPredValue(as.factor(pred), as.factor(data_wesad$metric), positive="1") # 0.36
recall <- sensitivity(as.factor(pred), as.factor(data_wesad$metric), positive="1") # 0.59
F1 <- (2 * precision * recall) / (precision + recall) # 0.45
colors<-c("white","lightsteelblue1","dodgerblue3")
text.scl<-5
actual = as.data.frame(table(as.factor(data_wesad$metric)))
names(actual) = c("Actual","ActualFreq")
confusion = as.data.frame(table(as.factor(data_wesad$metric), as.factor(pred)))
names(confusion) = c("Actual","Predicted","Freq")
confusion = merge(confusion, actual, by=c('Actual','Actual'))
confusion$Percent = confusion$Freq/confusion$ActualFreq*100
confusion$ColorScale<-confusion$Percent*-1
confusion[which(confusion$Actual==confusion$Predicted),]$ColorScale<-confusion[which(confusion$Actual==confusion$Predicted),]$ColorScale*-1
confusion$Label<-paste(round(confusion$Percent,0),"%",sep="")
ggplot() +
geom_tile(aes(x=Actual, y=Predicted,fill=ColorScale),data=confusion, color="black",size=0.1) +
labs(x="Actual",y="Predicted") +
geom_text(aes(x=Actual,y=Predicted, label=Label),data=confusion, size=text.scl, colour="black") +
scale_fill_gradient2(low=colors[2],high=colors[3],mid=colors[1],midpoint = 0,guide='none') +
theme_classic() +
theme(axis.title = element_text(size = 20, family="Times New Roman",face="bold")) +
theme(axis.text=element_text(size=12, family="Times New Roman",face="bold"))