Skip to content

Commit

Permalink
update result file
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaneMcKeon committed Jul 29, 2024
1 parent e24cff7 commit 619105f
Show file tree
Hide file tree
Showing 2 changed files with 295 additions and 1,170 deletions.
165 changes: 1 addition & 164 deletions Cortical_SNR_Development/05_createImputed_PCA_dataFrames.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ naoutlier <- function(x) ifelse(outliers(x), NA, x)
# 40-40 ----
## SNR ----

SNRallChans_outlier <- read.csv('/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Cortical_SNR_Development/results/allSubjectsSNR_allChans_allfreqs.csv')
SNRallChans_outlier_new <- read.csv('/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Cortical_SNR_Development/results/allSubjectsSNR_allChans_allfreqs.csv')
chanLocs <- read.csv('/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Cortical_SNR_Development/resources/ChannelLocs.csv')%>% select(-type)

SNRallChans_outlier4040 <- SNRallChans_outlier %>% filter(freqs == 40 & hertz == 40)
Expand Down Expand Up @@ -281,169 +281,6 @@ allSNRmeasures4040 <- rbind(snrAll %>% select(lunaID, visitno, age, pc1, pc2, pc
write.csv(allSNRmeasures4040, '/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Cortical_SNR_Development/results/SNRmeasures_PCAvalues.csv')


## FOOOF ----

fooof <- read.csv('/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Results/FOOOF/allSubjectsAllChannelsFooofMeasures_20230911.csv') %>% select(-X)
names(fooof)[names(fooof) == "Channel"] <- "labels"

### Outlier Detections for FOOOF ----
fooof_naout <- fooof %>%
group_by(Subject, Condition) %>%
mutate(across(c("Offset", "Exponent"), naoutlier)) %>% ungroup() %>% separate(Subject, c('lunaID','vdate')) %>% select(-sex)


# imput missing fooof data
unique_subjects <- unique(fooof_naout$lunaID)
unique_visits <- unique(fooof_naout$visitno)
imputeDataFOOOF <- fooof_naout


for (lunaID in unique_subjects) {
for (visit in unique_visits) {
if (any(imputeDataFOOOF$lunaID == lunaID & imputeDataFOOOF$visitno == visit)) {

subject_visit_data <-
imputeDataFOOOF[imputeDataFOOOF$lunaID == lunaID &
imputeDataFOOOF$visitno == visit,]

if (any(is.na(subject_visit_data$Exponent) | is.na(subject_visit_data$Offset))) {

miceData <- mice(select(subject_visit_data, c("Exponent", "Offset")), m = 20, method = 'mean')

imputed_micedata <- cbind(subject_visit_data[, c("lunaID", "visitno")], complete(miceData))

imputeDataFOOOF[imputeDataFOOOF$lunaID %in% imputed_micedata$lunaID & imputeDataFOOOF$visitno %in% imputed_micedata$visitno, c("Exponent", "Offset")] <- imputed_micedata[,c("Exponent", "Offset")]

}
}
}
}

write.csv(imputeDataFOOOF, '/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Cortical_SNR_Development/results/imputeFOOOF.csv')



exponent <- imputeDataFOOOF %>% select("labels", "lunaID", "Exponent", "Condition", "visitno") %>%
filter(labels %in% c('F3', 'F5', 'F7', 'F1', 'F2', 'F4', 'F6', 'F8', 'AFz', 'AF1', 'AF2', 'Fp1', 'Fp2', 'Fz', 'AF5', 'AF6')) %>%
pivot_wider(names_from = labels, values_from = c("Exponent"), id_cols = c("lunaID", "Condition", "visitno")) %>% select(-lunaID, -Condition, -visitno)

exponent.pca <- prcomp(scale(exponent))
summary(exponent.pca)
exponent.pca$rotation

lunaize(
ggplot(data=melt(exponent.pca$rotation[,1:6])) +
geom_bar(aes(x=Var2, y=value, fill=Var1), stat="identity", position='dodge')) +
labs(fill='RL Parameters', y='Loading', x='')


#calculate total variance explained by each principal component
var_explained = exponent.pca$sdev^2 / sum(exponent.pca$sdev^2)


qplot(c(1:6), var_explained[1:6]) +
geom_line() +
xlab("Principal Component") +
ylab("Variance Explained") +
ggtitle("Scree Plot") +
ylim(0, 1)



frontalChannels <- (c('F3', 'F5', 'F7', 'F1', 'F2', 'F4', 'F6', 'F8', 'AFz', 'AF1', 'AF2', 'Fp1', 'Fp2', 'Fz', 'AF5', 'AF6'))

exponentchanLocs <- chanLocs
exponentchanLocs$PC1 <- 0
exponentchanLocs$PC2 <- 0
exponentchanLocs$PC3 <- 0


exponentchanLocs[exponentchanLocs$labels %in% frontalChannels, c("PC1")] <- (exponent.pca$rotation[,1])
exponentchanLocs[exponentchanLocs$labels %in% frontalChannels, c("PC2")] <- (exponent.pca$rotation[,2])
exponentchanLocs[exponentchanLocs$labels %in% frontalChannels, c("PC3")] <- (exponent.pca$rotation[,3])

write.csv(exponentchanLocs, '/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Cortical_SNR_Development/results/exponentchanLocs.csv')


exponentAll <- imputeDataFOOOF %>% select("labels", "lunaID", "Exponent", "Condition", "age", "visitno") %>%
filter(labels %in% c('F3', 'F5', 'F7', 'F1', 'F2', 'F4', 'F6', 'F8', 'AFz', 'AF1', 'AF2', 'Fp1', 'Fp2', 'Fz', 'AF5', 'AF6')) %>%
pivot_wider(names_from = labels, values_from = c("Exponent"), id_cols = c("lunaID", "Condition", "age", "visitno"))


exponentAll$pc1 <- NA
exponentAll$pc1 <- unname(unlist(exponent.pca$x[,1]))
exponentAll$pc2 <- NA
exponentAll$pc2 <- unname(unlist(exponent.pca$x[,2]))
exponentAll$pc3 <- NA
exponentAll$pc3 <- unname(unlist(exponent.pca$x[,3]))

exponentAll <- exponentAll %>%
mutate(across(c("pc1", "pc2", "pc3"), naoutlier)) %>% ungroup()

write.csv(exponentAll, '/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Cortical_SNR_Development/results/exponentAll.csv')


Offset <- imputeDataFOOOF %>% select("labels", "lunaID", "Offset", "Condition", "visitno") %>%
filter(labels %in% c('F3', 'F5', 'F7', 'F1', 'F2', 'F4', 'F6', 'F8', 'AFz', 'AF1', 'AF2', 'Fp1', 'Fp2', 'Fz', 'AF5', 'AF6')) %>%
pivot_wider(names_from = labels, values_from = c("Offset"), id_cols = c("lunaID", "Condition", "visitno")) %>% select(-lunaID, -Condition, -visitno)

off.pca <- prcomp(scale(Offset))
summary(off.pca)
off.pca$rotation

lunaize(
ggplot(data=melt(off.pca$rotation[,1:6])) +
geom_bar(aes(x=Var2, y=value, fill=Var1), stat="identity", position='dodge')) +
labs(fill='RL Parameters', y='Loading', x='')

#calculate total variance explained by each principal component
var_explained = off.pca$sdev^2 / sum(off.pca$sdev^2)


qplot(c(1:6), var_explained[1:6]) +
geom_line() +
xlab("Principal Component") +
ylab("Variance Explained") +
ggtitle("Scree Plot") +
ylim(0, 1)


frontalChannels <- (c('F3', 'F5', 'F7', 'F1', 'F2', 'F4', 'F6', 'F8', 'AFz', 'AF1', 'AF2', 'Fp1', 'Fp2', 'Fz', 'AF5', 'AF6'))

offChanlocs <- chanLocs
offChanlocs$PC1 <- 0
offChanlocs$PC2 <- 0
offChanlocs$PC3 <- 0

offChanlocs[offChanlocs$labels %in% frontalChannels, c("PC1")] <- (off.pca$rotation[,1])
offChanlocs[offChanlocs$labels %in% frontalChannels, c("PC2")] <- (off.pca$rotation[,2])
offChanlocs[offChanlocs$labels %in% frontalChannels, c("PC3")] <- (off.pca$rotation[,3])

write.csv(offChanlocs, '/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Cortical_SNR_Development/results/offChanlocs.csv')


OffsetAll <- imputeDataFOOOF %>% select("labels", "lunaID", "Offset", "Condition", "age", "visitno") %>%
filter(labels %in% c('F3', 'F5', 'F7', 'F1', 'F2', 'F4', 'F6', 'F8', 'AFz', 'AF1', 'AF2', 'Fp1', 'Fp2', 'Fz', 'AF5', 'AF6')) %>%
pivot_wider(names_from = labels, values_from = c("Offset"), id_cols = c("lunaID", "Condition", "age", "visitno"))


OffsetAll$pc1 <- NA
OffsetAll$pc1 <- unname(unlist(off.pca$x[,1]))
OffsetAll$pc2 <- NA
OffsetAll$pc2 <- unname(unlist(off.pca$x[,2]))
OffsetAll$pc3 <- NA
OffsetAll$pc3 <- unname(unlist(off.pca$x[,3]))

OffsetAll <- OffsetAll %>%
mutate(across(c("pc1", "pc2", "pc3"), naoutlier)) %>% ungroup()


write.csv(OffsetAll, '/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Cortical_SNR_Development/results/OffsetAll.csv')






# 30-30 ----
## SNR ----
Expand Down
Loading

0 comments on commit 619105f

Please sign in to comment.