-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExperiment7.R
409 lines (344 loc) · 15.7 KB
/
Experiment7.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
# Experiment 7
# 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(ggplot2)
library(dplyr)
library(ggsci)
library(caret)
library(xgboost)
library(zoo)
library(e1071)
library(stresshelpers)
library(keras)
library(tensorflow)
library(TTR)
options(scipen=999)
set.seed(123)
tensorflow::set_random_seed(123)
#########################################################################################################################################################
# Load and Prep StressData for Training
#########################################################################################################################################################
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_ubfc <- stresshelpers::make_ubfc_data('UBFC', feature_engineering = TRUE)
# balancing across data sources for XGB
data_neuro$Balance <- 0
data_swell$Balance <- 0
data_wesad$Balance <- 0
data_ubfc$Balance <- 1
data <- rbind(data_neuro, data_swell, data_wesad, data_ubfc) # 99 subjects
data <- data %>% select(hrrange, hrvar, hrstd, hrmin, edarange, edastd, edavar, hrkurt, edamin, hrmax, Subject, metric, Balance)
rm(data_neuro, data_swell, data_wesad, data_ubfc)
gc()
#########################################################################################################################################################
# Model training - xgboost using optimal parameters with LOSO
#########################################################################################################################################################
subjects <- unique(data$Subject)
index <- 1
results <- NULL
# ensemble weighting
weighted_long <- function(xgb, ann) (xgb*0.4) + (ann*0.6)
weighted_short <- function(xgb, ann) (xgb*0.7) + (ann*0.3)
# found using hyper parameter search
params <- list(
eta = 0.5,
max_depth = 8,
subsample = 0.70,
colsample_bytree = 0.8
)
for (subject in subjects)
{
print(index)
val <- data[data$Subject == subject,]
temp <- data[!(data$Subject == subject),]
train.index <- createDataPartition(temp$metric, p = .7, list = FALSE) # 70/30 train/test split along subject
train <- temp[train.index,]
test <- temp[-train.index,]
# class balancing
scale_pos_weight = nrow(train[train$Balance==0,])/nrow(train[train$Balance==1,])
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 <- xgb.train(
params = params,
data = dtrain,
objective = "reg:logistic",
watchlist = watchlist,
nrounds = 500,
early_stopping_rounds = 3,
scale_pos_weight = scale_pos_weight,
verbose = 0
)
x_train <- train[,1:10]
y_train <- train$metric
x_test <- test[,1:10]
y_test <- test$metric
# scale
x_train <- scale(x_train)
x_test <- scale(x_test, center = attr(x_train, "scaled:center") , scale = attr(x_train, "scaled:scale"))
model_nn <- keras_model_sequential()
model_nn %>%
layer_dense(
units = 10,
kernel_initializer = "normal",
activation = "relu",
input_shape = ncol(x_train)) %>%
layer_dense(
units = 4,
kernel_initializer = "normal",
activation = "relu") %>%
layer_dense(
units = 1,
kernel_initializer = "normal",
activation = "linear") %>%
compile(
loss = "mse",
optimizer = optimizer_adamax()
)
history <- fit(
object = model_nn,
x = x_train,
y = y_train,
batch_size = 512,
epochs = 120,
validation_data = list(x_test, y_test),
shuffle = TRUE,
callbacks = list(callback_early_stopping(monitor = "val_loss", patience = 3, restore_best_weights = TRUE)),
verbose = 0
)
x_val <- val[,1:10]
yhat_xgb <- predict(model_xgb, as.matrix(x_val))
x_val <- scale(x_val, center = attr(x_train, "scaled:center") , scale = attr(x_train, "scaled:scale"))
yhat_nn <- as.data.frame(predict(model_nn, x_val))
yhat_nn <- yhat_nn[,1]
yhat_nn <- (yhat_nn - min(yhat_nn)) / (max(yhat_nn) - min(yhat_nn))
yhat_xgb <- round(yhat_xgb)
yhat_nn <- round(yhat_nn)
if (nrow(val) < 1000)
{
yhat_ens <- weighted_short(yhat_xgb, yhat_nn)
}
else
{
yhat_ens <- weighted_long(yhat_xgb, yhat_nn)
}
yhat_ens <- round(yhat_ens)
acc_xgb <- sum(as.numeric(val$metric == yhat_xgb))/nrow(val)
acc_ann <- sum(as.numeric(val$metric == yhat_nn))/nrow(val)
acc_ens <- sum(as.numeric(val$metric == yhat_ens))/nrow(val)
# precision, recall, F1 score
precision <- posPredValue(factor(yhat_ens, levels=c(0,1)), factor(val$metric, levels=c(0,1)), positive="1")
recall <- sensitivity(factor(yhat_ens, levels=c(0,1)), factor(val$metric, levels=c(0,1)), positive="1")
F1 <- (2 * precision * recall) / (precision + recall)
res <- cbind(subject, acc_xgb, acc_ann, acc_ens, precision, recall, F1)
res <- as.data.frame(res)
names(res) <- c("SUBJECT","XGB","ANN","ENS", "PRECISION", "RECALL", "F1")
results <- rbind(results, res)
index <- index + 1
}
results$XGB <- as.numeric(results$XGB)
results$ANN <- as.numeric(results$ANN)
results$ENS <- as.numeric(results$ENS)
results$PRECISION <- as.numeric(results$PRECISION)
results$RECALL <- as.numeric(results$RECALL)
results$F1 <- as.numeric(results$F1)
print(mean(results$XGB, na.rm=TRUE)) # 0.7865308
print(mean(results$ANN, na.rm=TRUE)) # 0.5523054
print(mean(results$ENS, na.rm=TRUE)) # 0.8033597
print(mean(results$PRECISION, na.rm=TRUE)) # 0.5637483
print(mean(results$RECALL, na.rm=TRUE)) # 0.752046
print(mean(results$F1, na.rm=TRUE)) # 0.468201
#########################################################################################################################################################
# Test
#########################################################################################################################################################
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_ubfc <- stresshelpers::make_ubfc_data('UBFC', feature_engineering = TRUE)
# balancing across data sources for XGB
data_neuro$Balance <- 0
data_swell$Balance <- 0
data_wesad$Balance <- 0
data_ubfc$Balance <- 1
data <- rbind(data_neuro, data_swell, data_wesad, data_ubfc) # 99 subjects
data <- data %>% select(hrrange, hrvar, hrstd, hrmin, edarange, edastd, edavar, hrkurt, edamin, hrmax, Subject, metric, Balance)
val <- data[(data$Subject %in% c("S9","W14","N13")),]
data <- data[!(data$Subject %in% c("S9","W14","N13")),]
rm(data_neuro, data_swell, data_wesad, data_ubfc)
gc()
# ensemble weighting
weighted_long <- function(xgb, ann) (xgb*0.4) + (ann*0.6)
# found using hyper parameter search
params <- list(
eta = 0.5,
max_depth = 8,
subsample = 0.70,
colsample_bytree = 0.8
)
train.index <- createDataPartition(data$metric, p = .7, list = FALSE) # 70/30 train/test split along subject
train <- data[train.index,]
test <- data[-train.index,]
# class balancing
scale_pos_weight = nrow(train[train$Balance==0,])/nrow(train[train$Balance==1,])
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 <- xgb.train(
params = params,
data = dtrain,
objective = "reg:logistic",
watchlist = watchlist,
nrounds = 500,
early_stopping_rounds = 3,
scale_pos_weight = scale_pos_weight,
verbose = 0
)
x_train <- train[,1:10]
y_train <- train$metric
x_test <- test[,1:10]
y_test <- test$metric
# scale
x_train <- scale(x_train)
x_test <- scale(x_test, center = attr(x_train, "scaled:center") , scale = attr(x_train, "scaled:scale"))
model_nn <- keras_model_sequential()
model_nn %>%
layer_dense(
units = 10,
kernel_initializer = "normal",
activation = "relu",
input_shape = ncol(x_train)) %>%
layer_dense(
units = 4,
kernel_initializer = "normal",
activation = "relu") %>%
layer_dense(
units = 1,
kernel_initializer = "normal",
activation = "linear") %>%
compile(
loss = "mse",
optimizer = optimizer_adamax()
)
history <- fit(
object = model_nn,
x = x_train,
y = y_train,
batch_size = 512,
epochs = 120,
validation_data = list(x_test, y_test),
shuffle = TRUE,
callbacks = list(callback_early_stopping(monitor = "val_loss", patience = 3, restore_best_weights = TRUE)),
verbose = 0
)
temp <- val[val$Subject=='S9',]
x_val <- temp[,1:10]
yhat_xgb <- predict(model_xgb, as.matrix(x_val))
x_val <- scale(x_val, center = attr(x_train, "scaled:center") , scale = attr(x_train, "scaled:scale"))
yhat_nn <- as.data.frame(predict(model_nn, x_val))
yhat_nn <- yhat_nn[,1]
yhat_nn <- (yhat_nn - min(yhat_nn)) / (max(yhat_nn) - min(yhat_nn))
yhat_ens <- weighted_long(yhat_xgb, yhat_nn)
temp <- cbind(yhat_xgb, yhat_nn, yhat_ens, temp$metric)
temp <- as.data.frame(temp)
names(temp) <- c("xgb","ann","ens","metric")
temp$ID <- seq.int(nrow(temp))
ggplot(temp, aes(x=ID)) +
geom_area(aes(y = metric, colour="STRESS"), fill="#FF6666", alpha=0.4, size=0.5) +
geom_line(aes(y = runMean(ens, 120) , colour="ENS"), size=0.5) +
scale_color_lancet() + scale_fill_lancet() +
labs(colour="Model") +
guides(color = guide_legend(override.aes = list(fill="white", size=5))) +
theme_classic() + ylab('Stress - S9') + xlab('Time (seconds)') +
scale_x_continuous(breaks=seq(0,nrow(temp)+2400,2400)) +
theme(axis.title = element_text(size = 22, family="Times New Roman",face="bold")) +
theme(axis.text=element_text(size=18, 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=18)) +
theme(legend.title = element_text(family="Times New Roman",face="bold", size=18)) +
theme(
axis.title.y = element_text(vjust = +1),
axis.title.x = element_text(vjust = -0.8)
)
temp <- val[val$Subject=='W14',]
x_val <- temp[,1:10]
yhat_xgb <- predict(model_xgb, as.matrix(x_val))
x_val <- scale(x_val, center = attr(x_train, "scaled:center") , scale = attr(x_train, "scaled:scale"))
yhat_nn <- as.data.frame(predict(model_nn, x_val))
yhat_nn <- yhat_nn[,1]
yhat_nn <- (yhat_nn - min(yhat_nn)) / (max(yhat_nn) - min(yhat_nn))
yhat_ens <- weighted_long(yhat_xgb, yhat_nn)
temp <- cbind(yhat_xgb, yhat_nn, yhat_ens, temp$metric)
temp <- as.data.frame(temp)
names(temp) <- c("xgb","ann","ens","metric")
temp$ID <- seq.int(nrow(temp))
ggplot(temp, aes(x=ID)) +
geom_area(aes(y = metric, colour="STRESS"), fill="#FF6666", alpha=0.4, size=0.5) +
geom_line(aes(y = runMean(ens, 60) , colour="ENS"), size=0.5) +
scale_color_lancet() + scale_fill_lancet() +
labs(colour="Model") +
guides(color = guide_legend(override.aes = list(fill="white", size=5))) +
theme_classic() + ylab('Stress - W14') + xlab('Time (seconds)') +
scale_x_continuous(breaks=seq(0,nrow(temp)+120,120)) +
theme(axis.title = element_text(size = 22, family="Times New Roman",face="bold")) +
theme(axis.text=element_text(size=18, 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=18)) +
theme(legend.title = element_text(family="Times New Roman",face="bold", size=18)) +
theme(
axis.title.y = element_text(vjust = +1),
axis.title.x = element_text(vjust = -0.8)
)
temp <- val[val$Subject=='N13',]
x_val <- temp[,1:10]
yhat_xgb <- predict(model_xgb, as.matrix(x_val))
x_val <- scale(x_val, center = attr(x_train, "scaled:center") , scale = attr(x_train, "scaled:scale"))
yhat_nn <- as.data.frame(predict(model_nn, x_val))
yhat_nn <- yhat_nn[,1]
yhat_nn <- (yhat_nn - min(yhat_nn)) / (max(yhat_nn) - min(yhat_nn))
yhat_ens <- weighted_long(yhat_xgb, yhat_nn)
temp <- cbind(yhat_xgb, yhat_nn, yhat_ens, temp$metric)
temp <- as.data.frame(temp)
names(temp) <- c("xgb","ann","ens","metric")
temp$ID <- seq.int(nrow(temp))
ggplot(temp, aes(x=ID)) +
geom_area(aes(y = metric, colour="STRESS"), fill="#FF6666", alpha=0.4, size=0.5) +
geom_line(aes(y = runMean(ens, 60) , colour="ENS"), size=0.5) +
scale_color_lancet() + scale_fill_lancet() +
labs(colour="Model") +
guides(color = guide_legend(override.aes = list(fill="white", size=5))) +
theme_classic() + ylab('Stress - N13') + xlab('Time (seconds)') +
scale_x_continuous(breaks=seq(0,nrow(temp)+120,120)) +
theme(axis.title = element_text(size = 22, family="Times New Roman",face="bold")) +
theme(axis.text=element_text(size=18, 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=18)) +
theme(legend.title = element_text(family="Times New Roman",face="bold", size=18)) +
theme(
axis.title.y = element_text(vjust = +1),
axis.title.x = element_text(vjust = -0.8)
)