library(dplyr)
library(tidyr)
library(stats)
library(data.table)

data  <-  read.csv("E:/Research/ADprs_cognition/Data/Analyses_noDementia/above100.csv")
da <- setDT(data)

group <- da[, .(task = PCA_, n = .N, mean_PCA = mean(PCA_), sd_PCA = sd(PCA_)), by = .(age, sex)]

ages <- c()
p_values <- c()
t_values <- c()
n_female <- c()
n_male <- c()

for (a in unique(group$age)) {
  data_male <- c()
  data_female <- c()
  for (gender in unique(group$sex)) {
    dat <- group %>%
      filter(age == a, sex == gender) %>%
      select(task) %>%
      unlist()
    if (gender == 1) {data_male <- dat} 
    else if (gender == 0) {data_female <- dat}
  }
  result <- t.test(data_male, data_female)
  t_stat <- result$statistic
  p_value <- result$p.value
  
  # Add results to respective vectors
  ages <- c(ages, a)
  t_values <- c(t_values, t_stat)
  p_values <- c(p_values, p_value)
  n_female <- c(n_female, length(data_female))
  n_male <- c(n_male, length(data_male))
}
print(p_values)
log_trans <- c()
log_trans <- -log(p_values)
print(log_trans)