forked from mkmiecik14/ml-eeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing-script.R
104 lines (81 loc) · 2.26 KB
/
testing-script.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
# Loading libraries
library(eegkitdata)
library(tidyverse)
data(eegdata) # loads in data
# Data set information
# unique(eegdata$channel)
# unique(eegdata$condition)
# unique(eegdata$trial)
# Subject-wise grand averages
ga_ss <-
eegdata %>%
group_by(subject, channel, time) %>%
summarise(m = mean(voltage)) %>%
ungroup()
# Subject-wise grand average plots
# ggplot(ga_ss, aes(time, m, group = subject)) +
# geom_line() +
# facet_wrap(~channel)
# grand average (all)
ga <-
ga_ss %>%
group_by(time, channel) %>%
summarise(M = mean(m), sd = sd(m), n = n(), sem = sd/sqrt(n)) %>%
ungroup()
# Grand average plot
# ggplot(ga, aes(time, M, group = channel)) +
# geom_line()
# grand average (groups)
ga_groups <-
ga_ss %>%
mutate(group = substr(subject, 4, 4)) %>%
group_by(group, channel, time) %>%
summarise(M = mean(m), sd = sd(m), n = n(), sem = sd/sqrt(n)) %>%
ungroup()
midline <- c("FPZ", "AFZ", "FZ", "FCZ", "CZ", "CPZ", "PZ", "POZ", "OZ")
ggplot(
ga_groups %>% filter(channel %in% midline),
aes(time, M, group = group, color = group)
) +
geom_line() +
facet_wrap(~channel)
ga_groups_midline <-
ga_groups %>%
filter(channel %in% midline) %>%
mutate(channel = fct_relevel(channel, midline), time_2 = time*(1000/256))
ggplot(
ga_groups_midline,
aes(time_2, M, group = group, color = group)
) +
geom_line() +
scale_x_continuous(breaks = seq(0, 1000, 100)) +
facet_wrap(~channel)
# Getting data ready for ML
head(eegdata)
channel_key <-
tibble(
channel = unique(eegdata$channel),
chan_key = 1:length(unique(eegdata$channel))
)
eegdata_ml <-
eegdata %>%
left_join(., channel_key, by = "channel") %>%
mutate(
subject = substr(subject, 5, 11),
group = ifelse(group == "a", 1, 2) # alcoholic = 1, control = 2
) %>%
select(-condition, -channel)
# 3d array
subs <- unique(ga_ss$subject)
chans <- unique(ga_ss$channel)
times <- unique(ga_ss$time)
ga_array <- vector(mode = "list", length = length(subs))
ga_ss_spread <-
ga_ss %>%
spread(channel, m) %>%
mutate(group = substr(subject, 4, 4))
for(i in 1:length(subs)){
this_sub <- subs[i] # channel selected for this loop
this_slice <- ga_ss_spread %>% filter(subject %in% this_sub) %>% select(-subject)
ga_array[[i]] <- this_slice
}