diff --git a/Entropy/01_Entropy_Preprocessing.sh b/Entropy/01_Entropy_Preprocessing.sh deleted file mode 100644 index d206c1c..0000000 --- a/Entropy/01_Entropy_Preprocessing.sh +++ /dev/null @@ -1,7 +0,0 @@ -# Preprocessing all resting state data for the Entropy project -# Pull in raw data from hera('Raw/EEG/7TBrainMech') -# run_preprocessing_pipeline.m (/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Preprocessing_Functions) -# Set task as 'Resting_State' -# Fully preprocessed data will be in Hera/Projects/7TBrainMech/scripts/eeg/Shane/preprocessed_data/Resting_State/AfterWhole/ICAwholeClean_homogenize - -matlab -nodesktop -r "addpath(genpath('../Preprocessing_Functions/')); run_preprocessing_pipeline('Resting_State')" diff --git a/Entropy/Calculate_EEG_Entropy_Values.m b/Entropy/Calculate_EEG_Entropy_Values.m deleted file mode 100644 index 6f894cb..0000000 --- a/Entropy/Calculate_EEG_Entropy_Values.m +++ /dev/null @@ -1,127 +0,0 @@ - - -addpath(genpath(hera('/Projects/7TBrainMech/scripts/eeg/Shane/resources/eeglab2022.1'))); -addpath(hera('/Projects/7TBrainMech/scripts/fieldtrip-20220104')) -ft_defaults - -datapath = hera('/Projects/7TBrainMech/scripts/eeg/Shane/preprocessed_data/Resting_State/AfterWhole/ICAwholeClean_homogenize'); -entropyPath = hera('/Projects/7TBrainMech/scripts/eeg/Shane/Entropy/Results/individual_subject_files/timeScale20/'); - -%load in all the delay files -setfiles0 = dir([datapath,'/*icapru.set']); -setfiles = {}; - -for epo = 1:length(setfiles0) - setfiles{epo,1} = fullfile(datapath, setfiles0(epo).name); % cell array with EEG file names - % setfiles = arrayfun(@(x) fullfile(folder, x.name), setfiles(~[setfiles.isdir]),folder, 'Uni',0); % cell array with EEG file names -end - -for j = 1 : length(setfiles0) - idvalues(j,:) = (setfiles0(j).name(1:14)); -end - -%% multiscale entropy -parpool('local', 20); - -for j = 1:length(setfiles0) - - idvalues(j,:) = (setfiles0(j).name(1:14)); - inputfile = setfiles{j}; - if ~isfile([entropyPath idvalues(j,:) '_MultiScaleEntropy_eyesClosed.csv']) - - EEG = pop_loadset(inputfile); % load in eeg file - %EEGopeneyes = pop_rmdat(EEG, {'16130', '15362','1'},[0 4] ,0); - EEGclosedeyes = pop_rmdat(EEG, {'16129', '15261','0'},[0 4] ,0); - - % onemin = EEGopeneyes.data(:,1:9000); - - - parfor c = 1:size(EEGopeneyes.data, 1) - - Mobj = MSobject("SampEn"); - [MSx(c,:), Ci(:,c)] = MSEn(EEGopeneyes.data(c,:), Mobj, 'Scales', 20, 'Methodx', 'coarse', 'RadNew', 0, 'Plotx', false); - - end - - MSxTable = array2table(MSx); - CiTable = array2table(Ci'); - - subjectTable = horzcat(MSxTable, CiTable); - - % Create a new column with subject ID repeated for every row - subjectIDColumn = repmat(idvalues(j,:), size(subjectTable, 1),1); - - % Add the new column to the existing table - subjectTable = [table(subjectIDColumn, 'VariableNames', {'Subject'}), subjectTable]; - - - subjectSavePath = [entropyPath idvalues(j,:) '_MultiScaleEntropy_eyesClosed.csv']; - writetable(subjectTable, subjectSavePath) - - - end - - clear Mobj - clear MSx - clear Ci - -end - - delete(gcp); - -%% spectral entropy - -% for j = 1:length(setfiles0) -% -% idvalues(j,:) = (setfiles0(j).name(1:14)); -% inputfile = setfiles{j}; -% if ~isfile([entropyPath idvalues(j,:) '_SpectralEntropy_broadband.csv']) -% -% EEG = pop_loadset(inputfile); % load in eeg file -% EEGopeneyes = pop_rmdat(EEG, {'16130'},[0 4] ,0); -% onemin = EEGopeneyes.data(:,1:9000); -% -% -% parpool('local', 25); -% -% parfor c = 1:size(onemin, 1) -% -% [Spec(c,:), gammaBandEn(c,:)] = SpecEn(onemin(c,:), 'N', 150, 'Freqs', [.4, 1], 'Logx', exp(1), 'Norm' , true); -% [Spec(c,:), betaBandEn(c,:)] = SpecEn(onemin(c,:), 'N', 150, 'Freqs', [.16, .4], 'Logx', exp(1), 'Norm' , true); -% [Spec(c,:), alphaBandEn(c,:)] = SpecEn(onemin(c,:), 'N', 150, 'Freqs', [.1, .16], 'Logx', exp(1), 'Norm' , true); -% [Spec(c,:), thetaBandEn(c,:)] = SpecEn(onemin(c,:), 'N', 150, 'Freqs', [.04, .1], 'Logx', exp(1), 'Norm' , true); -% -% end -% delete(gcp('nocreate')); -% -% -% gammaBandEnTable = array2table(gammaBandEn); -% betaBandEnTable = array2table(betaBandEn); -% alphaBandEnTable = array2table(alphaBandEn); -% thetaBandEnTable = array2table(thetaBandEn); -% SpecTable = array2table(Spec); -% -% -% -% subjectTable = horzcat(gammaBandEnTable, betaBandEnTable, alphaBandEnTable, thetaBandEnTable, SpecTable); -% -% % Create a new column with subject ID repeated for every row -% subjectIDColumn = repmat(idvalues(j,:), size(subjectTable, 1),1); -% -% % Add the new column to the existing table -% subjectTable = [table(subjectIDColumn, 'VariableNames', {'Subject'}), subjectTable]; -% -% -% subjectSavePath = [entropyPath idvalues(j,:) '_SpectralEntropy_broadband.csv']; -% writetable(subjectTable, subjectSavePath) -% -% -% end -% -% clear gammaBandEn -% clear betaBandEn -% clear alphaBandEn -% clear thetaBandEn -% clear Spec -% -% end \ No newline at end of file diff --git a/Entropy/CombineSubjectDataframes.R b/Entropy/CombineSubjectDataframes.R deleted file mode 100755 index 4e2107a..0000000 --- a/Entropy/CombineSubjectDataframes.R +++ /dev/null @@ -1,45 +0,0 @@ -# Libraries ---- - -library(LNCDR) -library(data.table) -library(dplyr) -library(factoextra) -library(ggplot2) -library(e1071) -library(caret) -attach(mtcars) -library(grid) -library(gridExtra) -library(plotrix) -library(mgcv) -library(readxl) -library(lme4) -library(lubridate) -library(checkmate) -library(lmerTest) -library(tidyr) -library(jtools) - -# Initialize directory and empty dataframes ---- -chanLocs <- read.csv('/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Entropy/Results/ChannelLocs.csv') - -# Set your working directory to the folder containing your CSV files -setwd("/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Entropy/Results/individual_subject_files/timeScale20") - -# List all CSV files in the directory -csv_files <- list.files(pattern = "MultiScaleEntropy.csv") - -# Initialize an empty data frame to store the combined data -combined_data <- data.frame() - -# Loop through each CSV file, read it, and combine it ---- -for (file in csv_files) { - data <- read.csv(file, header = TRUE) # Change header argument if needed - data <- cbind(data, chanLocs) - - combined_data <- rbind(combined_data, data) -} - - -write.csv(combined_data, file = '/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Entropy/Results/allSubjects_allChans_MSE20.csv', row.names = F) - diff --git a/Entropy/EEG_Entropy.R b/Entropy/EEG_Entropy.R deleted file mode 100755 index 2fd67dc..0000000 --- a/Entropy/EEG_Entropy.R +++ /dev/null @@ -1,805 +0,0 @@ - -# Load Libraries - -library(LNCDR) -library(data.table) -library(dplyr) -library(factoextra) -library(ggplot2) -library(e1071) -library(caret) -attach(mtcars) -library(grid) -library(gridExtra) -library(plotrix) -library(mgcv) -library(readxl) -library(lme4) -library(lubridate) -library(checkmate) -library(lmerTest) -library(tidyr) -library(jtools) -library(eegUtils) -library(tvem) -library(interactions) -library(akima) - - -outliers <- function(x) { - (abs(x - mean(x, na.rm= T)) > (sd(x, na.rm= T) * 2)) -} -naoutlier <- function(x) ifelse(outliers(x), NA, x) - -# MSE Entropy 10 ---- -# Load Dataframe ---- - -entropy <- read.csv('/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Entropy/Results/allSubjects_allChans_MultiScaleEntropy.csv') %>% select(c(-type)) - -# Entropy Outlier Detection ---- -entropy_outlier <- entropy %>% group_by(Subject) %>% - mutate(across(c("MSx1", "MSx2", "MSx3", "MSx4", "MSx5", "MSx6", - "MSx7", "MSx8", "MSx9", "MSx10", "Var1"), naoutlier)) %>% ungroup() - -merge7t <- read.csv('/Volumes/Hera/Projects/7TBrainMech/scripts/txt/merged_7t.csv') -ageValues <- merge7t[c("lunaid","eeg.date","visitno","eeg.age")] -colnames(ageValues) <- c("lunaID", "visitDate","visitno", "age") -ageValues$Subject <- paste(ageValues$lunaID, ageValues$visitDate, sep = "_") - -entropyAge <- merge(entropy_outlier, ageValues, by = "Subject") - -# Entropy averaged across all electrodes ---- -entropyAgeAvg <- aggregate(cbind(MSx1, MSx2, MSx3, MSx4, MSx5, MSx6, MSx7, MSx8, MSx9, MSx10, Var1, age) ~ Subject + visitno, data = entropyAge, FUN = mean) -write.csv(entropyAgeAvg, file = '/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Entropy/Results/allSubjects_multiscaleEntropy_allChansAvg.csv') - - - -entropyAgeAvg_outlier <- entropyAgeAvg %>% - mutate(across(c("MSx1", "MSx2", "MSx3", "MSx4", "MSx5", "MSx6", - "MSx7", "MSx8", "MSx9", "MSx10", "Var1"), naoutlier)) %>% separate(Subject, c('lunaID','vdate')) - - -lunaize(ggplot(data = entropyAgeAvg_outlier, aes(x = age, y = MSx6)) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(aes(group = 1), method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) - - -entropyAgeAvg_outlier_long <- entropyAgeAvg_outlier %>% - pivot_longer(cols = starts_with(c("MSx1", "MSx2", "MSx3", "MSx4", "MSx5", "MSx6", - "MSx7", "MSx8", "MSx9", "MSx10")), - names_to = c(".value", "timeScale"), - names_pattern = "(\\D+)(\\d+)") %>% mutate(ageGroup = cut(age, c(0,13,16,19,Inf), labels = c('10-12','13-15','16-18','Adults'))) - - -lunaize(ggplot(data = entropyAgeAvg_outlier_long, aes(x = as.numeric(timeScale), y = MSx)) + - geom_smooth(aes(group = ageGroup, color = ageGroup), method=mgcv::"gam", - formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) + xlab("Time Scale") - - -lunaize(ggplot(data = entropyAgeAvg_outlier_long, aes(x = age, y = Var1)) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(group = 1, method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) + ylab("AUC") - - -gam.model <- mgcv::gamm(Var1 ~ s(age, k = 3), data = entropyAgeAvg_outlier_long, random=list(lunaID=~1)) -summary(gam.model$gam) - - -# Find each subjects max entropy value on the MSE curve -subs = unique(entropyAgeAvg_outlier_long$lunaID) -visits = unique(entropyAgeAvg_outlier_long$visitno) - - -maxValues <- data.frame() - -for (subject in subs) { - # Check if the subject has the visit number in the dataset - for (visitNum in visits) { - if (any(entropyAgeAvg_outlier_long$lunaID == subject & entropyAgeAvg_outlier_long$visitno == visitNum)) { - subData <- entropyAgeAvg_outlier_long %>% filter(lunaID == subject) %>% filter(visitno == visitNum) - if (all(is.na(subData$MSx))) { - next - } - - maxSubEntropy <- max(subData$MSx, na.rm = T) - timescale <- subData$timeScale[which(subData$MSx == maxSubEntropy)] - - subInfo <- data.frame(lunaID = subject, visitno = visitNum, maxEntropy = maxSubEntropy, maxTimeScale = timescale) - maxValues <- rbind(maxValues, subInfo) - } - } -} - -maxValuesAge <- merge(maxValues, ageValues, by = c('lunaID', "visitno")) -write.csv(maxValues, file = '/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Entropy/Results/allSubjects_maxEntropy_allChansAvg.csv') - -lunaize(ggplot(data = maxValuesAge, aes(x = age, y = maxEntropy)) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(group = 1, method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) + ylab("Max Entropy") - - -gam.model <- mgcv::gamm(maxEntropy ~ s(age, k = 3), data = maxValuesAge, random=list(lunaID=~1)) -summary(gam.model$gam) - - -lunaize(ggplot(data = maxValuesAge %>% filter(maxTimeScale > 3), aes(x = age, y = as.numeric(maxTimeScale))) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(group = 1, method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) + ylab("Max Entropy TimeScale") - - -gam.model <- mgcv::gamm(maxEntropy ~ s(age, k = 3), data = maxValuesAge, random=list(lunaID=~1)) -summary(gam.model$gam) - - -# Entropy averaged across only frontal electrodes ---- -entropyFrontal <- entropyAge %>% filter(labels %in% c('F3', 'F5', 'F7', 'F1', 'F2', 'F4', 'F6', 'F8', 'AFz', 'AF1', 'AF2', 'Fp1', 'Fp2','Fz', 'AF5', 'AF6')) - -entropyFrontalAvg <- aggregate(cbind(MSx1, MSx2, MSx3, MSx4, MSx5, MSx6, MSx7, MSx8, MSx9, MSx10, Var1, age) ~ Subject + visitno, data = entropyFrontal, FUN = mean) - -entropyAgeAvg_outlier_frontal <- entropyFrontalAvg %>% - mutate(across(c("MSx1", "MSx2", "MSx3", "MSx4", "MSx5", "MSx6", - "MSx7", "MSx8", "MSx9", "MSx10", "Var1"), naoutlier)) %>% separate(Subject, c('lunaID','vdate')) - - -lunaize(ggplot(data = entropyAgeAvg_outlier_frontal, aes(x = age, y = MSx6)) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(aes(group = 1), method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) - - -entropyAgeAvg_outlier_frontal_long <- entropyAgeAvg_outlier_frontal %>% - pivot_longer(cols = starts_with(c("MSx1", "MSx2", "MSx3", "MSx4", "MSx5", "MSx6", - "MSx7", "MSx8", "MSx9", "MSx10")), - names_to = c(".value", "timeScale"), - names_pattern = "(\\D+)(\\d+)") %>% mutate(ageGroup = cut(age, c(0,14,17,20,23,Inf), labels = c('10-13','14-16','17-19','20-22','23-30'))) - - -lunaize(ggplot(data = entropyAgeAvg_outlier_frontal_long, aes(x = age, y = MSx, color = timeScale)) + - geom_smooth(aes(group = timeScale, color = timeScale), method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) - - -lunaize(ggplot(data = entropyAgeAvg_outlier_frontal_long, aes(x = age, y = Var1)) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(group = 1, method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) + ylab("AOC") - - -gam.model <- mgcv::gamm(Var1 ~ s(age, k = 3), data = entropyAgeAvg_outlier_frontal_long, random=list(lunaID=~1)) -summary(gam.model$gam) - - -# Find each subjects max entropy value on the MSE curve -subs = unique(entropyAgeAvg_outlier_frontal_long$lunaID) -visits = unique(entropyAgeAvg_outlier_frontal_long$visitno) - - -maxValuesFrontal <- data.frame() - -for (subject in subs) { - # Check if the subject has the visit number in the dataset - for (visitNum in visits) { - if (any(entropyAgeAvg_outlier_frontal_long$lunaID == subject & entropyAgeAvg_outlier_frontal_long$visitno == visitNum)) { - subData <- entropyAgeAvg_outlier_frontal_long %>% filter(lunaID == subject) %>% filter(visitno == visitNum) - maxSubEntropy <- max(subData$MSx) - - subInfo <- data.frame(lunaID = subject, visitno = visitNum, maxEntropy = maxSubEntropy) - maxValuesFrontal <- rbind(maxValuesFrontal, subInfo) - } - } -} - -maxValuesAgeFrontal <- merge(maxValuesFrontal, ageValues, by = c('lunaID', "visitno")) - - -lunaize(ggplot(data = maxValuesAge, aes(x = age, y = maxEntropy)) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(group = 1, method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) + ylab("Max Entropy") - - -gam.model <- mgcv::gamm(maxEntropy ~ s(age, k = 3), data = maxValuesAge, random=list(lunaID=~1)) -summary(gam.model$gam) - - -# Combine Entropy and Spontaneous by PCA ---- -allSNRmeasures <- read.csv('/Users/shanemckeon/Library/CloudStorage/OneDrive-UniversityofPittsburgh/Lab/Projects/SNR/rMarkdown/SNRmeasures_PCAvalues.csv') - -maxEntropySelect <- merge(entropyAgeAvg_outlier %>% select(lunaID, visitno, age, Var1), maxValuesAge, by = c("lunaID", "visitno", "age")) - -spontaneousEntropy <- merge(maxEntropy_frontal_AOC, allSNRmeasures, by = c("lunaID", "visitno", "age")) %>% mutate(ageGroup = as.factor(cut(age, c(0,17,Inf), labels = c('Adol','Adults')))) - - -lunaize(ggplot(data = spontaneousEntropy, aes(x = pc1, y = Var1)) + geom_point(aes(color = ageGroup)) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(group =1, method="lm", alpha=0.4, linewidth=2)) + ylab("Area under curve") + facet_wrap(~measure) - -lm.model <- lmer(Var1 ~ age + pc1 + (1|lunaID), data = spontaneousEntropy) -summary(lm.model) - -gam.model <- mgcv::gamm(Var1 ~ s(age, k = 3), data = spontaneousEntropy, random=list(lunaID=~1)) -summary(gam.model$gam) - - - - -lunaize(ggplot(data = spontaneousEntropy, aes(x = pc1, y = maxEntropy)) + geom_point(aes(color = ageGroup)) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(group = 1, method = "lm", alpha=0.4, linewidth=2)) + facet_wrap(~measure) - -lm.model <- lmer(maxEntropy ~ age + pc1 + (1|lunaID), data = spontaneousEntropy) -summary(lm.model) - -gam.model <- mgcv::gamm(maxEntropy ~ s(age, k = 3) + pc1, data = spontaneousEntropy, random=list(lunaID=~1)) -summary(gam.model$gam) - - - -spontaneousEntropy_long <- spontaneousEntropy %>% - pivot_longer(cols = starts_with(c("MSx1", "MSx2", "MSx3", "MSx4", "MSx5", "MSx6", - "MSx7", "MSx8", "MSx9", "MSx10")), - names_to = c(".value", "timeScale"), - names_pattern = "(\\D+)(\\d+)") %>% mutate(ageGroup = as.factor(cut(age, c(0,17,Inf), labels = c('Adol','Adults')))) - - -lunaize(ggplot(data = spontaneousEntropy_long, aes(x = pc1, y = MSx, color = timeScale)) + - geom_smooth(aes(group = timeScale, color = timeScale), method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) - - -# Combine Entropy and Spontaneous by All Channel ---- -## Find max entropy for every electrode ---- -# Find each subjects max entropy value on the MSE curve - -entropyAge_long <- entropyAge %>% - pivot_longer(cols = starts_with(c("MSx1", "MSx2", "MSx3", "MSx4", "MSx5", "MSx6", - "MSx7", "MSx8", "MSx9", "MSx10")), - names_to = c(".value", "timeScale"), - names_pattern = "(\\D+)(\\d+)") %>% mutate(ageGroup = cut(age, c(0,14,17,20,23,Inf), labels = c('10-13','14-16','17-19','20-22','23-30'))) - -subs = unique(entropyAge_long$Subject) -visits <- unique(na.omit(entropyAge_long$visitno)) - -maxValuesAllChans <- data.frame() - -for (subject in subs) { - # Check if the subject has the visit number in the dataset - - subData <- entropyAge_long %>% filter(Subject == subject) - chans = unique(subData$labels) - - for (chan in chans) { - # Check if the subject has the visit number in the dataset - - subChanData <- subData %>% filter(labels == chan) - maxSubchanEntropy <- max(subChanData$MSx) - - subChanInfo <- data.frame(Subject = subject, labels = chan, maxEntropy = maxSubchanEntropy) - maxValuesAllChans <- rbind(maxValuesAllChans, subChanInfo) - } -} - - - -maxValuesAllChansAge <- merge(maxValuesAllChans, ageValues, by = c('Subject')) %>% merge(., entropyAge %>% select("Subject", "labels", "Var1"), by = c("Subject", "labels")) - -SNRallChans_outlier <- read.csv('/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Results/SNR/allSubjectsSNR_allChans_allfreqs.csv') - -SNRallChans_outlier4040 <- SNRallChans_outlier %>% filter(freqs == 40 & hertz == 40) -SNRallChans_outlier4040$SNR <- log(SNRallChans_outlier4040$Evoked/SNRallChans_outlier4040$Induced) - -spontaneousEntropy <- merge(maxValuesAllChansAge, SNRallChans_outlier4040 %>% - select("lunaID", "visitno", "age", "labels", "Induced", "Evoked", "SNR", "Total"), by = c("lunaID", "visitno", "age", "labels")) %>% - mutate(ageGroup = as.factor(cut(age, c(0,17,Inf), labels = c('Adol','Adults')))) - -hist(log(spontaneousEntropy$Induced)) -hist((spontaneousEntropy$maxEntropy)) - -entropyAgeAvg <- aggregate(cbind(MSx1, MSx2, MSx3, MSx4, MSx5, MSx6, MSx7, MSx8, MSx9, MSx10, Var1, age) ~ Subject + visitno, data = entropyAge, FUN = mean) - -spontaneousEntropyAvg <- aggregate(cbind(Induced, Evoked, Total, SNR, Var1, maxEntropy, age) ~ lunaID + visitno, data = spontaneousEntropy, FUN = mean) - - -lunaize(ggplot(data = spontaneousEntropyAvg, aes(x = log(Induced), y = Var1)) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(group =1, method="lm", alpha=0.4, linewidth=2)) + ylab("Area under curve") - -lm.model <- lmer(Var1 ~ age + log(Induced) + (1|lunaID), data = spontaneousEntropyAvg) -summary(lm.model) - -lunaize(ggplot(data = spontaneousEntropyAvg, aes(x = log(Induced), y = maxEntropy)) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(group =1, method="lm", alpha=0.4, linewidth=2)) + ylab("Max Entropy") - -lm.model <- lmer(maxEntropy ~ age + log(Induced) + (1|lunaID), data = spontaneousEntropyAvg) -summary(lm.model) - -## Correlation between entropy and spontaneous ---- -subs = unique(spontaneousEntropy$Subject) - -subCors <- data.frame() - -for (subject in subs) { - - subDataFrame <- spontaneousEntropy %>% filter(Subject == subject) - subjectCor <- cor(x = scale(subDataFrame$maxEntropy), y = log(subDataFrame$Induced), method = "pearson", use = "complete.obs") - - subCorInfo <- data.frame(Subject = subject, Corr = subjectCor) - subCors <- rbind(subCors, subCorInfo) - -} - -subCorAge <- merge(subCors, ageValues, by = c("Subject")) - -lunaize(ggplot(data = subCorAge, aes(x = age, y = Corr)) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(group = 1, method="lm", alpha=0.4, linewidth=2)) - - -lm.model <- lmer(Corr ~ age + (1|lunaID), data = subCorAge) -summary(lm.model) - -# MSE Entropy vs FOOOF ---- - -entropy <- read.csv('/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Entropy/Results/allSubjects_allChans_MultiScaleEntropy.csv') %>% select(c(-type)) - -entropy_outlier <- entropy %>% group_by(Subject) %>% - mutate(across(c("MSx1", "MSx2", "MSx3", "MSx4", "MSx5", "MSx6", - "MSx7", "MSx8", "MSx9", "MSx10", "Var1"), naoutlier)) %>% ungroup() - -entropyAge <- merge(entropy_outlier, ageValues, by = "Subject") - -## Entropy averaged across all electrodes ---- -entropyAgeAvg <- aggregate(cbind(MSx1, MSx2, MSx3, MSx4, MSx5, MSx6, MSx7, MSx8, MSx9, MSx10, Var1, age) ~ Subject + visitno, data = entropyAge, FUN = mean) - -entropyAgeAvg_outlier <- entropyAgeAvg %>% - mutate(across(c("MSx1", "MSx2", "MSx3", "MSx4", "MSx5", "MSx6", - "MSx7", "MSx8", "MSx9", "MSx10", "Var1"), naoutlier)) - -entropyAgeAvg_outlier_long <- entropyAgeAvg_outlier %>% - pivot_longer(cols = starts_with(c("MSx1", "MSx2", "MSx3", "MSx4", "MSx5", "MSx6", - "MSx7", "MSx8", "MSx9", "MSx10")), - names_to = c(".value", "timeScale"), - names_pattern = "(\\D+)(\\d+)") %>% mutate(ageGroup = cut(age, c(0,14,17,20,23,Inf), labels = c('10-13','14-16','17-19','20-22','23-30'))) - -## Load and avg fooof across all electrodes ---- -fooofAllchans <- read.csv('/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Aperiodic_MRS_Development/Results/allSubjectsAllChannelsFooofMeasures_20230911.csv') - -fooofAllchans <- fooofAllchans %>% - rename(labels = Channel) - -fooofAvg <- aggregate(cbind(Offset, Exponent) ~ Subject + Condition, data = fooofAllchans, FUN = mean) - -fooofAvg_outlier <- fooofAvg %>% group_by(Condition) %>% - mutate(across(c("Offset", "Exponent"), naoutlier)) %>% ungroup() - -## Merge MSE and fooof ---- - -MSEfooof <- merge(fooofAvg_outlier, entropyAgeAvg_outlier, by = "Subject") %>% separate(Subject, c('lunaID','vdate')) %>% - merge(., maxEntropy, by = c("lunaID", "visitno")) - - -lunaize(ggplot(data = MSEfooof, aes(x = Exponent, y = MSx))+ - geom_smooth(aes(group = timeScale, color = timeScale), method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) - -### AUC ---- -lunaize(ggplot(data = MSEfooof, aes(y = Exponent, x = Var1)) + geom_point() + - geom_line(aes(group= interaction(lunaID, Condition)), alpha = 0.2) + - geom_smooth(aes(group = Condition, color = Condition), - method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2))+xlab("AUC") - - -gam.model <- mgcv::gamm(Exponent ~ s(age, k = 3)+ Condition + Var1, data = MSEfooof, random=list(lunaID=~1)) -summary(gam.model$gam) - -lunaize(ggplot(data = MSEfooof, aes(y = Offset, x = Var1))+ geom_point() + - geom_line(aes(group= interaction(lunaID, Condition)), alpha = 0.2)+ - geom_smooth(aes(group = Condition, color = Condition), method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2))+xlab("AUC") - -gam.model <- mgcv::gamm(Offset ~ s(age, k = 3)+ Condition + Var1, data = MSEfooof, random=list(lunaID=~1)) -summary(gam.model$gam) - -### Max Entropy ---- - -lunaize(ggplot(data = MSEfooof, aes(y = Exponent, x = maxEntropy)) + geom_point() + - geom_line(aes(group= interaction(lunaID, Condition)), alpha = 0.2) + - geom_smooth(aes(group = Condition, color = Condition), - method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) - - -gam.model <- mgcv::gamm(Exponent ~ s(age, k = 3)+ Condition + maxEntropy, data = MSEfooof, random=list(lunaID=~1)) -summary(gam.model$gam) - -lunaize(ggplot(data = MSEfooof, aes(y = Offset, x = maxEntropy))+ geom_point() + - geom_line(aes(group= interaction(lunaID, Condition)), alpha = 0.2)+ - geom_smooth(aes(group = Condition, color = Condition), - method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) - -gam.model <- mgcv::gamm(Offset ~ s(age, k = 3)+ Condition + maxEntropy, data = MSEfooof, random=list(lunaID=~1)) -summary(gam.model$gam) - -# Spectral Entropy ---- - -entropy <- read.csv('/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Results/Entropy/allSubjects_allChans_SpectralEntropy_broadband.csv') %>% select(c(-type)) - -entropy_outlier <- entropy %>% group_by(Subject) %>% - mutate(across(c("gammaBandEn", "betaBandEn", "alphaBandEn", "thetaBandEn", "Spec"), naoutlier)) %>% ungroup() - -merge7t <- read.csv('/Volumes/Hera/Projects/7TBrainMech/scripts/txt/merged_7t.csv') -ageValues <- merge7t[c("lunaid","eeg.date","visitno","eeg.age")] -colnames(ageValues) <- c("lunaID", "visitDate","visitno", "age") -ageValues$Subject <- paste(ageValues$lunaID, ageValues$visitDate, sep = "_") - -entropyAge <- merge(entropy_outlier, ageValues, by = "Subject") - -##Averaged across all electrodes ---- -entropyAgeAvg <- aggregate(cbind(gammaBandEn, betaBandEn, alphaBandEn, thetaBandEn, Spec, age) ~ Subject + visitno, data = entropyAge, FUN = mean) - -entropyAgeAvg_outlier <- entropyAgeAvg %>% - mutate(across(c("gammaBandEn", "betaBandEn", "alphaBandEn", "thetaBandEn"), naoutlier)) %>% separate(Subject, c('lunaID','vdate')) - - -lunaize(ggplot(data = entropyAgeAvg_outlier, aes(x = age, y = gammaBandEn)) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(aes(group = 1), method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) - - -lunaize(ggplot(data = entropyAgeAvg_outlier, aes(x = age, y = betaBandEn)) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(aes(group = 1), method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) - - -lunaize(ggplot(data = entropyAgeAvg_outlier, aes(x = age, y = alphaBandEn)) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(aes(group = 1), method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) - - -lunaize(ggplot(data = entropyAgeAvg_outlier, aes(x = age, y = thetaBandEn)) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(aes(group = 1), method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) - -lunaize(ggplot(data = entropyAgeAvg_outlier, aes(x = age, y = Spec)) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(aes(group = 1), method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) - -## All channels vs FOOOF ---- -entropy <- read.csv('/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Results/Entropy/allSubjects_allChans_SpectralEntropy_broadband.csv') %>% select(c(-type)) - -### Avg channels ---- -entropyAvg <- aggregate(cbind(gammaBandEn, betaBandEn, alphaBandEn, thetaBandEn, Spec) ~ Subject, data = entropy, FUN = mean) - -entropyAvg_outlier <- entropyAvg %>% - mutate(across(c("gammaBandEn", "betaBandEn", "alphaBandEn", "thetaBandEn", "Spec"), naoutlier)) %>% ungroup() - - -fooofAllchans <- read.csv('/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Results/FOOOF/allSubjectsAllChannelsFooofMeasures_20230911.csv') - -fooofAllchans <- fooofAllchans %>% - rename(labels = Channel) - -fooofAvg <- aggregate(cbind(Offset, Exponent) ~ Subject + Condition, data = fooofAllchans, FUN = mean) - -fooofAvg_outlier <- fooofAvg %>% group_by(Condition) %>% - mutate(across(c("Offset", "Exponent"), naoutlier)) %>% ungroup() - -### Merge Entropy and FOOOF ---- -entropyFooof <- merge(entropyAvg_outlier, fooofAvg_outlier, by = c("Subject")) %>% separate(Subject, c('lunaID','vdate')) - - -lunaize(ggplot(data = entropyFooof, aes(x = gammaBandEn, y = Exponent)) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(aes(group = Condition), method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) - - -lunaize(ggplot(data = entropyFooof, aes(x = gammaBandEn, y = Offset)) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(aes(group = 1), method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) - - -lunaize(ggplot(data = entropyFooof, aes(x = Spec, y = Exponent)) + geom_point() + - geom_line(aes(group= interaction(lunaID, Condition)), alpha = 0.2) + - geom_smooth(aes(group = 1), method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) + facet_wrap(~Condition) - - - -# Entropy vs MRS ---- -MRSlong <- read.csv("/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Aperiodic_MRS_Development/Results/allSubjectsDLPFCMRSMeasures_20230613.csv") - -## GLU VS Exponent ---- - -lunaize(ggplot(data = fooofMRS , - aes(x = Exponent, y = Glu_gamadj, by = luna, color = ageGroup))+ - geom_line(aes(group=interaction(luna,Region,Condition)), alpha = 0.2) + - geom_point(aes(shape=Region),alpha=.5) + geom_smooth(aes(group = 1), method="lm", alpha = 0.8) + - scale_color_manual(values=c("gold3", "blue4"))) + - ylab("Glutamate") + xlab("Exponent") + theme(text = element_text(size = 30))+ theme(legend.position='none') - - -lm.model <- lmer(Glu_gamadj ~ Exponent + age + Condition + Region + (1|luna), data = fooofMRS ) -car::Anova(lm.model) -summ(lm.model) - - -lm.model <- lmer(Glu_gamadj ~ Exponent * age + Condition + Region + (1|luna), data = fooofMRS ) -car::Anova(lm.model) -summary(lm.model) -summ(lm.model) - - -summary(lmerTest::lmer(Exponent ~ Glu_gamadj + age + Region + Condition + (1|subjID), data = fooofMRS %>% filter(zExp < 2))) -summary(lmerTest::lmer(Exponent ~ Glu_gamadj + inverseAge + Region + Condition + (1|luna), data = fooofMRS)) - -summary(lmerTest::lmer(Exponent ~ Glu_gamadj*age + Region + Condition + (1|subjID), data = fooofMRS %>% filter(zExp < 2))) -summary(lmerTest::lmer(Exponent ~ Glu_gamadj*inverseage + Region + Condition + (1|subjID), data = fooofMRS %>% filter(zExp < 2))) - - -# gam approach, with non-linear form of age -gam.model <- gamm(Exponent ~ Glu_gamadj + s(age, k=3) + Region + Condition, - data = fooofMRS, random=list(luna=~1)) -summary(gam.model$gam) -print(plot(getViz(gam.model$gam), allTerms = T), pages = 1) - - -AIC(lmer(Exponent ~ Glu_gamadj + age + Condition + Region + (1|luna), data = fooofMRS), - lmer(Exponent ~ Glu_gamadj + inverseAge + Condition + Region + (1|luna), data = fooofMRS)) - - - -## GLU VS Offset ---- -lunaize(ggplot(data = fooofMRS, - aes(x = Offset, y = Glu_gamadj, by = luna, color = ageGroup))+ - geom_line(aes(group=interaction(luna,Region,Condition)), alpha = 0.2) + - geom_point(aes(shape =Region),alpha=.5) + geom_smooth(aes(group = 1), method="lm", alpha = 0.8) + - scale_color_manual(values=c("gold3", "blue4"))) + - ylab("Glutamte") + xlab("Offset") + theme(text = element_text(size = 30))+ theme(legend.position='none') - - -lm.model <- lmer( Glu_gamadj ~ Offset + age + Condition + Region + (1|luna), data = fooofMRS ) -car::Anova(lm.model) -summary(lm.model) - - -lm.model <- lmer(Glu_gamadj ~ Offset * age +Condition +Region + (1|luna), data = fooofMRS ) -car::Anova(lm.model) -summary(lm.model) -summ(lm.model) - - -summary(lmerTest::lmer(Offset ~ Glu_gamadj + age + Region + Condition + (1|subjID), data = fooofMRS %>% filter(zOffset < 2))) -summary(lmerTest::lmer(Offset ~ Glu_gamadj + inverseage + Region + Condition + (1|subjID), data = fooofMRS %>% filter(zOffset < 2))) - -summary(lmerTest::lmer(Offset ~ Glu_gamadj*age + Region + Condition + (1|subjID), data = fooofMRS %>% filter(zOffset < 2))) -summary(lmerTest::lmer(Offset ~ Glu_gamadj*inverseage + Region + Condition + (1|subjID), data = fooofMRS %>% filter(zOffset < 2))) - - -# gam approach, with non-linear form of age -gam.model <- gamm(Offset ~ Glu_gamadj + s(age, k=3) + Region + Condition, - data = fooofMRS %>% filter(zOffset < 2), random=list(luna=~1)) - -summary(gam.model$gam) -print(plot(getViz(gam.model$gam), allTerms = T), pages = 1) - -# gam approach, with non-linear form of age -gam.model <- gamm(Offset ~ Glu_gamadj + s(age, k=3) + Region + Condition, - data = fooofMRS %>% filter(zOffset < 2), random=list(luna=~1)) - -summary(gam.model$gam) -print(plot(getViz(gam.model$gam), allTerms = T), pages = 1) - -AIC(lmer(Offset ~ Glu_gamadj + age + Condition + Region + (1|luna), data = fooofMRS), - lmer(Offset ~ Glu_gamadj + inverseAge + Condition + Region + (1|luna), data = fooofMRS)) - - - - -## GABA VS Exponent ---- - -lunaize(ggplot(data = fooofMRS , - aes(x = Exponent, y = GABA_gamadj, by = luna, color = ageGroup))+ - geom_line(aes(group=interaction(luna,Region,Condition)), alpha = 0.2) + - geom_point(aes(shape=Region),alpha=.5) + geom_smooth(aes(group = 1), method="lm", alpha = 0.8) + - scale_color_manual(values=c("gold3", "blue4"))) + - ylab("GABA") + xlab("Exponent") + theme(text = element_text(size = 30))+ theme(legend.position='none') - -lm.model <- lmer(GABA_gamadj ~ Exponent + age + Condition + Region + (1|luna), data = fooofMRS ) -summ(lm.model) - - -lm.model <- lmer(GABA_gamadj ~ Exponent * age +Condition + Region+ (1|luna), data = fooofMRS ) -summ(lm.model) - - -summary(lmerTest::lmer(Exponent ~ GABA_gamadj + age + Region + Condition + (1|subjID), data = fooofMRS %>% filter(zExp < 2))) -summary(lmerTest::lmer(Exponent ~ GABA_gamadj + inverseage + Region + Condition + (1|subjID), data = fooofMRS %>% filter(zExp < 2))) - -summary(lmerTest::lmer(Exponent ~ GABA_gamadj*age + Region + Condition + (1|subjID), data = fooofMRS %>% filter(zExp < 2))) -summary(lmerTest::lmer(Exponent ~ GABA_gamadj*inverseage + Region + Condition + (1|subjID), data = fooofMRS %>% filter(zExp < 2))) - -# gam approach, with non-linear form of age -gam.model <- gamm(Exponent ~ GABA_gamadj + s(age, k=3) + Region + Condition, - data = fooofMRS %>% filter(zExp < 2), random=list(luna=~1)) - -summary(gam.model$gam) -print(plot(getViz(gam.model$gam), allTerms = T), pages = 1) - -AIC(lmer(Exponent ~ GABA_gamadj + age + Condition + Region + (1|luna), data = fooofMRS), - lmer(Exponent ~ GABA_gamadj + inverseAge + Condition + Region + (1|luna), data = fooofMRS)) - -## GABA VS Offset ---- - -lunaize(ggplot(data = fooofMRS , - aes(x = Offset, y = GABA_gamadj, by = luna, color = ageGroup))+ - geom_line(aes(group=interaction(luna,Region,Condition)), alpha = 0.2) + - geom_point(aes(shape=Region),alpha=.5) + geom_smooth(aes(group = 1), method="lm", alpha = 0.8) + - scale_color_manual(values=c("gold3", "blue4"))) + - ylab("GABA") + xlab("Offset")+ theme(text = element_text(size = 30))+ theme(legend.position='none') - -lm.model <- lmer(GABA_gamadj ~ Offset + age + Condition + Region + (1|luna), data = fooofMRS ) -summ(lm.model) - - -lm.model <- lmer(GABA_gamadj ~ Offset * age + Condition +Region + (1|luna), data = fooofMRS ) -car::Anova(lm.model) -summ(lm.model) - - -summary(lmerTest::lmer(Offset ~ GABA_gamadj + age + Region + Condition + (1|subjID), data = fooofMRS %>% filter(zOffset < 2) )) -summary(lmerTest::lmer(Offset ~ GABA_gamadj + inverseage + Region + Condition + (1|subjID), data = fooofMRS %>% filter(zOffset < 2) )) - -summary(lmerTest::lmer(Offset ~ GABA_gamadj*age + Region + Condition + (1|subjID), data = fooofMRS %>% filter(zOffset < 2) )) -summary(lmerTest::lmer(Offset ~ GABA_gamadj*inverseage + Region + Condition + (1|subjID), data = fooofMRS %>% filter(zOffset < 2) )) - -# gam approach, with non-linear form of age -gam.model <- gamm(Offset ~ GABA_gamadj + s(age, k=3) + Region + Condition, - data = fooofMRS %>% filter(zOffset < 2), random=list(luna=~1)) - -summary(gam.model$gam) -print(plot(getViz(gam.model$gam), allTerms = T), pages = 1) - -AIC(lmer(Offset ~ GABA_gamadj + age + Condition + Region + (1|luna), data = fooofMRS), - lmer(Offset ~ GABA_gamadj + inverseAge + Condition + Region + (1|luna), data = fooofMRS)) - -## Ratio VS Exponent ---- - -lunaize(ggplot(data = fooofMRS, - aes(x = Exponent, y = Ratio_gamadj, by = luna, color = ageGroup)) + - geom_line(aes(group=interaction(luna,Region,Condition)), alpha = 0.2) + - geom_point(aes(shape=Region),alpha=.5) + - geom_smooth(aes(group = 1, alpha = 0.1), method="lm", alpha = 0.8) + - scale_color_manual(values=c("gold3", "blue4"))) + - ylab("Glu/GABA Ratio") + xlab("Exponent")+ theme(text = element_text(size = 30))+ theme(legend.position='none') - - -lm.model <- lmer(Ratio_gamadj ~ Exponent + age + Condition + Region + (1|luna), data = fooofMRS ) -summ(lm.model) -car::Anova(lm.model) - - -lm.model <- lmer(Ratio_gamadj ~ Exponent *age +Condition +Region+ (1|luna), data = fooofMRS ) -summ(lm.model) -car::Anova(lm.model) - - -summary(lmerTest::lmer(Exponent ~ Ratio_gamadj + age + Region + Condition + (1|subjID), data = fooofMRS %>% filter(zExp < 2))) -summary(lmerTest::lmer(Exponent ~ Ratio_gamadj + inverseage + Region + Condition + (1|subjID), data = fooofMRS %>% filter(zExp < 2))) - -summary(lmerTest::lmer(Exponent ~ Ratio_gamadj*age + Region + Condition + (1|subjID), data = fooofMRS %>% filter(zExp < 2))) -summary(lmerTest::lmer(Exponent ~ Ratio_gamadj*inverseage + Region + Condition + (1|subjID), data = fooofMRS %>% filter(zExp < 2))) - -# gam approach, with non-linear form of age -gam.model <- gamm(Exponent ~ Ratio_gamadj + s(age, k=3) + Region + Condition, - data = fooofMRS %>% filter(zExp < 2), random=list(luna=~1)) - -summary(gam.model$gam) -print(plot(getViz(gam.model$gam), allTerms = T), pages = 1) - -AIC(lmer(Exponent ~ Ratio_gamadj + age + Condition + Region + (1|luna), data = fooofMRS), - lmer(Exponent ~ Ratio_gamadj + inverseAge + Condition + Region + (1|luna), data = fooofMRS)) - - -## Ratio VS Offset ---- - -lunaize(ggplot(data = fooofMRS, - aes(x = Offset, y = Ratio_gamadj, by = luna, color = ageGroup)) + - geom_line(aes(group=interaction(luna,Region,Condition)), alpha = 0.2) + - geom_point(aes(shape=Region),alpha=.5) + - geom_smooth(aes(group = 1, alpha = 0.1), method="lm", alpha = 0.8) + - scale_color_manual(values=c("gold3", "blue4"))) + - ylab("Glu/GABA Ratio") + xlab("Offset")+ theme(text = element_text(size = 30))+ theme(legend.position='none') - - - -lm.model <- lmer(Ratio_gamadj ~ Offset + age + Condition + Region + (1|luna), data = fooofMRS) -summ(lm.model) -car::Anova(lm.model) - - -lm.model <- lmer(Ratio_gamadj ~ Offset *sex +age +Condition + Region + (1|luna), data = fooofMRS ) -summ(lm.model) -car::Anova(lm.model) - - -summary(lmerTest::lmer(Offset ~ Ratio_gamadj + age + Region + Condition + (1|subjID), data = fooofMRS %>% filter(zOffset < 2) )) -summary(lmerTest::lmer(Offset ~ Ratio_gamadj + inverseage + Region + Condition + (1|subjID), data = fooofMRS %>% filter(zOffset < 2) )) - -summary(lmerTest::lmer(Offset ~ Ratio_gamadj*age + Region + Condition + (1|subjID), data = fooofMRS %>% filter(zOffset < 2) )) -summary(lmerTest::lmer(Offset ~ Ratio_gamadj*inverseage + Region + Condition + (1|subjID), data = fooofMRS %>% filter(zOffset < 2) )) - -# gam approach, with non-linear form of age -gam.model <- gamm(Offset ~ Ratio_gamadj + s(age, k=3) + Region + Condition, - data = fooofMRS %>% filter(zOffset < 2), random=list(luna=~1)) -summary(gam.model$gam) -print(plot(getViz(gam.model$gam), allTerms = T), pages = 1) - - -# gam approach, with non-linear form of age -gam.model <- gamm(Offset ~ Ratio_gamadj + s(age, k=3) + Region + Condition, - data = fooofMRS %>% filter(zOffset < 2), random=list(luna=~1)) -summary(gam.model$gam) -print(plot(getViz(gam.model$gam), allTerms = T), pages = 1) - -AIC(lmer(Offset ~ Ratio_gamadj + age + Condition + Region + (1|luna), data = fooofMRS), - lmer(Offset ~ Ratio_gamadj + inverseAge + Condition + Region + (1|luna), data = fooofMRS)) - - -## Gaba glu imbalance VS offset ---- - -lunaize(ggplot(data = fooofMRS, - aes(x = Offset, y = GluGABAimbalanceABS, by =luna, color = ageGroup)) + - geom_line(aes(group=interaction(luna,Region, Condition)), alpha = 0.2) + - geom_point(aes(shape=Region),alpha=.5) + - geom_smooth(aes(group = 1, alpha = 0.01), method="lm",alpha=.8,size=1)) + - scale_color_manual(values=c("gold3", "blue4")) + - ylab("Glu GABA Imbalance") + xlab("Offset")+ theme(text = element_text(size = 30))+ theme(legend.position='none') - - -summary(lmerTest::lmer(Offset ~ GluGABAimbalanceABS + age + Region + Condition + (1|subjID), data = fooofMRS %>% filter(zOffset < 2) )) -summary(lmerTest::lmer(Offset ~ GluGABAimbalanceABS + inverseage + Region + Condition + (1|subjID), data = fooofMRS %>% filter(zOffset < 2) )) - -summary(lmerTest::lmer(Offset ~ GluGABAimbalanceABS*age + Region + Condition + (1|subjID), data = fooofMRS %>% filter(zOffset < 2) )) -summary(lmerTest::lmer(Offset ~ GluGABAimbalanceABS*inverseage + Region + Condition + (1|subjID), data = fooofMRS %>% filter(zOffset < 2) )) - -# gam approach, with non-linear form of age -gam.model <- gamm(Offset ~ GluGABAimbalanceABS + s(age, k=3) + Region + Condition, - data = fooofMRS %>% filter(zOffset < 2), random=list(luna=~1)) -summary(gam.model$gam) -print(plot(getViz(gam.model$gam), allTerms = T), pages = 1) - - -AIC(lmer(Offset ~ GluGABAimbalanceABS + age + Condition + Region + (1|luna), data = fooofMRS), - lmer(Offset ~ GluGABAimbalanceABS + inverseAge + Condition + Region + (1|luna), data = fooofMRS)) - - -## Gaba glu imbalance VS exponent ---- - -lunaize(ggplot(data = fooofMRS, - aes(y = GluGABAimbalanceABS, x = Exponent, by =luna, color = ageGroup)) + - geom_line(aes(group=interaction(luna,Region, Condition)), alpha = 0.2) + geom_point(aes(shape=Region),alpha=.5) + - geom_smooth(aes(group = 1, alpha = 0.01), method="lm",formula = y ~ poly(x,2),alpha=.8,size=1)) + - scale_color_manual(values=c("gold3", "blue4")) + - ylab("Glu GABA Imbalance") + xlab("Exponent")+ theme(text = element_text(size = 30))+ theme(legend.position='none') - - - - -# lmer approach, different forms of age, w/ and w/out interactions -summary(lmerTest::lmer(Exponent ~ GluGABAimbalanceABS + age + Region + Condition + (1|subjID), - data = fooofMRS %>% filter(zExp < 2))) -summary(lmerTest::lmer(Exponent ~ GluGABAimbalanceABS + inverseage + Region + Condition + (1|subjID), - data = fooofMRS %>% filter(zExp < 2) )) - -summary(lmerTest::lmer(Exponent ~ GluGABAimbalanceABS*age + Region + Condition + (1|subjID), - data = fooofMRS %>% filter(zExp < 2) )) -summary(lmerTest::lmer(Exponent ~ GluGABAimbalanceABS*inverseage + Region + Condition + (1|subjID), - data = fooofMRS %>% filter(zExp < 2) )) - -summary(lmerTest::lmer(Exponent ~ GluGABAimbalanceABS + inverseage + Condition + Region + (1|subjID), - data = fooofMRS %>% filter(zExp < 2) )) - - -# gam approach, with non-linear form of age -gam.model <- gamm(Exponent ~ GluGABAimbalanceABS + s(age, k=3) + Region + Condition, - data = fooofMRS %>% filter(zExp < 2), random=list(luna=~1)) -summary(gam.model$gam) -print(plot(getViz(gam.model$gam), allTerms = T), pages = 1) - -AIC(lmer(Exponent ~ GluGABAimbalanceABS + age + Condition + Region + (1|luna), data = fooofMRS), - lmer(Exponent ~ GluGABAimbalanceABS + inverseAge + Condition + Region + (1|luna), data = fooofMRS)) - - - diff --git a/Entropy/Entropy_Behavior.R b/Entropy/Entropy_Behavior.R deleted file mode 100644 index 9e162ba..0000000 --- a/Entropy/Entropy_Behavior.R +++ /dev/null @@ -1,757 +0,0 @@ - -# Load Libraries - -library(LNCDR) -library(data.table) -library(dplyr) -library(factoextra) -library(ggplot2) -library(e1071) -library(caret) -attach(mtcars) -library(grid) -library(gridExtra) -library(plotrix) -library(mgcv) -library(readxl) -library(lme4) -library(lubridate) -library(checkmate) -library(lmerTest) -library(tidyr) -library(jtools) -library(eegUtils) -library(tvem) -library(interactions) -library(akima) - - -outliers <- function(x) { - (abs(x - mean(x, na.rm= T)) > (sd(x, na.rm= T) * 2)) -} -naoutlier <- function(x) ifelse(outliers(x), NA, x) - - -# Load datasets ---- - -maxEntropy <- read.csv( '/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Entropy/Results/allSubjects_maxEntropy_allChansAvg.csv') -AvgChans_MSE <- read.csv('/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Entropy/Results/allSubjects_multiscaleEntropy_allChansAvg.csv')%>% separate(Subject, c('lunaID','vdate')) - - -AvgChans_MSE_outlier <- AvgChans_MSE %>% - mutate(across(c("MSx1", "MSx2", "MSx3", "MSx4", "MSx5", "MSx6", - "MSx7", "MSx8", "MSx9", "MSx10", "Var1"), naoutlier)) - - -merge7t <- read.csv('/Volumes/Hera/Projects/7TBrainMech/scripts/txt/merged_7t.csv') -behav <- merge7t[c("lunaid","visitno","eeg.age", "eeg.vgsLatency_DelayAll","eeg.BestError_DelayAll", "eeg.BestError_sd_DelayAll", - "eeg.mgsLatency_DelayAll", "eeg.mgsLatency_sd_DelayAll","cantab.ssp_max.span","cantab.ssp_nerrors", "cantab.ssp_ntrials", - "antiET.Cor", "antiET.Err", "antiET.ErrCor", "antiET.cor.lat")] - - -behav <- behav %>% - mutate(antiET.Cor = ifelse(antiET.Cor < 16, NA, antiET.Cor)) - -behav$corrat <- behav$antiET.Cor / (behav$antiET.Cor + behav$antiET.Err + behav$antiET.ErrCor) - -names(behav)[names(behav) == 'lunaid'] <- 'lunaID' -names(behav)[names(behav) == 'eeg.age'] <- 'age' - - -# Merge datasets ---- - -entropyBehav <- merge(maxEntropy %>% select(lunaID, visitno, maxEntropy), AvgChans_MSE_outlier %>% - select(lunaID, visitno, Var1, age), by = c('lunaID', 'visitno')) %>% - merge(., behav, by = c('lunaID', 'visitno', 'age'))%>% - mutate(ageGroup = cut(age, c(0,18,Inf), labels = c('Adol','Adults'))) - -names(entropyBehav)[names(entropyBehav) == 'Var1'] <- 'AOC' - - -# Age vs Anti Correct Response Rate ---- - -lunaize(ggplot(data = entropyBehav, aes(x = age, y = corrat)) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(aes(group = 1), method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) - -#Just Adults ---- -AdultsEntropyBehav <- entropyBehav %>% filter(age >=18) -# AOC ---- -##Anti Correct Response Rate ---- -### Quadratic ---- -lunaize(ggplot(data = AdultsEntropyBehav, aes(x = AOC, y = corrat)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE), se = FALSE, color = "blue")) + - xlab("AUC") + ylab("Anti Saccade Correct Rate") - - -model <- lmer(corrat ~ poly(AOC, 2, raw = TRUE) + age + (1|lunaID), data = AdultsEntropyBehav) -summary(model) - -### Linear ---- -lunaize(ggplot(data = AdultsEntropyBehav, aes(x = AOC, y = corrat)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", se = FALSE, color = "blue") ) + - xlab("AUC") + ylab("Anti Saccade Correct Rate") - - -model <- lmer(corrat ~ AOC + age + (1|lunaID), data = AdultsEntropyBehav) -summary(model) - - -## Anti Correct Response Rate Latency ---- -### Quadratic ---- - -lunaize(ggplot(data = AdultsEntropyBehav, aes(x = AOC, y = antiET.cor.lat)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE), se = FALSE, color = "blue") )+ - xlab("AUC") + ylab("Anti Saccade Correct Rate Latency") - - -model <- lmer(antiET.cor.lat ~ poly(AOC, 2, raw = TRUE) + age + (1|lunaID), data = AdultsEntropyBehav) -summary(model) - -### Linear ---- - -lunaize(ggplot(data = AdultsEntropyBehav, aes(x = AOC, y = antiET.cor.lat)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", se = FALSE, color = "blue") )+ - xlab("AUC") + ylab("Anti Saccade Correct Rate Latency") - - -model <- lmer(antiET.cor.lat ~ AOC + age + (1|lunaID), data = AdultsEntropyBehav) -summary(model) - - -## MGS Best Error ---- -### Quadratic ---- -lunaize(ggplot(data = AdultsEntropyBehav, aes(x = AOC, y = eeg.BestError_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE), se = FALSE, color = "blue") )+ - xlab("AUC") + ylab("MGS Accuracy") - -model <- lmer(eeg.BestError_DelayAll ~ poly(AOC, 2, raw = TRUE) + age + (1|lunaID), data = AdultsEntropyBehav) -summary(model) - -### Linear ---- -lunaize(ggplot(data = AdultsEntropyBehav, aes(x = AOC, y = eeg.BestError_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm" ,se = FALSE, color = "blue") )+ - xlab("AUC") + ylab("MGS Accuracy") - -model <- lmer(eeg.BestError_DelayAll ~ AOC + age + (1|lunaID), data = AdultsEntropyBehav) -summary(model) - -## MGS Best Error Var ---- -### Quadratic ---- -lunaize(ggplot(data = AdultsEntropyBehav, aes(x = AOC, y = eeg.BestError_sd_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE), se = FALSE, color = "blue") )+ - xlab("AUC") + ylab("MGS Accuracy Var") - - -model <- lmer(eeg.BestError_sd_DelayAll ~ poly(AOC, 2, raw = TRUE) + age + (1|lunaID), data = AdultsEntropyBehav) -summary(model) - -### linear ---- -lunaize(ggplot(data = AdultsEntropyBehav, aes(x = AOC, y = eeg.BestError_sd_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", se = FALSE, color = "blue") )+ - xlab("AUC") + ylab("MGS Accuracy Var") - - -model <- lmer(eeg.BestError_sd_DelayAll ~ AOC + age + (1|lunaID), data = AdultsEntropyBehav) -summary(model) - -## MGS Latency ---- -### Quadratic ---- -lunaize(ggplot(data = AdultsEntropyBehav, aes(x = AOC, y = eeg.mgsLatency_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE), se = FALSE, color = "blue") )+ - xlab("AUC") + ylab("MGS Latency") - - -model <- lmer(eeg.mgsLatency_DelayAll ~ poly(AOC, 2, raw = TRUE) + age + (1|lunaID), data = AdultsEntropyBehav) -summary(model) - -### Linear ---- -lunaize(ggplot(data = AdultsEntropyBehav, aes(x = AOC, y = eeg.mgsLatency_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", se = FALSE, color = "blue") )+ - xlab("AUC") + ylab("MGS Latency") - - -model <- lmer(eeg.mgsLatency_DelayAll ~ AOC + age + (1|lunaID), data = AdultsEntropyBehav) -summary(model) - - -## MGS Latency Var ---- -### Quadratic ---- -lunaize(ggplot(data = AdultsEntropyBehav, aes(x = AOC, y = eeg.mgsLatency_sd_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE), se = FALSE, color = "blue") )+ - xlab("AUC") + ylab("MGS Latency Var") - - -model <- lmer(eeg.mgsLatency_sd_DelayAll ~ poly(AOC, 2, raw = TRUE) + age + (1|lunaID), data = AdultsEntropyBehav) -summary(model) - -### Linear ---- -lunaize(ggplot(data = AdultsEntropyBehav, aes(x = AOC, y = eeg.mgsLatency_sd_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", se = FALSE, color = "blue") )+ - xlab("AUC") + ylab("MGS Latency Var") - - -model <- lmer(eeg.mgsLatency_sd_DelayAll ~ AOC + age + (1|lunaID), data = AdultsEntropyBehav) -summary(model) - - -## Spatial Span ---- -### Quadratic ---- -lunaize(ggplot(data = AdultsEntropyBehav, aes(x = AOC, y = cantab.ssp_max.span)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE), se = FALSE, color = "blue") )+ - xlab("AUC") + ylab("Spatial Span") - - -model <- lmer(cantab.ssp_max.span ~ poly(AOC, 2, raw = TRUE) + age + (1|lunaID), data = AdultsEntropyBehav) -summary(model) - -### Linear ---- -lunaize(ggplot(data = AdultsEntropyBehav, aes(x = AOC, y = cantab.ssp_max.span)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", se = FALSE, color = "blue") )+ - xlab("AUC") + ylab("Spatial Span") - - -model <- lmer(cantab.ssp_max.span ~ AOC + age + (1|lunaID), data = AdultsEntropyBehav) -summary(model) - -# Max Entropy ---- -##Anti Correct Response Rate ---- -### Quadratic ---- -lunaize(ggplot(data = AdultsEntropyBehav, aes(x = maxEntropy, y = corrat)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE), se = FALSE, color = "blue") )+ - xlab("Max Entropy") + ylab("Anti Saccade Correct Response Rate") - - -model <- lmer(corrat ~ poly(maxEntropy, 2, raw = TRUE) + age + (1|lunaID), data = AdultsEntropyBehav) -summary(model) - -### Linear ---- -lunaize(ggplot(data = AdultsEntropyBehav, aes(x = maxEntropy, y = corrat)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", se = FALSE, color = "blue") )+ - xlab("Max Entropy") + ylab("Anti Saccade Correct Response Rate") - - -model <- lmer(corrat ~ maxEntropy + age + (1|lunaID), data = AdultsEntropyBehav) -summary(model) - - -## Anti Correct Response Rate Latency ---- -### Quadratic ---- - -lunaize(ggplot(data = AdultsEntropyBehav, aes(x = maxEntropy, y = antiET.cor.lat)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE), se = FALSE, color = "blue") )+ - xlab("Max Entropy") + ylab("Anti Saccade Correct Response Rate Lat") - - -model <- lmer(antiET.cor.lat ~ poly(maxEntropy, 2, raw = TRUE) + age + (1|lunaID), data = AdultsEntropyBehav) -summary(model) - -### Linear ---- - -lunaize(ggplot(data = AdultsEntropyBehav, aes(x = maxEntropy, y = antiET.cor.lat)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", se = FALSE, color = "blue") )+ - xlab("Max Entropy") + ylab("Anti Saccade Correct Response Rate Lat") - - - -model <- lmer(antiET.cor.lat ~ maxEntropy + age + (1|lunaID), data = AdultsEntropyBehav) -summary(model) - - -## MGS Best Error ---- -### Quadratic ---- -lunaize(ggplot(data = AdultsEntropyBehav, aes(x = maxEntropy, y = eeg.BestError_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE), se = FALSE, color = "blue") )+ - xlab("Max Entropy") + ylab("MGS Accuracy") - -model <- lmer(eeg.BestError_DelayAll ~ poly(maxEntropy, 2, raw = TRUE) + age + (1|lunaID), data = AdultsEntropyBehav) -summary(model) - -### Linear ---- -lunaize(ggplot(data = AdultsEntropyBehav, aes(x = maxEntropy, y = eeg.BestError_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm" ,se = FALSE, color = "blue") )+ - xlab("Max Entropy") + ylab("MGS Accuracy") - -model <- lmer(eeg.BestError_DelayAll ~ maxEntropy + age + (1|lunaID), data = AdultsEntropyBehav) -summary(model) - -## MGS Best Error Var ---- -### Quadratic ---- -lunaize(ggplot(data = AdultsEntropyBehav, aes(x = maxEntropy, y = eeg.BestError_sd_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE), se = FALSE, color = "blue") )+ - xlab("Max Entropy") + ylab("MGS Accuracy Var") - - -model <- lmer(eeg.BestError_sd_DelayAll ~ poly(maxEntropy, 2, raw = TRUE) + age + (1|lunaID), data = AdultsEntropyBehav) -summary(model) - -### linear ---- -lunaize(ggplot(data = AdultsEntropyBehav, aes(x = maxEntropy, y = eeg.BestError_sd_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", se = FALSE, color = "blue") )+ - xlab("Max Entropy") + ylab("MGS Accuracy Var") - - -model <- lmer(eeg.BestError_sd_DelayAll ~ maxEntropy + age + (1|lunaID), data = AdultsEntropyBehav) -summary(model) - -## MGS Latency ---- -### Quadratic ---- -lunaize(ggplot(data = AdultsEntropyBehav, aes(x = maxEntropy, y = eeg.mgsLatency_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE), se = FALSE, color = "blue") )+ - xlab("Max Entropy") + ylab("MGS Latency") - - -model <- lmer(eeg.mgsLatency_DelayAll ~ poly(maxEntropy, 2, raw = TRUE) + age + (1|lunaID), data = AdultsEntropyBehav) -summary(model) - -### Linear ---- -lunaize(ggplot(data = AdultsEntropyBehav, aes(x = maxEntropy, y = eeg.mgsLatency_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", se = FALSE, color = "blue") )+ - xlab("Max Entropy") + ylab("MGS Latency") - - -model <- lmer(eeg.mgsLatency_DelayAll ~ maxEntropy + age + (1|lunaID), data = AdultsEntropyBehav) -summary(model) - - -## MGS Latency Var ---- -### Quadratic ---- -lunaize(ggplot(data = AdultsEntropyBehav, aes(x = maxEntropy, y = eeg.mgsLatency_sd_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE), se = FALSE, color = "blue") )+ - xlab("Max Entropy") + ylab("MGS Latency Var") - - -model <- lmer(eeg.mgsLatency_sd_DelayAll ~ poly(maxEntropy, 2, raw = TRUE) + age + (1|lunaID), data = AdultsEntropyBehav) -summary(model) - -### Linear ---- -lunaize(ggplot(data = AdultsEntropyBehav, aes(x = maxEntropy, y = eeg.mgsLatency_sd_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", se = FALSE, color = "blue") )+ - xlab("Max Entropy") + ylab("MGS Latency Var") - - -model <- lmer(eeg.mgsLatency_sd_DelayAll ~ maxEntropy + age + (1|lunaID), data = AdultsEntropyBehav) -summary(model) - - -## Spatial Span ---- -### Quadratic ---- -lunaize(ggplot(data = AdultsEntropyBehav, aes(x = maxEntropy, y = cantab.ssp_max.span)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE), se = FALSE, color = "blue") )+ - xlab("Max Entropy") + ylab("Spatial Span") - - -model <- lmer(cantab.ssp_max.span ~ poly(maxEntropy, 2, raw = TRUE) + age + (1|lunaID), data = AdultsEntropyBehav) -summary(model) - -### Linear ---- -lunaize(ggplot(data = AdultsEntropyBehav, aes(x = maxEntropy, y = cantab.ssp_max.span)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", se = FALSE, color = "blue") )+ - xlab("Max Entropy") + ylab("Spatial Span") - - -model <- lmer(cantab.ssp_max.span ~ maxEntropy + age + (1|lunaID), data = AdultsEntropyBehav) -summary(model) - -# All Subjects ---- - -# AOC ---- -##Anti Correct Response Rate ---- -### Quadratic ---- -lunaize(ggplot(data = entropyBehav, aes(x = AOC, y = corrat)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE), se = FALSE, color = "blue")) + - xlab("AUC") + ylab("Anti Saccade Correct Rate") - - -model <- lmer(corrat ~ poly(AOC, 2, raw = TRUE) + age + (1|lunaID), data = entropyBehav) -summary(model) - -### Linear ---- -lunaize(ggplot(data = entropyBehav, aes(x = AOC, y = corrat)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", se = FALSE, color = "blue") ) + - xlab("AUC") + ylab("Anti Saccade Correct Rate") - - -model <- lmer(corrat ~ AOC + age + (1|lunaID), data = entropyBehav) -summary(model) - -model <- lmer(corrat ~ AOC*age + (1|lunaID), data = entropyBehav) -summary(model) - - -## Anti Correct Response Rate Latency ---- -### Quadratic ---- - -lunaize(ggplot(data = entropyBehav, aes(x = AOC, y = antiET.cor.lat)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE), se = FALSE, color = "blue") )+ - xlab("AUC") + ylab("Anti Saccade Correct Rate Latency") - - -model <- lmer(antiET.cor.lat ~ poly(AOC, 2, raw = TRUE) + age + (1|lunaID), data = entropyBehav) -summary(model) - -### Linear ---- - -lunaize(ggplot(data = entropyBehav, aes(x = AOC, y = antiET.cor.lat)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", se = FALSE, color = "blue") )+ - xlab("AUC") + ylab("Anti Saccade Correct Rate Latency") - - -model <- lmer(antiET.cor.lat ~ AOC + age + (1|lunaID), data = entropyBehav) -summary(model) - -model <- lmer(antiET.cor.lat ~ AOC*age + (1|lunaID), data = entropyBehav) -summary(model) - - -## MGS Best Error ---- -### Quadratic ---- -lunaize(ggplot(data = entropyBehav, aes(x = AOC, y = eeg.BestError_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE), se = FALSE, color = "blue") )+ - xlab("AUC") + ylab("MGS Accuracy") - -model <- lmer(eeg.BestError_DelayAll ~ poly(AOC, 2, raw = TRUE) + age + (1|lunaID), data = entropyBehav) -summary(model) - -### Linear ---- -lunaize(ggplot(data = entropyBehav%>% - mutate(ageGroup = cut(age, c(0,18.24,Inf), labels = c('Adol','Adults'))), - aes(x = AOC, y = eeg.BestError_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(aes(group = ageGroup, color = ageGroup), method = "lm" ,se = FALSE) )+ - xlab("AUC") + ylab("MGS Accuracy") - -model <- lmer(eeg.BestError_DelayAll ~ AOC + age + (1|lunaID), data = entropyBehav) -summary(model) - -model <- lmer(eeg.BestError_DelayAll ~ AOC*age + (1|lunaID), data = entropyBehav) -summary(model) - -## try johnson neyman plots -johnson_neyman(model, pred = AOC, modx = age, plot = TRUE, title = "MGS Accuracy ~ AOC*age") - - -## MGS Best Error Var ---- -### Quadratic ---- -lunaize(ggplot(data = entropyBehav, aes(x = AOC, y = eeg.BestError_sd_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE), se = FALSE, color = "blue") )+ - xlab("AUC") + ylab("MGS Accuracy Var") - - -model <- lmer(eeg.BestError_sd_DelayAll ~ poly(AOC, 2, raw = TRUE) + age + (1|lunaID), data = entropyBehav) -summary(model) - -### linear ---- -lunaize(ggplot(data = entropyBehav, aes(x = AOC, y = eeg.BestError_sd_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", se = FALSE, color = "blue") )+ - xlab("AUC") + ylab("MGS Accuracy Var") - - -model <- lmer(eeg.BestError_sd_DelayAll ~ AOC + age + (1|lunaID), data = entropyBehav) -summary(model) - -model <- lmer(eeg.BestError_sd_DelayAll ~ AOC*age + (1|lunaID), data = entropyBehav) -summary(model) - -## MGS Latency ---- -### Quadratic ---- -lunaize(ggplot(data = entropyBehav, aes(x = AOC, y = eeg.mgsLatency_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE), se = FALSE, color = "blue") )+ - xlab("AUC") + ylab("MGS Latency") - - -model <- lmer(eeg.mgsLatency_DelayAll ~ poly(AOC, 2, raw = TRUE) + age + (1|lunaID), data = entropyBehav) -summary(model) - -### Linear ---- - - -lunaize(ggplot(data = entropyBehav%>% - mutate(ageGroup = cut(age, c(0,28.9,Inf), labels = c('10-28','28+'))), - aes(x = AOC, y = eeg.mgsLatency_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(aes(group = ageGroup, color = ageGroup), method = "lm" ,se = FALSE) )+ - xlab("AUC") + ylab("MGS Latency") - - - -model <- lmer(eeg.mgsLatency_DelayAll ~ AOC + age + (1|lunaID), data = entropyBehav) -summary(model) - -model <- lmer(eeg.mgsLatency_DelayAll ~ AOC*age + (1|lunaID), data = entropyBehav) -summary(model) - -## try johnson neyman plots -johnson_neyman(model, pred = AOC, modx = age, plot = TRUE, title = "MGS Latency ~ AOC*age") - - -## MGS Latency Var ---- -### Quadratic ---- -lunaize(ggplot(data = entropyBehav, aes(x = AOC, y = eeg.mgsLatency_sd_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE), se = FALSE, color = "blue") )+ - xlab("AUC") + ylab("MGS Latency Var") - - -model <- lmer(eeg.mgsLatency_sd_DelayAll ~ poly(AOC, 2, raw = TRUE) + age + (1|lunaID), data = entropyBehav) -summary(model) - -### Linear ---- -lunaize(ggplot(data = entropyBehav, aes(x = AOC, y = eeg.mgsLatency_sd_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", se = FALSE, color = "blue") )+ - xlab("AUC") + ylab("MGS Latency Var") - - -model <- lmer(eeg.mgsLatency_sd_DelayAll ~ AOC + age + (1|lunaID), data = entropyBehav) -summary(model) - -model <- lmer(eeg.mgsLatency_sd_DelayAll ~ AOC*age + (1|lunaID), data = entropyBehav) -summary(model) - - -## Spatial Span ---- -### Quadratic ---- -lunaize(ggplot(data = entropyBehav, aes(x = AOC, y = cantab.ssp_max.span)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE), se = FALSE, color = "blue") )+ - xlab("AUC") + ylab("Spatial Span") - - -model <- lmer(cantab.ssp_max.span ~ poly(AOC, 2, raw = TRUE) + age + (1|lunaID), data = entropyBehav) -summary(model) - -### Linear ---- - - -lunaize(ggplot(data = entropyBehav%>% - mutate(ageGroup = cut(age, c(0,16.29,Inf), labels = c('10-16.29','16.29+'))), - aes(x = AOC, y = cantab.ssp_max.span)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(aes(group = ageGroup, color = ageGroup), method = "lm" ,se = FALSE) )+ - xlab("AUC") + ylab("Spatial Span") - - -lunaize(ggplot(data = entropyBehav, aes(x = AOC, y = cantab.ssp_max.span)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", se = FALSE, color = "blue") )+ - xlab("AUC") + ylab("Spatial Span") - - -model <- lmer(cantab.ssp_max.span ~ AOC + age + (1|lunaID), data = entropyBehav) -summary(model) - -model <- lmer(cantab.ssp_max.span ~ AOC*age + (1|lunaID), data = entropyBehav) -summary(model) - -## try johnson neyman plots -johnson_neyman(model, pred = AOC, modx = age, plot = TRUE, title = "Spatial Span ~ AOC*age") - - - -# Max Entropy ---- -##Anti Correct Response Rate ---- -### Quadratic ---- -lunaize(ggplot(data = entropyBehav, aes(x = maxEntropy, y = corrat)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE), se = FALSE, color = "blue") )+ - xlab("Max Entropy") + ylab("Anti Saccade Correct Response Rate") - - -model <- lmer(corrat ~ poly(maxEntropy, 2, raw = TRUE) + age + (1|lunaID), data = entropyBehav) -summary(model) - -### Linear ---- -lunaize(ggplot(data = entropyBehav, aes(x = maxEntropy, y = corrat)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", se = FALSE, color = "blue") )+ - xlab("Max Entropy") + ylab("Anti Saccade Correct Response Rate") - - -model <- lmer(corrat ~ maxEntropy + age + (1|lunaID), data = entropyBehav) -summary(model) - -model <- lmer(corrat ~ maxEntropy*age + (1|lunaID), data = entropyBehav) -summary(model) - -## Anti Correct Response Rate Latency ---- -### Quadratic ---- - -lunaize(ggplot(data = entropyBehav, aes(x = maxEntropy, y = antiET.cor.lat)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE), se = FALSE, color = "blue") )+ - xlab("Max Entropy") + ylab("Anti Saccade Correct Response Rate Lat") - - -model <- lmer(antiET.cor.lat ~ poly(maxEntropy, 2, raw = TRUE) + age + (1|lunaID), data = entropyBehav) -summary(model) - -### Linear ---- - -lunaize(ggplot(data = entropyBehav, aes(x = maxEntropy, y = antiET.cor.lat)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", se = FALSE, color = "blue") )+ - xlab("Max Entropy") + ylab("Anti Saccade Correct Response Rate Lat") - - - -model <- lmer(antiET.cor.lat ~ maxEntropy + age + (1|lunaID), data = entropyBehav) -summary(model) - -model <- lmer(antiET.cor.lat ~ maxEntropy*age + (1|lunaID), data = entropyBehav) -summary(model) - - -## MGS Best Error ---- -### Quadratic ---- -lunaize(ggplot(data = entropyBehav, aes(x = maxEntropy, y = eeg.BestError_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE), se = FALSE, color = "blue") )+ - xlab("Max Entropy") + ylab("MGS Accuracy") - -model <- lmer(eeg.BestError_DelayAll ~ poly(maxEntropy, 2, raw = TRUE) + age + (1|lunaID), data = entropyBehav) -summary(model) - -### Linear ---- -lunaize(ggplot(data = entropyBehav, aes(x = maxEntropy, y = eeg.BestError_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm" ,se = FALSE, color = "blue") )+ - xlab("Max Entropy") + ylab("MGS Accuracy") - -model <- lmer(eeg.BestError_DelayAll ~ maxEntropy + age + (1|lunaID), data = entropyBehav) -summary(model) - -model <- lmer(eeg.BestError_DelayAll ~ maxEntropy*age + (1|lunaID), data = entropyBehav) -summary(model) - -## MGS Best Error Var ---- -### Quadratic ---- -lunaize(ggplot(data = entropyBehav, aes(x = maxEntropy, y = eeg.BestError_sd_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE), se = FALSE, color = "blue") )+ - xlab("Max Entropy") + ylab("MGS Accuracy Var") - - -model <- lmer(eeg.BestError_sd_DelayAll ~ poly(maxEntropy, 2, raw = TRUE) + age + (1|lunaID), data = entropyBehav) -summary(model) - -### linear ---- -lunaize(ggplot(data = entropyBehav, aes(x = maxEntropy, y = eeg.BestError_sd_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", se = FALSE, color = "blue") )+ - xlab("Max Entropy") + ylab("MGS Accuracy Var") - - -model <- lmer(eeg.BestError_sd_DelayAll ~ maxEntropy + age + (1|lunaID), data = entropyBehav) -summary(model) - -model <- lmer(eeg.BestError_sd_DelayAll ~ maxEntropy*age + (1|lunaID), data = entropyBehav) -summary(model) - -## MGS Latency ---- -### Quadratic ---- -lunaize(ggplot(data = entropyBehav, aes(x = maxEntropy, y = eeg.mgsLatency_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE), se = FALSE, color = "blue") )+ - xlab("Max Entropy") + ylab("MGS Latency") - - -model <- lmer(eeg.mgsLatency_DelayAll ~ poly(maxEntropy, 2, raw = TRUE) + age + (1|lunaID), data = entropyBehav) -summary(model) - -### Linear ---- -lunaize(ggplot(data = entropyBehav, aes(x = maxEntropy, y = eeg.mgsLatency_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", se = FALSE, color = "blue") )+ - xlab("Max Entropy") + ylab("MGS Latency") - - -model <- lmer(eeg.mgsLatency_DelayAll ~ maxEntropy + age + (1|lunaID), data = entropyBehav) -summary(model) - -model <- lmer(eeg.mgsLatency_DelayAll ~ maxEntropy*age + (1|lunaID), data = entropyBehav) -summary(model) - -## MGS Latency Var ---- -### Quadratic ---- -lunaize(ggplot(data = entropyBehav, aes(x = maxEntropy, y = eeg.mgsLatency_sd_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE), se = FALSE, color = "blue") )+ - xlab("Max Entropy") + ylab("MGS Latency Var") - - -model <- lmer(eeg.mgsLatency_sd_DelayAll ~ poly(maxEntropy, 2, raw = TRUE) + age + (1|lunaID), data = entropyBehav) -summary(model) - -### Linear ---- -lunaize(ggplot(data = entropyBehav, aes(x = maxEntropy, y = eeg.mgsLatency_sd_DelayAll)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", se = FALSE, color = "blue") )+ - xlab("Max Entropy") + ylab("MGS Latency Var") - - -model <- lmer(eeg.mgsLatency_sd_DelayAll ~ maxEntropy + age + (1|lunaID), data = entropyBehav) -summary(model) - -model <- lmer(eeg.mgsLatency_sd_DelayAll ~ maxEntropy*age + (1|lunaID), data = entropyBehav) -summary(model) - -## Spatial Span ---- -### Quadratic ---- -lunaize(ggplot(data = entropyBehav, aes(x = maxEntropy, y = cantab.ssp_max.span)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE), se = FALSE, color = "blue") )+ - xlab("Max Entropy") + ylab("Spatial Span") - - -model <- lmer(cantab.ssp_max.span ~ poly(maxEntropy, 2, raw = TRUE) + age + (1|lunaID), data = entropyBehav) -summary(model) - -### Linear ---- -lunaize(ggplot(data = entropyBehav, aes(x = maxEntropy, y = cantab.ssp_max.span)) + geom_point(na.rm=T) + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2, na.rm=T) + - geom_smooth(method = "lm", se = FALSE, color = "blue") )+ - xlab("Max Entropy") + ylab("Spatial Span") - - -model <- lmer(cantab.ssp_max.span ~ maxEntropy + age + (1|lunaID), data = entropyBehav) -summary(model) - -model <- lmer(cantab.ssp_max.span ~ maxEntropy*age + (1|lunaID), data = entropyBehav) -summary(model) diff --git a/Entropy/Entropy_Hurst.R b/Entropy/Entropy_Hurst.R deleted file mode 100755 index 1f0c173..0000000 --- a/Entropy/Entropy_Hurst.R +++ /dev/null @@ -1,182 +0,0 @@ - -# Load Libraries - -library(LNCDR) -library(dplyr) -library(ggplot2) -library(mgcv) -library(lme4) -library(lmerTest) -library(tidyr) - - -outliers <- function(x) { - (abs(x - mean(x, na.rm= T)) > (sd(x, na.rm= T) * 2)) -} -naoutlier <- function(x) ifelse(outliers(x), NA, x) -# Prep Dataframe ---- -## Load datasets ---- - -MSE <- read.csv( '/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Entropy/Results/allSubjects_allChans_MultiScaleEntropy.csv') - -## Entropy Outlier Detection ---- -entropy_outlier <- MSE %>% group_by(Subject) %>% - mutate(across(c("MSx1", "MSx2", "MSx3", "MSx4", "MSx5", "MSx6", - "MSx7", "MSx8", "MSx9", "MSx10", "Var1"), naoutlier)) %>% ungroup() - -merge7t <- read.csv('/Volumes/Hera/Projects/7TBrainMech/scripts/txt/merged_7t.csv') -ageValues <- merge7t[c("lunaid","eeg.date","visitno","eeg.age")] -colnames(ageValues) <- c("lunaID", "visitDate","visitno", "age") -ageValues$Subject <- paste(ageValues$lunaID, ageValues$visitDate, sep = "_") - -entropyAge <- merge(entropy_outlier, ageValues, by = "Subject") - -## Entropy averaged across only DLPFC electrodes ---- -entropyLDLPFC <- entropyAge %>% filter(labels %in% c('F3', 'F5', 'F7')) -entropyRDLPFC <- entropyAge %>% filter(labels %in% c('F4', 'F6', 'F8')) - -entropyLDLPFCAvg <- aggregate(cbind(MSx1, MSx2, MSx3, MSx4, MSx5, MSx6, MSx7, MSx8, MSx9, MSx10, Var1, age) ~ Subject + visitno, data = entropyLDLPFC, FUN = mean) -entropyRDLPFCAvg <- aggregate(cbind(MSx1, MSx2, MSx3, MSx4, MSx5, MSx6, MSx7, MSx8, MSx9, MSx10, Var1, age) ~ Subject + visitno, data = entropyRDLPFC, FUN = mean) - -entropyDLPFC <- merge(entropyLDLPFCAvg, entropyRDLPFCAvg, by = c("Subject", "visitno","age"), suffixes = c("_LDLPFC", "_RDLPFC")) - - -entropyAgeAvg_outlier_DLPFC <- entropyDLPFC %>% - mutate(across(c("MSx1_LDLPFC", "MSx2_LDLPFC", -"MSx3_LDLPFC", "MSx4_LDLPFC", "MSx5_LDLPFC", "MSx6_LDLPFC", "MSx7_LDLPFC", -"MSx8_LDLPFC", "MSx9_LDLPFC", "MSx10_LDLPFC", "Var1_LDLPFC", -"MSx1_RDLPFC", "MSx2_RDLPFC", "MSx3_RDLPFC", "MSx4_RDLPFC", "MSx5_RDLPFC", -"MSx6_RDLPFC", "MSx7_RDLPFC", "MSx8_RDLPFC", "MSx9_RDLPFC", "MSx10_RDLPFC", -"Var1_RDLPFC"), naoutlier)) %>% separate(Subject, c('lunaID','vdate')) - -entropyAgeAvg_outlier_long <- entropyAgeAvg_outlier_DLPFC %>% - pivot_longer( - cols = starts_with("MSx"), - names_to = c("timescale", "region"), - names_pattern = "MSx(\\d+)_(.*)", - values_to = "value" - ) %>% - pivot_wider( - names_from = region, - values_from = value, - names_prefix = "MSx_" - ) - - -# Find each subjects max entropy value on the MSE curve ---- -subs = unique(entropyAgeAvg_outlier_long$lunaID) -visits = unique(entropyAgeAvg_outlier_long$visitno) - - -maxValues <- data.frame() - -for (subject in subs) { - # Check if the subject has the visit number in the dataset - for (visitNum in visits) { - if (any(entropyAgeAvg_outlier_long$lunaID == subject & entropyAgeAvg_outlier_long$visitno == visitNum)) { - subData <- entropyAgeAvg_outlier_long %>% filter(lunaID == subject) %>% filter(visitno == visitNum) - maxSubEntropy_LDLPFC <- max(subData$MSx_LDLPFC) - maxSubEntropy_RDLPFC <- max(subData$MSx_RDLPFC) - - subInfo <- data.frame(lunaID = subject, visitno = visitNum, maxEntropy_LDLPFC = maxSubEntropy_LDLPFC, maxEntropy_RDLPFC = maxSubEntropy_RDLPFC) - maxValues <- rbind(maxValues, subInfo) - } - } -} - -entropyValuesMax <- merge(entropyAgeAvg_outlier_DLPFC, maxValues, by = c("lunaID", "visitno")) - -## Extract DLPFC Hurst from merge 7t ---- - -hurst <- merge7t[c("lunaid","visitno","rest.hurst.LDLPFC", "rest.hurst.RDLPFC")] -names(hurst)[names(hurst) == 'lunaid'] <- 'lunaID' -hurst$avgDLPFChurst <- rowMeans(hurst[, c("rest.hurst.LDLPFC", "rest.hurst.RDLPFC")], na.rm = TRUE) - -## Merge Entropy and Hurst ---- - -entropyHurst <- merge(entropyValuesMax, hurst, by = c("lunaID", "visitno")) -names(entropyHurst)[names(entropyHurst) == 'Var1_LDLPFC'] <- 'AUC_LDLPFC' -names(entropyHurst)[names(entropyHurst) == 'Var1_RDLPFC'] <- 'AUC_RDLPFC' - - -# Max Entropy vs Hurst ---- -## LDLPFC ---- -### Gam ---- -lunaize(ggplot(data = entropyHurst, aes(x = maxEntropy_LDLPFC, y = rest.hurst.LDLPFC)) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(group = 1, method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) - - -gam.model <- mgcv::gamm(rest.hurst.LDLPFC ~ maxEntropy_LDLPFC + s(age, k = 3), data = entropyHurst, random=list(lunaID=~1)) -summary(gam.model$gam) - -### Linear ---- -lunaize(ggplot(data = entropyHurst, aes(x = maxEntropy_LDLPFC, y = rest.hurst.LDLPFC)) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(group = 1, method="lm", alpha=0.4, linewidth=2)) - - -lm.model <- lmer(rest.hurst.LDLPFC ~ maxEntropy_LDLPFC + age + (1|lunaID), data = entropyHurst) -summary(lm.model) - -## RDLPFC ---- -### Gam ---- -lunaize(ggplot(data = entropyHurst, aes(x = maxEntropy_RDLPFC, y = rest.hurst.RDLPFC)) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(group = 1, method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) - - -gam.model <- mgcv::gamm(rest.hurst.RDLPFC ~ maxEntropy_RDLPFC + s(age, k = 3), data = entropyHurst, random=list(lunaID=~1)) -summary(gam.model$gam) - -### Linear ---- -lunaize(ggplot(data = entropyHurst, aes(x = maxEntropy_RDLPFC, y = rest.hurst.RDLPFC)) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(group = 1, method="lm", alpha=0.4, linewidth=2)) - - -lm.model <- lmer(rest.hurst.RDLPFC ~ maxEntropy_RDLPFC + age + (1|lunaID), data = entropyHurst) -summary(lm.model) - - - -# AUC vs Hurst ---- -## LDLPFC ---- -### Gam ---- -lunaize(ggplot(data = entropyHurst, aes(x = AUC_LDLPFC, y = rest.hurst.LDLPFC)) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(group = 1, method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) - - -gam.model <- mgcv::gamm(rest.hurst.LDLPFC ~ AUC_LDLPFC + s(age, k = 3), data = entropyHurst, random=list(lunaID=~1)) -summary(gam.model$gam) - -### Linear ---- -lunaize(ggplot(data = entropyHurst, aes(x = AUC_LDLPFC, y = rest.hurst.LDLPFC)) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(group = 1, method="lm", alpha=0.4, linewidth=2)) - - -lm.model <- lmer(rest.hurst.LDLPFC ~ AUC_LDLPFC + age + (1|lunaID), data = entropyHurst) -summary(lm.model) - -## RDLPFC ---- -### Gam ---- -lunaize(ggplot(data = entropyHurst, aes(x = AUC_RDLPFC, y = rest.hurst.RDLPFC)) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(group = 1, method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) - - -gam.model <- mgcv::gamm(rest.hurst.RDLPFC ~ AUC_RDLPFC + s(age, k = 3), data = entropyHurst, random=list(lunaID=~1)) -summary(gam.model$gam) - -### Linear ---- -lunaize(ggplot(data = entropyHurst, aes(x = AUC_RDLPFC, y = rest.hurst.RDLPFC)) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(group = 1, method="lm", alpha=0.4, linewidth=2)) - - -lm.model <- lmer(rest.hurst.RDLPFC ~ AUC_RDLPFC + age + (1|lunaID), data = entropyHurst) -summary(lm.model) - - diff --git a/Entropy/MSE20.R b/Entropy/MSE20.R deleted file mode 100755 index dfa5aff..0000000 --- a/Entropy/MSE20.R +++ /dev/null @@ -1,396 +0,0 @@ -# MSE Entropy 20 ---- - -entropy <- read.csv('/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Entropy/Results/allSubjects_allChans_MSE20.csv') %>% select(c(-type)) - -# Entropy Outlier Detection ---- -entropy_outlier <- entropy %>% group_by(Subject) %>% - mutate(across(c("MSx1", "MSx2", "MSx3", "MSx4", "MSx5", "MSx6", - "MSx7", "MSx8", "MSx9", "MSx10", "MSx11", "MSx12", "MSx13", "MSx14", - "MSx15", "MSx16", "MSx17", "MSx18", "MSx19", "MSx20", "Var1"), naoutlier)) %>% ungroup() - -merge7t <- read.csv('/Volumes/Hera/Projects/7TBrainMech/scripts/txt/merged_7t.csv') -ageValues <- merge7t[c("lunaid","eeg.date","visitno","eeg.age")] -colnames(ageValues) <- c("lunaID", "visitDate","visitno", "age") -ageValues$Subject <- paste(ageValues$lunaID, ageValues$visitDate, sep = "_") - -entropyAge <- merge(entropy_outlier, ageValues, by = "Subject") - -# Entropy averaged across all electrodes ---- -entropyAgeAvg <- aggregate(cbind(MSx1, MSx2, MSx3, MSx4, MSx5, MSx6, MSx7, MSx8, MSx9, MSx10, MSx11, MSx12, MSx13, MSx14, MSx15, MSx16, MSx17, MSx18, MSx19, MSx20, Var1, age) ~ Subject + visitno, data = entropyAge, FUN = mean) -write.csv(entropyAgeAvg, file = '/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Entropy/Results/allSubjects_MSE20_allChansAvg.csv') - - - -entropyAgeAvg_outlier <- entropyAgeAvg %>% - mutate(across(c("MSx1", "MSx2", "MSx3", "MSx4", "MSx5", "MSx6", - "MSx7", "MSx8", "MSx9", "MSx10", "MSx11", "MSx12", "MSx13", "MSx14", - "MSx15", "MSx16", "MSx17", "MSx18", "MSx19", "MSx20", "Var1"), naoutlier)) %>% separate(Subject, c('lunaID','vdate')) - - -lunaize(ggplot(data = entropyAgeAvg_outlier, aes(x = age, y = MSx6)) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(aes(group = 1), method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) - - -entropyAgeAvg_outlier_long <- entropyAgeAvg_outlier %>% - pivot_longer(cols = starts_with(c("MSx1", "MSx2", "MSx3", "MSx4", "MSx5", "MSx6", - "MSx7", "MSx8", "MSx9", "MSx10", "MSx11", "MSx12", "MSx13", "MSx14", - "MSx15", "MSx16", "MSx17", "MSx18", "MSx19", "MSx20")), - names_to = c(".value", "timeScale"), - names_pattern = "(\\D+)(\\d+)") %>% mutate(ageGroup = cut(age, c(0,13,16,19,Inf), labels = c('10-12','13-15','16-18','Adults'))) %>% - mutate(timeScale = as.numeric(timeScale)) - - -lunaize(ggplot(data = entropyAgeAvg_outlier_long, aes(x = as.numeric(timeScale), y = MSx)) + geom_point(aes(color = ageGroup)) + - geom_smooth(aes(group = ageGroup, color = ageGroup), method=mgcv::"gam", - formula = y ~ s(x, k = 5, fx = T), alpha=0.4, linewidth=1)) + xlab("Time Scale") - - - -entropy_with_se <- entropyAgeAvg_outlier_long %>% - group_by(timeScale, ageGroup) %>% - summarise( - MSx_mean = mean(MSx, na.rm = TRUE), - MSx_se = sd(MSx, na.rm = TRUE) / sqrt(n()) - ) - -lunaize(ggplot(data = entropy_with_se, aes(x = as.factor(timeScale), y = MSx_mean)) + geom_point(aes(color=ageGroup), position = position_dodge(width = 0.5))+ - geom_errorbar(aes(ymin = MSx_mean - MSx_se, ymax = MSx_mean + MSx_se, color = ageGroup), position = position_dodge(width = 0.5), width = 0.2)) + - xlab("Time Scale") + - ylab("Mean MSx") - -lunaize(ggplot(data = entropyAgeAvg_outlier, aes(x = age, y = MSx13)) + geom_point()+ - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(aes(group = 1), method="loess", alpha=0.2, linewidth=1)) - - -lunaize(ggplot(data = entropyAgeAvg_outlier_long %>% filter(timeScale >14 & timeScale <=20), aes(x = age, y = MSx, color = timeScale)) + - geom_smooth(aes(group = timeScale, color = timeScale), method="loess", alpha=0.2, linewidth=1)) - -lunaize(ggplot(data = entropyAgeAvg_outlier_long %>% filter(timeScale >=1 & timeScale <=5), aes(x = age, y = MSx, color = timeScale)) + - geom_smooth(aes(group = timeScale, color = timeScale), method="loess", alpha=0.2, linewidth=1)) - - -lunaize(ggplot(data = entropyAgeAvg_outlier_long %>% arrange(timeScale), - aes(x = as.numeric(timeScale), y = MSx, - group = interaction(lunaID, visitno), color = age)) + - geom_line(alpha = 0.4)) + xlab("Time Scale") - -lunaize(ggplot(data = entropyAgeAvg_outlier_long, aes(x = age, y = Var1)) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(group = 1, method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) + ylab("AUC") - - -gam.model <- mgcv::gamm(Var1 ~ s(age, k = 3), data = entropyAgeAvg_outlier_long, random=list(lunaID=~1)) -summary(gam.model$gam) - -## predict timescale by MSE for each ageGroup ---- -entropyAgeAvg_outlier_long <- entropyAgeAvg_outlier_long %>% mutate(timeScale_factor = as.factor(timeScale)) - -interactionmodel <- lmer(MSx ~ ageGroup*(timeScale_factor) + (1|lunaID), data = entropyAgeAvg_outlier_long) -summary(interactionmodel) - -prediction<- ggpredict(interactionmodel, terms = c("timeScale_factor", "ageGroup")) -plot(prediction) - -lunaize(ggplot(data = prediction %>% filter(x %in% seq(2, 20, by = 2)), - aes(x = x, y = predicted, group = group, color = group)) + - geom_point(position = position_dodge(width = 0.5), size =2) + # Dodge the points - geom_errorbar(aes(ymin = conf.low, ymax = conf.high), - width = 0.2, - position = position_dodge(width = 0.5)) ) +xlab("Time Scale") +ylab("predicted MSE") - - -ggeffects::hypothesis_test(interactionmodel, terms = c("timeScale_factor[2,18]", "ageGroup")) - - - -e# Find each subjects max entropy value on the MSE curve -subs = unique(entropyAgeAvg_outlier_long$lunaID) -visits = unique(entropyAgeAvg_outlier_long$visitno) - - -maxValues <- data.frame() - -for (subject in subs) { - # Check if the subject has the visit number in the dataset - for (visitNum in visits) { - if (any(entropyAgeAvg_outlier_long$lunaID == subject & entropyAgeAvg_outlier_long$visitno == visitNum)) { - subData <- entropyAgeAvg_outlier_long %>% filter(lunaID == subject) %>% filter(visitno == visitNum) - if (all(is.na(subData$MSx))) { - next - } - - maxSubEntropy <- max(subData$MSx, na.rm = T) - timescale <- subData$timeScale[which(subData$MSx == maxSubEntropy)] - - subInfo <- data.frame(lunaID = subject, visitno = visitNum, maxEntropy = maxSubEntropy, maxTimeScale = timescale) - maxValues <- rbind(maxValues, subInfo) - } - } -} - -maxValuesAge <- merge(maxValues, ageValues, by = c('lunaID', "visitno")) -write.csv(maxValues, file = '/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Entropy/Results/allSubjects_maxEntropy_allChansAvg.csv') - -lunaize(ggplot(data = maxValuesAge, aes(x = age, y = maxEntropy)) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(group = 1, method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) + ylab("Max Entropy") - - -gam.model <- mgcv::gamm(maxEntropy ~ s(age, k = 3), data = maxValuesAge, random=list(lunaID=~1)) -summary(gam.model$gam) - - -lunaize(ggplot(data = maxValuesAge, aes(x = age, y = as.numeric(maxTimeScale))) + geom_point() + - geom_line(aes(group= interaction(lunaID)), alpha = 0.2) + - geom_smooth(group = 1, method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) + ylab("Max Entropy TimeScale") - - -gam.model <- mgcv::gamm(maxEntropy ~ s(age, k = 3), data = maxValuesAge, random=list(lunaID=~1)) -summary(gam.model$gam) - -# MSE Entropy vs FOOOF ---- - - -## Load and avg fooof across all electrodes ---- -fooofAllchans <- read.csv('/Volumes/Hera/Projects/7TBrainMech/scripts/eeg/Shane/Aperiodic_MRS_Development/Results/allSubjectsAllChannelsFooofMeasures_20230911.csv') - -fooofAllchans <- fooofAllchans %>% - rename(labels = Channel) - -fooofAvg <- aggregate(cbind(Offset, Exponent) ~ Subject + Condition, data = fooofAllchans, FUN = mean) - -fooofAvg_outlier <- fooofAvg %>% group_by(Condition) %>% - mutate(across(c("Offset", "Exponent"), naoutlier)) %>% ungroup() %>% separate(Subject, c('lunaID','vdate')) - -## Merge MSE and fooof ---- - -MSEfooof <- merge(fooofAvg_outlier, entropyAgeAvg_outlier_long, by = c('lunaID','vdate')) %>% - merge(., maxValuesAge, by = c("lunaID", "visitno", "age")) %>% mutate(timeScale = (as.numeric(timeScale))) - - -lunaize(ggplot(data = MSEfooof %>% filter(Condition == 'eyesOpen') %>% filter(timeScale >=14 & timeScale<=20) %>% filter(age>18), aes(x = Exponent, y = MSx))+ - geom_smooth(aes(group = timeScale, color = timeScale), - method= "loess", alpha=0.2, linewidth=2)) - - -model <- lmer(MSx ~ Exponent + age + timeScale + (1|lunaID), data = MSEfooof %>% filter(Condition == 'eyesOpen')) -summary(model) - -model <- lmer(MSx ~ Exponent *age + timeScale + (1|lunaID), data = MSEfooof %>% filter(Condition == 'eyesOpen')) -summary(model) - -interactionmodel <- lmer(MSx ~ ageGroup + Exponent*timeScale + (1|lunaID), data = MSEfooof %>% filter(Condition == 'eyesOpen')) -summary(model) - -prediction<- ggpredict(interactionmodel, terms = c("Exponent", "timeScale[2,4,6,8,10,14,16,18,20]", "ageGroup")) - -plot(prediction) - -ggeffects::hypothesis_test(interactionmodel, terms = c("Exponent", "timeScale[2,6,10,15,20]", "ageGroup"), test = NULL) - - -### AUC ---- -lunaize(ggplot(data = MSEfooof, aes(y = Exponent, x = Var1)) + geom_point() + - geom_line(aes(group= interaction(lunaID, Condition)), alpha = 0.2) + - geom_smooth(aes(group = Condition, color = Condition), - method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2))+xlab("AUC") - - -gam.model <- mgcv::gamm(Exponent ~ s(age, k = 3)+ Condition + Var1, data = MSEfooof, random=list(lunaID=~1)) -summary(gam.model$gam) - -lunaize(ggplot(data = MSEfooof, aes(y = Offset, x = Var1))+ geom_point() + - geom_line(aes(group= interaction(lunaID, Condition)), alpha = 0.2)+ - geom_smooth(aes(group = Condition, color = Condition), method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2))+xlab("AUC") - -gam.model <- mgcv::gamm(Offset ~ s(age, k = 3)+ Condition + Var1, data = MSEfooof, random=list(lunaID=~1)) -summary(gam.model$gam) - -### Max Entropy ---- - -lunaize(ggplot(data = MSEfooof, aes(y = Exponent, x = maxEntropy)) + geom_point() + - geom_line(aes(group= interaction(lunaID, Condition)), alpha = 0.2) + - geom_smooth(aes(group = Condition, color = Condition), - method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) - - -gam.model <- mgcv::gamm(Exponent ~ s(age, k = 3)+ Condition + maxEntropy, data = MSEfooof, random=list(lunaID=~1)) -summary(gam.model$gam) - -lunaize(ggplot(data = MSEfooof, aes(y = Offset, x = maxEntropy))+ geom_point() + - geom_line(aes(group= interaction(lunaID, Condition)), alpha = 0.2)+ - geom_smooth(aes(group = Condition, color = Condition), - method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) - -gam.model <- mgcv::gamm(Offset ~ s(age, k = 3)+ Condition + maxEntropy, data = MSEfooof, random=list(lunaID=~1)) -summary(gam.model$gam) - -# Regions ---- -regionalEntropy <- entropyAge %>% - mutate(region = case_when( - startsWith(labels, "F") ~ "Frontal", - startsWith(labels, "C") ~ "Central", - startsWith(labels, "P") ~ "Parietal", - startsWith(labels, "O") ~ "Occipital" - )) - - -regionalEntropyAvg <- aggregate(cbind(MSx1, MSx2, MSx3, MSx4, MSx5, MSx6, MSx7, MSx8, MSx9, MSx10, MSx11, MSx12, MSx13, MSx14, MSx15, MSx16, MSx17, MSx18, MSx19, MSx20, Var1, age) ~ Subject + visitno + region, data = regionalEntropy, FUN = mean) - -regionalEntropyAvg_outlier <- regionalEntropyAvg %>% - mutate(across(c("MSx1", "MSx2", "MSx3", "MSx4", "MSx5", "MSx6", - "MSx7", "MSx8", "MSx9", "MSx10", "MSx11", "MSx12", "MSx13", "MSx14", - "MSx15", "MSx16", "MSx17", "MSx18", "MSx19", "MSx20", "Var1"), naoutlier)) %>% separate(Subject, c('lunaID','vdate')) - - -regionalEntropyAvg_outlier_long <- regionalEntropyAvg_outlier %>% - pivot_longer(cols = starts_with(c("MSx1", "MSx2", "MSx3", "MSx4", "MSx5", "MSx6", - "MSx7", "MSx8", "MSx9", "MSx10", "MSx11", "MSx12", "MSx13", "MSx14", - "MSx15", "MSx16", "MSx17", "MSx18", "MSx19", "MSx20")), - names_to = c(".value", "timeScale"),names_pattern = "(\\D+)(\\d+)") %>% mutate(ageGroup = cut(age, c(0,13,16,19,Inf), labels = c('10-12','13-15','16-18','Adults'))) %>% - mutate(timeScale = as.numeric(timeScale)) - -lunaize(ggplot(data = regionalEntropyAvg_outlier_long, aes(x = age, y = MSx, color = as.numeric(timeScale))) + - geom_smooth(aes(group = as.numeric(timeScale), color = as.numeric(timeScale)), method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) + - facet_wrap(~region) - -regionalEntropyAvg_outlier_long_se <- regionalEntropyAvg_outlier_long %>% - group_by(timeScale, ageGroup,region) %>% - summarise( - MSx_mean = mean(MSx, na.rm = TRUE), - MSx_se = sd(MSx, na.rm = TRUE) / sqrt(n()) - ) - -lunaize(ggplot(data = regionalEntropyAvg_outlier_long_se%>% filter(timeScale %in% seq(2, 20, by = 2)), aes(x = as.factor(timeScale), y = MSx_mean))) + geom_point(aes(color=ageGroup, shape = region), position = position_dodge(width = 0.7))+ - geom_errorbar(aes(ymin = MSx_mean - MSx_se, ymax = MSx_mean + MSx_se, color = ageGroup), position = position_dodge(width = 0.7), width = 0.2) + - xlab("Time Scale") + - ylab("Mean MSx") - - -## AUC ---- -lunaize(ggplot(data = regionalEntropyAvg_outlier_long, aes(x = age, y = Var1, color = region)) + geom_point() + - geom_line(aes(group= interaction(lunaID, region)), alpha = 0.2) + - geom_smooth(aes(group = region), method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) + ylab("AOC") - - -gam.model <- mgcv::gamm(Var1 ~ s(age, k = 3) + region, data = regionalEntropyAvg_outlier_long, random=list(lunaID=~1)) -summary(gam.model$gam) - -## Max entropy ---- -# Find each subjects max entropy value on the MSE curve -subs = unique(regionalEntropyAvg_outlier_long$lunaID) -visits = unique(regionalEntropyAvg_outlier_long$visitno) -regions = unique(regionalEntropyAvg_outlier_long$region) - - - -maxValuesRegions <- data.frame() - -for (subject in subs) { - # Check if the subject has the visit number in the dataset - for (visitNum in visits) { - for (regionNow in regions) { - if (any(regionalEntropyAvg_outlier_long$lunaID == subject & regionalEntropyAvg_outlier_long$visitno == visitNum & regionalEntropyAvg_outlier_long$region == regionNow)) { - subData <- regionalEntropyAvg_outlier_long %>% filter(lunaID == subject) %>% filter(visitno == visitNum)%>% filter(region == regionNow) - if (all(is.na(subData$MSx))) { - next - } - - maxSubEntropy <- max(subData$MSx, na.rm = T) - timescale <- subData$timeScale[which(subData$MSx == maxSubEntropy)] - - subInfo <- data.frame(lunaID = subject, visitno = visitNum, maxEntropy = maxSubEntropy, maxTimeScale = timescale, region = regionNow) - maxValuesRegions <- rbind(maxValuesRegions, subInfo) - } - } - } -} - -maxValuesRegions <- merge(maxValuesRegions, ageValues, by = c('lunaID', "visitno")) - - -lunaize(ggplot(data = maxValuesRegions, aes(x = age, y = maxEntropy, color = region)) + geom_point(aes(color = region)) + - geom_line(aes(group= interaction(lunaID, region)), alpha = 0.2) + - geom_smooth(aes(group = region), method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) + ylab("Max Entropy") - - -gam.model <- mgcv::gamm(maxEntropy ~ s(age, k = 3) + region, data = maxValuesRegions, random=list(lunaID=~1)) -summary(gam.model$gam) - - -lunaize(ggplot(data = maxValuesRegions, aes(x = age, y = as.numeric(maxTimeScale), color = region)) + geom_point() + - geom_line(aes(group= interaction(lunaID, region)), alpha = 0.2) + - geom_smooth(aes(group = region), method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) + ylab("Max Entropy TimeScale") - - -gam.model <- mgcv::gamm(maxEntropy ~ s(age, k = 3), data = maxValuesAge, random=list(lunaID=~1)) -summary(gam.model$gam) - - -# Regions vs FOOOF ---- - - -MSEfooof <- merge(fooofAvg_outlier, regionalEntropyAvg_outlier_long, by = c('lunaID','vdate')) %>% - merge(., maxValuesAge, by = c("lunaID", "visitno", "age")) %>% mutate(timeScale = (as.numeric(timeScale))) - - -lunaize(ggplot(data = MSEfooof %>% filter(Condition == 'eyesOpen') %>% filter(timeScale >=14 & timeScale<=20), - aes(x = Exponent, y = MSx))+ - geom_smooth(aes(group = timeScale, color = timeScale), - method= "loess", alpha=0.2, linewidth=2)) + facet_wrap(~region) - - -model <- lmer(MSx ~ Exponent + age + timeScale + (1|lunaID), data = MSEfooof %>% filter(Condition == 'eyesOpen')) -summary(model) - -model <- lmer(MSx ~ Exponent *age + timeScale + (1|lunaID), data = MSEfooof %>% filter(Condition == 'eyesOpen')) -summary(model) - -interactionmodel <- lmer(MSx ~ ageGroup + Exponent*timeScale +region + (1|lunaID), data = MSEfooof %>% filter(Condition == 'eyesOpen')) -summary(interactionmodel) - -prediction<- ggpredict(interactionmodel, terms = c("Exponent", "timeScale[2,4,6,8,10,14,16,18,20]", "ageGroup", "region")) - -plot(prediction) - -ggeffects::hypothesis_test(interactionmodel, terms = c("Exponent", "timeScale[2,6,10,15,20]", "ageGroup"), test = NULL) - - -### AUC ---- -lunaize(ggplot(data = MSEfooof, aes(y = Exponent, x = Var1)) + geom_point() + - geom_line(aes(group= interaction(lunaID, Condition)), alpha = 0.2) + - geom_smooth(aes(group = Condition, color = Condition), - method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2))+xlab("AUC") - - -gam.model <- mgcv::gamm(Exponent ~ s(age, k = 3)+ Condition + Var1, data = MSEfooof, random=list(lunaID=~1)) -summary(gam.model$gam) - -lunaize(ggplot(data = MSEfooof, aes(y = Offset, x = Var1))+ geom_point() + - geom_line(aes(group= interaction(lunaID, Condition)), alpha = 0.2)+ - geom_smooth(aes(group = Condition, color = Condition), method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2))+xlab("AUC") - -gam.model <- mgcv::gamm(Offset ~ s(age, k = 3)+ Condition + Var1, data = MSEfooof, random=list(lunaID=~1)) -summary(gam.model$gam) - -### Max Entropy ---- - -lunaize(ggplot(data = MSEfooof, aes(y = Exponent, x = maxEntropy)) + geom_point() + - geom_line(aes(group= interaction(lunaID, Condition)), alpha = 0.2) + - geom_smooth(aes(group = Condition, color = Condition), - method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) - - -gam.model <- mgcv::gamm(Exponent ~ s(age, k = 3)+ Condition + maxEntropy, data = MSEfooof, random=list(lunaID=~1)) -summary(gam.model$gam) - -lunaize(ggplot(data = MSEfooof, aes(y = Offset, x = maxEntropy))+ geom_point() + - geom_line(aes(group= interaction(lunaID, Condition)), alpha = 0.2)+ - geom_smooth(aes(group = Condition, color = Condition), - method=mgcv::"gam", formula = y ~ s(x, k = 3, fx = T), alpha=0.4, linewidth=2)) - -gam.model <- mgcv::gamm(Offset ~ s(age, k = 3)+ Condition + maxEntropy, data = MSEfooof, random=list(lunaID=~1)) -summary(gam.model$gam) - diff --git a/Entropy/eeglabhist.m b/Entropy/eeglabhist.m deleted file mode 100755 index ecba5d8..0000000 --- a/Entropy/eeglabhist.m +++ /dev/null @@ -1,30 +0,0 @@ -% EEGLAB history file generated on the 08-Mar-2024 -% ------------------------------------------------ - -EEG.etc.eeglabvers = '14.1.2'; % this tracks which version of EEGLAB is being used, you may ignore it -EEG.setname='10173_20180801_REST_Rem'; -EEG = eeg_checkset( EEG ); -EEG = eeg_checkset( EEG ); -EEG = eeg_checkset( EEG ); -EEG = eeg_checkset( EEG ); -EEG.setname='10173_20180801_REST_Rem'; -EEG = eeg_checkset( EEG ); -EEG = eeg_checkset( EEG ); -EEG = eeg_checkset( EEG ); -EEG = eeg_checkset( EEG ); -EEG = eeg_checkset( EEG ); -EEG.setname='10173_20180801_REST_Rem_avref'; -EEG = eeg_checkset( EEG ); -EEG = eeg_checkset( EEG ); -EEG.setname='10173_20180801_REST_Rem_rerefwhole'; -EEG = eeg_checkset( EEG ); -EEG = eeg_checkset( EEG ); -EEG = eeg_checkset( EEG ); -EEG = eeg_checkset( EEG ); -EEG = eeg_checkset( EEG ); -EEG.etc.eeglabvers = '2022.1'; % this tracks which version of EEGLAB is being used, you may ignore it -EEG = eeg_checkset( EEG ); -EEG = eeg_checkset( EEG ); -EEG = pop_rmdat( EEG, {'16130','16256'},[0 4] ,0); -EEG = eeg_checkset( EEG ); -pop_eegplot( EEG, 1, 1, 1); diff --git a/Entropy/sEEG/CombineSubjectDataframes.R b/Entropy/sEEG/CombineSubjectDataframes.R deleted file mode 100644 index 7fa4a78..0000000 --- a/Entropy/sEEG/CombineSubjectDataframes.R +++ /dev/null @@ -1,41 +0,0 @@ -# Libraries ---- - -library(LNCDR) -library(data.table) -library(dplyr) -library(factoextra) -library(ggplot2) -library(e1071) -library(caret) -attach(mtcars) -library(grid) -library(gridExtra) -library(plotrix) -library(mgcv) -library(readxl) -library(lme4) -library(lubridate) -library(checkmate) -library(lmerTest) -library(tidyr) -library(jtools) - -# Initialize directory and empty dataframes ---- - -# Set your working directory to the folder containing your CSV files -setwd('/Users/shanemckeon/Library/CloudStorage/OneDrive-UniversityofPittsburgh/PBE Lab/entropy/individualSubjectFiles/') - -# List all CSV files in the directory -csv_files <- list.files(pattern = "permEntropy.csv") - -# Initialize an empty data frame to store the combined data -combined_data <- data.frame() - -# Loop through each CSV file, read it, and combine it ---- -for (file in csv_files) { - data <- read.csv(file, header = TRUE) # Change header argument if needed - combined_data <- rbind(combined_data, data) -} - - -write.csv(combined_data, file = '/Users/shanemckeon/Library/CloudStorage/OneDrive-UniversityofPittsburgh/PBE Lab/entropy/allSubjectsEntropy.csv', row.names = F) diff --git a/Entropy/sEEG/entropy.R b/Entropy/sEEG/entropy.R deleted file mode 100644 index 63ae362..0000000 --- a/Entropy/sEEG/entropy.R +++ /dev/null @@ -1,63 +0,0 @@ - - -library(LNCDR) -library(data.table) -library(dplyr) -library(factoextra) -library(ggplot2) -library(e1071) -library(caret) -attach(mtcars) -library(grid) -library(gridExtra) -library(plotrix) -library(mgcv) -library(readxl) -library(lme4) -library(lubridate) -library(checkmate) -library(lmerTest) -library(tidyr) -library(jtools) -library(eegUtils) -library(tvem) -library(interactions) -library(akima) -library(mice) - - -entropy <- read.csv('/Users/shanemckeon/Library/CloudStorage/OneDrive-UniversityofPittsburgh/PBE Lab/entropy/allSubjectsEntropy.csv') - -nonEpContacts <- read.csv('/Users/shanemckeon/Library/CloudStorage/OneDrive-UniversityofPittsburgh/PBE Lab/nonEPcontacts.csv') - -# List of subjects -subjects <-intersect(unique(nonEpContacts$Subject), unique(entropy$Subject)) - - -# Initialize an empty data frame to store the results -result <- data.frame() - -# Loop through subjects -for (subject in subjects) { - # Select contacts for the current subject - selectContacts <- nonEpContacts %>% - filter(Subject == subject) %>% - select(nonEpContacts) - - # Create a formatted string - formattedString <- paste(selectContacts$nonEpContacts, collapse = '|') - - # Filter rows in entropy for the current subject - filteredData <- entropy[grep(formattedString, entropy$label), ] %>% - subset(., !grepl('ET', label)) %>% - subset(., !grepl('EEG', label)) %>% - filter(Subject == subject) - - # Append the result to the overall result data frame - subjectResult <- bind_rows(result, filteredData) - - result <- rbind(result, subjectResult) -} - - - diff --git a/Entropy/sEEG/sEEG_analysis.m b/Entropy/sEEG/sEEG_analysis.m deleted file mode 100755 index a9e46ed..0000000 --- a/Entropy/sEEG/sEEG_analysis.m +++ /dev/null @@ -1,69 +0,0 @@ - -addpath(('/Users/shanemckeon/Library/CloudStorage/OneDrive-UniversityofPittsburgh/1. Recording & Stimulation of iEEG - pbelchps files/Image Reconstruction/customFcns')) -addpath(genpath('/Users/shanemckeon/Library/CloudStorage/OneDrive-UniversityofPittsburgh/abelCode/ripple/Tools/neuroshare')) - -datapath = ('/Users/shanemckeon/Library/CloudStorage/OneDrive-UniversityofPittsburgh/PBE Lab/sEEG_backup/sEEG_rawData/'); -patientAnatPath = ('/Users/shanemckeon/Library/CloudStorage/OneDrive - University of Pittsburgh/1. Recording & Stimulation of iEEG - pbelchps files/Image Reconstruction/patients/'); -savePath = ('/Users/shanemckeon/Library/CloudStorage/OneDrive-UniversityofPittsburgh/PBE Lab/sEEG_backup/sEEG_rawData/'); - -%load in all the files -setfiles0 = dir([datapath, 'P*/Rest/rest*ns2*']); -setfiles = {}; - -for epo = 1:length(setfiles0) - setfiles{epo,1} = fullfile(datapath, setfiles0(epo).name); % cell array with EEG file names - % setfiles = arrayfun(@(x) fullfile(folder, x.name), setfiles(~[setfiles.isdir]),folder, 'Uni',0); % cell array with EEG file names -end - -for j = 1:length(setfiles0) - idvalues(j,:) = (setfiles0(j).folder(106:112)); - disp(idvalues(j,:)) - if isfile([savePath, idvalues(j,:), '/Rest/data.mat']) % is there a .mat file for the patient - continue - else % no .mat file for patient. we need to make it - [nsResult,hFile] = ns_OpenFile(fullfile([setfiles0(j).folder,'/', setfiles0(j).name])); - mtgTbl = readtable(fullfile([datapath, idvalues(j,:), '/Rest/montage', idvalues(j,:),'.xlsx'])); - [chans,isort] = sort(mtgTbl{~isnan(mtgTbl{:,1}),2}); - info.chanNames = chans; - % get indices of all channels labeled 'lfp #' - labels = {hFile.Entity.Label}; - iLfp = find(cellfun(@(x) contains(x,'lfp '),labels)); - % extract block of all data - [nsResult,dataBlock] = ns_GetAnalogDataBlock(hFile,iLfp,1,hFile.Entity(iLfp(1)).Count); - data = dataBlock(:,isort); - - save(fullfile([savePath, idvalues(j,:), '/Rest/data.mat']),'info','data','-v7.3') - - - end - -end - -for j = 1:length(setfiles0) - idvalues(j,:) = (setfiles0(j).folder(99:105)); - disp(idvalues(j,:)) - if isfile([savePath, idvalues(j,:), '/Rest/data.mat']) % is there a .mat file for the patient, if there is lets make an roi file - - anatFolder = ([patientAnatPath, idvalues(j,:)]); - - if isfile([patientAnatPath, idvalues(j,:), '/anat.mat']) % is there a anat file - load([anatFolder, '/anat.mat']); % load anat file - load([savePath, idvalues(j,:), '/data.mat']); % load data file - - roi = chan2roi(idvalues(j,:),info.chanNames); % run chan2roi function - - save([savePath, idvalues(j,:), '/roi.mat'], 'roi') - writecell(roi,[savePath, idvalues(j,:), '/roi.csv']); - else %if there is no anat file, we have to break the loop, cause we cant make an roi file - continue - - end - - else % if there isnt a data.mat file, break the loop and move on - continue - end - -end - - - diff --git a/Entropy/sEEG/sEEG_entropyOneDrive.m b/Entropy/sEEG/sEEG_entropyOneDrive.m deleted file mode 100644 index 595eb5e..0000000 --- a/Entropy/sEEG/sEEG_entropyOneDrive.m +++ /dev/null @@ -1,81 +0,0 @@ - -addpath(('/Users/shanemckeon/Library/CloudStorage/OneDrive-UniversityofPittsburgh/1. Recording & Stimulation of iEEG - pbelchps files/Image Reconstruction/customFcns')) -addpath(genpath('/Users/shanemckeon/Library/CloudStorage/OneDrive-UniversityofPittsburgh/abelCode/ripple/Tools/neuroshare')) - -datapath = ('/Users/shanemckeon/Library/CloudStorage/OneDrive-UniversityofPittsburgh/PBE Lab/sEEG_backup/sEEG_rawData/'); -patientAnatPath = ('/Users/shanemckeon/Library/CloudStorage/OneDrive - University of Pittsburgh/1. Recording & Stimulation of iEEG - pbelchps files/Image Reconstruction/patients/'); -savePath = ('/Users/shanemckeon/Library/CloudStorage/OneDrive-UniversityofPittsburgh/PBE Lab/sEEG_backup/sEEG_rawData/'); -entropyPath = ('/Users/shanemckeon/Library/CloudStorage/OneDrive-UniversityofPittsburgh/PBE Lab/entropy/individualSubjectFiles/'); - - -agefile = readtable('/Users/shanemckeon/Library/CloudStorage/OneDrive-UniversityofPittsburgh/PBE Lab/PBE_agefile.csv'); - -%load in all the files -setfiles0 = dir([datapath, 'P*/Rest/rest*ns2*']); -setfiles = {}; - -for epo = 1:length(setfiles0) - setfiles{epo,1} = fullfile(datapath, setfiles0(epo).name); % cell array with EEG file names - % setfiles = arrayfun(@(x) fullfile(folder, x.name), setfiles(~[setfiles.isdir]),folder, 'Uni',0); % cell array with EEG file names -end - - -for j = 1:length(setfiles0) - - idvalues(j,:) = (setfiles0(j).folder(106:112)); - - if ~isfile([entropyPath idvalues(j,:) '_permEntropy.csv']) - - if isfile([savePath, idvalues(j,:), '/Rest/data.mat']) % is there a .mat file for the patient - load([savePath, idvalues(j,:), '/Rest/data.mat']); - oneMin = data(1:60000, :); - dataDown = downsample(oneMin, 2); - montage = readtable([savePath, idvalues(j,:), '/Rest/montage', idvalues(j,:), '.xlsx']); - - % Identify rows where the first column has NaN in the last two rows - rowsToRemove = isnan(montage{end-1:end, 1}); - - if any(rowsToRemove) - % Remove the identified rows - montage(end-1:end, :) = []; - disp('Rows removed.'); - else - disp('No rows to remove.'); - end - - - for c = 1:size(data, 2) - [Perm(c,:), Pnorm(c,:), cPE(c,:)] = PermEn(oneMin(:,c), 'm', 3, 'tau', 1); - - % Mobj = MSobject("SampEn"); - % [MSx, Ci(:,c)] = MSEn(dataDown(:,c), Mobj, 'Scales', 20, 'Methodx', 'coarse', 'RadNew', 0, 'Plotx', false); - - end - - permTable = array2table(Perm, 'VariableNames', {'perm1', 'perm2', 'perm3'}); - pnormTable = array2table(Pnorm, 'VariableNames', {'pnorm1', 'pnorm2', 'pnorm3'}); - cPETable = array2table(cPE, 'VariableNames', {'cPE1', 'cPE2'}); - - subjectTable = horzcat(montage, permTable, pnormTable, cPETable); - - % Create a new column with subject ID repeated for every row - subjectIDColumn = repmat(idvalues(j,:), size(subjectTable, 1),1); - - % Add the new column to the existing table - subjectTable = [table(subjectIDColumn, 'VariableNames', {'Subject'}), subjectTable]; - - - subjectSavePath = [entropyPath idvalues(j,:) '_permEntropy.csv']; - writetable(subjectTable, subjectSavePath) - - end - - end - - clear Perm - clear permTable - clear pnormTable - clear cPETable - clear Pnorm - clear cPE -end \ No newline at end of file