KBB rsEEG Power Spectrum - LeoLedesma237/LeoWebsite GitHub Wiki
Part 1: Loading in the data
# Set working directory
setwd("Z:/Leo/KBB_EEG_Pilot/FFT rsEEG data")
# Obtain the names of all of the Eyes open files
Eyes.Open.FFT <- list.files(pattern = "Open")
# Load them into a list
data <- list()
for(ii in 1:length(Eyes.Open.FFT)) {
data[ii](/LeoLedesma237/LeoWebsite/wiki/ii) <- tibble(read.csv(Eyes.Open.FFT[ii]))
}
Part 2: Plot the Power Spectrum of Cz for each subject
# Obtain the power spectrum for Cz for each file
Cz.power.spectrum <- list()
for(ii in 1:length(data)) {
# Average all of the channels into one
current.data <- data[ii](/LeoLedesma237/LeoWebsite/wiki/ii)
Cz.channel <- current.data %>%
filter(Channels == "Cz")
removed.channels.variable <- Cz.channel[,4:length(Cz.channel)]
# Keep whole number data
Frequencies.of.interest <- paste("x", 1:45, sep = "")
Frequencies.of.interest.df <- removed.channels.variable %>%
select(all_of(Frequencies.of.interest))
x.axis.frequency.char <- names(Frequencies.of.interest.df)
x.axis.frequency.num <- as.numeric(gsub("x", "", x.axis.frequency.char))
y.axis = unlist(Frequencies.of.interest.df)
# Create a new data frame with x and y axis
Cz.power.spectrum[ii](/LeoLedesma237/LeoWebsite/wiki/ii) <- data.frame(Frequency = x.axis.frequency.num,
Power = y.axis) %>% tibble()
}
# Plot the data
Cz.power.spectrum[1](/LeoLedesma237/LeoWebsite/wiki/1) %>%
ggplot(aes(x = Frequency, y = Power, group = 1)) +
geom_line(size = 2, color = "red") +
geom_point() +
labs(title = "CZ Power Spectrum Eyes Open Ex: 1") +
theme_classic()
Cz.power.spectrum[2](/LeoLedesma237/LeoWebsite/wiki/2) %>%
ggplot(aes(x = Frequency, y = Power, group = 1)) +
geom_line(size = 2, color = "red") +
geom_point() +
labs(title = "CZ Power Spectrum Eyes Open Ex: 2") +
theme_classic()
Cz.power.spectrum[3](/LeoLedesma237/LeoWebsite/wiki/3) %>%
ggplot(aes(x = Frequency, y = Power, group = 1)) +
geom_line(size = 2, color = "red") +
geom_point() +
labs(title = "CZ Power Spectrum Eyes Open Ex: 3") +
theme_classic()
Part 3: Create an averaged Cz Power Spectrum including all subjects
# Combined into one
Averaged.Cz.power.spectrum <- data.frame(Power1 = Cz.power.spectrum[1](/LeoLedesma237/LeoWebsite/wiki/1)$Power,
Power2 = Cz.power.spectrum[2](/LeoLedesma237/LeoWebsite/wiki/2)$Power,
Power3 = Cz.power.spectrum[3](/LeoLedesma237/LeoWebsite/wiki/3)$Power)
# Obtain the mean Power for each Frequency
Combined.Cz.power.spectrum <- data.frame(Frequency = x.axis.frequency.num,
mean.Power = rowMeans(Averaged.Cz.power.spectrum))
# Plot the graph
Combined.Cz.power.spectrum %>%
ggplot(aes(x = Frequency, y = mean.Power, group = 1)) +
geom_line(size = 2, color = "blue") +
geom_point() +
labs(title = "CZ Mean Power Spectrum Eyes Open") +
theme_classic()
![CZ Mean Power Spectrum Eyes Open](https://github.com/LeoLedesma237/LeoWebsite/assets/81386436/a9d197f9-b259-459f-9140-5624b9e43637)