-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_analyzation.R
More file actions
279 lines (228 loc) · 8.43 KB
/
Copy pathdata_analyzation.R
File metadata and controls
279 lines (228 loc) · 8.43 KB
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
# Interaction fatigue <-> physiological data
# 1. Read data
path = './Output/'
data.mean = read.csv(paste(path, "combined_data_mean.csv", sep=""), header=TRUE, sep=",")
str(data.mean)
n = nrow(data.mean)
p = 12
variables = names(dat)[1:p]
# factor variables
data.mean$VAS = as.factor(data.mean$VAS)
levels(data.mean$VAS)
levels(data.mean$VAS) = c("vigilant", "fatigued")
data.mean$MF = as.factor(data.mean$MF)
levels(data.mean$MF)
levels(data.mean$MF) = c("vigilant", "fatigued")
data.mean$phF = as.factor(data.mean$phF)
levels(data.mean$phF)
levels(data.mean$phF) = c("vigilant", "fatigued")
data.mean$ReIP = as.factor(data.mean$ReIP)
levels(data.mean$ReIP)
levels(data.mean$ReIP) = c("worse", "same", "better")
# TODO: mean of ActivitiyClass (a factor var.) is meaningless -> change! (to most common value)
unique(data.mean$ActivityClass)
data.mean$ActivityClass = as.factor(data.mean$ActivityClass)
levels(data.mean$ActivityClass)
levels(data.mean$ActivityClass) = c("undefined", "resting", "other", "biking", "running", "walking")
# remove metadata
dat = subset(data.mean, select=c(-X, -subjectID, -date, -sport, -n_answers, -timezone))
# TODO: include ActivityClass
p = 11
variables = names(dat)[1:p]
dat = subset(dat, select=-ActivityClass)
str(dat)
# 2. Statistical Analysis
library(ggplot2)
library(GGally)
library(reshape)
library(plot.matrix)
melted = melt(dat)
# intra-subject variability
dat2 = subset(data.mean, select=c(-X, -date, -sport, -n_answers, -timezone, -ActivityClass))
dat2$subjectID = as.factor(dat2$subjectID)
melted2 = melt(dat2)
ggplot(data=melted2, aes(x=variable, y=value)) +
geom_boxplot(aes(fill=subjectID)) +
facet_wrap(~variable, scales="free")
# Shapiro-Wilk test
p.values = matrix(nrow=27, ncol=p)
sample.size = matrix(nrow=27, ncol=p)
for (subj in 1:27) {
for (var in 1:p) {
variable = names(data.mean)[var + 3]
data.subj = data.mean[, variable][data.mean$subjectID == subj]
sample.size[subj, var] = length(na.omit(data.subj))
}
}
sample.size
for (subj in 1:27) {
for (var in 1:p) {
variable = names(data.mean)[var + 3]
data.subj = data.mean[, variable][data.mean$subjectID == subj]
p.values[subj, var] = ifelse(sample.size[subj, var] < 3, NA, shapiro.test(data.subj)$p.value)
}
}
p.values
alpha = 0.05
result = p.values < alpha # H0: data is normally distributed -> if p < alpha: reject H0
colnames(result) = names(data.mean)[4:(3+p)]
plot(result, col=c("green", "red"), las=2, xlab="Variable", ylab="Subject",
main="Shapiro-Wilk test (red: reject H0 (normality assumption) under a = 0.05)")
# questionnaires per subject
y = numeric(27)
for (i in 1:27) {
y[i] = sum(dat2$subjectID == i)
}
x = 1:27
table(data.mean$subjectID) # treats as categorical data with multiple draws
barplot(table(data.mean$subjectID),
main="Days with filled out questionnaires",
xlab="subject",
ylab="days")
sort(table(data.mean$subjectID))[23:27] # top 4 “contributors“
(sum(sort(table(data.mean$subjectID))[23:27]) / n) * 100 # 4/27 give data for 65%
# VAS
sub = table(data.mean$VAS, data.mean$subjectID) # with fatigue labels
barplot(sub,
main="Days with filled out questionnaires (fatigue: VAS)",
xlab="subject",
ylab="days",
col=c("blue", "red"))
legend("topleft", legend=rownames(sub), fill=c("blue", "red"))
apply(sub, MARGIN=1, FUN=sum) # ratio vigilant/fatigued
apply(sub, MARGIN=1, FUN=sum) / sum(sub) # percentage vigilant/fatigued
# phF
sub = table(data.mean$phF, data.mean$subjectID) # with fatigue labels
barplot(sub,
main="Days with filled out questionnaires (fatigue: phF)",
xlab="subject",
ylab="days",
col=c("blue", "red"))
legend("topleft", legend=rownames(sub), fill=c("blue", "red"))
apply(sub, MARGIN=1, FUN=sum) # ratio vigilant/fatigued
apply(sub, MARGIN=1, FUN=sum) / sum(sub) # percentage vigilant/fatigued
# MF
sub = table(data.mean$MF, data.mean$subjectID) # with fatigue labels
barplot(sub,
main="Days with filled out questionnaires (fatigue: MF)",
xlab="subject",
ylab="days",
col=c("blue", "red"))
legend("topleft", legend=rownames(sub), fill=c("blue", "red"))
apply(sub, MARGIN=1, FUN=sum) # ratio vigilant/fatigued
apply(sub, MARGIN=1, FUN=sum) / sum(sub) # percentage vigilant/fatigued
# boxplots
# boxplot for relationship: VAS <-> physiological variables
ggplot(data=melted, aes(x=variable, y=value)) +
geom_boxplot(aes(fill=VAS)) +
facet_wrap(~variable, scales="free")
# boxplot for relationship: phF <-> physiological variables
ggplot(data=melted, aes(x=variable, y=value)) +
geom_boxplot(aes(fill=phF)) +
facet_wrap(~variable, scales="free")
# boxplot for relationship: MF <-> physiological variables
ggplot(data=melted, aes(x=variable, y=value)) +
geom_boxplot(aes(fill=MF)) +
facet_wrap(~variable, scales="free")
# boxplot for relationship: ReIP <-> physiological variables
ggplot(data=melted, aes(x=variable, y=value)) +
geom_boxplot(aes(fill=ReIP)) +
facet_wrap(~variable, scales="free")
# t-test
t.tests = data.frame(matrix(nrow=3, ncol=p),
row.names=c("VAS", "phF", "MF"))
colnames(t.tests) = variables
for (fatigue in 12:14) {
p.values = numeric(p)
for (col in 1:p) {
p.values[col] = (t.test(dat[, col]~dat[, fatigue]))$p.value
}
t.tests[fatigue-11, ] = p.values
}
t.tests
# a) significance = 0.01
significance.level = 0.01
plot(as.matrix(t.tests < significance.level), las=2, cex.axis=0.7, col=c("white", "black"))
response = matrix(nrow=3, ncol=p)
colnames(response) = variables
rownames(response) = c("VAS", "phF", "MF")
significance = t.tests < significance.level
fatigue = c("VAS", "phF", "MF")
for (row in 1:3) {
for (col in 1:11) {
label = fatigue[row]
var = variables[col]
selection.fatigue = dat[, label] == "fatigued"
selection.vigilant = dat[, label] == "vigilant"
var.fatigue = na.omit(dat[selection.fatigue, var])
var.vigilant = na.omit(dat[selection.vigilant, var])
mean.fatigue = mean(var.fatigue)
mean.vigilant = mean(var.vigilant)
# only if significant
if (significance[row, col]) {
response[row, col] = ifelse(mean.fatigue > mean.vigilant,
"↑", "↓")
}
else {
response[row, col] = NA
}
}
}
response
plot(response, las=2, cex.axis=0.7, col=c("green", "red"),
main="physiological response to fatigue")
# b) significance = 0.05
significance.level = 0.05
plot(as.matrix(t.tests < significance.level), las=2, cex.axis=0.7, col=c("white", "black"))
response = matrix(nrow=3, ncol=p)
colnames(response) = variables
rownames(response) = c("VAS", "phF", "MF")
significance = t.tests < significance.level
fatigue = c("VAS", "phF", "MF")
for (row in 1:3) {
for (col in 1:11) {
label = fatigue[row]
var = variables[col]
selection.fatigue = dat[, label] == "fatigued"
selection.vigilant = dat[, label] == "vigilant"
var.fatigue = na.omit(dat[selection.fatigue, var])
var.vigilant = na.omit(dat[selection.vigilant, var])
mean.fatigue = mean(var.fatigue)
mean.vigilant = mean(var.vigilant)
# only if significant
if (significance[row, col]) {
response[row, col] = ifelse(mean.fatigue > mean.vigilant,
"↑", "↓")
}
else {
response[row, col] = NA
}
}
}
response
plot(response, las=2, cex.axis=0.7, col=c("green", "red"),
main="physiological response to fatigue")
# pairs plot
pairs(dat)
pairs(subset(dat, select=variables), cex.labels=1.2)
# covariance
covariance.matrix = cov(na.omit(subset(dat, select=variables)))
for (i in 1:ncol(covariance.matrix)) { # set upper triangle to NaN
for (j in i:ncol(covariance.matrix)) {
covariance.matrix[i, j] = NA
}
}
plot(covariance.matrix, las=2, cex.axis=0.7, breaks=6, digits=4, text.cell=list(cex=0.5)) # different than in suppl. material???
# correlation
correlation.matrix = cor(na.omit(subset(dat, select=variables)), method="spearman")
for (i in 1:ncol(correlation.matrix)) { # set upper triangle to NaN
for (j in i:ncol(correlation.matrix)) {
correlation.matrix[i, j] = NA
}
}
plot(correlation.matrix, las=2, cex.axis=0.7, breaks=6, digits=4, text.cell=list(cex=1.5)) # different than in suppl. material???
plot(abs(correlation.matrix), las=2, cex.axis=0.7, breaks=6, digits=4, text.cell=list(cex=1.5)) # different than in suppl. material???
# histogram
ggplot(data=melted, aes(x=value)) +
geom_histogram(position="identity") +
facet_wrap(~variable, scales="free")