This script combines 5 blocks to 1 data set for each participant (change info in “Data set Configuration”). Script also prepare merging for each group (done in BEH2) and checks for validity effect per DON’T FORGET TO TAG REMOVED ARTIFACT-EPOCH IN DATASET CONF Look for “# Particpant ID” to configure participant details
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
# library(rstatix)
# library(flextable)
library(patchwork)
library(cowplot)
fpath <- paste0("C:/MATLAB/exp_1/results/BEH/", grp, "/", ID, '/')
if(file.exists(paste0(fpath, "block_1.csv"))) {
block_1 <- read_csv(paste0(fpath, "block_1.csv"))
}
if(file.exists(paste0(fpath, "block_2.csv"))) {
block_2 <- read_csv(paste0(fpath, "block_2.csv"))
}
if(file.exists(paste0(fpath, "block_3.csv"))) {
block_3 <- read_csv(paste0(fpath, "block_3.csv"))
}
if(file.exists(paste0(fpath, "block_4.csv"))) {
block_4 <- read_csv(paste0(fpath, "block_4.csv"))
}
if(file.exists(paste0(fpath, "block_5.csv"))) {
block_5 <- read_csv(paste0(fpath, "block_5.csv"))
}
# block_1 <- read_csv(paste0(fpath, "block_1.csv"))
# block_2 <- read_csv(paste0(fpath, "block_2.csv"))
# block_3 <- read_csv(paste0(fpath, "block_3.csv"))
# block_4 <- read_csv(paste0(fpath, "block_4.csv"))
# block_5 <- read_csv(paste0(fpath, "block_5.csv"))
# Merge each participant's blocks
if (exists("block_5") && !is.null(block_5)) {
rawData <- full_join(block_1, block_2) |>
full_join(block_3) |>
full_join(block_4) |>
full_join(block_5) |>
mutate(id = ID,
group = grp,
.before = 1)
} else {
rawData <- full_join(block_1, block_2) |>
full_join(block_3) |>
full_join(block_4) |>
mutate(id = ID,
group = grp,
.before = 1)
}
# # Alternative
# fileList <- c(paste0(fpath, "block_1.csv"),
# paste0(fpath, "block_2.csv"),
# paste0(fpath, "block_3.csv"),
# paste0(fpath, "block_4.csv")
# )
#
# rawData <- lapply(fileList, read_csv) |>
# bind_rows() |>
# mutate(id = ID,
# group = grp,
# .before = 1)
Make sure triggers are set as character/factor and not numbers. Also relabel for easier input
# Set all matrices from numeric to character
rawData$fixJitData <- as.character(rawData$fixJitData)
rawData$cueJitData <- as.character(rawData$cueJitData)
rawData$trialType <- as.character(rawData$trialType) # 0 = NoGrat; 1 = Grat > CHANGE TO LABEL
rawData$cuePos <- as.character(rawData$cuePos) # 0 = left; 1 = right; 2 = Neutral > CHANGE TO LABEL
rawData$targetLR <- as.character(rawData$targetLR) # 0 = left; 1 = right > CHANGE TO LABEL
rawData$gapLoc <- as.character(rawData$gapLoc) # specific gap location
rawData$validity <- as.character(rawData$validity) # 0 = invalid; 1 = valid; 2 = neutral > CHANGE TO LABEL
rawData$correctDet <- as.character(rawData$correctDet) # 0 = incorrect; 1 = correct > CHANGE TO LABEL
rawData$correctAcc <- as.character(rawData$correctAcc) # 0 = incorrect; 1 = correct > CHANGE TO LABEL
# Relabel (in case of filter, easier to id)
rawData <-
rawData |>
mutate(trialType = recode(trialType, "0" = "NG",
"1" = "G"),
cuePos = recode(cuePos, "0" = "left",
"1" = "right",
"2" ="neutral"),
targetLR = recode(targetLR, "0" = "left",
"1" = "right"),
validity = recode(validity, "0" = "invalid",
"1" = "valid",
"2" = "neutral"),
correctDet = recode(correctDet, "0" = "incorrect",
"1" = "correct"),
correctAcc = recode(correctAcc, "0" = "incorrect",
"1" = "correct")
)
This code chunk separates the raw data set by each trialType (i.e. NG L-R-N & G L-R-N) and gives number for each condition group (i.e. #1:end for every trialType) to match with EEG epochs numbers
## NG Left
alphaBeh_NG_L <-
rawData |>
filter(trialType == "NG",
cuePos == "left") |>
# numLabel each trialType with numbers (i.e. new column with numbers starting from 1:last row)
mutate(numLabel = as.character(1:n()),
.before = 1)
## NG Right
alphaBeh_NG_R <-
rawData |>
filter(trialType == "NG",
cuePos == "right") |>
# numLabel each trialType with numbers (i.e. new column with numbers starting from 1:last row)
mutate(numLabel = as.character(1:n()),
.before = 1)
## NG Neutral
alphaBeh_NG_N <-
rawData |>
filter(trialType == "NG",
cuePos == "neutral") |>
# numLabel each trialType with numbers (i.e. new column with numbers starting from 1:last row)
mutate(numLabel = as.character(1:n()),
.before = 1)
## G Left
alphaBeh_G_L <-
rawData |>
filter(trialType == "G",
cuePos == "left") |>
# numLabel each trialType with numbers (i.e. new column with numbers starting from 1:last row)
mutate(numLabel = as.character(1:n()),
.before = 1)
## G Right
alphaBeh_G_R <-
rawData |>
filter(trialType == "G",
cuePos == "right") |>
# numLabel each trialType with numbers (i.e. new column with numbers starting from 1:last row)
mutate(numLabel = as.character(1:n()),
.before = 1)
## G Neutral
alphaBeh_G_N <-
rawData |>
filter(trialType == "G",
cuePos == "neutral") |>
# numLabel each trialType with numbers (i.e. new column with numbers starting from 1:last row)
mutate(numLabel = as.character(1:n()),
.before = 1)
## Remerge seperated data set
rawData <-
full_join(alphaBeh_NG_L, alphaBeh_NG_R) |>
full_join(alphaBeh_NG_N)|>
full_join(alphaBeh_G_L)|>
full_join(alphaBeh_G_R)|>
full_join(alphaBeh_G_N)
# <<<<<<<<<<< OUTLIER METHOD >>>>>>>>>>>
# # Z Score
# DataZ <- rawData |>
# mutate(zscore = (rtDet - mean(rtDet))/sd(rtDet)) |>
# filter(zscore > -3 & zscore < 3)
# <<<<<<<<<<<<<<<< OR >>>>>>>>>>>>>>>>>>>
# # 2sd
# mean_rt <- mean(rawData$rtDet)
# sd_rt <- sd(rawData$rtDet)
# Data2sd <- rawData |>
# filter(rtDet > mean_rt - 2.5*sd_rt & rtDet < mean_rt + 2.5*sd_rt)
# <<<<<<<<<<<<<<<< OR >>>>>>>>>>>>>>>>>>>
# IQR
DataIQR <-
rawData|>
rstatix::identify_outliers(rtDet)
# List every rtDet classified as outlier (is.outlier vs is.extreme)
outlierList <- c(DataIQR$rtDet[DataIQR$is.outlier == TRUE])
# Remove rows with outlier based on above
DataIQR <-
rawData|>
filter(!rtDet %in% outlierList[])
# before removal
distCheck <-
ggplot(rawData, aes(rtDet)) +
geom_histogram(bins = 50) +
labs(title = "Before Outlier Removal")
print(distCheck)
# IQR removal
distCheckIQR <-
ggplot(DataIQR, aes(rtDet)) +
geom_histogram(bins = 50) +
labs(title = "IQR")
print(distCheckIQR)
# #Z removal
# distCheckZ <- ggplot(DataZ, aes(rtDet)) + geom_histogram(bins = 50) + labs(title = "Z Score")
# print(distCheckZ)
#
# #2SD removal
# distCheck2sd <- ggplot(Data2sd, aes(rtDet)) + geom_histogram(bins = 50) + labs(title = "2SD")
# print(distCheck2sd)
# rawData|>
# group_by(validity, targetLR, trialType) |>
# get_summary_stats(rtDet, type = "mean_sd") |>
#
# flextable(cwidth = .95) |>
# fontsize(size = 8) |>
# fontsize(size = 10, part = "header") |>
#
# set_header_labels(validity = "Validity",
# targetLR = "Left/Right Target",
# trialType = "Gratings",
# variable = "Variable")
In order to have clean correct EEG data (correct answers) we need to remove the epoch/trial number in the EEG data where it is incorrect behaviourally. Chunk below retrieve the number of the incorrect trials/epochs the retrieved numbers will then be fee to the EEG script, isolating the beh-correct EEG trials only.
# NG Left
behOut <-
rawData|>
filter(rtDet %in% outlierList[], # filter/find rows where rtDet is in the outlierList
trialType == "NG",
cuePos == "left")
behOut_NG_L <- behOut$numLabel
# NG Right
behOut <-
rawData|>
filter(rtDet %in% outlierList[],
trialType == "NG",
cuePos == "right")
behOut_NG_R <- behOut$numLabel
# NG Neutral
behOut <-
rawData|>
filter(rtDet %in% outlierList[],
trialType == "NG",
cuePos == "neutral")
behOut_NG_N <- behOut$numLabel
# G Left
behOut <-
rawData|>
filter(rtDet %in% outlierList[],
trialType == "G",
cuePos == "left")
behOut_G_L <- behOut$numLabel
# G Right
behOut <-
rawData|>
filter(rtDet %in% outlierList[],
trialType == "G",
cuePos == "right")
behOut_G_R <- behOut$numLabel
# G Neutral
behOut <-
rawData|>
filter(rtDet %in% outlierList[],
trialType == "G",
cuePos == "neutral")
behOut_G_N <- behOut$numLabel
# # Removed epoch with artifact
# # for each condType, input the exact epochs removed in matlab, to be removed here
#
# NG Left
eegOut_NG_L <- c(0)
eegOut_NG_L <- as.character(eegOut_NG_L)
# NG Right
eegOut_NG_R <- c(0)
eegOut_NG_R <- as.character(eegOut_NG_R)
# NG Neutral
eegOut_NG_N <- c(0)
eegOut_NG_N <- as.character(eegOut_NG_N)
# G Left
eegOut_G_L <- c(0)
eegOut_G_L <- as.character(eegOut_G_L)
# G Right
eegOut_G_R <- c(0)
eegOut_G_R <- as.character(eegOut_G_R)
# G Neutral
eegOut_G_N <- c(0)
eegOut_G_N <- as.character(eegOut_G_N)
## NG Left
clean_NG_L <-
rawData |>
filter(trialType == "NG",
cuePos == "left",
!numLabel %in% behOut_NG_L[],
!numLabel %in% eegOut_NG_L[])
## NG Right
clean_NG_R <-
rawData |>
filter(trialType == "NG",
cuePos == "right",
!numLabel %in% behOut_NG_R[],
!numLabel %in% eegOut_NG_R[])
## NG Neutral
clean_NG_N <-
rawData |>
filter(trialType == "NG",
cuePos == "neutral",
!numLabel %in% behOut_NG_N[],
!numLabel %in% eegOut_NG_N[])
## G Left
clean_G_L <-
rawData |>
filter(trialType == "G",
cuePos == "left",
!numLabel %in% behOut_G_L[],
!numLabel %in% eegOut_G_L[])
## G Right
clean_G_R <-
rawData |>
filter(trialType == "G",
cuePos == "right",
!numLabel %in% behOut_G_R[],
!numLabel %in% eegOut_G_R[])
## G Neutral
clean_G_N <-
rawData |>
filter(trialType == "G",
cuePos == "neutral",
!numLabel %in% behOut_G_N[],
!numLabel %in% eegOut_G_N[])
## Remerge clean data set
cleanData <-
full_join(clean_NG_L, clean_NG_R) |>
full_join(clean_NG_N)|>
full_join(clean_G_L)|>
full_join(clean_G_R)|>
full_join(clean_G_N)
## Joining with `by = join_by(numLabel, id, group, fixJitData, cueJitData,
## trialType, cuePos, targetLR, gapLoc, validity, correctDet, rtDet, correctAcc,
## rtAcc)`
## Joining with `by = join_by(numLabel, id, group, fixJitData, cueJitData,
## trialType, cuePos, targetLR, gapLoc, validity, correctDet, rtDet, correctAcc,
## rtAcc)`
## Joining with `by = join_by(numLabel, id, group, fixJitData, cueJitData,
## trialType, cuePos, targetLR, gapLoc, validity, correctDet, rtDet, correctAcc,
## rtAcc)`
## Joining with `by = join_by(numLabel, id, group, fixJitData, cueJitData,
## trialType, cuePos, targetLR, gapLoc, validity, correctDet, rtDet, correctAcc,
## rtAcc)`
## Joining with `by = join_by(numLabel, id, group, fixJitData, cueJitData,
## trialType, cuePos, targetLR, gapLoc, validity, correctDet, rtDet, correctAcc,
## rtAcc)`
write_csv(cleanData, paste0("C:/R/exp_1/results/BEH/indv_merge_dataset/", participantSave, ".csv"))
just in case manual tagging in 8-10 might be wrong, always recheck against the auto IQR removal data set
# Behaviour outlier removal in the cleanData is based on comparing rtDet in 6 decimal places.
# Do this to make sure cleanData match DataIQR
arsenal::comparedf(cleanData, DataIQR)
## Compare Object
##
## Function Call:
## arsenal::comparedf(x = cleanData, y = DataIQR)
##
## Shared: 14 non-by variables and 450 observations.
## Not shared: 0 variables and 0 observations.
##
## Differences found in 0/14 variables compared.
## 0 variables compared have non-identical attributes.
Check for validity effect, mark participants with no effect
data <- cleanData
# NoGrat
noGratRt <-
filter(data,
trialType == "NG", # Only want NoGrat
correctDet == "correct") # Only want correct response
# Set plot contents
noGratRt <-
ggplot(noGratRt, aes(validity, rtDet, fill = validity)) +
# Set whiskers
stat_boxplot(geom = 'errorbar',
width = 0.3,
coef = 1.5) +
# Set plot design
geom_jitter(aes(color=validity),
width = 0.3,
alpha = 0.1) +
scale_color_manual(values=c("indianred3", "limegreen", "dodgerblue")) +
guides(color = FALSE) +
geom_boxplot(alpha = 0.8,
outlier.shape = NaN) +
# Set mean
stat_summary(fun = mean,
geom = "point",
shape = 18,
size = 3,
show.legend = F) +
# Set major labels (title, Y, X axis)
labs(title = "No Gratings",
x = " ",
y = "RT (Secs)") +
# Rename each X axis character label
scale_x_discrete(limits = c("invalid", "valid", "neutral"),
labels = c("Invalid", "Valid", "Neutral")) +
# Set Y axis range
ylim(0, 3) +
# Set colour
scale_fill_manual(values=c("coral", "seagreen1", "cadetblue1")) +
# Plot theme
theme_bw() +
# Remove legend
guides(fill=FALSE)
# Gratings
gratRt <- filter(data,
trialType == "G",
correctDet == "correct")
# Set plot contents
gratRt <-
ggplot(gratRt, aes(validity, rtDet, fill = validity)) +
# Set whiskers
stat_boxplot(geom = 'errorbar',
width = 0.3,
coef = 1.5) +
# Set plot design
geom_jitter(aes(color=validity),
width = 0.3,
alpha = 0.1) +
scale_color_manual(values=c("indianred3", "limegreen", "dodgerblue")) +
guides(color = FALSE) +
geom_boxplot(alpha = 0.8,
outlier.shape = NaN) +
# Set mean
stat_summary(fun = mean,
geom = "point",
shape = 18,
size = 3,
show.legend = F) +
# Set major labels (title, Y, X axis)
labs(title = "No Gratings",
x = " ",
y = "RT (Secs)") +
# Rename each X axis character label
scale_x_discrete(limits = c("invalid", "valid", "neutral"),
labels = c("Invalid", "Valid", "Neutral")) +
# Set Y axis range
ylim(0, 3) +
# Set colour
scale_fill_manual(values=c("coral", "seagreen1", "cadetblue1")) +
# Plot theme
theme_bw() +
# Remove legend
guides(fill=FALSE)
rtPlot <- noGratRt + gratRt
# ggsave("C:/MATLAB/exp_1/results/figures/RT_plot.png", rtPlot)
print(rtPlot)
# rtPlot <- plot_grid(noGratRt, gratRt, labels=c("A", "B"), ncol = 2, nrow = 1)
# # ggsave("C:/MATLAB/exp_1/results/figures/RT_plot.png", rtPlot)
# print(rtPlot)
——————————————-[CTRL + SHIFT + ALT + P] FROM HERE——————————————–
Configure which participant to preprocess here. Then ctrl+shift+alt+p from above.
# Select Participant
grp <- "C"
num <- "4"
ID <- paste0("participant_", num)
participantSave <- paste0(grp, "_", num)
# rawData|>
# group_by(validity, targetLR, trialType) |>
# get_summary_stats(rtDet, type = "mean_sd") |>
#
# flextable(cwidth = .95) |>
# fontsize(size = 8) |>
# fontsize(size = 10, part = "header") |>
#
# set_header_labels(validity = "Validity",
# targetLR = "Left/Right Target",
# trialType = "Gratings",
# variable = "Variable")
# # Full raw data set for each validity type
# rawData_Val <- rawData |> filter(validity == "valid")
# rawData_Inval <- rawData |> filter(validity == "invalid")
# rawData_Neut <- rawData |> filter(validity == "neutral")
#
# # Correct only data set per validity type
# alphaBeh_Val <- alphaBeh |> filter(validity == "valid")
# alphaBeh_Inval <- alphaBeh |> filter(validity == "invalid")
# alphaBeh_Neut <- alphaBeh |> filter(validity == "neutral")
#
# # NG raw data set for each validity type
# rawDataNG <- rawData |> filter(trialType == "NG")
# rawDataNG_Val <- rawDataNG |> filter(validity == "valid")
# rawDataNG_Inval <- rawDataNG |> filter(validity == "invalid")
# rawDataNG_Neut <- rawDataNG |> filter(validity == "neutral")
#
# # G raw data set for each validity type
# rawDataG <- rawData |> filter(trialType == "G")
# rawDataG_Val <- rawDataG |> filter(validity == "valid")
# rawDataG_Inval <- rawDataG |> filter(validity == "invalid")
# rawDataG_Neut <- rawDataG |> filter(validity == "neutral")
#
# # Correct only NG data set per validity type
# alphaBehNG <- alphaBeh |> filter(trialType == "NG")
# alphaBehNG_Val <- alphaBehNG |> filter(validity == "valid")
# alphaBehNG_Inval <- alphaBehNG |> filter(validity == "invalid")
# alphaBehNG_Neut <- alphaBehNG |> filter(validity == "neutral")
#
# # Correct only G data set per validity type
# alphaBehG <- alphaBeh |> filter(trialType == "G")
# alphaBehG_Val <- alphaBehG |> filter(validity == "valid")
# alphaBehG_Inval <- alphaBehG |> filter(validity == "invalid")
# alphaBehG_Neut <- alphaBehG |> filter(validity == "neutral")
#
# percAcc <- data.frame(group = grp,
# id = ID,
# tTcollapsed = (nrow(alphaBeh) / nrow(rawData)),
# tTcollapsed_Val = (nrow(alphaBeh_Val) / nrow(rawData_Val)),
# tTcollapsed_Inval = (nrow(alphaBeh_Inval) / nrow(rawData_Inval)),
# tTcollapsed_Neut = (nrow(alphaBeh_Neut) / nrow(rawData_Neut)),
# allNG = (nrow(alphaBehNG) / nrow(rawDataNG)),
# NG_Val = (nrow(alphaBehNG_Val) / nrow(rawDataNG_Val)),
# NG_Inval = (nrow(alphaBehNG_Inval) / nrow(rawDataNG_Inval)),
# NG_Neut = (nrow(alphaBehNG_Neut) / nrow(rawDataNG_Neut)),
# allG = (nrow(alphaBehG) / nrow(rawDataG)),
# G_Val = (nrow(alphaBehG_Val) / nrow(rawDataG_Val)),
# G_Inval = (nrow(alphaBehG_Inval) / nrow(rawDataG_Inval)),
# G_Neut =(nrow(alphaBehG_Neut) / nrow(rawDataG_Neut))
# )
#
# write_csv(percAcc, paste0("C:/R/exp_1/results/BEH/indv_merge_dataset/alphaBeh/", participantSave, ".csv"))
# # Easier to view removed label (trial) for each Det and Acc to match EEG Data
# maxLength <- max(c(length(beh_NG_L_Det), length(beh_NG_L_Acc),
# length(beh_NG_R_Det), length(beh_NG_R_Acc),
# length(beh_NG_N_Det), length(beh_NG_N_Acc),
# length(beh_G_L_Det), length(beh_G_L_Acc),
# length(beh_G_R_Det), length(beh_G_R_Acc),
# length(beh_G_N_Det), length(beh_G_N_Acc)))
#
# eeg_beh_list <- data.frame(NG_L_Det = c(beh_NG_L_Det,rep(NA, maxLength - length(beh_NG_L_Det))),
# NG_L_Acc = c(beh_NG_L_Acc,rep(NA, maxLength - length(beh_NG_L_Acc))),
# NG_R_Det = c(beh_NG_R_Det,rep(NA, maxLength - length(beh_NG_R_Det))),
# NG_R_Acc = c(beh_NG_R_Acc,rep(NA, maxLength - length(beh_NG_R_Acc))),
# NG_N_Det = c(beh_NG_N_Det,rep(NA, maxLength - length(beh_NG_N_Det))),
# NG_N_Acc = c(beh_NG_N_Acc,rep(NA, maxLength - length(beh_NG_N_Acc))),
# G_L_Det = c(beh_G_L_Det,rep(NA, maxLength - length(beh_G_L_Det))),
# G_L_Acc = c(beh_G_L_Acc,rep(NA, maxLength - length(beh_G_L_Acc))),
# G_R_Det = c(beh_G_R_Det,rep(NA, maxLength - length(beh_G_R_Det))),
# G_R_Acc = c(beh_G_R_Acc,rep(NA, maxLength - length(beh_G_R_Acc))),
# G_N_Det = c(beh_G_N_Det,rep(NA, maxLength - length(beh_G_N_Det))),
# G_N_Acc = c(beh_G_N_Acc,rep(NA, maxLength - length(beh_G_N_Acc))))
# ## NG Left
#
# alphaBeh_NG_L <- rawData |>
# filter(trialType == "NG",
# cuePos == "left") |>
# # numLabel each trialType with numbers (i.e. new column with numbers starting from 1:last row)
# mutate(numLabel = as.character(1:n()),
# .before = 1)
#
# ## NG Right
#
# alphaBeh_NG_R <- rawData |>
# filter(trialType == "NG",
# cuePos == "right") |>
# # numLabel each trialType with numbers (i.e. new column with numbers starting from 1:last row)
# mutate(numLabel = as.character(1:n()),
# .before = 1)
#
# ## NG Neutral
#
# alphaBeh_NG_N <- rawData |>
# filter(trialType == "NG",
# cuePos == "neutral") |>
# # numLabel each trialType with numbers (i.e. new column with numbers starting from 1:last row)
# mutate(numLabel = as.character(1:n()),
# .before = 1)
#
# ## G Left
#
# alphaBeh_G_L <- rawData |>
# filter(trialType == "G",
# cuePos == "left") |>
# # numLabel each trialType with numbers (i.e. new column with numbers starting from 1:last row)
# mutate(numLabel = as.character(1:n()),
# .before = 1)
#
# ## G Right
#
# alphaBeh_G_R <- rawData |>
# filter(trialType == "G",
# cuePos == "right") |>
# # numLabel each trialType with numbers (i.e. new column with numbers starting from 1:last row)
# mutate(numLabel = as.character(1:n()),
# .before = 1)
#
# ## G Neutral
#
# alphaBeh_G_N <- rawData |>
# filter(trialType == "G",
# cuePos == "neutral") |>
# # numLabel each trialType with numbers (i.e. new column with numbers starting from 1:last row)
# mutate(numLabel = as.character(1:n()),
# .before = 1)
#
# ## Remerge seperated data set
#
# rawData <- full_join(alphaBeh_NG_L, alphaBeh_NG_R) |>
# full_join(alphaBeh_NG_N)|>
# full_join(alphaBeh_G_L)|>
# full_join(alphaBeh_G_R)|>
# full_join(alphaBeh_G_N)