cat("\014") # clean terminal
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')
# target_and_standard_name <- file.path(data_dir, 'average_voltage_275_to_425_auditory_oddball_standard_and_target.txt')
words_name <- file.path(data_dir, 'average_voltage_500_to_700_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 = "_")
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_late_data_clean.csv'), 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 14 30
Sum 38 39 77
Primer black, target red.
Mean amplitude [500.0 700.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. :-31.7584 Female:1974 Invulnerable:2646 Believer :1596 Fronto-central Line:1386
1st Qu.: -4.8759 Male :1260 Vulnerable : 588 Unbeliever:1638 Left frontal :1848
Median : -1.8726
Mean : -2.3199
3rd Qu.: 0.4122
Max. : 17.6155
word_type congruency word_order num_id
Magic :1078 Congruent :1617 Target:3234 1 : 42
Religious:1078 Incongruent:1617 3 : 42
Secular :1078 6 : 42
15 : 42
16 : 42
20 : 42
(Other):2982
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))nice(words_rep_anova)Anova Table (Type 3 tests)
Response: uvolts
Effect df MSE F ges p.value
1 belief 1, 75 79.18 0.23 .003 .636
2 word_type 1.66, 124.87 4.83 2.26 .003 .118
3 belief:word_type 1.66, 124.87 4.83 2.40 .003 .104
4 congruency 1, 75 1.78 1.86 <.001 .177
5 belief:congruency 1, 75 1.78 4.19 * .001 .044
6 word_type:congruency 1.94, 145.80 3.17 0.58 <.001 .555
7 belief:word_type:congruency 1.94, 145.80 3.17 0.11 <.001 .888
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘+’ 0.1 ‘ ’ 1
Sphericity correction method: GG
a_posteriori(words_rep_anova)$emmeans
belief congruency emmean SE df lower.CL upper.CL
Believer Congruent -2.01 0.599 75 -3.20 -0.812
Unbeliever Congruent -1.36 0.591 75 -2.54 -0.180
Believer Incongruent -1.92 0.593 75 -3.10 -0.740
Unbeliever Incongruent -1.78 0.585 75 -2.95 -0.616
Results are averaged over the levels of: word_type
Confidence level used: 0.95
$contrasts
contrast estimate SE df t.ratio p.value
Believer Congruent - Unbeliever Congruent -0.6475 0.842 75 -0.769 0.8681
Believer Congruent - Believer Incongruent -0.0848 0.177 75 -0.480 0.9632
Believer Congruent - Unbeliever Incongruent -0.2246 0.837 75 -0.268 0.9932
Unbeliever Congruent - Believer Incongruent 0.5627 0.837 75 0.672 0.9073
Unbeliever Congruent - Unbeliever Incongruent 0.4229 0.174 75 2.427 0.0807
Believer Incongruent - Unbeliever Incongruent -0.1398 0.833 75 -0.168 0.9983
Results are averaged over the levels of: word_type
P value adjustment: tukey method for comparing a family of 4 estimates
____________________________________________________________________________________________________
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))nice(words_rep_anova)Anova Table (Type 3 tests)
Response: uvolts
Effect df MSE F ges p.value
1 belief 1, 75 85.22 0.50 .005 .480
2 word_type 1.48, 111.07 8.48 2.16 .003 .133
3 belief:word_type 1.48, 111.07 8.48 3.01 + .005 .069
4 congruency 1, 75 1.92 2.03 <.001 .159
5 belief:congruency 1, 75 1.92 4.72 * .001 .033
6 word_type:congruency 1.91, 143.22 3.89 0.65 <.001 .518
7 belief:word_type:congruency 1.91, 143.22 3.89 0.31 <.001 .724
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘+’ 0.1 ‘ ’ 1
Sphericity correction method: GG
a_posteriori(words_rep_anova)$emmeans
belief congruency emmean SE df lower.CL upper.CL
Believer Congruent -2.43 0.618 75 -3.66 -1.201
Unbeliever Congruent -1.54 0.610 75 -2.75 -0.326
Believer Incongruent -2.33 0.619 75 -3.57 -1.102
Unbeliever Incongruent -2.00 0.611 75 -3.22 -0.788
Results are averaged over the levels of: word_type
Confidence level used: 0.95
$contrasts
contrast estimate SE df t.ratio p.value
Believer Congruent - Unbeliever Congruent -0.8910 0.868 75 -1.027 0.7343
Believer Congruent - Believer Incongruent -0.0969 0.184 75 -0.527 0.9522
Believer Congruent - Unbeliever Incongruent -0.4267 0.869 75 -0.491 0.9608
Unbeliever Congruent - Believer Incongruent 0.7942 0.869 75 0.914 0.7974
Unbeliever Congruent - Unbeliever Incongruent 0.4643 0.181 75 2.560 0.0590
Believer Incongruent - Unbeliever Incongruent -0.3299 0.870 75 -0.379 0.9813
Results are averaged over the levels of: word_type
P value adjustment: tukey method for comparing a family of 4 estimates
____________________________________________________________________________________________________
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))nice(words_rep_anova)Anova Table (Type 3 tests)
Response: uvolts
Effect df MSE F ges p.value
1 belief 1, 75 91.25 0.31 .003 .582
2 word_type 1.54, 115.45 8.76 2.59 + .004 .093
3 belief:word_type 1.54, 115.45 8.76 2.34 .004 .114
4 congruency 1, 75 1.98 1.07 <.001 .303
5 belief:congruency 1, 75 1.98 4.61 * .001 .035
6 word_type:congruency 1.90, 142.76 4.28 0.59 <.001 .550
7 belief:word_type:congruency 1.90, 142.76 4.28 0.53 <.001 .580
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘+’ 0.1 ‘ ’ 1
Sphericity correction method: GG
a_posteriori(words_rep_anova)$emmeans
belief congruency emmean SE df lower.CL upper.CL
Believer Congruent -2.48 0.642 75 -3.76 -1.201
Unbeliever Congruent -1.71 0.633 75 -2.97 -0.445
Believer Incongruent -2.33 0.637 75 -3.60 -1.064
Unbeliever Incongruent -2.12 0.629 75 -3.38 -0.870
Results are averaged over the levels of: word_type
Confidence level used: 0.95
$contrasts
contrast estimate SE df t.ratio p.value
Believer Congruent - Unbeliever Congruent -0.772 0.902 75 -0.857 0.8270
Believer Congruent - Believer Incongruent -0.145 0.186 75 -0.781 0.8629
Believer Congruent - Unbeliever Incongruent -0.356 0.899 75 -0.396 0.9788
Unbeliever Congruent - Believer Incongruent 0.627 0.898 75 0.698 0.8976
Unbeliever Congruent - Unbeliever Incongruent 0.417 0.184 75 2.267 0.1152
Believer Incongruent - Unbeliever Incongruent -0.210 0.895 75 -0.235 0.9954
Results are averaged over the levels of: word_type
P value adjustment: tukey method for comparing a family of 4 estimates
____________________________________________________________________________________________________
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))nice(words_rep_anova)Anova Table (Type 3 tests)
Response: uvolts
Effect df MSE F ges p.value
1 belief 1, 75 116.25 0.01 <.001 .943
2 word_type 1.64, 122.89 10.25 0.52 <.001 .558
3 belief:word_type 1.64, 122.89 10.25 2.92 + .004 .068
4 congruency 1, 75 2.61 0.90 <.001 .347
5 belief:congruency 1, 75 2.61 2.91 + <.001 .092
6 word_type:congruency 1.82, 136.38 4.97 0.32 <.001 .705
7 belief:word_type:congruency 1.82, 136.38 4.97 0.31 <.001 .715
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘+’ 0.1 ‘ ’ 1
Sphericity correction method: GG
a_posteriori(words_rep_anova)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))nice(words_rep_anova)Anova Table (Type 3 tests)
Response: uvolts
Effect df MSE F ges p.value
1 belief 1, 75 144.64 0.01 <.001 .920
2 word_type 1.47, 110.40 18.42 0.61 .001 .496
3 belief:word_type 1.47, 110.40 18.42 3.40 + .006 .051
4 congruency 1, 75 3.51 1.12 <.001 .292
5 belief:congruency 1, 75 3.51 2.04 <.001 .157
6 word_type:congruency 1.63, 122.45 8.20 0.40 <.001 .629
7 belief:word_type:congruency 1.63, 122.45 8.20 0.15 <.001 .822
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘+’ 0.1 ‘ ’ 1
Sphericity correction method: GG
a_posteriori(words_rep_anova)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))nice(words_rep_anova)Anova Table (Type 3 tests)
Response: uvolts
Effect df MSE F ges p.value
1 belief 1, 75 153.54 0.19 .002 .665
2 word_type 1.67, 125.61 14.13 0.42 <.001 .619
3 belief:word_type 1.67, 125.61 14.13 2.99 + .005 .063
4 congruency 1, 75 4.74 0.53 <.001 .470
5 belief:congruency 1, 75 4.74 1.63 <.001 .206
6 word_type:congruency 1.60, 119.68 9.54 0.18 <.001 .784
7 belief:word_type:congruency 1.60, 119.68 9.54 1.47 .002 .234
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘+’ 0.1 ‘ ’ 1
Sphericity correction method: GG
a_posteriori(words_rep_anova)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))nice(words_rep_anova)Anova Table (Type 3 tests)
Response: uvolts
Effect df MSE F ges p.value
1 belief 1, 75 147.07 0.01 <.001 .926
2 word_type 1.67, 124.99 13.40 0.23 <.001 .756
3 belief:word_type 1.67, 124.99 13.40 2.48 + .004 .098
4 congruency 1, 75 4.36 1.00 <.001 .320
5 belief:congruency 1, 75 4.36 2.87 + <.001 .094
6 word_type:congruency 1.70, 127.20 9.17 0.09 <.001 .884
7 belief:word_type:congruency 1.70, 127.20 9.17 0.74 <.001 .459
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘+’ 0.1 ‘ ’ 1
Sphericity correction method: GG
a_posteriori(words_rep_anova)