Functional connectivity

Mean correlation maps

library(abind)

# List to store all 2D matrices
matrices <- list()

# Loop through all .tsv files in the folder
files <- list.files(path = "conmats_filtered_aromanogsr/", pattern = "*.tsv",full.names = TRUE)
for (file in files) {
  mat <- read.table(file, sep = "\t", header = FALSE)
  matrices[[file]] <- mat
}

# Combine all matrices into a single 3D matrix
result_matrix <- abind(matrices, along = 3)
dim(result_matrix)
## [1] 410 410  54
results_brain_labs <- brainregions$X2

# List to store all 2D matrices
interferon_matrices <- list()
# Loop through all .tsv files in the folder
files <- list.files(path = "conmats_filtered_aromanogsr/", pattern = "ses-I",full.names = TRUE)
for (file in files) {
  mat <- read.table(file, sep = "\t", header = FALSE)
  interferon_matrices[[file]] <- mat
}
# Combine all matrices into a single 3D matrix
result_interferon_matrix <- abind(interferon_matrices, along = 3)

# List to store all 2D matrices
placebo_matrices <- list()
# Loop through all .tsv files in the folder
files <- list.files(path = "conmats_filtered_aromanogsr/", pattern = "ses-P",full.names = TRUE)
for (file in files) {
  mat <- read.table(file, sep = "\t", header = FALSE)
  placebo_matrices[[file]] <- mat
}
# Combine all matrices into a single 3D matrix
result_placebo_matrix <- abind(placebo_matrices, along = 3)

# nnodes <- length(brainregions$X2)
# tri_pos <- which(upper.tri(matrix(nrow = nnodes, ncol = nnodes)), arr.ind = T)
# Load the R.matlab package
library(R.matlab)

# Specify the file path and name
file <- "C:\\Users\\saptaf1\\Downloads\\archives\\NBS_benchmarking-master\\interferon_files\\aromanogsr_funccon_54.mat"

# Write the 3D matrix to the .mat file
writeMat(funccon=atanh(result_matrix), file)

file <- "C:\\Users\\saptaf1\\Downloads\\archives\\NBS_benchmarking-master\\interferon_files\\yeo_410.mat"

writeMat(yeo410=adjacency_matrix, file)
n_missing <- sum(is.na(result_placebo_matrix))

mean_placebo_funcconn <- apply(result_placebo_matrix, c(1, 2), function(x) mean(x, na.rm = TRUE))
diag(mean_placebo_funcconn) <- 0
z_mean_placebo_funcconn <- atanh(mean_placebo_funcconn)
z_mean_placebo_funcconn[z_mean_placebo_funcconn == 0] <- .Machine$double.xmin
  

# problems with 1121023_ses-I,0821008_ses-I,1121022_ses-I
n_missing <- sum(is.na(result_interferon_matrix))
n_missing <- apply(result_interferon_matrix, c(3), function(x) sum(is.na(x)))
print(n_missing)
## conmats_filtered_aromanogsr/sub-0122025_ses-I_task-rest_space-MNI152NLin2009cAsym_atlas-Combined_measure-pearsoncorrelation_conmat.tsv 
##                                                                                                                                      0 
## conmats_filtered_aromanogsr/sub-0122026_ses-I_task-rest_space-MNI152NLin2009cAsym_atlas-Combined_measure-pearsoncorrelation_conmat.tsv 
##                                                                                                                                      0 
## conmats_filtered_aromanogsr/sub-0122027_ses-I_task-rest_space-MNI152NLin2009cAsym_atlas-Combined_measure-pearsoncorrelation_conmat.tsv 
##                                                                                                                                      0 
## conmats_filtered_aromanogsr/sub-0222028_ses-I_task-rest_space-MNI152NLin2009cAsym_atlas-Combined_measure-pearsoncorrelation_conmat.tsv 
##                                                                                                                                      0 
## conmats_filtered_aromanogsr/sub-0222029_ses-I_task-rest_space-MNI152NLin2009cAsym_atlas-Combined_measure-pearsoncorrelation_conmat.tsv 
##                                                                                                                                      0 
## conmats_filtered_aromanogsr/sub-0322030_ses-I_task-rest_space-MNI152NLin2009cAsym_atlas-Combined_measure-pearsoncorrelation_conmat.tsv 
##                                                                                                                                      0 
## conmats_filtered_aromanogsr/sub-0322031_ses-I_task-rest_space-MNI152NLin2009cAsym_atlas-Combined_measure-pearsoncorrelation_conmat.tsv 
##                                                                                                                                      0 
## conmats_filtered_aromanogsr/sub-0322032_ses-I_task-rest_space-MNI152NLin2009cAsym_atlas-Combined_measure-pearsoncorrelation_conmat.tsv 
##                                                                                                                                      0 
## conmats_filtered_aromanogsr/sub-0421002_ses-I_task-rest_space-MNI152NLin2009cAsym_atlas-Combined_measure-pearsoncorrelation_conmat.tsv 
##                                                                                                                                      0 
## conmats_filtered_aromanogsr/sub-0422033_ses-I_task-rest_space-MNI152NLin2009cAsym_atlas-Combined_measure-pearsoncorrelation_conmat.tsv 
##                                                                                                                                      0 
## conmats_filtered_aromanogsr/sub-0422034_ses-I_task-rest_space-MNI152NLin2009cAsym_atlas-Combined_measure-pearsoncorrelation_conmat.tsv 
##                                                                                                                                      0 
## conmats_filtered_aromanogsr/sub-0422035_ses-I_task-rest_space-MNI152NLin2009cAsym_atlas-Combined_measure-pearsoncorrelation_conmat.tsv 
##                                                                                                                                      0 
## conmats_filtered_aromanogsr/sub-0521003_ses-I_task-rest_space-MNI152NLin2009cAsym_atlas-Combined_measure-pearsoncorrelation_conmat.tsv 
##                                                                                                                                      0 
## conmats_filtered_aromanogsr/sub-0522036_ses-I_task-rest_space-MNI152NLin2009cAsym_atlas-Combined_measure-pearsoncorrelation_conmat.tsv 
##                                                                                                                                      0 
## conmats_filtered_aromanogsr/sub-0721005_ses-I_task-rest_space-MNI152NLin2009cAsym_atlas-Combined_measure-pearsoncorrelation_conmat.tsv 
##                                                                                                                                      0 
## conmats_filtered_aromanogsr/sub-0821006_ses-I_task-rest_space-MNI152NLin2009cAsym_atlas-Combined_measure-pearsoncorrelation_conmat.tsv 
##                                                                                                                                      0 
## conmats_filtered_aromanogsr/sub-0821007_ses-I_task-rest_space-MNI152NLin2009cAsym_atlas-Combined_measure-pearsoncorrelation_conmat.tsv 
##                                                                                                                                      0 
## conmats_filtered_aromanogsr/sub-0921009_ses-I_task-rest_space-MNI152NLin2009cAsym_atlas-Combined_measure-pearsoncorrelation_conmat.tsv 
##                                                                                                                                      0 
## conmats_filtered_aromanogsr/sub-0921010_ses-I_task-rest_space-MNI152NLin2009cAsym_atlas-Combined_measure-pearsoncorrelation_conmat.tsv 
##                                                                                                                                      0 
## conmats_filtered_aromanogsr/sub-0921012_ses-I_task-rest_space-MNI152NLin2009cAsym_atlas-Combined_measure-pearsoncorrelation_conmat.tsv 
##                                                                                                                                      0 
## conmats_filtered_aromanogsr/sub-0921014_ses-I_task-rest_space-MNI152NLin2009cAsym_atlas-Combined_measure-pearsoncorrelation_conmat.tsv 
##                                                                                                                                      0 
## conmats_filtered_aromanogsr/sub-0921015_ses-I_task-rest_space-MNI152NLin2009cAsym_atlas-Combined_measure-pearsoncorrelation_conmat.tsv 
##                                                                                                                                      0 
## conmats_filtered_aromanogsr/sub-0921016_ses-I_task-rest_space-MNI152NLin2009cAsym_atlas-Combined_measure-pearsoncorrelation_conmat.tsv 
##                                                                                                                                      0 
## conmats_filtered_aromanogsr/sub-0921017_ses-I_task-rest_space-MNI152NLin2009cAsym_atlas-Combined_measure-pearsoncorrelation_conmat.tsv 
##                                                                                                                                      0 
## conmats_filtered_aromanogsr/sub-1021019_ses-I_task-rest_space-MNI152NLin2009cAsym_atlas-Combined_measure-pearsoncorrelation_conmat.tsv 
##                                                                                                                                      0 
## conmats_filtered_aromanogsr/sub-1121020_ses-I_task-rest_space-MNI152NLin2009cAsym_atlas-Combined_measure-pearsoncorrelation_conmat.tsv 
##                                                                                                                                      0 
## conmats_filtered_aromanogsr/sub-1121021_ses-I_task-rest_space-MNI152NLin2009cAsym_atlas-Combined_measure-pearsoncorrelation_conmat.tsv 
##                                                                                                                                      0
mean_interferon_funcconn <- apply(result_interferon_matrix, c(1, 2), function(x) mean(x, na.rm = TRUE))
diag(mean_interferon_funcconn) <- 0
z_mean_interferon_funcconn <- atanh(mean_interferon_funcconn)
z_mean_interferon_funcconn[z_mean_interferon_funcconn == 0] <- .Machine$double.xmin


mean_diff_funcconn <- apply(result_placebo_matrix-result_interferon_matrix, c(1, 2), mean)
diag(mean_diff_funcconn) <- 0
#z_mean_diff_funcconn <- atanh(mean_diff_funcconn)
z_mean_diff_funcconn <- z_mean_placebo_funcconn-z_mean_interferon_funcconn

h1 <- Heatmap(z_mean_placebo_funcconn, name = "Fisher Z", row_split = network.combined$name, column_split = network.combined$name, row_title_rot = 0, show_column_dend = FALSE, show_row_dend = FALSE, show_column_names = FALSE, column_title = "Placebo", column_title_gp = gpar(fontsize = 20, fontface = "bold"), clustering_distance_rows="pearson", clustering_distance_columns ="pearson")

h2 <- Heatmap(z_mean_interferon_funcconn, name = "Fisher Z", row_split= factor(network.combined$name, levels = (names(row_order(h1)))), column_split= factor(network.combined$name, levels = (names(row_order(h1)))), row_title_rot = 0, show_column_dend = FALSE, show_row_dend = FALSE, show_column_names = FALSE, column_title = "Interferon", column_title_gp = gpar(fontsize = 20, fontface = "bold"), cluster_rows = FALSE, cluster_columns = FALSE, cluster_row_slices = FALSE, cluster_column_slices = FALSE, row_order = unlist(row_order(h1)), column_order = unlist(row_order(h1)) )

h3_v2 <- Heatmap(z_mean_diff_funcconn, name = "Fisher Z", row_split= factor(network.combined$name, levels = (names(row_order(h1)))), column_split= factor(network.combined$name, levels = (names(row_order(h1)))),  row_title_rot = 0, show_column_dend = FALSE, show_row_dend = FALSE, show_column_names = FALSE, column_title = "Placebo-Interferon", column_title_gp = gpar(fontsize = 20, fontface = "bold"), cluster_rows = FALSE, cluster_columns = FALSE, cluster_row_slices = FALSE, cluster_column_slices = FALSE, row_order = unlist(row_order(h1)), column_order = unlist(row_order(h1)))

h3_v2_adjusted <- Heatmap(z_mean_diff_funcconn, name = "Fisher Z", row_split= factor(network.combined$name, levels = (names(row_order(h1)))), column_split= factor(network.combined$name, levels = (names(row_order(h1)))),  row_title_rot = 0, show_column_dend = FALSE, show_row_dend = FALSE, show_column_names = FALSE, column_title = "Placebo-Interferon", column_title_gp = gpar(fontsize = 20, fontface = "bold"), cluster_rows = FALSE, cluster_columns = FALSE, cluster_row_slices = FALSE, cluster_column_slices = FALSE, row_order = unlist(row_order(h1)), column_order = unlist(row_order(h1)), col=colorRamp2(c(-1, 0, 1), c("blue", "white", "red")))

h1+h2+h3_v2_adjusted

h1+h2

h3_v2

hist(z_mean_diff_funcconn[upper.tri(z_mean_diff_funcconn, diag = FALSE)], main = "Histogram of functional connectivity difference values")

g1  <- graph.adjacency(z_mean_placebo_funcconn,weighted=TRUE,mode = "upper", diag = FALSE)
g2  <- graph.adjacency(z_mean_interferon_funcconn,weighted=TRUE,mode = "upper", diag = FALSE)

df <- bind_rows(get.data.frame(g1) %>% mutate(condition="Placebo"), get.data.frame(g2) %>% mutate(condition="Interferon"))

ggplot(df, aes(x=weight, fill=condition)) +
  geom_density(alpha=.25)

df %>% mutate(from = gsub("V", "ROI_", from), to = gsub("V", "ROI_", to)) %>% rowwise() %>% mutate(from_network = ifelse(is.na(match(from, network.combined$ROI)), from, network.combined$name[match(from, network.combined$ROI)]), to_network = ifelse(is.na(match(to, network.combined$ROI)), to, network.combined$name[match(to, network.combined$ROI)]), from2to = paste0(sort(c(from_network,to_network))[1],"2",sort(c(from_network,to_network))[2])) -> df_ext

ggplot(df_ext, aes(x=weight, fill=condition)) +
  geom_density(alpha=.25) + facet_wrap(~from2to)

df_ext %>% mutate(fromNum=parse_number(from), toNum = parse_number(to)) -> df_ext

df_ext$from2toIndex <- as.integer(factor(df_ext$from2to))

nodes <- unique(c(df_ext$fromNum, df_ext$toNum))
adjacency_matrix <- matrix(0, nrow = length(nodes), ncol = length(nodes))
colnames(adjacency_matrix) <- rownames(adjacency_matrix) <- nodes
adjacency_matrix[cbind(match(df_ext$fromNum, nodes), match(df_ext$toNum, nodes))] <- df_ext$from2toIndex

write.csv(adjacency_matrix, file = "yeo_edge_group.csv", row.names = FALSE)

Wilcoxon

tmp = data.frame()
# run np test within-subject differences in edges
for (i in seq(1,dim(result_placebo_matrix)[3])) {
  # print(i)
  A <- atanh(result_placebo_matrix[,,i])
  A[A == 0] <- .Machine$double.xmin
  
  B <- atanh(result_interferon_matrix[,,i])
  B[B == 0] <- .Machine$double.xmin
  
  g1  <- graph.adjacency(A, weighted=TRUE, mode = "upper", diag = FALSE)
  g2  <- graph.adjacency(B, weighted=TRUE, mode = "upper", diag = FALSE)
  
  if (nrow(get.data.frame(g1)) != nrow(get.data.frame(g2))) {
    break
  }
  
  df <- bind_rows(get.data.frame(g1) %>% mutate(condition="Placebo"), get.data.frame(g2) %>% mutate(condition="Interferon")) %>% mutate(sub = i)
  
  tmp = bind_rows(tmp,df)
}
# Perform Wilcoxon signed-rank test
knitr::kable(tmp %>% wilcox_test(weight ~ condition, paired = TRUE, detailed = TRUE, exact=TRUE))
estimate .y. group1 group2 n1 n2 statistic p conf.low conf.high method alternative
-0.0620572 weight Interferon Placebo 2263815 2263815 1.028623e+12 0 -0.0624888 -0.0615767 Wilcoxon two.sided
# Perform Wilcoxon signed-rank test
print(wilcox.test(tmp %>% filter(condition=="Placebo")%>%.$weight, tmp %>% filter(condition=="Interferon")%>%.$weight, paired = TRUE))
## 
##  Wilcoxon signed rank test with continuity correction
## 
## data:  tmp %>% filter(condition == "Placebo") %>% .$weight and tmp %>% filter(condition == "Interferon") %>% .$weight
## V = 1.5338e+12, p-value < 2.2e-16
## alternative hypothesis: true location shift is not equal to 0
rm(tmp)

List of most Interferon affected connections in graph

Where is the most condition related change happening?

library(igraph)

g_diff_pMinusI  <- graph.adjacency(z_mean_diff_funcconn,weighted=TRUE,mode = "upper", diag = FALSE)

df_diff_pMinusI <- get.data.frame(g_diff_pMinusI) %>% mutate(from = gsub("V", "ROI_", from), to = gsub("V", "ROI_", to)) %>% rowwise() %>% mutate(from_network = ifelse(is.na(match(from, network.combined$ROI)), from, network.combined$name[match(from, network.combined$ROI)]), to_network = ifelse(is.na(match(to, network.combined$ROI)), to, network.combined$name[match(to, network.combined$ROI)]), from2to = paste0(sort(c(from_network,to_network))[1],"2",sort(c(from_network,to_network))[2])) %>% mutate(from_roi = ifelse(is.na(match(from, brainregions$ROI_X)), from, brainregions$X2[match(from, brainregions$ROI_X)]), to_roi = ifelse(is.na(match(to, brainregions$ROI_X)), from, brainregions$X2[match(to, brainregions$ROI_X)]), abs_weight = abs(weight))

df_diff_pMinusI$abs_weight_decile <- ntile(df_diff_pMinusI$abs_weight, 10)  

DT::datatable(df_diff_pMinusI  %>% filter(abs_weight_decile >= 10))
# top 20% of affected edges
df_diff_pMinusI %>% filter(abs_weight_decile >= 9) %>% ggplot(., aes(abs_weight, fill = from2to)) +
  geom_histogram(binwidth = 0.07)

df_diff_pMinusI %>% filter(abs_weight_decile >= 9)  %>% ggplot(., aes(from2to)) +
  geom_bar() + coord_flip()

# top 10% of affected edges
df_diff_pMinusI %>% filter(abs_weight_decile >= 10) %>% ggplot(., aes(abs_weight, fill = from2to)) +
  geom_histogram(binwidth = 0.1)

df_diff_pMinusI %>% filter(abs_weight_decile >= 10)  %>% ggplot(., aes(from2to)) +
  geom_bar() + coord_flip()

library(igraph)

df_list <- list()

for (i in 1:dim(result_matrix)[3]) {
  #print(i)
  g  <- graph.adjacency(result_matrix[,,i],weighted=TRUE,mode = "upper", diag = FALSE)
  df_list[[i]] <- get.data.frame(g) %>% mutate(fileIndex=i)
}

data.funccon <- bind_rows(df_list) %>% left_join(.,variables_ext)

Grouped by scale, condition, subject

Unthresholded

data.funccon %>% mutate(weight = atanh(weight)) %>% group_by(Subj_ID,condition) %>% summarise(meanFuncCon = mean(weight)) -> data.aggr


anova_summary <- nice(aov_ez("Subj_ID", "meanFuncCon", data.aggr, within ="condition"))

knitr::kable(anova_summary)
Effect df MSE F ges p.value
condition 1, 26 0.01 5.36 * .030 .029
ggwithinstats(
    data             = data.aggr,
  x                = condition,
  y                = meanFuncCon,
  type             = "np",
  bf.message = FALSE)

Only positive weight > 0

data.funccon %>% mutate(weight = atanh(weight)) %>% group_by(Subj_ID,condition) %>% filter(weight>0) %>% summarise(meanFuncCon = mean(weight))-> data.aggr


anova_summary <- nice(aov_ez("Subj_ID", "meanFuncCon", data.aggr, within ="condition"))

knitr::kable(anova_summary)
Effect df MSE F ges p.value
condition 1, 26 0.01 9.78 ** .044 .004
ggwithinstats(
    data             = data.aggr,
  x                = condition,
  y                = meanFuncCon,
  type             = "np",
  bf.message = FALSE)

Proportional thresholding based on on weight (not abs_weight)

# total edges per session: EDGES
plots_list <- list()
x <- c(0.1,0.15,0.2,0.25,0.3,0.35)
for (i in 1:6) {
  
  p <- ggwithinstats(
      data             = data.funccon %>% mutate(weight = atanh(weight), abs_weight = abs(weight)) %>% group_by(Subj_ID,condition) %>% top_n(round(EDGES*x[i]),weight) %>% dplyr::summarise(meanFuncCon = mean(weight)),
    x                = condition,
    y                = meanFuncCon,
    type             = "np",
    title =x[i] ,
    bf.message = FALSE)
  
    plots_list[[i]] <- p

}

# arrange the plots in a grid using grid.arrange from gridExtra
gridExtra::grid.arrange(grobs = plots_list)

Grouped by scale, condition, subject and age_cat

data.funccon %>% mutate(weight = atanh(weight)) %>% group_by(Subj_ID,condition,Age_cat) %>% summarise(meanFuncCon = mean(weight)) -> data.aggr

anova_summary <- nice(aov_ez("Subj_ID", "meanFuncCon", data.aggr, within ="condition", between="Age_cat"))

knitr::kable(anova_summary)
Effect df MSE F ges p.value
Age_cat 1, 25 0.06 1.41 .046 .247
condition 1, 25 0.01 5.74 * .033 .024
Age_cat:condition 1, 25 0.01 1.75 .010 .198

Grouped by scale, condition, subject and network:yeo

  • Unthresholded
data.funccon %>% mutate(weight = atanh(weight), from = gsub("V", "ROI_", from), to = gsub("V", "ROI_", to)) %>% rowwise() %>% mutate(from_network = ifelse(is.na(match(from, network.combined$ROI)), from, network.combined$name[match(from, network.combined$ROI)]), to_network = ifelse(is.na(match(to, network.combined$ROI)), to, network.combined$name[match(to, network.combined$ROI)]), from2to = paste0(sort(c(from_network,to_network))[1],"2",sort(c(from_network,to_network))[2])) -> data.funccon.ext


data.funccon.ext %>% mutate(abs_weight = abs(weight)) %>% group_by(Subj_ID,condition,from2to)  -> tmp

# Calculate the number of occurrences of each network within each condition
df_counts <- table(tmp$from2to, tmp$condition)
knitr::kable(df_counts)
Interferon Placebo
Default2Default 89667 89667
Default2Dorsal Attention 97416 97416
Default2Frontoparietal 99630 99630
Default2Limbic 64206 64206
Default2Somatomotor 117342 117342
Default2Subcortical 109962 110700
Default2Ventral Attention 106272 106272
Default2Visual 130626 130626
Dorsal Attention2Dorsal Attention 25542 25542
Dorsal Attention2Frontoparietal 53460 53460
Dorsal Attention2Limbic 34452 34452
Dorsal Attention2Somatomotor 62964 62964
Dorsal Attention2Subcortical 59004 59400
Dorsal Attention2Ventral Attention 57024 57024
Dorsal Attention2Visual 70092 70092
Frontoparietal2Frontoparietal 26730 26730
Frontoparietal2Limbic 35235 35235
Frontoparietal2Somatomotor 64395 64395
Frontoparietal2Subcortical 60345 60750
Frontoparietal2Ventral Attention 58320 58320
Frontoparietal2Visual 71685 71685
Limbic2Limbic 10962 10962
Limbic2Somatomotor 41499 41499
Limbic2Subcortical 38889 39150
Limbic2Ventral Attention 37584 37584
Limbic2Visual 46197 46197
Somatomotor2Somatomotor 37206 37206
Somatomotor2Subcortical 71073 71550
Somatomotor2Ventral Attention 68688 68688
Somatomotor2Visual 84429 84429
Subcortical2Subcortical 32670 33075
Subcortical2Ventral Attention 64368 64800
Subcortical2Visual 79119 79650
Ventral Attention2Ventral Attention 30456 30456
Ventral Attention2Visual 76464 76464
Visual2Visual 46197 46197
df_counts <- table(tmp$from2to)

# Calculate the number of occurrences of each network within each condition
df_counts <- table(tmp$from2to)
# Convert the counts to a data frame and add column names
df_counts <- as.data.frame(df_counts)
names(df_counts) <- c("network", "count")

# Create pie chart faceted by condition
ggplot(df_counts, aes(x = "", y = count, fill = network)) +
  geom_bar(stat = "identity", width = 1) +
  coord_polar("y", start = 0) +
  theme_void()

data.funccon.ext %>% group_by(Subj_ID,condition,from2to) %>% summarise(meanFuncConn = mean(weight)) -> data.funccon.aggr

# grouped_ggwithinstats(
#     data             = data.aggr,
#   x                = condition,
#   y                = meanFuncConn,
#   grouping.var = from2to,
#   type             = "p",
#   bf.message = FALSE)

# main anova
main_anova_summary <- aov_ez("Subj_ID", "meanFuncConn", data.funccon.aggr,within =c("condition","from2to"))
knitr::kable(nice(main_anova_summary))
Effect df MSE F ges p.value
condition 1, 26 0.35 5.71 * .026 .024
from2to 35, 910 0.01 81.13 *** .298 <.001
condition:from2to 35, 910 0.01 3.04 *** .008 <.001
emmip(main_anova_summary, from2to ~ condition)

EMM <- emmeans(main_anova_summary, ~  condition | from2to)   # where treat has 2 levels
EMM.unadj <- summary(pairs(EMM), by = NULL, adjust = "none") %>% as.data.frame() %>% dplyr::rename(.,p.value.unadj = p.value)
EMM.fdr <- summary(pairs(EMM), by = NULL, adjust = "fdr") %>% as.data.frame() %>% dplyr::rename(.,p.value.fdr = p.value)

# corrected and uncorrected
knitr::kable(left_join(EMM.unadj,EMM.fdr))
contrast from2to estimate SE df t.ratio p.value.unadj p.value.fdr
Interferon - Placebo Default2Default -0.0815160 0.0304683 26 -2.6754390 0.0127396 0.0585078
Interferon - Placebo Default2Dorsal.Attention -0.0666573 0.0382110 26 -1.7444525 0.0928927 0.1520062
Interferon - Placebo Default2Frontoparietal -0.0506589 0.0285547 26 -1.7740987 0.0877599 0.1504456
Interferon - Placebo Default2Limbic -0.0540492 0.0210194 26 -2.5713963 0.0161985 0.0585078
Interferon - Placebo Default2Somatomotor -0.0747728 0.0381428 26 -1.9603374 0.0607544 0.1286563
Interferon - Placebo Default2Subcortical -0.0228950 0.0209601 26 -1.0923129 0.2847141 0.3416569
Interferon - Placebo Default2Ventral.Attention -0.0842890 0.0366841 26 -2.2976969 0.0298771 0.0768269
Interferon - Placebo Default2Visual -0.0553459 0.0347972 26 -1.5905295 0.1238027 0.1782759
Interferon - Placebo Dorsal.Attention2Dorsal.Attention -0.0947438 0.0408481 26 -2.3194173 0.0284922 0.0768269
Interferon - Placebo Dorsal.Attention2Frontoparietal -0.0667919 0.0349703 26 -1.9099605 0.0672287 0.1344575
Interferon - Placebo Dorsal.Attention2Limbic -0.0613602 0.0273448 26 -2.2439410 0.0335718 0.0805723
Interferon - Placebo Dorsal.Attention2Somatomotor -0.1194299 0.0419414 26 -2.8475392 0.0084935 0.0585078
Interferon - Placebo Dorsal.Attention2Subcortical -0.0424110 0.0301826 26 -1.4051451 0.1718145 0.2304044
Interferon - Placebo Dorsal.Attention2Ventral.Attention -0.1016978 0.0355715 26 -2.8589660 0.0082651 0.0585078
Interferon - Placebo Dorsal.Attention2Visual -0.0687718 0.0382276 26 -1.7990106 0.0836360 0.1504456
Interferon - Placebo Frontoparietal2Frontoparietal -0.0340307 0.0293319 26 -1.1601926 0.2565134 0.3184304
Interferon - Placebo Frontoparietal2Limbic -0.0327962 0.0233959 26 -1.4017917 0.1728033 0.2304044
Interferon - Placebo Frontoparietal2Somatomotor -0.0634794 0.0354959 26 -1.7883579 0.0853787 0.1504456
Interferon - Placebo Frontoparietal2Subcortical -0.0177767 0.0235041 26 -0.7563213 0.4562553 0.4977331
Interferon - Placebo Frontoparietal2Ventral.Attention -0.0833424 0.0346003 26 -2.4087182 0.0233922 0.0701765
Interferon - Placebo Frontoparietal2Visual -0.0545244 0.0338889 26 -1.6089172 0.1197111 0.1782759
Interferon - Placebo Limbic2Limbic -0.0507585 0.0246055 26 -2.0628958 0.0492427 0.1107962
Interferon - Placebo Limbic2Somatomotor -0.0869891 0.0303276 26 -2.8683153 0.0080826 0.0585078
Interferon - Placebo Limbic2Subcortical -0.0108675 0.0195128 26 -0.5569417 0.5823303 0.5989683
Interferon - Placebo Limbic2Ventral.Attention -0.0408933 0.0249445 26 -1.6393700 0.1131819 0.1771542
Interferon - Placebo Limbic2Visual -0.0701318 0.0271168 26 -2.5862831 0.0156550 0.0585078
Interferon - Placebo Somatomotor2Somatomotor -0.1653478 0.0555821 26 -2.9748401 0.0062556 0.0585078
Interferon - Placebo Somatomotor2Subcortical -0.0197026 0.0341184 26 -0.5774766 0.5685851 0.5989683
Interferon - Placebo Somatomotor2Ventral.Attention -0.1142752 0.0444659 26 -2.5699529 0.0162522 0.0585078
Interferon - Placebo Somatomotor2Visual -0.1003946 0.0379336 26 -2.6465854 0.0136224 0.0585078
Interferon - Placebo Subcortical2Subcortical 0.0052534 0.0301009 26 0.1745270 0.8628033 0.8628033
Interferon - Placebo Subcortical2Ventral.Attention -0.0322010 0.0321531 26 -1.0014873 0.3258214 0.3665491
Interferon - Placebo Subcortical2Visual -0.0281104 0.0273554 26 -1.0275998 0.3136038 0.3641851
Interferon - Placebo Ventral.Attention2Ventral.Attention -0.1232454 0.0359941 26 -3.4240502 0.0020561 0.0585078
Interferon - Placebo Ventral.Attention2Visual -0.0808948 0.0329065 26 -2.4583232 0.0209353 0.0685156
Interferon - Placebo Visual2Visual -0.0557824 0.0421366 26 -1.3238459 0.1970764 0.2533840
# aov_test <- aov(meanFuncConn ~ condition*from2to + Error(Subj_ID/(condition*from2to)), data=data.funccon.aggr)
# report(aov_test)

# uncorrected p values
# data.funccon.aggr %>% ungroup() %>%
#   nest_by(from2to) %>%
#   mutate(Model = list(nice(aov_ez("Subj_ID", "meanFuncConn", data, within ="condition")))) %>% select(-data) %>% unnest(Model) -> anova_summary
# 
# knitr::kable(anova_summary)
  • 10% threshold
data.funccon.ext %>% mutate(abs_weight = abs(weight)) %>% group_by(Subj_ID,condition) %>% top_n(round(EDGES*0.1),weight) %>% group_by(Subj_ID,condition, from2to) %>% dplyr::summarise(meanFuncConn = mean(weight)) -> data.funccon.aggr

data.funccon.ext %>% mutate(abs_weight = abs(weight)) %>% group_by(Subj_ID,condition,from2to) %>% top_n(round(EDGES*0.1),weight) -> tmp

# Calculate the number of occurrences of each network within each condition
df_counts <- table(tmp$from2to, tmp$condition)
knitr::kable(df_counts)
Interferon Placebo
Default2Default 89667 89667
Default2Dorsal Attention 97416 97416
Default2Frontoparietal 99630 99630
Default2Limbic 64206 64206
Default2Somatomotor 117342 117342
Default2Subcortical 109962 110700
Default2Ventral Attention 106272 106272
Default2Visual 130626 130626
Dorsal Attention2Dorsal Attention 25542 25542
Dorsal Attention2Frontoparietal 53460 53460
Dorsal Attention2Limbic 34452 34452
Dorsal Attention2Somatomotor 62964 62964
Dorsal Attention2Subcortical 59004 59400
Dorsal Attention2Ventral Attention 57024 57024
Dorsal Attention2Visual 70092 70092
Frontoparietal2Frontoparietal 26730 26730
Frontoparietal2Limbic 35235 35235
Frontoparietal2Somatomotor 64395 64395
Frontoparietal2Subcortical 60345 60750
Frontoparietal2Ventral Attention 58320 58320
Frontoparietal2Visual 71685 71685
Limbic2Limbic 10962 10962
Limbic2Somatomotor 41499 41499
Limbic2Subcortical 38889 39150
Limbic2Ventral Attention 37584 37584
Limbic2Visual 46197 46197
Somatomotor2Somatomotor 37206 37206
Somatomotor2Subcortical 71073 71550
Somatomotor2Ventral Attention 68688 68688
Somatomotor2Visual 84429 84429
Subcortical2Subcortical 32670 33075
Subcortical2Ventral Attention 64368 64800
Subcortical2Visual 79119 79650
Ventral Attention2Ventral Attention 30456 30456
Ventral Attention2Visual 76464 76464
Visual2Visual 46197 46197
df_counts <- table(tmp$from2to)

# Calculate the number of occurrences of each network within each condition
df_counts <- table(tmp$from2to)
# Convert the counts to a data frame and add column names
df_counts <- as.data.frame(df_counts)
names(df_counts) <- c("network", "count")

# Create pie chart faceted by condition
ggplot(df_counts, aes(x = "", y = count, fill = network)) +
  geom_bar(stat = "identity", width = 1) +
  coord_polar("y", start = 0) +
  theme_void()

# main anova
main_anova_summary <- aov_ez("Subj_ID", "meanFuncConn", data.funccon.aggr,within =c("condition","from2to"))
knitr::kable(nice(main_anova_summary))
Effect df MSE F ges p.value
condition 1, 25 0.47 9.18 ** .050 .006
from2to 35, 875 0.00 86.31 *** .059 <.001
condition:from2to 35, 875 0.00 1.47 * <.001 .040
emmip(main_anova_summary, from2to ~ condition)

EMM <- emmeans(main_anova_summary, ~  condition | from2to)   # where treat has 2 levels
EMM.unadj <- summary(pairs(EMM), by = NULL, adjust = "none") %>% as.data.frame() %>% dplyr::rename(.,p.value.unadj = p.value)
EMM.fdr <- summary(pairs(EMM), by = NULL, adjust = "fdr") %>% as.data.frame() %>% dplyr::rename(.,p.value.fdr = p.value)

# corrected and uncorrected
knitr::kable(left_join(EMM.unadj,EMM.fdr))
contrast from2to estimate SE df t.ratio p.value.unadj p.value.fdr
Interferon - Placebo Default2Default -0.0875643 0.0296459 25 -2.953670 0.0067472 0.0116009
Interferon - Placebo Default2Dorsal.Attention -0.0948400 0.0312645 25 -3.033474 0.0055708 0.0116009
Interferon - Placebo Default2Frontoparietal -0.0968643 0.0303833 25 -3.188072 0.0038261 0.0116009
Interferon - Placebo Default2Limbic -0.0900159 0.0327840 25 -2.745730 0.0110233 0.0137268
Interferon - Placebo Default2Somatomotor -0.1018386 0.0319537 25 -3.187067 0.0038356 0.0116009
Interferon - Placebo Default2Subcortical -0.0895642 0.0326953 25 -2.739365 0.0111878 0.0137268
Interferon - Placebo Default2Ventral.Attention -0.0977249 0.0309474 25 -3.157775 0.0041203 0.0116009
Interferon - Placebo Default2Visual -0.0876043 0.0329325 25 -2.660115 0.0134403 0.0146622
Interferon - Placebo Dorsal.Attention2Dorsal.Attention -0.0976720 0.0322773 25 -3.026026 0.0056717 0.0116009
Interferon - Placebo Dorsal.Attention2Frontoparietal -0.0986600 0.0296995 25 -3.321945 0.0027517 0.0116009
Interferon - Placebo Dorsal.Attention2Limbic -0.1054951 0.0344958 25 -3.058206 0.0052479 0.0116009
Interferon - Placebo Dorsal.Attention2Somatomotor -0.1028056 0.0326582 25 -3.147924 0.0042205 0.0116009
Interferon - Placebo Dorsal.Attention2Subcortical -0.0865863 0.0339517 25 -2.550281 0.0172703 0.0182861
Interferon - Placebo Dorsal.Attention2Ventral.Attention -0.1101701 0.0324692 25 -3.393067 0.0023063 0.0116009
Interferon - Placebo Dorsal.Attention2Visual -0.0930802 0.0314987 25 -2.955052 0.0067250 0.0116009
Interferon - Placebo Frontoparietal2Frontoparietal -0.0916145 0.0292997 25 -3.126806 0.0044433 0.0116009
Interferon - Placebo Frontoparietal2Limbic -0.0994302 0.0339011 25 -2.932947 0.0070895 0.0116009
Interferon - Placebo Frontoparietal2Somatomotor -0.0974657 0.0342780 25 -2.843392 0.0087674 0.0126251
Interferon - Placebo Frontoparietal2Subcortical -0.0872243 0.0317403 25 -2.748065 0.0109634 0.0137268
Interferon - Placebo Frontoparietal2Ventral.Attention -0.0995183 0.0302164 25 -3.293519 0.0029521 0.0116009
Interferon - Placebo Frontoparietal2Visual -0.0919560 0.0319877 25 -2.874732 0.0081414 0.0122121
Interferon - Placebo Limbic2Limbic -0.0672763 0.0354489 25 -1.897841 0.0693226 0.0693226
Interferon - Placebo Limbic2Somatomotor -0.1054085 0.0335075 25 -3.145816 0.0042423 0.0116009
Interferon - Placebo Limbic2Subcortical -0.0927505 0.0330045 25 -2.810239 0.0094791 0.0131249
Interferon - Placebo Limbic2Ventral.Attention -0.1057917 0.0339639 25 -3.114831 0.0045747 0.0116009
Interferon - Placebo Limbic2Visual -0.0995749 0.0320278 25 -3.109016 0.0046398 0.0116009
Interferon - Placebo Somatomotor2Somatomotor -0.1260985 0.0393978 25 -3.200646 0.0037101 0.0116009
Interferon - Placebo Somatomotor2Subcortical -0.0943096 0.0349695 25 -2.696913 0.0123461 0.0138894
Interferon - Placebo Somatomotor2Ventral.Attention -0.1093486 0.0367210 25 -2.977819 0.0063683 0.0116009
Interferon - Placebo Somatomotor2Visual -0.0893942 0.0327473 25 -2.729821 0.0114390 0.0137268
Interferon - Placebo Subcortical2Subcortical -0.0938103 0.0324883 25 -2.887511 0.0078985 0.0122121
Interferon - Placebo Subcortical2Ventral.Attention -0.0806648 0.0335897 25 -2.401476 0.0240877 0.0247759
Interferon - Placebo Subcortical2Visual -0.0910929 0.0337008 25 -2.702991 0.0121737 0.0138894
Interferon - Placebo Ventral.Attention2Ventral.Attention -0.1121883 0.0324130 25 -3.461215 0.0019456 0.0116009
Interferon - Placebo Ventral.Attention2Visual -0.0953847 0.0320365 25 -2.977372 0.0063751 0.0116009
Interferon - Placebo Visual2Visual -0.0889294 0.0302238 25 -2.942359 0.0069320 0.0116009
  • 35% threshold
data.funccon.ext %>% mutate(abs_weight = abs(weight)) %>% group_by(Subj_ID,condition) %>% top_n(round(EDGES*0.35),weight) %>% group_by(Subj_ID,condition, from2to) %>% dplyr::summarise(meanFuncConn = mean(weight)) -> data.funccon.aggr

data.funccon.ext %>% mutate(abs_weight = abs(weight)) %>% group_by(Subj_ID,condition,from2to) %>% top_n(round(EDGES*0.35),weight) -> tmp

# Calculate the number of occurrences of each network within each condition
df_counts <- table(tmp$from2to, tmp$condition)
knitr::kable(df_counts)
Interferon Placebo
Default2Default 89667 89667
Default2Dorsal Attention 97416 97416
Default2Frontoparietal 99630 99630
Default2Limbic 64206 64206
Default2Somatomotor 117342 117342
Default2Subcortical 109962 110700
Default2Ventral Attention 106272 106272
Default2Visual 130626 130626
Dorsal Attention2Dorsal Attention 25542 25542
Dorsal Attention2Frontoparietal 53460 53460
Dorsal Attention2Limbic 34452 34452
Dorsal Attention2Somatomotor 62964 62964
Dorsal Attention2Subcortical 59004 59400
Dorsal Attention2Ventral Attention 57024 57024
Dorsal Attention2Visual 70092 70092
Frontoparietal2Frontoparietal 26730 26730
Frontoparietal2Limbic 35235 35235
Frontoparietal2Somatomotor 64395 64395
Frontoparietal2Subcortical 60345 60750
Frontoparietal2Ventral Attention 58320 58320
Frontoparietal2Visual 71685 71685
Limbic2Limbic 10962 10962
Limbic2Somatomotor 41499 41499
Limbic2Subcortical 38889 39150
Limbic2Ventral Attention 37584 37584
Limbic2Visual 46197 46197
Somatomotor2Somatomotor 37206 37206
Somatomotor2Subcortical 71073 71550
Somatomotor2Ventral Attention 68688 68688
Somatomotor2Visual 84429 84429
Subcortical2Subcortical 32670 33075
Subcortical2Ventral Attention 64368 64800
Subcortical2Visual 79119 79650
Ventral Attention2Ventral Attention 30456 30456
Ventral Attention2Visual 76464 76464
Visual2Visual 46197 46197
df_counts <- table(tmp$from2to)

# Calculate the number of occurrences of each network within each condition
df_counts <- table(tmp$from2to)
# Convert the counts to a data frame and add column names
df_counts <- as.data.frame(df_counts)
names(df_counts) <- c("network", "count")

# Create pie chart faceted by condition
ggplot(df_counts, aes(x = "", y = count, fill = network)) +
  geom_bar(stat = "identity", width = 1) +
  coord_polar("y", start = 0) +
  theme_void()

# main anova
main_anova_summary <- aov_ez("Subj_ID", "meanFuncConn", data.funccon.aggr,within =c("condition","from2to"))
knitr::kable(nice(main_anova_summary))
Effect df MSE F ges p.value
condition 1, 26 0.44 7.95 ** .041 .009
from2to 35, 910 0.00 98.27 *** .101 <.001
condition:from2to 35, 910 0.00 2.59 *** .001 <.001
emmip(main_anova_summary, from2to ~ condition)

EMM <- emmeans(main_anova_summary, ~  condition | from2to)   # where treat has 2 levels
EMM.unadj <- summary(pairs(EMM), by = NULL, adjust = "none") %>% as.data.frame() %>% dplyr::rename(.,p.value.unadj = p.value)
EMM.fdr <- summary(pairs(EMM), by = NULL, adjust = "fdr") %>% as.data.frame() %>% dplyr::rename(.,p.value.fdr = p.value)

# corrected and uncorrected
knitr::kable(left_join(EMM.unadj,EMM.fdr))
contrast from2to estimate SE df t.ratio p.value.unadj p.value.fdr
Interferon - Placebo Default2Default -0.0855354 0.0282202 26 -3.030996 0.0054581 0.0183278
Interferon - Placebo Default2Dorsal.Attention -0.0928198 0.0294128 26 -3.155765 0.0040193 0.0183278
Interferon - Placebo Default2Frontoparietal -0.0803523 0.0287010 26 -2.799638 0.0095172 0.0210624
Interferon - Placebo Default2Limbic -0.0757287 0.0289449 26 -2.616300 0.0146103 0.0236781
Interferon - Placebo Default2Somatomotor -0.0973355 0.0322256 26 -3.020442 0.0056002 0.0183278
Interferon - Placebo Default2Subcortical -0.0753463 0.0291738 26 -2.582669 0.0157854 0.0236781
Interferon - Placebo Default2Ventral.Attention -0.0918269 0.0301183 26 -3.048870 0.0052253 0.0183278
Interferon - Placebo Default2Visual -0.0910042 0.0294374 26 -3.091448 0.0047084 0.0183278
Interferon - Placebo Dorsal.Attention2Dorsal.Attention -0.0975653 0.0314085 26 -3.106329 0.0045395 0.0183278
Interferon - Placebo Dorsal.Attention2Frontoparietal -0.0867298 0.0291966 26 -2.970549 0.0063209 0.0189628
Interferon - Placebo Dorsal.Attention2Limbic -0.0879343 0.0325801 26 -2.699017 0.0120583 0.0225117
Interferon - Placebo Dorsal.Attention2Somatomotor -0.1041896 0.0335683 26 -3.103809 0.0045677 0.0183278
Interferon - Placebo Dorsal.Attention2Subcortical -0.0802048 0.0323001 26 -2.483114 0.0197987 0.0263983
Interferon - Placebo Dorsal.Attention2Ventral.Attention -0.0989879 0.0317584 26 -3.116906 0.0044231 0.0183278
Interferon - Placebo Dorsal.Attention2Visual -0.0823756 0.0309218 26 -2.664000 0.0130830 0.0225117
Interferon - Placebo Frontoparietal2Frontoparietal -0.0750047 0.0258955 26 -2.896431 0.0075565 0.0191252
Interferon - Placebo Frontoparietal2Limbic -0.0759694 0.0321210 26 -2.365100 0.0257684 0.0318013
Interferon - Placebo Frontoparietal2Somatomotor -0.0869983 0.0335606 26 -2.592276 0.0154410 0.0236781
Interferon - Placebo Frontoparietal2Subcortical -0.0666030 0.0304656 26 -2.186169 0.0379992 0.0404439
Interferon - Placebo Frontoparietal2Ventral.Attention -0.0918914 0.0287749 26 -3.193452 0.0036617 0.0183278
Interferon - Placebo Frontoparietal2Visual -0.0801337 0.0320051 26 -2.503781 0.0188950 0.0261624
Interferon - Placebo Limbic2Limbic -0.0449193 0.0318295 26 -1.411246 0.1700270 0.1700270
Interferon - Placebo Limbic2Somatomotor -0.0874129 0.0328324 26 -2.662397 0.0131318 0.0225117
Interferon - Placebo Limbic2Subcortical -0.0715271 0.0323647 26 -2.210034 0.0361101 0.0404439
Interferon - Placebo Limbic2Ventral.Attention -0.0766561 0.0332114 26 -2.308126 0.0292047 0.0339151
Interferon - Placebo Limbic2Visual -0.0843513 0.0314519 26 -2.681912 0.0125491 0.0225117
Interferon - Placebo Somatomotor2Somatomotor -0.1399491 0.0414672 26 -3.374935 0.0023272 0.0183278
Interferon - Placebo Somatomotor2Subcortical -0.0855542 0.0340732 26 -2.510896 0.0185929 0.0261624
Interferon - Placebo Somatomotor2Ventral.Attention -0.1041441 0.0374484 26 -2.781001 0.0099461 0.0210624
Interferon - Placebo Somatomotor2Visual -0.0917021 0.0319048 26 -2.874245 0.0079688 0.0191252
Interferon - Placebo Subcortical2Subcortical -0.0669558 0.0306612 26 -2.183732 0.0381970 0.0404439
Interferon - Placebo Subcortical2Ventral.Attention -0.0678052 0.0317615 26 -2.134825 0.0423677 0.0435782
Interferon - Placebo Subcortical2Visual -0.0739030 0.0311656 26 -2.371299 0.0254177 0.0318013
Interferon - Placebo Ventral.Attention2Ventral.Attention -0.1060082 0.0341916 26 -3.100414 0.0046060 0.0183278
Interferon - Placebo Ventral.Attention2Visual -0.0880488 0.0304776 26 -2.888971 0.0076928 0.0191252
Interferon - Placebo Visual2Visual -0.0727204 0.0309134 26 -2.352394 0.0265011 0.0318013

Grouped by scale, condition, subject and network:yeo and age

#slow to calc
data.funccon %>% mutate(weight = atanh(weight), from = gsub("V", "ROI_", from), to = gsub("V", "ROI_", to)) %>% rowwise() %>% mutate(from_network = ifelse(is.na(match(from, network.combined$ROI)), from, network.combined$name[match(from, network.combined$ROI)]), to_network = ifelse(is.na(match(to, network.combined$ROI)), to, network.combined$name[match(to, network.combined$ROI)]), from2to = paste0(sort(c(from_network,to_network))[1],"2",sort(c(from_network,to_network))[2])) %>% group_by(Subj_ID,condition,from2to, Age_cat) %>% summarise(meanFuncConn = mean(weight)) -> data.funccon.network.aggr

# regular anova
main_anova_summary <- aov_ez("Subj_ID", "meanFuncConn", data.funccon.network.aggr,within =c("condition","from2to"), between = "Age_cat")
knitr::kable(nice(main_anova_summary))
Effect df MSE F ges p.value
Age_cat 1, 25 1.96 1.22 .032 .279
condition 1, 25 0.34 6.12 * .028 .021
Age_cat:condition 1, 25 0.34 1.78 .008 .195
from2to 35, 875 0.01 84.28 *** .309 <.001
Age_cat:from2to 35, 875 0.01 2.11 *** .011 <.001
condition:from2to 35, 875 0.01 3.06 *** .008 <.001
Age_cat:condition:from2to 35, 875 0.01 0.84 .002 .738
EMM <- emmeans(main_anova_summary, ~  condition)   
EMM.unadj <- summary(pairs(EMM), by = NULL, adjust = "none") %>% as.data.frame() %>% dplyr::rename(.,p.value.unadj = p.value)
EMM.fdr <- summary(pairs(EMM), by = NULL, adjust = "fdr") %>% as.data.frame() %>% dplyr::rename(.,p.value.fdr = p.value)

# corrected and uncorrected
knitr::kable(left_join(EMM.unadj,EMM.fdr))
contrast estimate SE df t.ratio p.value.unadj p.value.fdr
Interferon - Placebo -0.0653514 0.0264275 25 -2.472862 0.0205555 0.0205555
# data.funccon.network.aggr %>% ungroup() %>%
#   nest_by(from2to) %>%
#   mutate(Model = list(nice(aov_ez("Subj_ID", "meanFuncConn", data, within ="condition", between = "Age_cat")))) %>% select(-data) %>% unnest(Model) -> anova_summary
# 
# knitr::kable(anova_summary)

Summary

NBS

Node degree

Grouped by scale, condition, subject

data %>% pivot_longer(
    cols = starts_with("ROI"),
    names_to = "ROI",
    values_to = "degree") %>% left_join(.,variables_ext) %>% group_by(threshold,Subj_ID,condition) %>% summarise(meanDegree = mean(degree)) -> data.aggr

anova_summary <- aov_ez("Subj_ID", "meanDegree", data.aggr, within =c("condition","threshold"))

knitr::kable(nice(anova_summary))
Effect df MSE F ges p.value
condition 1, 26 5.94 7.35 * .089 .012
threshold 1, 26 2.32 121615.21 *** .998 <.001
condition:threshold 1, 26 1.57 14.97 *** .050 <.001
emmip(anova_summary, condition~threshold )

grouped_ggwithinstats(
    data             = data.aggr,
  x                = condition,
  y                = meanDegree,
  grouping.var     = threshold,
  type             = "np",
  bf.message = FALSE,
  exact = FALSE
  )

Grouped by scale, condition, subject and age_cat

data %>% pivot_longer(
    cols = starts_with("ROI"),
    names_to = "ROI",
    values_to = "degree") %>% left_join(.,variables_ext) %>% group_by(threshold,Subj_ID,condition, Age_cat) %>% summarise(meanDegree = mean(degree)) -> data.aggr

anova_summary <- nice(aov_ez("Subj_ID", "meanDegree", data.aggr, within =c("condition","threshold"), between = "Age_cat"))

knitr::kable(anova_summary)
Effect df MSE F ges p.value
Age_cat 1, 25 7.24 1.40 .023 .248
condition 1, 25 6.17 7.08 * .092 .013
Age_cat:condition 1, 25 6.17 0.01 <.001 .941
threshold 1, 25 2.31 122179.17 *** .998 <.001
Age_cat:threshold 1, 25 2.31 1.15 .006 .294
condition:threshold 1, 25 1.61 14.79 *** .052 <.001
Age_cat:condition:threshold 1, 25 1.61 0.41 .002 .529
# 
# data.aggr %>% ungroup() %>%
#   nest_by(threshold) %>%
#   mutate(Model = list(nice(aov_ez("Subj_ID", "meanDegree", data, within ="condition", between="Age_cat")))) %>% select(-data) %>% unnest(Model) -> anova_summary
# 
# knitr::kable(anova_summary)
# 
# # aw <- aov_ez("Subj_ID", "meanDegree", data.aggr, within ="condition", between="Age_cat")
# # p_an <- afex_plot(aw, x = "condition", trace = "Age_cat")  + ggtitle("test") +   theme(legend.position = c(0.87, 0.15),legend.key.size = unit(0.3, "cm"))
# 
# # plot anovas
# 
# d_nested <- data.aggr %>% ungroup() %>%
#   nest_by(threshold)
# 
# d_plots <- 
#   d_nested %>% 
#   mutate(aov = list(map(data,~aov_ez("Subj_ID", "meanDegree", data, within ="condition", between="Age_cat")))) %>%
#   mutate(plot = list(map2(aov,threshold,~afex_plot(., x = "condition", trace = "Age_cat") +ggtitle(paste("threshold:",threshold)) + theme(legend.position = c(0.87, 0.15),legend.key.size = unit(0.3, "cm")))))
# 
# d_plots$plot_one <- lapply(d_plots$plot, "[[", 1)
# 
# library(gridExtra)
# do.call(grid.arrange, c(d_plots$plot_one))

Grouped by scale, condition, subject and network:yeo

data %>% pivot_longer(
    cols = starts_with("ROI"),
    names_to = "ROI",
    values_to = "degree") %>% left_join(.,network.combined) %>% left_join(.,variables_ext) %>% group_by(threshold,Subj_ID,condition,name) %>% summarise(meanDegree = mean(degree)) -> data.aggr

# regular anova
main_anova_summary <- aov_ez("Subj_ID", "meanDegree", data.aggr,within =c("condition","name","threshold"))
knitr::kable(nice(main_anova_summary))
Effect df MSE F ges p.value
condition 1, 26 50.57 4.96 * .006 .035
name 5.11, 132.83 136.59 1.74 .028 .129
threshold 1, 26 21.77 103702.63 *** .982 <.001
condition:name 5.44, 141.41 87.59 0.53 .006 .772
condition:threshold 1, 26 13.29 13.27 ** .004 .001
name:threshold 5.32, 138.45 31.72 1.16 .005 .331
condition:name:threshold 5.45, 141.62 21.72 0.89 .003 .494
#emmip(main_anova_summary, condition~threshold)

# average across threshold
main_anova_summary <- aov_ez("Subj_ID", "meanDegree", data.aggr,within =c("condition","name"))

knitr::kable(nice(main_anova_summary))
Effect df MSE F ges p.value
condition 1, 26 25.29 4.96 * .007 .035
name 5.11, 132.83 68.30 1.74 .035 .129
condition:name 5.44, 141.41 43.79 0.53 .007 .772
EMM <- emmeans(main_anova_summary, ~  condition | name)  %>% contrast(interaction = c( "consec", "consec"))
  
EMM.unadj <- summary(EMM, by = NULL, adjust = "none") %>% as.data.frame() %>% dplyr::rename(.,p.value.unadj = p.value)
EMM.fdr <- summary(EMM, by = NULL, adjust = "fdr") %>% as.data.frame() %>% dplyr::rename(.,p.value.fdr = p.value)

# corrected and uncorrected
knitr::kable(left_join(EMM.unadj,EMM.fdr))
condition_consec name estimate SE df t.ratio p.value.unadj p.value.fdr
Placebo - Interferon Default -2.3071364 1.353114 26 -1.7050576 0.1001058 0.3916545
Placebo - Interferon Dorsal.Attention 0.1031145 1.544432 26 0.0667653 0.9472793 0.9472793
Placebo - Interferon Frontoparietal -2.2592593 1.510885 26 -1.4953219 0.1468704 0.3916545
Placebo - Interferon Limbic 0.1915709 1.904128 26 0.1006082 0.9206333 0.9472793
Placebo - Interferon Somatomotor -0.2603075 1.596335 26 -0.1630656 0.8717276 0.9472793
Placebo - Interferon Subcortical -0.7014815 1.469710 26 -0.4772923 0.6371418 0.9472793
Placebo - Interferon Ventral.Attention -0.7500000 1.550439 26 -0.4837341 0.6326239 0.9472793
Placebo - Interferon Visual -2.6377903 1.509883 26 -1.7470168 0.0924388 0.3916545
# focus on threshold=3.5
knitr::kable(nice(aov_ez("Subj_ID", "meanDegree", data.aggr %>% filter(threshold=="3.5"),within =c("condition","name"))))
Effect df MSE F ges p.value
condition 1, 26 52.98 8.00 ** .012 .009
name 5.36, 139.38 132.72 1.58 .031 .164
condition:name 5.49, 142.71 90.53 0.63 .009 .695
main_anova_summary <- aov_ez("Subj_ID", "meanDegree", data.aggr,within =c("condition","name","threshold"))
knitr::kable(nice(main_anova_summary))
Effect df MSE F ges p.value
condition 1, 26 50.57 4.96 * .006 .035
name 5.11, 132.83 136.59 1.74 .028 .129
threshold 1, 26 21.77 103702.63 *** .982 <.001
condition:name 5.44, 141.41 87.59 0.53 .006 .772
condition:threshold 1, 26 13.29 13.27 ** .004 .001
name:threshold 5.32, 138.45 31.72 1.16 .005 .331
condition:name:threshold 5.45, 141.62 21.72 0.89 .003 .494
EMM <- emmeans(main_anova_summary, ~  condition | name, at=list(threshold="X3.5"))
  
EMM.unadj <- summary(pairs(EMM), by = NULL, adjust = "none") %>% as.data.frame() %>% dplyr::rename(.,p.value.unadj = p.value)
EMM.fdr <- summary(pairs(EMM), by = NULL, adjust = "fdr") %>% as.data.frame() %>% dplyr::rename(.,p.value.fdr = p.value)

# corrected and uncorrected
knitr::kable(left_join(EMM.unadj,EMM.fdr))
contrast name estimate SE df t.ratio p.value.unadj p.value.fdr
Interferon - Placebo Default 3.2859079 1.892675 26 1.7361188 0.0943806 0.2516817
Interferon - Placebo Dorsal.Attention 0.4747475 2.303777 26 0.2060735 0.8383386 0.9725700
Interferon - Placebo Frontoparietal 4.2880658 2.233913 26 1.9195312 0.0659543 0.2516817
Interferon - Placebo Limbic 0.3793103 2.841336 26 0.1334972 0.8948281 0.9725700
Interferon - Placebo Somatomotor 0.0768693 2.214126 26 0.0347177 0.9725700 0.9725700
Interferon - Placebo Subcortical 1.2533333 2.039670 26 0.6144785 0.5442395 0.8707832
Interferon - Placebo Ventral.Attention 1.5000000 2.243949 26 0.6684644 0.5097289 0.8707832
Interferon - Placebo Visual 4.5900816 2.163629 26 2.1214731 0.0435750 0.2516817
# data.aggr %>% ungroup() %>%
#   nest_by(threshold,name) %>%
#   mutate(Model = list(nice(aov_ez("Subj_ID", "meanDegree", data, within ="condition")))) %>% select(-data) %>% unnest(Model) -> anova_summary
# 
# knitr::kable(anova_summary)

Grouped by scale, condition, subject, age_cat and network:yeo

data %>% pivot_longer(
    cols = starts_with("ROI"),
    names_to = "ROI",
    values_to = "degree") %>% left_join(.,network.combined) %>% left_join(.,variables_ext) %>% group_by(threshold,Subj_ID,condition,name,Age_cat) %>% summarise(meanDegree = mean(degree)) -> data.aggr

# regular anova
main_anova_summary <- aov_ez("Subj_ID", "meanDegree", data.aggr,within =c("condition","name","threshold"), between = "Age_cat")

knitr::kable(nice(main_anova_summary)) 
Effect df MSE F ges p.value
Age_cat 1, 25 66.89 0.67 .001 .420
condition 1, 25 52.52 4.80 * .006 .038
Age_cat:condition 1, 25 52.52 0.04 <.001 .853
name 5.16, 128.88 137.25 1.76 .029 .124
Age_cat:name 5.16, 128.88 137.25 0.64 .011 .673
threshold 1, 25 21.98 102602.39 *** .982 <.001
Age_cat:threshold 1, 25 21.98 0.75 <.001 .393
condition:name 5.33, 133.17 89.96 0.51 .006 .782
Age_cat:condition:name 5.33, 133.17 89.96 0.85 .010 .525
condition:threshold 1, 25 13.47 13.28 ** .004 .001
Age_cat:condition:threshold 1, 25 13.47 0.64 <.001 .433
name:threshold 5.33, 133.16 32.62 1.12 .005 .352
Age_cat:name:threshold 5.33, 133.16 32.62 0.27 .001 .937
condition:name:threshold 5.39, 134.74 22.25 0.88 .003 .502
Age_cat:condition:name:threshold 5.39, 134.74 22.25 0.65 .002 .671
emmip(main_anova_summary, name~condition | Age_cat)

emmip(main_anova_summary, name~Age_cat)

main_anova_summary <- aov_ez("Subj_ID", "meanDegree", data.aggr,within =c("condition","name"),between = "Age_cat")

knitr::kable(nice(main_anova_summary))
Effect df MSE F ges p.value
Age_cat 1, 25 33.45 0.67 .001 .420
condition 1, 25 26.26 4.80 * .008 .038
Age_cat:condition 1, 25 26.26 0.04 <.001 .853
name 5.16, 128.88 68.63 1.76 .037 .124
Age_cat:name 5.16, 128.88 68.63 0.64 .014 .673
condition:name 5.33, 133.17 44.98 0.51 .007 .782
Age_cat:condition:name 5.33, 133.17 44.98 0.85 .012 .525
EMM <- emmeans(main_anova_summary, ~  condition:Age_cat | name)  %>% contrast(interaction = c( "consec", "consec"))
  
EMM.unadj <- summary(EMM, by = NULL, adjust = "none") %>% as.data.frame() %>% dplyr::rename(.,p.value.unadj = p.value)
EMM.fdr <- summary(EMM, by = NULL, adjust = "fdr") %>% as.data.frame() %>% dplyr::rename(.,p.value.fdr = p.value)

# corrected and uncorrected
knitr::kable(left_join(EMM.unadj,EMM.fdr))
condition_consec Age_cat_consec name estimate SE df t.ratio p.value.unadj p.value.fdr
Placebo - Interferon Y - O Default -1.3027674 2.749397 25 -0.4738374 0.6397279 0.9112563
Placebo - Interferon Y - O Dorsal.Attention -4.8753122 2.997600 25 -1.6264049 0.1164043 0.5514486
Placebo - Interferon Y - O Frontoparietal -1.1170330 3.075625 25 -0.3631889 0.7195167 0.9112563
Placebo - Interferon Y - O Limbic 0.0832702 3.886306 25 0.0214266 0.9830755 0.9830755
Placebo - Interferon Y - O Somatomotor 3.3207029 3.189726 25 1.0410621 0.3078106 0.8208283
Placebo - Interferon Y - O Subcortical -0.7774725 2.995658 25 -0.2595331 0.7973492 0.9112563
Placebo - Interferon Y - O Ventral.Attention -1.3258929 3.153328 25 -0.4204742 0.6777314 0.9112563
Placebo - Interferon Y - O Visual 4.5163904 2.946330 25 1.5328871 0.1378622 0.5514486
# focus on threshold=3.5
main_anova_summary <- aov_ez("Subj_ID", "meanDegree", data.aggr,within =c("condition","name","threshold"),between = "Age_cat")

knitr::kable(nice(main_anova_summary))
Effect df MSE F ges p.value
Age_cat 1, 25 66.89 0.67 .001 .420
condition 1, 25 52.52 4.80 * .006 .038
Age_cat:condition 1, 25 52.52 0.04 <.001 .853
name 5.16, 128.88 137.25 1.76 .029 .124
Age_cat:name 5.16, 128.88 137.25 0.64 .011 .673
threshold 1, 25 21.98 102602.39 *** .982 <.001
Age_cat:threshold 1, 25 21.98 0.75 <.001 .393
condition:name 5.33, 133.17 89.96 0.51 .006 .782
Age_cat:condition:name 5.33, 133.17 89.96 0.85 .010 .525
condition:threshold 1, 25 13.47 13.28 ** .004 .001
Age_cat:condition:threshold 1, 25 13.47 0.64 <.001 .433
name:threshold 5.33, 133.16 32.62 1.12 .005 .352
Age_cat:name:threshold 5.33, 133.16 32.62 0.27 .001 .937
condition:name:threshold 5.39, 134.74 22.25 0.88 .003 .502
Age_cat:condition:name:threshold 5.39, 134.74 22.25 0.65 .002 .671
EMM <- emmeans(main_anova_summary, ~  condition:Age_cat | name, at=list(threshold="X3.5")) %>% contrast(interaction = c( "consec", "consec"))
  
EMM.unadj <- summary(EMM, by = NULL, adjust = "none") %>% as.data.frame() %>% dplyr::rename(.,p.value.unadj = p.value)
EMM.fdr <- summary(EMM, by = NULL, adjust = "fdr") %>% as.data.frame() %>% dplyr::rename(.,p.value.fdr = p.value)

# corrected and uncorrected
knitr::kable(left_join(EMM.unadj,EMM.fdr))
condition_consec Age_cat_consec name estimate SE df t.ratio p.value.unadj p.value.fdr
Placebo - Interferon Y - O Default -2.321563 3.834958 25 -0.6053684 0.5503908 0.8121239
Placebo - Interferon Y - O Dorsal.Attention -6.400849 4.524404 25 -1.4147385 0.1694805 0.6779219
Placebo - Interferon Y - O Frontoparietal -2.151038 4.539093 25 -0.4738915 0.6396898 0.8121239
Placebo - Interferon Y - O Limbic -1.391436 5.792509 25 -0.2402130 0.8121239 0.8121239
Placebo - Interferon Y - O Somatomotor 4.929090 4.410212 25 1.1176537 0.2743408 0.7315754
Placebo - Interferon Y - O Subcortical -1.926593 4.145112 25 -0.4647868 0.6461069 0.8121239
Placebo - Interferon Y - O Ventral.Attention -1.458791 4.570616 25 -0.3191673 0.7522512 0.8121239
Placebo - Interferon Y - O Visual 6.053734 4.246766 25 1.4254929 0.1663818 0.6779219
# data.aggr %>% ungroup() %>%
#   nest_by(threshold,name) %>%
#   mutate(Model = list(nice(aov_ez("Subj_ID", "meanDegree", data, within ="condition",between="Age_cat")))) %>% select(-data) %>% unnest(Model) -> anova_summary
# 
# knitr::kable(anova_summary)

Hubness?

data %>% pivot_longer(
    cols = starts_with("ROI"),
    names_to = "ROI",
    values_to = "degree") %>% left_join(.,variables_ext) %>% group_by(threshold,Subj_ID,condition) %>% mutate(quartile = ntile(degree, 4)) -> data.aggr

fit1 <- lmer(degree ~ condition*quartile + (1 | Subj_ID), data.aggr )

fit2 <- lmer(degree ~ condition*quartile + Age + (1 | Subj_ID), data.aggr )

fit3 <- lmer(degree ~ condition*quartile*Age + (1 | Subj_ID), data.aggr )

tab_model(fit1,fit2,fit3)
  degree degree degree
Predictors Estimates CI p Estimates CI p Estimates CI p
(Intercept) -11.40 -13.22 – -9.57 <0.001 -10.78 -12.95 – -8.61 <0.001 -10.17 -14.60 – -5.74 <0.001
condition [Placebo] -6.19 -8.77 – -3.60 <0.001 -6.19 -8.77 – -3.60 <0.001 -6.58 -12.84 – -0.32 0.040
quartile 41.70 41.04 – 42.37 <0.001 41.70 41.04 – 42.37 <0.001 41.48 39.86 – 43.10 <0.001
condition [Placebo] ×
quartile
1.97 1.02 – 2.91 <0.001 1.97 1.02 – 2.91 <0.001 2.10 -0.19 – 4.39 0.073
Age -0.01 -0.04 – 0.01 0.301 -0.03 -0.11 – 0.06 0.552
condition [Placebo] × Age 0.01 -0.11 – 0.13 0.892
quartile × Age 0.00 -0.03 – 0.04 0.761
(condition [Placebo] ×
quartile) × Age
-0.00 -0.05 – 0.04 0.904
Random Effects
σ2 3218.54 3218.54 3218.75
τ00 0.00 Subj_ID 0.00 Subj_ID 0.00 Subj_ID
N 27 Subj_ID 27 Subj_ID 27 Subj_ID
Observations 44280 44280 44280
Marginal R2 / Conditional R2 0.415 / NA 0.415 / NA 0.415 / NA
plot_model(fit1, type = "pred", terms = c("quartile[all]","condition") )

fit1 <- lmer(degree ~ condition*quartile + (1 | Subj_ID), data.aggr %>% filter(threshold==3.5))

fit2 <- lmer(degree ~ condition*quartile + Age + (1 | Subj_ID), data.aggr %>% filter(threshold==3.5))

fit3 <- lmer(degree ~ condition*quartile*Age + (1 | Subj_ID), data.aggr %>% filter(threshold==3.5))

tab_model(fit1,fit2,fit3)
  degree degree degree
Predictors Estimates CI p Estimates CI p Estimates CI p
(Intercept) 3.03 1.83 – 4.23 <0.001 4.08 2.04 – 6.11 <0.001 5.39 2.49 – 8.29 <0.001
condition [Placebo] -9.78 -11.16 – -8.39 <0.001 -9.78 -11.16 – -8.39 <0.001 -10.08 -13.43 – -6.73 <0.001
quartile 56.60 56.24 – 56.96 <0.001 56.60 56.24 – 56.96 <0.001 56.12 55.25 – 56.98 <0.001
condition [Placebo] ×
quartile
3.04 2.53 – 3.54 <0.001 3.04 2.53 – 3.54 <0.001 3.08 1.85 – 4.30 <0.001
Age -0.02 -0.06 – 0.01 0.211 -0.05 -0.11 – 0.01 0.080
condition [Placebo] × Age 0.01 -0.06 – 0.07 0.847
quartile × Age 0.01 -0.01 – 0.03 0.230
(condition [Placebo] ×
quartile) × Age
-0.00 -0.02 – 0.02 0.944
Random Effects
σ2 461.00 461.00 461.00
τ00 3.43 Subj_ID 3.34 Subj_ID 3.34 Subj_ID
ICC 0.01 0.01 0.01
N 27 Subj_ID 27 Subj_ID 27 Subj_ID
Observations 22140 22140 22140
Marginal R2 / Conditional R2 0.901 / 0.902 0.901 / 0.902 0.901 / 0.902
plot_model(fit1, type = "pred", terms = c("quartile[all]","condition") )

Summary

Betweenness centrality

Grouped by scale, condition, subject

data %>% pivot_longer(
    cols = starts_with("ROI"),
    names_to = "ROI",
    values_to = "bwc") %>% left_join(.,variables_ext) %>% group_by(threshold,Subj_ID,condition) %>% summarise(meanBWC = mean(bwc)) -> data.aggr

main_anova_summary <- aov_ez("Subj_ID", "meanBWC", data.aggr,within =c("condition","threshold"))
DT::datatable(nice(main_anova_summary))
emmip(main_anova_summary, condition~threshold)

grouped_ggwithinstats(
    data             = data.aggr,
  x                = condition,
  y                = meanBWC,
  grouping.var     = threshold,
  type             = "np",
  bf.message = FALSE,
  )

Grouped by scale, condition, subject and age_cat

data %>% pivot_longer(
    cols = starts_with("ROI"),
    names_to = "ROI",
    values_to = "bwc") %>% left_join(.,variables_ext) %>% group_by(threshold,Subj_ID,condition, Age_cat) %>% summarise(meanBWC = mean(bwc)) -> data.aggr

# regular anova
main_anova_summary <- aov_ez("Subj_ID", "meanBWC", data.aggr,within =c("condition","threshold"), between = "Age_cat")
DT::datatable(nice(main_anova_summary)) 
emmip(main_anova_summary, condition~Age_cat)

# looking at threshold = 3.5
main_anova_summary <- aov_ez("Subj_ID", "meanBWC", data.aggr,within =c("condition","threshold"), between = "Age_cat")
EMM <- emmeans(main_anova_summary, ~condition:Age_cat, at=list(threshold="X3.5")) %>% contrast(interaction = c( "consec", "consec"))
DT::datatable(EMM %>% as.data.frame()) 
# data.aggr %>% ungroup() %>%
#   nest_by(threshold) %>%
#   mutate(Model = list(nice(aov_ez("Subj_ID", "meanBWC", data, within ="condition", between="Age_cat")))) %>% select(-data) %>% unnest(Model) -> anova_summary
# 
# DT::datatable(anova_summary)
# 
# # aw <- aov_ez("Subj_ID", "meanDegree", data.aggr, within ="condition", between="Age_cat")
# # p_an <- afex_plot(aw, x = "condition", trace = "Age_cat")  + ggtitle("test") +   theme(legend.position = c(0.87, 0.15),legend.key.size = unit(0.3, "cm"))
# 
# # plot anovas
# 
# d_nested <- data.aggr %>% ungroup() %>%
#   nest_by(threshold)
# 
# d_plots <- 
#   d_nested %>% 
#   mutate(aov = list(map(data,~aov_ez("Subj_ID", "meanBWC", data, within ="condition", between="Age_cat")))) %>%
#   mutate(plot = list(map2(aov,threshold,~afex_plot(., x = "condition", trace = "Age_cat") +ggtitle(paste("threshold:",threshold)) + theme(legend.position = c(0.87, 0.15),legend.key.size = unit(0.3, "cm")))))
# 
# d_plots$plot_one <- lapply(d_plots$plot, "[[", 1)
# 
# library(gridExtra)
# do.call(grid.arrange, c(d_plots$plot_one))

Grouped by scale, condition, subject and network:yeo

data %>% pivot_longer(
    cols = starts_with("ROI"),
    names_to = "ROI",
    values_to = "bwc") %>% left_join(.,network.combined) %>% left_join(.,variables_ext) %>% group_by(threshold,Subj_ID,condition,name) %>% summarise(meanBWC = mean(bwc)) -> data.aggr

# regular anova
main_anova_summary <- aov_ez("Subj_ID", "meanBWC", data.aggr,within =c("condition","name","threshold"))
DT::datatable(nice(main_anova_summary))
#emmip(main_anova_summary, condition~threshold)

# average across threshold
main_anova_summary <- aov_ez("Subj_ID", "meanBWC", data.aggr,within =c("condition","name"))

DT::datatable(nice(main_anova_summary))
EMM <- emmeans(main_anova_summary, ~  condition | name)  %>% contrast(interaction = c( "consec", "consec"))
  
EMM.unadj <- summary(EMM, by = NULL, adjust = "none") %>% as.data.frame() %>% dplyr::rename(.,p.value.unadj = p.value)
EMM.fdr <- summary(EMM, by = NULL, adjust = "fdr") %>% as.data.frame() %>% dplyr::rename(.,p.value.fdr = p.value)

# corrected and uncorrected
DT::datatable(left_join(EMM.unadj,EMM.fdr))
# focus on threshold=3.5
DT::datatable(nice(aov_ez("Subj_ID", "meanBWC", data.aggr %>% filter(threshold=="3.5"),within =c("condition","name"))))
main_anova_summary <- aov_ez("Subj_ID", "meanBWC", data.aggr,within =c("condition","name","threshold"))
DT::datatable(nice(main_anova_summary))
EMM <- emmeans(main_anova_summary, ~  condition | name, at=list(threshold="X3.5"))
  
EMM.unadj <- summary(pairs(EMM), by = NULL, adjust = "none") %>% as.data.frame() %>% dplyr::rename(.,p.value.unadj = p.value)
EMM.fdr <- summary(pairs(EMM), by = NULL, adjust = "fdr") %>% as.data.frame() %>% dplyr::rename(.,p.value.fdr = p.value)

# corrected and uncorrected
DT::datatable(left_join(EMM.unadj,EMM.fdr))
# data.aggr %>% ungroup() %>%
#   nest_by(threshold,name) %>%
#   mutate(Model = list(nice(aov_ez("Subj_ID", "meanBWC", data, within ="condition")))) %>% select(-data) %>% unnest(Model) -> anova_summary
# 
# DT::datatable(anova_summary)

Grouped by scale, condition, subject, age_cat and network:yeo

data %>% pivot_longer(
    cols = starts_with("ROI"),
    names_to = "ROI",
    values_to = "bwc") %>% left_join(.,network.combined) %>% left_join(.,variables_ext) %>% group_by(threshold,Subj_ID,condition,name,Age_cat) %>% summarise(meanBWC = mean(bwc)) -> data.aggr

# regular anova
main_anova_summary <- aov_ez("Subj_ID", "meanBWC", data.aggr,within =c("condition","name","threshold"), between = "Age_cat")

DT::datatable(nice(main_anova_summary)) 
emmip(main_anova_summary, name~Age_cat | threshold)

emmip(main_anova_summary, ~condition | Age_cat)

# average across threshold
main_anova_summary <- aov_ez("Subj_ID", "meanBWC", data.aggr,within =c("condition","name"),between = "Age_cat")
DT::datatable(nice(main_anova_summary))
EMM <- emmeans(main_anova_summary, ~  condition:Age_cat | name)  %>% contrast(interaction = c( "consec", "consec"))
  
EMM.unadj <- summary(EMM, by = NULL, adjust = "none") %>% as.data.frame() %>% dplyr::rename(.,p.value.unadj = p.value)
EMM.fdr <- summary(EMM, by = NULL, adjust = "fdr") %>% as.data.frame() %>% dplyr::rename(.,p.value.fdr = p.value)

# corrected and uncorrected
DT::datatable(left_join(EMM.unadj,EMM.fdr))
# focus on threshold=3.5
main_anova_summary <- aov_ez("Subj_ID", "meanBWC", data.aggr,within =c("condition","name","threshold"), between = "Age_cat")

DT::datatable(nice(main_anova_summary))
EMM <- emmeans(main_anova_summary, ~  condition:Age_cat | name, at=list(threshold="X3.5")) %>% contrast(interaction = c( "consec", "consec"))
  
EMM.unadj <- summary(EMM, by = NULL, adjust = "none") %>% as.data.frame() %>% dplyr::rename(.,p.value.unadj = p.value)
EMM.fdr <- summary(EMM, by = NULL, adjust = "fdr") %>% as.data.frame() %>% dplyr::rename(.,p.value.fdr = p.value)

# corrected and uncorrected
DT::datatable(left_join(EMM.unadj,EMM.fdr))

Subgroup analysis: Placebo condition only

# testing

data %>% pivot_longer(
    cols = starts_with("ROI"),
    names_to = "ROI",
    values_to = "bwc") %>% left_join(.,network.combined) %>% left_join(.,variables_ext) %>% group_by(threshold,Subj_ID,condition,Age_cat) %>% summarise(meanBWC = mean(bwc)) %>% filter(condition=="Placebo") -> data.aggr.age

# regular anova
main_anova_summary_a <- aov_ez("Subj_ID", "meanBWC", data.aggr.age, between = "Age_cat")
nice(main_anova_summary_a)
## Anova Table (Type 3 tests)
## 
## Response: meanBWC
##    Effect    df    MSE    F  ges p.value
## 1 Age_cat 1, 25 913.52 0.20 .008    .655
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1
afex_plot(main_anova_summary_a, "Age_cat")

data %>% pivot_longer(
    cols = starts_with("ROI"),
    names_to = "ROI",
    values_to = "bwc") %>% left_join(.,network.combined) %>% left_join(.,variables_ext) %>% group_by(Subj_ID,condition,Age_cat) %>% summarise(meanBWC = mean(bwc)) %>% filter(condition=="Placebo") -> data.aggr.age

ggbetweenstats(
    data             = data.aggr.age,
  x                = Age_cat,
  y                = meanBWC,
  type             = "p",
  bf.message = FALSE,
  )

data %>% pivot_longer(
    cols = starts_with("ROI"),
    names_to = "ROI",
    values_to = "bwc") %>% left_join(.,network.combined) %>% left_join(.,variables_ext) %>% group_by(Subj_ID,condition,Age) %>% summarise(meanBWC = mean(bwc)) %>% filter(condition=="Placebo") -> data.aggr.age

ggscatterstats(
  data = data.aggr.age, ## data frame from which variables are taken
  x = Age, ## predictor/independent variable
  y = meanBWC ## dependent variabl
)

grouped_ggbetweenstats(
    data             = data.aggr,
  x                = Age_cat,
  y                = meanBWC,
  grouping.var     = threshold,
  type             = "p",
  bf.message = FALSE,
  )

# regular anova
main_anova_summary <- aov_ez("Subj_ID", "meanBWC", data.aggr,within =c("threshold"), between = "Age_cat")
DT::datatable(nice(main_anova_summary)) 
data %>% pivot_longer(
    cols = starts_with("ROI"),
    names_to = "ROI",
    values_to = "bwc") %>% left_join(.,network.combined) %>% left_join(.,variables_ext) %>% group_by(threshold,Subj_ID,condition,name,Age_cat) %>% summarise(meanBWC = mean(bwc)) %>% filter(condition=="Placebo") -> data.aggr

# regular anova
main_anova_summary <- aov_ez("Subj_ID", "meanBWC", data.aggr,within =c("name","threshold"), between = "Age_cat")

DT::datatable(nice(main_anova_summary)) 
emmip(main_anova_summary, name~Age_cat | threshold)

emmip(main_anova_summary, ~ Age_cat)

# average across threshold
main_anova_summary <- aov_ez("Subj_ID", "meanBWC", data.aggr,within =c("name"),between = "Age_cat")
DT::datatable(nice(main_anova_summary))
EMM <- emmeans(main_anova_summary, ~  Age_cat | name)  %>% contrast(interaction = c( "consec", "consec"))
  
EMM.unadj <- summary(EMM, by = NULL, adjust = "none") %>% as.data.frame() %>% dplyr::rename(.,p.value.unadj = p.value)
EMM.fdr <- summary(EMM, by = NULL, adjust = "fdr") %>% as.data.frame() %>% dplyr::rename(.,p.value.fdr = p.value)

# corrected and uncorrected
DT::datatable(left_join(EMM.unadj,EMM.fdr))
# focus on threshold=3.5
main_anova_summary <- aov_ez("Subj_ID", "meanBWC", data.aggr,within =c("name","threshold"), between = "Age_cat")

DT::datatable(nice(main_anova_summary))
EMM <- emmeans(main_anova_summary, ~  Age_cat | name, at=list(threshold="X3.5")) %>% contrast(interaction = c( "consec", "consec"))
  
EMM.unadj <- summary(EMM, by = NULL, adjust = "none") %>% as.data.frame() %>% dplyr::rename(.,p.value.unadj = p.value)
EMM.fdr <- summary(EMM, by = NULL, adjust = "fdr") %>% as.data.frame() %>% dplyr::rename(.,p.value.fdr = p.value)

# corrected and uncorrected
DT::datatable(left_join(EMM.unadj,EMM.fdr))

Summary

Global efficiency

Grouped by scale, condition, subject

data %>% pivot_longer(
    cols = starts_with("ROI"),
    names_to = "ROI",
    values_to = "geff") %>% left_join(.,variables_ext) %>% group_by(threshold,Subj_ID,condition) %>% summarise(meangeff = mean(geff)) -> data.aggr

# regular anova
main_anova_summary <- aov_ez("Subj_ID", "meangeff", data.aggr,within =c("condition","threshold"))
DT::datatable(nice(main_anova_summary) ) 
emmip(main_anova_summary, condition~threshold )

grouped_ggwithinstats(
    data             = data.aggr,
  x                = condition,
  y                = meangeff,
  grouping.var     = threshold,
  type             = "np",
  bf.message = FALSE,
  )

Grouped by scale, condition, subject and age_cat

data %>% pivot_longer(
    cols = starts_with("ROI"),
    names_to = "ROI",
    values_to = "geff") %>% left_join(.,variables_ext) %>% group_by(threshold,Subj_ID,condition, Age_cat) %>% summarise(meangeff = mean(geff)) -> data.aggr

# regular anova
main_anova_summary <- aov_ez("Subj_ID", "meangeff", data.aggr,within =c("condition","threshold"), between = "Age_cat")
DT::datatable(nice(main_anova_summary))
# anova averaging across threshold
main_anova_summary <- aov_ez("Subj_ID", "meangeff", data.aggr,within =c("condition"), between = "Age_cat")
DT::datatable(nice(main_anova_summary))
# looking at threshold = 3.5
main_anova_summary <- aov_ez("Subj_ID", "meangeff", data.aggr,within =c("condition","threshold"), between = "Age_cat")
EMM <- emmeans(main_anova_summary, ~condition:Age_cat, at=list(threshold="X3.5")) %>% contrast(interaction = c( "consec", "consec"))
DT::datatable(EMM %>% as.data.frame()) 
# data.aggr %>% ungroup() %>%
#   nest_by(threshold) %>%
#   mutate(Model = list(nice(aov_ez("Subj_ID", "meangeff", data, within ="condition", between="Age_cat")))) %>% select(-data) %>% unnest(Model) -> anova_summary
# 
# DT::datatable(anova_summary)

# aw <- aov_ez("Subj_ID", "meanDegree", data.aggr, within ="condition", between="Age_cat")
# p_an <- afex_plot(aw, x = "condition", trace = "Age_cat")  + ggtitle("test") +   theme(legend.position = c(0.87, 0.15),legend.key.size = unit(0.3, "cm"))

# plot anovas
# 
# d_nested <- data.aggr %>% ungroup() %>%
#   nest_by(threshold)
# 
# d_plots <- 
#   d_nested %>% 
#   mutate(aov = list(map(data,~aov_ez("Subj_ID", "meangeff", data, within ="condition", between="Age_cat")))) %>%
#   mutate(plot = list(map2(aov,threshold,~afex_plot(., x = "condition", trace = "Age_cat") +ggtitle(paste("threshold:",threshold)) + theme(legend.position = c(0.87, 0.15),legend.key.size = unit(0.3, "cm")))))
# 
# d_plots$plot_one <- lapply(d_plots$plot, "[[", 1)
# 
# library(gridExtra)
# do.call(grid.arrange, c(d_plots$plot_one))

Grouped by scale, condition, subject and network:yeo

data %>% pivot_longer(
    cols = starts_with("ROI"),
    names_to = "ROI",
    values_to = "n_geff") %>% left_join(.,variables_ext) %>% group_by(threshold,Subj_ID,condition) -> data.aggr

# regular anova
main_anova_summary <- aov_ez("Subj_ID", "n_geff", data.aggr,within =c("network","condition","threshold"))
DT::datatable(nice(main_anova_summary))
emmip(main_anova_summary, network~threshold)

emmip(main_anova_summary, condition~threshold)

emmip(main_anova_summary, condition~threshold   | network)

# anova averaging across threshold
main_anova_summary <- aov_ez("Subj_ID", "n_geff", data.aggr,within =c("network","condition"))
DT::datatable(nice(main_anova_summary))
main_anova_summary <- aov_ez("Subj_ID", "n_geff", data.aggr,within =c("network","condition"))
EMM <- emmeans(main_anova_summary, ~  condition | network)
  
EMM.unadj <- summary(pairs(EMM), by = NULL, adjust = "none") %>% as.data.frame() %>% dplyr::rename(.,p.value.unadj = p.value)
EMM.fdr <- summary(pairs(EMM), by = NULL, adjust = "fdr") %>% as.data.frame() %>% dplyr::rename(.,p.value.fdr = p.value)

# corrected and uncorrected
DT::datatable(left_join(EMM.unadj,EMM.fdr))
# looking at threshold = 3.5
main_anova_summary <- aov_ez("Subj_ID", "n_geff", data.aggr,within =c("network","condition","threshold"))
EMM <- emmeans(main_anova_summary, ~  condition | network, at=list(threshold="X3.5"))
  
EMM.unadj <- summary(pairs(EMM), by = NULL, adjust = "none") %>% as.data.frame() %>% dplyr::rename(.,p.value.unadj = p.value)
EMM.fdr <- summary(pairs(EMM), by = NULL, adjust = "fdr") %>% as.data.frame() %>% dplyr::rename(.,p.value.fdr = p.value)

# corrected and uncorrected
DT::datatable(left_join(EMM.unadj,EMM.fdr))

Grouped by scale, condition, subject, age_cat and network:yeo

data %>% pivot_longer(
    cols = starts_with("ROI"),
    names_to = "ROI",
    values_to = "n_geff") %>% left_join(.,variables_ext) %>% group_by(threshold,Subj_ID,condition) -> data.aggr

# regular anova
main_anova_summary <- aov_ez("Subj_ID", "n_geff", data.aggr,within =c("network","condition","threshold"), between = "Age_cat")
DT::datatable(nice(main_anova_summary))
emmip(main_anova_summary, condition~Age_cat | network)

# anova averaging across threshold
main_anova_summary <- aov_ez("Subj_ID", "n_geff", data.aggr,within =c("network","condition"), between = "Age_cat")
DT::datatable(nice(main_anova_summary))
main_anova_summary <- aov_ez("Subj_ID", "n_geff", data.aggr,within =c("network","condition"))
EMM <- emmeans(main_anova_summary, ~  condition | network)
  
EMM.unadj <- summary(pairs(EMM), by = NULL, adjust = "none") %>% as.data.frame() %>% dplyr::rename(.,p.value.unadj = p.value)
EMM.fdr <- summary(pairs(EMM), by = NULL, adjust = "fdr") %>% as.data.frame() %>% dplyr::rename(.,p.value.fdr = p.value)

# corrected and uncorrected
DT::datatable(left_join(EMM.unadj,EMM.fdr))
# looking at threshold = 3.5
main_anova_summary <- aov_ez("Subj_ID", "n_geff", data.aggr,within =c("condition","threshold","network"), between = "Age_cat")
EMM <- emmeans(main_anova_summary, ~condition:Age_cat | network, at=list(threshold="X3.5")) %>% contrast(interaction = c( "consec", "consec"))
DT::datatable(EMM %>% as.data.frame()) 

Summary

Local efficiency

Grouped by scale, condition, subject

data %>% pivot_longer(
    cols = starts_with("ROI"),
    names_to = "ROI",
    values_to = "leff") %>% left_join(.,variables_ext) %>% group_by(threshold,Subj_ID,condition) %>% summarise(meanleff = mean(leff)) -> data.aggr

# regular anova
main_anova_summary <- aov_ez("Subj_ID", "meanleff", data.aggr,within =c("condition","threshold"))

DT::datatable(nice(main_anova_summary))
emmip(main_anova_summary, condition~threshold)

grouped_ggwithinstats(
    data             = data.aggr,
  x                = condition,
  y                = meanleff,
  grouping.var     = threshold,
  type             = "np",
  bf.message = FALSE,
  )

Grouped by scale, condition, subject and age_cat

data %>% pivot_longer(
    cols = starts_with("ROI"),
    names_to = "ROI",
    values_to = "leff") %>% left_join(.,variables_ext) %>% group_by(threshold,Subj_ID,condition, Age_cat) %>% summarise(meanleff = mean(leff)) -> data.aggr

# regular anova
main_anova_summary <- aov_ez("Subj_ID", "meanleff", data.aggr,within =c("condition","threshold"), between = "Age_cat")
DT::datatable(nice(main_anova_summary))
main_anova_summary <- aov_ez("Subj_ID", "meanleff", data.aggr,within =c("condition","threshold"), between = "Age_cat")
EMM <- emmeans(main_anova_summary, ~condition:Age_cat, at=list(threshold="X1")) %>% contrast(interaction = c( "consec", "consec"))
EMM.unadj <- summary(EMM, by = NULL, adjust = "none") %>% as.data.frame() %>% dplyr::rename(.,p.value.unadj = p.value)
EMM.fdr <- summary(EMM, by = NULL, adjust = "fdr") %>% as.data.frame() %>% dplyr::rename(.,p.value.fdr = p.value)

# corrected and uncorrected
DT::datatable(left_join(EMM.unadj,EMM.fdr))
# data.aggr %>% ungroup() %>%
#   nest_by(threshold) %>%
#   mutate(Model = list(nice(aov_ez("Subj_ID", "meanleff", data, within ="condition", between="Age_cat")))) %>% select(-data) %>% unnest(Model) -> anova_summary
# 
# DT::datatable(anova_summary)

# aw <- aov_ez("Subj_ID", "meanDegree", data.aggr, within ="condition", between="Age_cat")
# p_an <- afex_plot(aw, x = "condition", trace = "Age_cat")  + ggtitle("test") +   theme(legend.position = c(0.87, 0.15),legend.key.size = unit(0.3, "cm"))

# plot anovas

# d_nested <- data.aggr %>% ungroup() %>%
#   nest_by(threshold)
# 
# d_plots <- 
#   d_nested %>% 
#   mutate(aov = list(map(data,~aov_ez("Subj_ID", "meanleff", data, within ="condition", between="Age_cat")))) %>%
#   mutate(plot = list(map2(aov,threshold,~afex_plot(., x = "condition", trace = "Age_cat") +ggtitle(paste("threshold:",threshold)) + theme(legend.position = c(0.87, 0.15),legend.key.size = unit(0.3, "cm")))))
# 
# d_plots$plot_one <- lapply(d_plots$plot, "[[", 1)
# 
# library(gridExtra)
# do.call(grid.arrange, c(d_plots$plot_one))

Grouped by scale, condition, subject and network:yeo

data %>% pivot_longer(
    cols = starts_with("ROI"),
    names_to = "ROI",
    values_to = "leff") %>% left_join(.,network.combined) %>% left_join(.,variables_ext) %>% group_by(threshold,Subj_ID,condition,name) %>% summarise(meanleff = mean(leff)) -> data.aggr

# # regular anova
main_anova_summary <- afex::aov_ez("Subj_ID", "meanleff", data.aggr,within =c("condition","name","threshold"))
# DT::datatable(nice(main_anova_summary) )
# 
# # anova averaging across threshold
# main_anova_summary <- aov_ez("Subj_ID", "meanleff", data.aggr,within =c("condition","name"))
# DT::datatable(nice(main_anova_summary))
# 
# # looking at threshold = 1
# main_anova_summary <- aov_ez("Subj_ID", "meanleff", data.aggr,within =c("condition","name","threshold"))
# EMM <- emmeans(main_anova_summary, ~condition | name, at=list(threshold="X1")) %>% contrast(interaction = c( "consec", "consec"))
# EMM.unadj <- summary(EMM, by = NULL, adjust = "none") %>% as.data.frame() %>% dplyr::rename(.,p.value.unadj = p.value)
# EMM.fdr <- summary(EMM, by = NULL, adjust = "fdr") %>% as.data.frame() %>% dplyr::rename(.,p.value.fdr = p.value)
# 
# # corrected and uncorrected
# DT::datatable(left_join(EMM.unadj,EMM.fdr))


# data.aggr %>% ungroup() %>%
#   nest_by(threshold,name) %>%
#   mutate(Model = list(nice(aov_ez("Subj_ID", "meanleff", data, within ="condition")))) %>% select(-data) %>% unnest(Model) -> anova_summary
# 
# DT::datatable(anova_summary)

Grouped by scale, condition, subject, age_cat and network:yeo

data %>% pivot_longer(
    cols = starts_with("ROI"),
    names_to = "ROI",
    values_to = "leff") %>% left_join(.,network.combined) %>% left_join(.,variables_ext) %>% group_by(threshold,Subj_ID,condition,name,Age_cat) %>% summarise(meanleff = mean(leff)) -> data.aggr

# regular anova
main_anova_summary <- aov_ez("Subj_ID", "meanleff", data.aggr,within =c("condition","name","threshold"), between = "Age_cat")

emmip(main_anova_summary, condition~Age_cat | name)

DT::datatable(nice(main_anova_summary))
# anova averaging across threshold
main_anova_summary <- aov_ez("Subj_ID", "meanleff", data.aggr,within =c("condition","name"),between = "Age_cat")

DT::datatable(nice(main_anova_summary) )
# looking at threshold = 1
main_anova_summary <- aov_ez("Subj_ID", "meanleff", data.aggr,within =c("condition","name","threshold"),between = "Age_cat")

EMM <- emmeans(main_anova_summary, ~condition:Age_cat | name, at=list(threshold="X1")) %>% contrast(interaction = c( "consec", "consec"))
EMM.unadj <- summary(EMM, by = NULL, adjust = "none") %>% as.data.frame() %>% dplyr::rename(.,p.value.unadj = p.value)
EMM.fdr <- summary(EMM, by = NULL, adjust = "fdr") %>% as.data.frame() %>% dplyr::rename(.,p.value.fdr = p.value)

# corrected and uncorrected
DT::datatable(left_join(EMM.unadj,EMM.fdr))
# data.aggr %>% ungroup() %>%
#   nest_by(threshold,name) %>%
#   mutate(Model = list(nice(aov_ez("Subj_ID", "meanleff", data, within ="condition",between="Age_cat")))) %>% select(-data) %>% unnest(Model) -> anova_summary
# 
# DT::datatable(anova_summary)

Summary