rm(list = ls()) # clean workspace
try(dev.off(), silent = TRUE) # close all plots
library(afex)
library(emmeans)
library(ggplot2)
library(ggridges)
library(ggdist)
library(dplyr)
library(reshape2)
library(GGally)
library(forcats)
library(readxl)
library(tidyr)exclude_bad_eeg <- TRUE
theme_set(
theme_minimal()
)
a_posteriori <- function(afex_aov, sig_level = .05) {
factors <- as.list(rownames(afex_aov$anova_table))
for (j in 1:length(factors)) {
if (grepl(":", factors[[j]])) {
factors[[j]] <- unlist(strsplit(factors[[j]], ":"))
}
}
p_values <- afex_aov$anova_table$`Pr(>F)`
for (i in 1:length(p_values)) {
if (p_values[i] <= sig_level) {
print(emmeans(afex_aov, factors[[i]], contr = "pairwise"))
cat(rep("_", 100), '\n', sep = "")
}
}
}eeg_check <- read_excel(file.path('..', 'bad channels words pemycrep 2022.xlsx'))
eeg_check <- eeg_check %>%
mutate(badchan_num = ifelse(badchan == '0', 0, sapply(strsplit(badchan, " "), length)))
bad_eeg <- eeg_check$name[eeg_check$commentary != 'ok']
data_dir <- file.path('..', 'results')
words_name <- file.path(data_dir, 'average_voltage_300_to_500_pemycrep_words.txt')
words_data <- read.table(words_name, header = TRUE, strip.white = TRUE, sep = "\t")
names(words_data)[names(words_data) == "value"] <- "uvolts"
names(words_data)[names(words_data) == "binlabel"] <- "stimulus"
words_data$num_id <- readr::parse_number(words_data$ERPset)
words_data$vulnerability[ grepl("nVul", words_data$ERPset)] <- "Invulnerable"
words_data$vulnerability[!grepl("nVul", words_data$ERPset)] <- "Vulnerable"
words_data$belief[ grepl("nCr", words_data$ERPset)] <- "Unbeliever"
words_data$belief[!grepl("nCr", words_data$ERPset)] <- "Believer"
words_data$sex[ grepl("F", words_data$ERPset)] <- "Female"
words_data$sex[!grepl("F", words_data$ERPset)] <- "Male"
words_data$area[words_data$chlabel %in% c('FCz', 'E086', 'Fz')] <- 'Fronto-central Line'
words_data$area[words_data$chlabel %in% c('E090', 'E091', 'E095', 'E096')] <- 'Left frontal'
words_data$num_id <- factor(words_data$num_id)
words_data$vulnerability <- factor(words_data$vulnerability)
words_data$sex <- factor(words_data$sex)
words_data$belief <- factor(words_data$belief)
words_data$stimulus <- factor(words_data$stimulus)
words_data$area <- factor(words_data$area)
words_data <- words_data %>% separate(stimulus, c("word_type","congruency", "word_order"), sep = "_", remove = FALSE)
words_data$word_type <- factor(words_data$word_type)
words_data$congruency <- factor(words_data$congruency)
words_data$word_order <- factor(words_data$word_order)
if (exclude_bad_eeg) {
words_data <- words_data[!(words_data$ERPset %in% bad_eeg), ]
}
write.csv(words_data, file.path(data_dir, 'words_n400_data_clean.csv'), row.names = FALSE)
subj_block <- unique(words_data[c("worklat", "mlabel", "ERPset", "num_id" ,"vulnerability" ,"belief" ,"sex")])
subj_block$RUT <- sub("_wrd.*", "", subj_block$ERPset)
subj_block$RUT <- sub(".*F", "", subj_block$RUT)
subj_block$RUT <- sub(".*M", "", subj_block$RUT)
N400_diff_LeftFrontal <- c()
N400_diff_Central <- c()
for (subj in subj_block$ERPset) {
subject_data <- subset(words_data, ERPset == subj)
N400_diff_LeftFrontal <- c(N400_diff_LeftFrontal,
mean(subject_data$uvolts[subject_data$area == "Left frontal" & subject_data$stimulus == "Religious_Incongruent_Target"]) -
mean(subject_data$uvolts[subject_data$area == "Left frontal" & subject_data$stimulus == "Secular_Incongruent_Target"])
)
N400_diff_Central <- c(N400_diff_Central,
mean(subject_data$uvolts[subject_data$area == "Fronto-central Line" & subject_data$stimulus == "Religious_Incongruent_Target"]) -
mean(subject_data$uvolts[subject_data$area == "Fronto-central Line" & subject_data$stimulus == "Secular_Incongruent_Target"])
)
}
N400_difference_by_subject <- data.frame(subj_block, N400_diff_LeftFrontal, N400_diff_Central)
write.csv(N400_difference_by_subject, paste(data_dir, '/N400_difference_by_subject.csv', sep = ''), row.names = FALSE)options(width = 100)
mytable <- xtabs(~ sex + belief, data = words_data) / length(unique(words_data$chindex)) / length(unique(words_data$bini))
ftable(addmargins(mytable)) belief Believer Unbeliever Sum
sex
Female 22 25 47
Male 16 13 29
Sum 38 38 76
Primer black, target red.
Mean amplitude [300.0 500.0]
options(width = 100)
summary(words_data[c('uvolts', 'sex', 'vulnerability', 'belief', 'area', 'word_type', 'congruency', 'word_order', 'num_id')]) uvolts sex vulnerability belief area
Min. :-29.505 Female:1974 Invulnerable:2604 Believer :1596 Fronto-central Line:1368
1st Qu.: -7.487 Male :1218 Vulnerable : 588 Unbeliever:1596 Left frontal :1824
Median : -3.885
Mean : -4.367
3rd Qu.: -1.175
Max. : 12.840
word_type congruency word_order num_id
Magic :1064 Congruent :1596 Target:3192 1 : 42
Religious:1064 Incongruent:1596 3 : 42
Secular :1064 6 : 42
15 : 42
16 : 42
20 : 42
(Other):2940
options(width = 100)
electrode_data <- words_data[words_data$chlabel == "FCz", ]
words_rep_anova <- aov_ez("num_id", "uvolts", electrode_data, within = c("word_type", "congruency"), between = c("belief"))Contrasts set to contr.sum for the following variables: belief
words_afex_plot <-
afex_plot(
words_rep_anova,
x = "word_type",
trace = "congruency",
panel = "belief",
error = "within",
error_arg = list(width = .1),
dodge = -.5,
mapping = c("color"),
point_arg = list(size = 4)
)
suppressWarnings(print(words_afex_plot))Anova Table (Type 3 tests)
Response: uvolts
Effect df MSE F ges p.value
1 belief 1, 74 86.47 0.01 <.001 .938
2 word_type 1.95, 144.58 4.47 1.46 .002 .235
3 belief:word_type 1.95, 144.58 4.47 2.49 + .003 .088
4 congruency 1, 74 1.98 1.98 <.001 .163
5 belief:congruency 1, 74 1.98 2.33 <.001 .131
6 word_type:congruency 1.92, 141.85 2.65 0.05 <.001 .942
7 belief:word_type:congruency 1.92, 141.85 2.65 0.19 <.001 .819
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘+’ 0.1 ‘ ’ 1
Sphericity correction method: GG
options(width = 100)
electrode_data <- words_data[words_data$chlabel == "E086", ]
words_rep_anova <- aov_ez("num_id", "uvolts", electrode_data, within = c("word_type", "congruency"), between = c("belief"))Contrasts set to contr.sum for the following variables: belief
words_afex_plot <-
afex_plot(
words_rep_anova,
x = "word_type",
trace = "congruency",
panel = "belief",
error = "within",
error_arg = list(width = .1),
dodge = -.5,
mapping = c("color"),
point_arg = list(size = 4)
)
suppressWarnings(print(words_afex_plot))Anova Table (Type 3 tests)
Response: uvolts
Effect df MSE F ges p.value
1 belief 1, 74 98.03 0.04 <.001 .840
2 word_type 1.71, 126.89 7.42 1.61 .002 .208
3 belief:word_type 1.71, 126.89 7.42 2.50 + .004 .095
4 congruency 1, 74 2.27 1.71 <.001 .195
5 belief:congruency 1, 74 2.27 2.55 <.001 .114
6 word_type:congruency 1.92, 142.21 3.20 0.03 <.001 .964
7 belief:word_type:congruency 1.92, 142.21 3.20 0.30 <.001 .729
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘+’ 0.1 ‘ ’ 1
Sphericity correction method: GG
options(width = 100)
electrode_data <- words_data[words_data$chlabel == "Fz", ]
words_rep_anova <- aov_ez("num_id", "uvolts", electrode_data, within = c("word_type", "congruency"), between = c("belief"))Contrasts set to contr.sum for the following variables: belief
words_afex_plot <-
afex_plot(
words_rep_anova,
x = "word_type",
trace = "congruency",
panel = "belief",
error = "within",
error_arg = list(width = .1),
dodge = -.5,
mapping = c("color"),
point_arg = list(size = 4)
)
suppressWarnings(print(words_afex_plot))Anova Table (Type 3 tests)
Response: uvolts
Effect df MSE F ges p.value
1 belief 1, 74 104.61 0.00 <.001 .951
2 word_type 1.76, 130.26 7.55 1.86 .003 .165
3 belief:word_type 1.76, 130.26 7.55 2.36 .003 .105
4 congruency 1, 74 2.47 1.65 <.001 .204
5 belief:congruency 1, 74 2.47 1.83 <.001 .180
6 word_type:congruency 1.91, 141.36 3.48 0.03 <.001 .965
7 belief:word_type:congruency 1.91, 141.36 3.48 0.66 <.001 .512
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘+’ 0.1 ‘ ’ 1
Sphericity correction method: GG
options(width = 100)
electrode_data <- words_data[words_data$chlabel == "E090", ]
words_rep_anova <- aov_ez("num_id", "uvolts", electrode_data, within = c("word_type", "congruency"), between = c("belief"))Contrasts set to contr.sum for the following variables: belief
words_afex_plot <-
afex_plot(
words_rep_anova,
x = "word_type",
trace = "congruency",
panel = "belief",
error = "within",
error_arg = list(width = .1),
dodge = -.5,
mapping = c("color"),
point_arg = list(size = 4)
)
suppressWarnings(print(words_afex_plot))Anova Table (Type 3 tests)
Response: uvolts
Effect df MSE F ges p.value
1 belief 1, 74 121.05 0.03 <.001 .858
2 word_type 1.87, 138.63 8.20 0.66 <.001 .509
3 belief:word_type 1.87, 138.63 8.20 2.44 + .003 .094
4 congruency 1, 74 3.08 1.49 <.001 .226
5 belief:congruency 1, 74 3.08 0.91 <.001 .343
6 word_type:congruency 1.91, 141.18 3.63 0.03 <.001 .967
7 belief:word_type:congruency 1.91, 141.18 3.63 0.14 <.001 .860
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘+’ 0.1 ‘ ’ 1
Sphericity correction method: GG
options(width = 100)
electrode_data <- words_data[words_data$chlabel == "E091", ]
words_rep_anova <- aov_ez("num_id", "uvolts", electrode_data, within = c("word_type", "congruency"), between = c("belief"))Contrasts set to contr.sum for the following variables: belief
words_afex_plot <-
afex_plot(
words_rep_anova,
x = "word_type",
trace = "congruency",
panel = "belief",
error = "within",
error_arg = list(width = .1),
dodge = -.5,
mapping = c("color"),
point_arg = list(size = 4)
)
suppressWarnings(print(words_afex_plot))Anova Table (Type 3 tests)
Response: uvolts
Effect df MSE F ges p.value
1 belief 1, 74 146.38 0.02 <.001 .877
2 word_type 1.68, 124.61 13.14 0.91 .001 .389
3 belief:word_type 1.68, 124.61 13.14 3.04 + .005 .060
4 congruency 1, 74 3.76 2.01 <.001 .160
5 belief:congruency 1, 74 3.76 1.00 <.001 .320
6 word_type:congruency 1.87, 138.71 5.33 0.01 <.001 .993
7 belief:word_type:congruency 1.87, 138.71 5.33 0.06 <.001 .933
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘+’ 0.1 ‘ ’ 1
Sphericity correction method: GG
options(width = 100)
electrode_data <- words_data[words_data$chlabel == "E095", ]
words_rep_anova <- aov_ez("num_id", "uvolts", electrode_data, within = c("word_type", "congruency"), between = c("belief"))Contrasts set to contr.sum for the following variables: belief
words_afex_plot <-
afex_plot(
words_rep_anova,
x = "word_type",
trace = "congruency",
panel = "belief",
error = "within",
error_arg = list(width = .1),
dodge = -.5,
mapping = c("color"),
point_arg = list(size = 4)
)
suppressWarnings(print(words_afex_plot))Anova Table (Type 3 tests)
Response: uvolts
Effect df MSE F ges p.value
1 belief 1, 74 151.47 0.05 <.001 .818
2 word_type 1.90, 140.61 10.39 0.48 <.001 .610
3 belief:word_type 1.90, 140.61 10.39 2.16 .003 .121
4 congruency 1, 74 4.27 1.40 <.001 .240
5 belief:congruency 1, 74 4.27 1.12 <.001 .293
6 word_type:congruency 1.89, 139.62 5.68 0.42 <.001 .647
7 belief:word_type:congruency 1.89, 139.62 5.68 0.78 <.001 .453
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘+’ 0.1 ‘ ’ 1
Sphericity correction method: GG
options(width = 100)
electrode_data <- words_data[words_data$chlabel == "E096", ]
words_rep_anova <- aov_ez("num_id", "uvolts", electrode_data, within = c("word_type", "congruency"), between = c("belief"))Contrasts set to contr.sum for the following variables: belief
words_afex_plot <-
afex_plot(
words_rep_anova,
x = "word_type",
trace = "congruency",
panel = "belief",
error = "within",
error_arg = list(width = .1),
dodge = -.5,
mapping = c("color"),
point_arg = list(size = 4)
)
suppressWarnings(print(words_afex_plot))Anova Table (Type 3 tests)
Response: uvolts
Effect df MSE F ges p.value
1 belief 1, 74 143.62 0.01 <.001 .921
2 word_type 1.88, 139.27 9.78 0.38 <.001 .669
3 belief:word_type 1.88, 139.27 9.78 1.77 .002 .175
4 congruency 1, 74 3.93 2.40 <.001 .126
5 belief:congruency 1, 74 3.93 1.16 <.001 .284
6 word_type:congruency 1.97, 145.85 5.20 0.29 <.001 .749
7 belief:word_type:congruency 1.97, 145.85 5.20 0.29 <.001 .742
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘+’ 0.1 ‘ ’ 1
Sphericity correction method: GG
Fahrenfort, J. J., van Driel, J., van Gaal, S., & Olivers, C. N. L. (2018). From ERPs to MVPA using the Amsterdam Decoding and Modeling toolbox (ADAM). Frontiers in Neuroscience, 12(JUL), 351586. https://doi.org/10.3389/FNINS.2018.00368/BIBTEX
All channels, reference to infinite, 40 Hz
low-pass
Dong, L., Li, F., Liu, Q., Wen, X., Lai, Y., Xu, P., & Yao, D.
(2017). MATLAB Toolboxes for Reference Electrode Standardization
Technique (REST) of Scalp EEG. Frontiers in Neuroscience, 11(OCT), 601.
https://doi.org/10.3389/fnins.2017.00601