Setup

Load libraries

library(readr)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ purrr     1.0.2
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(readxl)
library(mice)
## 
## Attaching package: 'mice'
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following objects are masked from 'package:base':
## 
##     cbind, rbind

Load data

MergeFlankerDec8 <- read_excel("C:/Users/John Majoubi/Downloads/MergeFlankerDec8.xlsx")
All_Jan2_April4 <- read_csv("~/All_Jan2_April4.csv")
## New names:
## Rows: 354 Columns: 236
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," chr
## (16): StartDate, EndDate, IPAddress, SONA/SFSU ID...11, SONA/SFSU ID...... dbl
## (220): Index, Progress, Duration__in_seconds, Finished, LocationLatitude...
## ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
## Specify the column types or set `show_col_types = FALSE` to quiet this message.
## • `SONA/SFSU ID` -> `SONA/SFSU ID...11`
## • `SONA/SFSU ID` -> `SONA/SFSU ID...12`
MergeSimonDec8th <- read_excel("C:/Users/John Majoubi/Downloads/MergeSimonDec8th.xlsx")
MergeStroopDec8Full <- read_excel("C:/Users/John Majoubi/Downloads/MergeStroopDec8Full.xlsx")

Data Processing

MergeFlankerDec8_c <- MergeFlankerDec8 %>% 
  dplyr::select(Subject, Session, Block, 9:13, 29:32, 80:92) 
MergeSimonDec8th_c <- MergeSimonDec8th %>% 
  dplyr::select(Subject, Session, Block, 9:13, 29:32, 80:92)
MergeStroopDec8Full_c <- MergeStroopDec8Full %>% 
  dplyr::select(Subject, Session, Block, 9:13, 29:32, 80:92)
All_Jan2_April4_c <- All_Jan2_April4 %>% 
  dplyr::select(`SONA/SFSU ID...11`, `SONA/SFSU ID...12`, SFSUID, 58:233, GPA) %>% 
  mutate(`SONA/SFSU ID...11` = as.numeric(`SONA/SFSU ID...11`),
         `SONA/SFSU ID...12` = as.numeric(`SONA/SFSU ID...12`),
         SFSUID = as.numeric(SFSUID)) %>% 
  mutate(`SONA/SFSU ID...12` = ifelse(is.na(`SONA/SFSU ID...12`), SFSUID, `SONA/SFSU ID...12`))
## Warning: There were 3 warnings in `mutate()`.
## The first warning was:
## ℹ In argument: `SONA/SFSU ID...11 = as.numeric(`SONA/SFSU ID...11`)`.
## Caused by warning:
## ! NAs introduced by coercion
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 2 remaining warnings.
library(readr)
library(tidyverse)
library(readxl)
library(mice)
impute_missing_values <- function(df) {
  numeric_cols <- sapply(df, is.numeric)
  na_columns <- colnames(df)[numeric_cols & colSums(is.na(df)) > 0]
  df[na_columns] <- lapply(df[na_columns], function(x) replace(x, is.na(x), mean(x, na.rm = TRUE)))
  return(df)
}

# Apply the function to your datasets
MergeFlankerDec8_c <- impute_missing_values(MergeFlankerDec8_c)
MergeSimonDec8th_c <- impute_missing_values(MergeSimonDec8th_c)
MergeStroopDec8Full_c <- impute_missing_values(MergeStroopDec8Full_c)
All_Jan2_April4_c <- impute_missing_values(All_Jan2_April4_c)

Flanker

Data Joining

  • only 198 IDs matched
joined_data_F <- MergeFlankerDec8_c %>%
  inner_join(
    All_Jan2_April4_c,
    by = c("Subject" = "SONA/SFSU ID...12")
  )
## Warning in inner_join(., All_Jan2_April4_c, by = c(Subject = "SONA/SFSU ID...12")): Detected an unexpected many-to-many relationship between `x` and `y`.
## ℹ Row 2775 of `x` matches multiple rows in `y`.
## ℹ Row 254 of `y` matches multiple rows in `x`.
## ℹ If a many-to-many relationship is expected, set `relationship =
##   "many-to-many"` to silence this warning.
joined_data_F %>% 
  dim()
## [1] 8351  204

Simon

Data Joining

  • 200 IDs matched
joined_data_S <- MergeSimonDec8th_c %>% 
  inner_join(
    All_Jan2_April4_c,
    by = c("Subject" = "SONA/SFSU ID...12")
  )
## Warning in inner_join(., All_Jan2_April4_c, by = c(Subject = "SONA/SFSU ID...12")): Detected an unexpected many-to-many relationship between `x` and `y`.
## ℹ Row 13657 of `x` matches multiple rows in `y`.
## ℹ Row 28 of `y` matches multiple rows in `x`.
## ℹ If a many-to-many relationship is expected, set `relationship =
##   "many-to-many"` to silence this warning.
joined_data_S %>% 
  dim()
## [1] 17550   204

Stroop

Data Joining

  • 199 IDs matched
joined_data_St <- MergeStroopDec8Full_c %>% 
  inner_join(
    All_Jan2_April4_c,
    by = c("Subject" = "SONA/SFSU ID...12")
  )
## Warning in inner_join(., All_Jan2_April4_c, by = c(Subject = "SONA/SFSU ID...12")): Detected an unexpected many-to-many relationship between `x` and `y`.
## ℹ Row 8581 of `x` matches multiple rows in `y`.
## ℹ Row 28 of `y` matches multiple rows in `x`.
## ℹ If a many-to-many relationship is expected, set `relationship =
##   "many-to-many"` to silence this warning.
joined_data_St %>% 
  dim()
## [1] 11253   204
# Load necessary libraries
library(dplyr)
library(tidyr)
library(readr)

# Convert Task.RT to numeric and remove zero values
joined_data_F <- joined_data_F %>%
  mutate(Task.RT = as.numeric(as.character(Task.RT))) %>%  # Ensure Task.RT is numeric
  dplyr::filter(Task.RT > 00, FinalScore > 0)  # Remove rows where Task.RT is zero
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `Task.RT = as.numeric(as.character(Task.RT))`.
## Caused by warning:
## ! NAs introduced by coercion
# Proceed with previous steps if necessary to recheck data types or handle NAs
sum(is.na(joined_data_S$Task.RT))  # Check for NAs introduced by conversion
## [1] 0
# Convert Task.RT to numeric and remove zero values
joined_data_S <- joined_data_S %>%
  mutate(Task.RT = as.numeric(as.character(Task.RT))) %>%  # Ensure Task.RT is numeric
  dplyr::filter(Task.RT > 0, FinalScore > 0)  # Remove rows where Task.RT is zero
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `Task.RT = as.numeric(as.character(Task.RT))`.
## Caused by warning:
## ! NAs introduced by coercion
# Proceed with previous steps if necessary to recheck data types or handle NAs
sum(is.na(joined_data_S$Task.RT))  # Check for NAs introduced by conversio
## [1] 0
joined_data_F %>% distinct(Subject)
## # A tibble: 177 × 1
##    Subject
##      <dbl>
##  1   94558
##  2   94789
##  3  104065
##  4  102454
##  5  104068
##  6  104161
##  7  103141
##  8  104323
##  9  103669
## 10  104320
## # ℹ 167 more rows
joined_data_S %>% distinct(Subject)
## # A tibble: 200 × 1
##    Subject
##      <dbl>
##  1   89223
##  2  984514
##  3  659709
##  4  955615
##  5  793920
##  6   83029
##  7  765061
##  8  715947
##  9  486783
## 10  675062
## # ℹ 190 more rows

Quick EDA

joined_data_F %>% 
  select(Subject, Task.RT, FinalScore) %>% 
  mutate(Task.RT = as.numeric(Task.RT)) %>% 
  group_by(Subject) %>% 
  summarise(Task.RT_Mean = mean(Task.RT, na.rm = TRUE),
            FinalScore_Mean = mean(FinalScore, na.rm = TRUE)) %>% 
  ggplot(aes(x = Task.RT_Mean, y = FinalScore_Mean)) +
  geom_point() +
  geom_smooth()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

library(DataExplorer)

joined_data_F %>% 
  select(Subject, Task.RT, FinalScore) %>% 
  mutate(Task.RT = as.numeric(Task.RT)) %>% 
  group_by(Subject) %>% 
  summarise(Task.RT_Mean = mean(Task.RT, na.rm = TRUE),
            FinalScore_Mean = mean(FinalScore, na.rm = TRUE)) %>% 
  plot_correlation()

Mean RT Scores

RT_Scores <- joined_data_F %>% 
  select(Subject, Task.RT, FinalScore, TrialType) %>% 
  mutate(Task.RT = as.numeric(Task.RT)) %>% 
  group_by(Subject) %>% 
  summarise(Task.RT_Mean = mean(Task.RT, na.rm = TRUE),
            Task.RT_FI = mean(Task.RT[TrialType == "FullyIncongruent"], na.rm = TRUE),
            Task.RT_SFI = mean(Task.RT[TrialType %in% c("FullyIncongruent", "StimCongruentRespIncongruent")], na.rm = TRUE),
            Task.RT_SI = mean(Task.RT[TrialType %in% c("StimCongruentRespIncongruent")], na.rm = TRUE),
            Task.RT_FC = mean(Task.RT[TrialType %in% c("FullyCongruent")], na.rm = TRUE),
            FinalScore_Mean = mean(FinalScore, na.rm = TRUE))  %>% 
  ungroup()
AllJan_scores <- joined_data_F %>% 
  dplyr::select(Subject, `Simon SQ`, `Flanker SQ`, `Stroop SQ`, Anti1MeanRT, Anti2MeanRT, SwitchMean, SwitchCostRT, MixingCostRT, MixingCostPC, NeutralPC, SwitchCostPC, Anti1PC, Anti2PC, RavenSum, RepeatPC, BSCmean, PerseveranceMean, ACSmean, PremedMean, UrgencyMeanR, RavenSum, MentalHealthMean, HappinessMean, SelfEsteemMean, GPA) %>% 
  pivot_longer(!Subject) %>% 
  group_by(Subject, name) %>% 
  summarise(Mean = mean(value, na.rm = TRUE)) %>% 
  ungroup() %>% 
  pivot_wider(names_from = name, values_from = Mean)
## `summarise()` has grouped output by 'Subject'. You can override using the
## `.groups` argument.

#outlier cleaning

# Convert Task.RT to numeric
# This step also checks for non-numeric characters which will be turned into NA
joined_data_F$Task.RT <- as.numeric(joined_data_F$Task.RT)

# Check if there were any non-numeric values that got converted to NA
sum(is.na(joined_data_F$Task.RT))
## [1] 0
remove_outliers_iqr <- function(data, cols, multiplier = 1.5) {
  for (col in cols) {
    if (col %in% names(data) && is.numeric(data[[col]])) {
      Q1 <- quantile(data[[col]], 0.25, na.rm = TRUE)
      Q3 <- quantile(data[[col]], 0.75, na.rm = TRUE)
      IQR <- Q3 - Q1
      lower_bound <- Q1 - multiplier * IQR
      upper_bound <- Q3 + multiplier * IQR
      data <- data[data[[col]] >= lower_bound & data[[col]] <= upper_bound, ]
    }
  }
  return(data)
}
joined_data_F %>% distinct(Subject)
## # A tibble: 177 × 1
##    Subject
##      <dbl>
##  1   94558
##  2   94789
##  3  104065
##  4  102454
##  5  104068
##  6  104161
##  7  103141
##  8  104323
##  9  103669
## 10  104320
## # ℹ 167 more rows
#Assuming remove_outliers_iqr function is defined as above and handles only numeric columns
numeric_columns <- c("Task.RT", "FinalScore", "Anti1MeanRT", "Anti2MeanRT", "SwitchCostRT", "MixingCostRT")
joined_data_F <- remove_outliers_iqr(joined_data_F, numeric_columns)
library(ggplot2)
ggplot(joined_data_F, aes(x = Task.RT)) +
geom_histogram(bins = 30, fill = 'blue') +
ggtitle("Distribution of Task.RT After Cleaning and Outlier Removal")

LMM

# Filter out only the subjects and trial types you are interested in
tt <- joined_data_F %>%
  dplyr::filter(Subject %in% c("101245", "793920", "102151", "93940", "102037")) %>%
  dplyr::filter(!is.na(Task.RT)) %>%
  droplevels() # This function drops unused levels from factors

# Check to ensure that you have data for each trial type and subject combination
table(tt$TrialType, tt$Subject)
##                               
##                                93940 101245 102037 793920
##   FullyCongruent                  14     20     15     16
##   FullyIncongruent                15     20     14     15
##   StimCongruentRespIncongruent    15     20     15     16
##   StimIncongruentRespCongruent    15     20     14     16
# Convert TrialType to a factor if not already
joined_data_F$TrialType <- as.factor(joined_data_F$TrialType)

# Print out the levels to confirm they are correct
print(levels(joined_data_F$TrialType))
## [1] "FullyCongruent"               "FullyIncongruent"            
## [3] "StimCongruentRespIncongruent" "StimIncongruentRespCongruent"
library(lme4)
## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
library(lmerTest)
## 
## Attaching package: 'lmerTest'
## The following object is masked from 'package:lme4':
## 
##     lmer
## The following object is masked from 'package:stats':
## 
##     step
#requesting the model on these data with 2 random intercepts:
model = lme4::lmer(as.numeric(Task.RT) ~ TrialType + (1|Subject), data = joined_data_F) 
summary(model)
## Linear mixed model fit by REML ['lmerMod']
## Formula: as.numeric(Task.RT) ~ TrialType + (1 | Subject)
##    Data: joined_data_F
## 
## REML criterion at convergence: 70601.4
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -3.0930 -0.6470 -0.1696  0.4536  4.3032 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  Subject  (Intercept) 356429   597.0   
##  Residual             363774   603.1   
## Number of obs: 4487, groups:  Subject, 131
## 
## Fixed effects:
##                                       Estimate Std. Error t value
## (Intercept)                            1957.88      55.30   35.40
## TrialTypeFullyIncongruent               487.28      25.63   19.01
## TrialTypeStimCongruentRespIncongruent   337.13      25.48   13.23
## TrialTypeStimIncongruentRespCongruent   308.95      25.24   12.24
## 
## Correlation of Fixed Effects:
##             (Intr) TrlTFI TTSCRI
## TrlTypFllyI -0.224              
## TrlTypStCRI -0.227  0.491       
## TrlTypStIRC -0.230  0.495  0.498
library(performance)
icc(model)
## # Intraclass Correlation Coefficient
## 
##     Adjusted ICC: 0.495
##   Unadjusted ICC: 0.474
which(cooks.distance(model) >= 1, arr.ind = TRUE)
## named integer(0)

Convert TrialType to a factor if not already

# Convert TrialType to a factor if not already
joined_data_F$TrialType <- as.factor(joined_data_F$TrialType)

# Check the levels of TrialType
levels(joined_data_F$TrialType)
## [1] "FullyCongruent"               "FullyIncongruent"            
## [3] "StimCongruentRespIncongruent" "StimIncongruentRespCongruent"

DIFFERENCE SCORES (TRADITIONAL)

Calculate the mean for combined trial types and for FullyCongruent separately

# Calculate the mean for combined trial types and for FullyCongruent separately
combined_scores <- joined_data_F %>%
  group_by(Subject) %>%
  summarise(
    FullyIncongruent = mean(Task.RT[TrialType == "FullyIncongruent"], na.rm = TRUE),
    StimCongruentRespIncongruent = mean(Task.RT[TrialType == "StimCongruentRespIncongruent"], na.rm = TRUE),
    StimIncongruentRespCongruent = mean(Task.RT[TrialType == "StimIncongruentRespCongruent"], na.rm = TRUE),
    FullyCongruent = mean(Task.RT[TrialType == "FullyCongruent"], na.rm = TRUE)
  ) %>%
  mutate(
    Combined_Mean = (FullyIncongruent + StimCongruentRespIncongruent + StimIncongruentRespCongruent) / 3,
    Difference = Combined_Mean - FullyCongruent
  ) %>%
  left_join(select(joined_data_F, Subject, `Simon SQ`, ACSmean, Anti1MeanRT, BSCmean, UrgencyMeanR, PerseveranceMean, MentalHealthMean, PremedMean), by = "Subject") %>%
  distinct()

# Remove Difference scores below zero
combined_scores <- combined_scores %>%
  dplyr::filter(Difference > 0)

# Calculate correlations
correlation_SimonSQ <- cor(combined_scores$Difference, combined_scores$`Simon SQ`, use = "complete.obs")
correlation_ACSmean <- cor(combined_scores$Difference, combined_scores$ACSmean, use = "complete.obs")
correlation_Anti1MeanRT <- cor(combined_scores$Difference, combined_scores$Anti1MeanRT, use = "complete.obs")
correlation_BSCmean <- cor(combined_scores$Difference, combined_scores$BSCmean, use = "complete.obs")
correlation_UrgencyMeanR <- cor(combined_scores$Difference, combined_scores$UrgencyMeanR, use = "complete.obs")
correlation_PerseveranceMean <- cor(combined_scores$Difference, combined_scores$PerseveranceMean, use = "complete.obs")
correlation_MentalHealthMean <- cor(combined_scores$Difference, combined_scores$MentalHealthMean, use = "complete.obs")
correlation_PremedMean <- cor(combined_scores$Difference, combined_scores$PremedMean, use = "complete.obs")

# Print correlations
print(correlation_SimonSQ)
## [1] -0.3568693
print(correlation_ACSmean)
## [1] -0.2380533
print(correlation_Anti1MeanRT)
## [1] 0.214659
print(correlation_BSCmean)
## [1] -0.1446599
print(correlation_UrgencyMeanR)
## [1] -0.09240762
print(correlation_PerseveranceMean)
## [1] -0.04120186
print(correlation_PremedMean)
## [1] -0.1184899
# Load necessary libraries
library(dplyr)
library(tidyr)
library(ggplot2)
library(ggcorrplot)
## Warning: package 'ggcorrplot' was built under R version 4.4.1
# Calculate the mean for combined trial types and for FullyCongruent separately
combined_scores <- joined_data_F %>%
  group_by(Subject) %>%
  summarise(
    FullyIncongruent = mean(Task.RT[TrialType == "FullyIncongruent"], na.rm = TRUE),
    StimCongruentRespIncongruent = mean(Task.RT[TrialType == "StimCongruentRespIncongruent"], na.rm = TRUE),
    StimIncongruentRespCongruent = mean(Task.RT[TrialType == "StimIncongruentRespCongruent"], na.rm = TRUE),
    FullyCongruent = mean(Task.RT[TrialType == "FullyCongruent"], na.rm = TRUE)
  ) %>%
  mutate(
    Combined_Mean = (FullyIncongruent + StimCongruentRespIncongruent + StimIncongruentRespCongruent) / 3,
    Difference = Combined_Mean - FullyCongruent
  ) %>%
  left_join(select(joined_data_F, Subject, `Simon SQ`, ACSmean, Anti1MeanRT, BSCmean, UrgencyMeanR, PerseveranceMean, MentalHealthMean, PremedMean), by = "Subject") %>%
  distinct()

# Remove Difference scores below zero
combined_scores <- combined_scores %>%
  dplyr::filter(Difference > 0)

# Create a correlation matrix
cor_matrix <- combined_scores %>%
  select(Difference, ACSmean,  BSCmean, UrgencyMeanR, PerseveranceMean,  PremedMean) %>%
  cor(use = "complete.obs")

# Print correlation matrix
print(cor_matrix)
##                   Difference     ACSmean    BSCmean UrgencyMeanR
## Difference        1.00000000 -0.23805327 -0.1446599  -0.09240762
## ACSmean          -0.23805327  1.00000000  0.5466952   0.49423934
## BSCmean          -0.14465986  0.54669524  1.0000000   0.59009516
## UrgencyMeanR     -0.09240762  0.49423934  0.5900952   1.00000000
## PerseveranceMean -0.04120186  0.42592951  0.5838358   0.25500439
## PremedMean       -0.11848995  0.08072497  0.2574365   0.16169311
##                  PerseveranceMean  PremedMean
## Difference            -0.04120186 -0.11848995
## ACSmean                0.42592951  0.08072497
## BSCmean                0.58383582  0.25743648
## UrgencyMeanR           0.25500439  0.16169311
## PerseveranceMean       1.00000000  0.26455800
## PremedMean             0.26455800  1.00000000
# Create heatmap with correlations written in the boxes, with reversed color gradient
ggcorrplot(cor_matrix, method = "circle", type = "lower", lab = TRUE, lab_size = 3, 
           colors = c("blue", "white", "red"), title = "Correlation Matrix Heatmap", 
           ggtheme = theme_minimal())

# Alternatively, using ggplot2 for a more customized plot with reversed color gradient
cor_matrix_melt <- as.data.frame(as.table(cor_matrix))

ggplot(cor_matrix_melt, aes(Var1, Var2, fill = Freq)) +
  geom_tile(color = "white") +
  scale_fill_gradient2(low = "blue", high = "red", mid = "white", midpoint = 0, limit = c(-1, 1), space = "Lab", name = "Correlation") +
  geom_text(aes(Var1, Var2, label = round(Freq, 2)), color = "black", size = 4) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, vjust = 1, size = 12, hjust = 1)) +
  coord_fixed() +
  labs(title = "Correlation Matrix Heatmap", x = "", y = "")

Calculate the difference for each N-1 FullyCongruent trial

# Load necessary libraries
library(dplyr)
library(tidyr)
library(readr)
library(ggplot2)
library(reshape2)
## 
## Attaching package: 'reshape2'
## The following object is masked from 'package:tidyr':
## 
##     smiths
library(ggpubr)





# Ensure Task.RT and other necessary variables are numeric
joined_data_F <- joined_data_F %>%
  mutate(Task.RT = as.numeric(as.character(Task.RT)),
         ACSmean = as.numeric(as.character(ACSmean)),
         BSCmean = as.numeric(as.character(BSCmean)),
         PerseveranceMean = as.numeric(as.character(PerseveranceMean)),
         PremedMean = as.numeric(as.character(PremedMean)),
         UrgencyMeanR = as.numeric(as.character(UrgencyMeanR)),
         `Flanker SQ` = as.numeric(as.character(`Flanker SQ`)),
         `Simon SQ` = as.numeric(as.character(`Simon SQ`)),
         `Stroop SQ` = as.numeric(as.character(`Stroop SQ`)),
         Anti1MeanRT = as.numeric(as.character(Anti1MeanRT)),
         SwitchCostRT = as.numeric(as.character(SwitchCostRT)))

# Calculate the difference score correctly
N_1FullyCongruent <- joined_data_F %>%
  mutate(Next_Task_RT = dplyr::lead(Task.RT),  # Create a lead column for Task.RT
         Next_TrialType = dplyr::lead(TrialType)) %>%  # Create a lead column for TrialType as well
  dplyr::filter(TrialType == "FullyCongruent",  # Keep only FullyCongruent trials
                Next_TrialType != "FullyCongruent") %>%  # Exclude cases where the next trial is also FullyCongruent
  mutate(Difference_Score = Next_Task_RT - Task.RT)  # Calculate the difference as (next - current)

# View the result
print(N_1FullyCongruent)
## # A tibble: 1,118 × 207
##    Subject Session Block FinalScore FullyCongruentFinalScore
##      <dbl>   <dbl> <dbl>      <dbl>                    <dbl>
##  1   94558       1    14         33                        9
##  2   94558       1    17         33                        9
##  3   94558       1    24         33                        9
##  4   94558       1    27         33                        9
##  5   94558       1    30         33                        9
##  6   94558       1    34         33                        9
##  7   94558       1    38         33                        9
##  8   94558       1    42         33                        9
##  9   94558       1    45         33                        9
## 10   94789       1    10         28                        8
## # ℹ 1,108 more rows
## # ℹ 202 more variables: FullyCongruentTrialsCount <dbl>,
## #   FullyIncongruentFinalScore <dbl>, FullyIncongruentTrialsCount <dbl>,
## #   StimCongruentRespIncongruentFinalScore <dbl>,
## #   StimCongruentRespIncongruentTrialsCount <dbl>,
## #   StimIncongruentRespCongruentFinalScore <dbl>,
## #   StimIncongruentRespCongruentTrialsCount <dbl>, Task.DurationError <chr>, …
# Optionally, if you need to merge this back to the original data or perform further analysis:
joined_data_F <- joined_data_F %>%
  left_join(N_1FullyCongruent %>% select(Subject, Difference_Score), by = "Subject")
## Warning in left_join(., N_1FullyCongruent %>% select(Subject, Difference_Score), : Detected an unexpected many-to-many relationship between `x` and `y`.
## ℹ Row 1 of `x` matches multiple rows in `y`.
## ℹ Row 1 of `y` matches multiple rows in `x`.
## ℹ If a many-to-many relationship is expected, set `relationship =
##   "many-to-many"` to silence this warning.
# Check the updated dataset
print(head(joined_data_F))
## # A tibble: 6 × 205
##   Subject Session Block FinalScore FullyCongruentFinalS…¹ FullyCongruentTrials…²
##     <dbl>   <dbl> <dbl>      <dbl>                  <dbl>                  <dbl>
## 1   94558       1    13         33                      9                      9
## 2   94558       1    13         33                      9                      9
## 3   94558       1    13         33                      9                      9
## 4   94558       1    13         33                      9                      9
## 5   94558       1    13         33                      9                      9
## 6   94558       1    13         33                      9                      9
## # ℹ abbreviated names: ¹​FullyCongruentFinalScore, ²​FullyCongruentTrialsCount
## # ℹ 199 more variables: FullyIncongruentFinalScore <dbl>,
## #   FullyIncongruentTrialsCount <dbl>,
## #   StimCongruentRespIncongruentFinalScore <dbl>,
## #   StimCongruentRespIncongruentTrialsCount <dbl>,
## #   StimIncongruentRespCongruentFinalScore <dbl>,
## #   StimIncongruentRespCongruentTrialsCount <dbl>, Task.DurationError <chr>, …
# Calculate the average Difference_Score for each subject
N_1FullyCongruent_avg_Score <- N_1FullyCongruent %>%
  group_by(Subject) %>%
  summarise(N_1FullyCongruent_avg_Score = mean(Difference_Score, na.rm = TRUE))

# Merge the average difference scores with ACSmean, BSCmean, PerseveranceMean, PremedMean, UrgencyMeanR, and Flanker SQ
merged_data <- joined_data_F %>%
  select(Subject, ACSmean, BSCmean, PerseveranceMean, PremedMean, UrgencyMeanR, `Flanker SQ`) %>%
  distinct() %>%
  left_join(N_1FullyCongruent_avg_Score, by = "Subject")

# Create a correlation matrix
cor_matrix <- merged_data %>%
  select(N_1FullyCongruent_avg_Score, ACSmean, BSCmean, PerseveranceMean, PremedMean, UrgencyMeanR, `Flanker SQ`) %>%
  cor(use = "complete.obs")

# Melt the correlation matrix
melted_cor_matrix <- melt(cor_matrix)

# Plot the correlation matrix heatmap
ggplot(data = melted_cor_matrix, aes(x = Var1, y = Var2, fill = value)) +
  geom_tile(color = "white") +
  scale_fill_gradient2(low = "blue", high = "red", mid = "white", 
                       midpoint = 0, limit = c(-1, 1), space = "Lab", 
                       name = "Correlation") +
  theme_minimal() + 
  theme(axis.text.x = element_text(angle = 45, vjust = 1, 
                                   size = 12, hjust = 1)) +
  coord_fixed() +
  geom_text(aes(label = round(value, 2)), color = "black", size = 4) +
  labs(title = "Correlation Matrix Heatmap")

# Merge the average difference scores with the selected variables
merged_data <- joined_data_F %>%
  select(Subject, `Simon SQ`, `Flanker SQ`, `Stroop SQ`, Anti1MeanRT, SwitchCostRT) %>%
  distinct() %>%
  left_join(N_1FullyCongruent_avg_Score, by = "Subject")
# Create a correlation matrix
cor_matrix <- merged_data %>%
  select(N_1FullyCongruent_avg_Score, `Simon SQ`, `Flanker SQ`, `Stroop SQ`, Anti1MeanRT, SwitchCostRT) %>%
  cor(use = "complete.obs")

# Melt the correlation matrix
library(reshape2)
melted_cor_matrix <- melt(cor_matrix)

# Filter out negative scores from Simon SQ, Flanker SQ, and Stroop SQ
joined_data_F <- joined_data_F %>%
  dplyr::filter(`Simon SQ` >= 0, `Flanker SQ` >= 0, `Stroop SQ` >= 0)

# Print dimensions to see how many rows remain after filtering
#print(dim(joined_data_F))
joined_data_F %>% distinct(Subject)
## # A tibble: 130 × 1
##    Subject
##      <dbl>
##  1   94558
##  2   94789
##  3  104065
##  4  102454
##  5  104068
##  6  104161
##  7  103141
##  8  104323
##  9  103669
## 10   99631
## # ℹ 120 more rows
# Plot the correlation matrix heatmap
library(ggplot2)
ggplot(data = melted_cor_matrix, aes(x = Var1, y = Var2, fill = value)) +
  geom_tile(color = "white") +
  scale_fill_gradient2(low = "blue", high = "red", mid = "white", 
                       midpoint = 0, limit = c(-1, 1), space = "Lab", 
                       name = "Correlation") +
  theme_minimal() + 
  theme(axis.text.x = element_text(angle = 45, vjust = 1, 
                                   size = 12, hjust = 1)) +
  coord_fixed() +
  geom_text(aes(label = round(value, 2)), color = "black", size = 4) +
  labs(title = "Correlation Matrix Heatmap")

N+1 Fully Congruent

Merge the average difference scores with ACSmean, BSCmean, PerseveranceMean, PremedMean, and UrgencyMeanR

# Load necessary libraries
library(dplyr)
library(tidyr)
library(readr)
library(ggplot2)
library(reshape2)
library(ggpubr)

# Ensure Task.RT is numeric
joined_data_F <- joined_data_F %>%
  mutate(Task.RT = as.numeric(as.character(Task.RT)),
         ACSmean = as.numeric(as.character(ACSmean)),
         BSCmean = as.numeric(as.character(BSCmean)),
         PerseveranceMean = as.numeric(as.character(PerseveranceMean)),
         PremedMean = as.numeric(as.character(PremedMean)),
         UrgencyMeanR = as.numeric(as.character(UrgencyMeanR)),
         `Flanker SQ` = as.numeric(as.character(`Flanker SQ`)))

# Calculate the difference score correctly
difference_scores <- joined_data_F %>%
  mutate(Prev_Task_RT = dplyr::lag(Task.RT),  # Create a lag column for Task.RT
         Prev_TrialType = dplyr::lag(TrialType)) %>%  # Create a lag column for TrialType as well
  dplyr::filter(TrialType == "FullyCongruent",  # Keep only FullyCongruent trials
         Prev_TrialType != "FullyCongruent") %>%  # Exclude cases where the previous trial is also FullyCongruent
  mutate(Difference_Score = Prev_Task_RT - Task.RT)  # Calculate the difference as (prev - current)

# View the result
print(difference_scores)
## # A tibble: 1,114 × 207
##    Subject Session Block FinalScore FullyCongruentFinalScore
##      <dbl>   <dbl> <dbl>      <dbl>                    <dbl>
##  1   94558       1    14         33                        9
##  2   94558       1    17         33                        9
##  3   94558       1    24         33                        9
##  4   94558       1    27         33                        9
##  5   94558       1    30         33                        9
##  6   94558       1    34         33                        9
##  7   94558       1    38         33                        9
##  8   94558       1    42         33                        9
##  9   94558       1    45         33                        9
## 10   94789       1    10         28                        8
## # ℹ 1,104 more rows
## # ℹ 202 more variables: FullyCongruentTrialsCount <dbl>,
## #   FullyIncongruentFinalScore <dbl>, FullyIncongruentTrialsCount <dbl>,
## #   StimCongruentRespIncongruentFinalScore <dbl>,
## #   StimCongruentRespIncongruentTrialsCount <dbl>,
## #   StimIncongruentRespCongruentFinalScore <dbl>,
## #   StimIncongruentRespCongruentTrialsCount <dbl>, Task.DurationError <chr>, …
# Optionally, if you need to merge this back to the original data or perform further analysis:
joined_data_F <- joined_data_F %>%
  left_join(difference_scores %>% select(Subject, Difference_Score), by = "Subject")
## Warning in left_join(., difference_scores %>% select(Subject, Difference_Score), : Detected an unexpected many-to-many relationship between `x` and `y`.
## ℹ Row 1 of `x` matches multiple rows in `y`.
## ℹ Row 1 of `y` matches multiple rows in `x`.
## ℹ If a many-to-many relationship is expected, set `relationship =
##   "many-to-many"` to silence this warning.
# Check the updated dataset
print(head(joined_data_F))
## # A tibble: 6 × 206
##   Subject Session Block FinalScore FullyCongruentFinalS…¹ FullyCongruentTrials…²
##     <dbl>   <dbl> <dbl>      <dbl>                  <dbl>                  <dbl>
## 1   94558       1    13         33                      9                      9
## 2   94558       1    13         33                      9                      9
## 3   94558       1    13         33                      9                      9
## 4   94558       1    13         33                      9                      9
## 5   94558       1    13         33                      9                      9
## 6   94558       1    13         33                      9                      9
## # ℹ abbreviated names: ¹​FullyCongruentFinalScore, ²​FullyCongruentTrialsCount
## # ℹ 200 more variables: FullyIncongruentFinalScore <dbl>,
## #   FullyIncongruentTrialsCount <dbl>,
## #   StimCongruentRespIncongruentFinalScore <dbl>,
## #   StimCongruentRespIncongruentTrialsCount <dbl>,
## #   StimIncongruentRespCongruentFinalScore <dbl>,
## #   StimIncongruentRespCongruentTrialsCount <dbl>, Task.DurationError <chr>, …
# Quick visualization of the difference scores
ggplot(difference_scores, aes(x = Difference_Score)) +
  geom_histogram(binwidth = 10, fill = "blue", color = "black") +
  labs(title = "Distribution of N-1 Congruent Difference Scores",
       x = "Difference Score (Previous Trial RT - FullyCongruent Trial RT)",
       y = "Frequency")

# Check for non-finite values
difference_scores %>%
  dplyr::filter(!is.finite(Difference_Score)) %>%
  summarise(Count_NA = sum(is.na(Difference_Score)),
            Count_Inf = sum(is.infinite(Difference_Score)))
## # A tibble: 1 × 2
##   Count_NA Count_Inf
##      <int>     <int>
## 1        0         0
# Optionally, decide to remove non-finite values if they are not critical
difference_scores <- difference_scores %>%
  dplyr::filter(is.finite(Difference_Score))

# Re-check the histogram
ggplot(difference_scores, aes(x = Difference_Score)) +
  geom_histogram(binwidth = 10, fill = "blue", color = "black") +
  labs(title = "Distribution of N-1 Congruent Difference Scores",
       x = "Difference Score (Previous Trial RT - FullyCongruent Trial RT)",
       y = "Frequency")

# Calculate the average Difference_Score for each subject
avg_difference_score <- difference_scores %>%
  group_by(Subject) %>%
  summarise(avg_Difference_Score = mean(Difference_Score, na.rm = TRUE))

# Merge the average difference scores with ACSmean, BSCmean, PerseveranceMean, PremedMean, UrgencyMeanR, and Flanker SQ
merged_data <- joined_data_F %>%
  select(Subject, ACSmean, BSCmean, PerseveranceMean, PremedMean, UrgencyMeanR, `Flanker SQ`) %>%
  distinct() %>%
  left_join(avg_difference_score, by = "Subject")

# Create a correlation matrix
cor_matrix <- merged_data %>%
  select(avg_Difference_Score, ACSmean, BSCmean, PerseveranceMean, PremedMean, UrgencyMeanR, `Flanker SQ`) %>%
  cor(use = "complete.obs")

# Melt the correlation matrix
melted_cor_matrix <- melt(cor_matrix)

# Plot the correlation matrix heatmap
ggplot(data = melted_cor_matrix, aes(x = Var1, y = Var2, fill = value)) +
  geom_tile(color = "white") +
  scale_fill_gradient2(low = "blue", high = "red", mid = "white", 
                       midpoint = 0, limit = c(-1, 1), space = "Lab", 
                       name = "Correlation") +
  theme_minimal() + 
  theme(axis.text.x = element_text(angle = 45, vjust = 1, 
                                   size = 12, hjust = 1)) +
  coord_fixed() +
  geom_text(aes(label = round(value, 2)), color = "black", size = 4) +
  labs(title = "Correlation Matrix Heatmap")

Merge the mean Global RT with self-ratings on Subject to align them for correlation calculation

# Load necessary libraries
library(dplyr)
library(ggplot2)
library(reshape2)

# Assuming joined_data_F is already loaded and cleaned

# Compute mean Task.RT for each individual
flanker_global_rt <- joined_data_F %>%
  group_by(Subject) %>%
  summarise(`Flanker Global RT` = mean(Task.RT, na.rm = TRUE))

# Select the relevant columns for self-ratings
self_ratings <- joined_data_F %>%
  select(Subject, ACSmean, BSCmean, PerseveranceMean, UrgencyMeanR, PremedMean) %>%
  distinct()

# Merge the mean Task.RT with self-ratings on Subject to align them for correlation calculation
merged_data <- inner_join(flanker_global_rt, self_ratings, by = "Subject")

# Calculate the correlation matrix
cor_matrix <- merged_data %>%
  select(`Flanker Global RT`, ACSmean, BSCmean, PerseveranceMean, UrgencyMeanR, PremedMean) %>%
  cor(use = "complete.obs")

# Print the correlation matrix
print(cor_matrix)
##                   Flanker Global RT     ACSmean     BSCmean PerseveranceMean
## Flanker Global RT        1.00000000 -0.11631257 -0.02193105       0.09976674
## ACSmean                 -0.11631257  1.00000000  0.52637588       0.40132826
## BSCmean                 -0.02193105  0.52637588  1.00000000       0.57541424
## PerseveranceMean         0.09976674  0.40132826  0.57541424       1.00000000
## UrgencyMeanR            -0.08980026  0.49852883  0.54203403       0.22593137
## PremedMean              -0.02822361  0.09988654  0.26197840       0.27950371
##                   UrgencyMeanR  PremedMean
## Flanker Global RT  -0.08980026 -0.02822361
## ACSmean             0.49852883  0.09988654
## BSCmean             0.54203403  0.26197840
## PerseveranceMean    0.22593137  0.27950371
## UrgencyMeanR        1.00000000  0.18233831
## PremedMean          0.18233831  1.00000000
# Melt the correlation matrix for visualization
cor_matrix_melt <- melt(cor_matrix)

# Create the heatmap
ggplot(cor_matrix_melt, aes(Var1, Var2, fill = value)) +
  geom_tile(color = "white") +
  scale_fill_gradient2(low = "blue", high = "red", mid = "white", midpoint = 0, limit = c(-1, 1), space = "Lab", name = "Correlation") +
  geom_text(aes(Var1, Var2, label = round(value, 2)), color = "black", size = 4) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, vjust = 1, size = 12, hjust = 1)) +
  coord_fixed() +
  labs(title = "Correlation Matrix Heatmap", x = "", y = "")