rm(list = ls()) # clean workspace
try(dev.off(), silent = TRUE) # close all plots
library(readxl)
library(afex)
library(emmeans)
library(ggplot2)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) {
cat(rep("_", 100), '\n', sep = "")
print(emmeans(afex_aov, factors[[i]], contr = "pairwise"))
}
}
}painEmpathy_p2_name <- file.path('average_voltage_180_to_230_painEmpathy.txt')
painEmpathy_p2_data <- read.table(painEmpathy_p2_name, header = TRUE, strip.white = TRUE, sep = "\t")
painEmpathy_p3_name <- file.path('average_voltage_300_to_500_painEmpathy.txt')
painEmpathy_p3_data <- read.table(painEmpathy_p3_name, header = TRUE, strip.white = TRUE, sep = "\t")
painEmpathy_data <- rbind(painEmpathy_p2_data, painEmpathy_p3_data)
painEmpathy_data$worklat <- gsub(".0", "", painEmpathy_data$worklat, fixed = TRUE)
names(painEmpathy_data)[names(painEmpathy_data) == "mlabel"] <- "component"
names(painEmpathy_data)[names(painEmpathy_data) == "value"] <- "amplitude"
names(painEmpathy_data)[names(painEmpathy_data) == "binlabel"] <- "stimulus"
names(painEmpathy_data)[names(painEmpathy_data) == "chlabel"] <- "electrode"
painEmpathy_data$num_id <- readr::parse_number(painEmpathy_data$ERPset)
painEmpathy_data$sex[ grepl("female", painEmpathy_data$ERPset)] <- "Female"
painEmpathy_data$sex[!grepl("female", painEmpathy_data$ERPset)] <- "Male"
painEmpathy_data$num_id <- factor(painEmpathy_data$num_id)
painEmpathy_data$chindex <- factor(painEmpathy_data$chindex)
painEmpathy_data$bini <- factor(painEmpathy_data$bini)
painEmpathy_data[sapply(painEmpathy_data, is.character)] <- lapply(painEmpathy_data[sapply(painEmpathy_data, is.character)], as.factor)
painEmpathy_data$stimulus <- relevel(painEmpathy_data$stimulus, "Pain")
sass_data <- read_xlsx(file.path('..', 'bad channels empathy huepe.xlsx'))
names(sass_data)[names(sass_data) == "SASS_TOTAL"] <- "sass"
sass_data$num_id <- readr::parse_number(sass_data$ID_gsheet)
painEmpathy_data <- merge(painEmpathy_data, sass_data[c('sass', 'num_id')], by = 'num_id')
painEmpathy_data$sass_centered <- painEmpathy_data$sass - mean(painEmpathy_data$sass, na.rm=TRUE)
write.csv(painEmpathy_data, file.path('painEmpathy_data_clean.csv'), row.names = FALSE)options(width = 100)
xtabs(~ sex, data = painEmpathy_data) / length(unique(painEmpathy_data$chindex)) / length(unique(painEmpathy_data$stimulus)) / length(unique(painEmpathy_data$component))sex
Female Male
34 25
options(width = 100)
ggplot(
painEmpathy_data, aes(x = amplitude, fill = component, color = component)) +
geom_histogram(alpha = .4, position = "identity") num_id worklat component amplitude chindex electrode bini stimulus
1 : 8 [180 230]:236 P2:236 Min. :-13.376 25:236 PO7:236 1:236 Pain :236
3 : 8 [300 500]:236 P3:236 1st Qu.: 2.342 62:236 PO8:236 2:236 NoPain:236
4 : 8 Median : 5.104
6 : 8 Mean : 5.533
7 : 8 3rd Qu.: 8.408
11 : 8 Max. : 22.731
(Other):424
ERPset sex sass sass_centered
S01_female_painEmpathy: 8 Female:272 Min. :27.00 Min. :-15.5172
S03_female_painEmpathy: 8 Male :200 1st Qu.:36.00 1st Qu.: -6.5172
S04_male_painEmpathy : 8 Median :42.00 Median : -0.5172
S06_male_painEmpathy : 8 Mean :42.52 Mean : 0.0000
S07_male_painEmpathy : 8 3rd Qu.:49.00 3rd Qu.: 6.4828
S11_female_painEmpathy: 8 Max. :55.00 Max. : 12.4828
(Other) :424 NA's :8 NA's :8
options(width = 100)
painEmpathy_p2_rep_anova = aov_ez(
"num_id", "amplitude", subset(painEmpathy_data, component == "P2",),
within = c("stimulus", "electrode"), between = "sex", covariate = "sass_centered", factorize = FALSE
)Warning: Numerical variables NOT centered on 0 (matters if variable in interaction):
NA
Warning: Missing values for 1 ID(s), which were removed before analysis:
99
Below the first few rows (in wide format) of the removed cases with missing data.
Contrasts set to contr.sum for the following variables: sex
painEmpathy_p2_afex_plot <-
afex_plot(
painEmpathy_p2_rep_anova,
x = "electrode",
trace = "stimulus",
panel = "sex",
error = "within",
error_arg = list(width = .25),
dodge = -.5,
mapping = c("color"),
point_arg = list(size = 4)
)
nice(painEmpathy_p2_rep_anova)Anova Table (Type 3 tests)
Response: amplitude
Effect df MSE F ges p.value
1 sex 1, 55 46.68 1.17 .018 .284
2 sass_centered 1, 55 46.68 0.49 .008 .486
3 stimulus 1, 55 1.65 0.16 <.001 .688
4 sex:stimulus 1, 55 1.65 0.01 <.001 .910
5 sass_centered:stimulus 1, 55 1.65 2.44 .001 .124
6 electrode 1, 55 5.47 10.16 ** .018 .002
7 sex:electrode 1, 55 5.47 1.23 .002 .272
8 sass_centered:electrode 1, 55 5.47 2.03 .004 .160
9 stimulus:electrode 1, 55 0.47 0.13 <.001 .725
10 sex:stimulus:electrode 1, 55 0.47 3.17 + <.001 .080
11 sass_centered:stimulus:electrode 1, 55 0.47 1.15 <.001 .288
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘+’ 0.1 ‘ ’ 1
____________________________________________________________________________________________________
$emmeans
electrode emmean SE df lower.CL upper.CL
PO7 3.65 0.445 55 2.76 4.54
PO8 4.64 0.511 55 3.61 5.66
Results are averaged over the levels of: sex, stimulus
Confidence level used: 0.95
$contrasts
contrast estimate SE df t.ratio p.value
PO7 - PO8 -0.989 0.31 55 -3.188 0.0024
Results are averaged over the levels of: sex, stimulus
options(width = 100)
painEmpathy_p3_rep_anova = aov_ez(
"num_id", "amplitude", subset(painEmpathy_data, component == "P3"),
within = c("stimulus", "electrode"), between = "sex", covariate = "sass_centered", factorize = FALSE
)Warning: Numerical variables NOT centered on 0 (matters if variable in interaction):
NA
Warning: Missing values for 1 ID(s), which were removed before analysis:
99
Below the first few rows (in wide format) of the removed cases with missing data.
Contrasts set to contr.sum for the following variables: sex
painEmpathy_p3_afex_plot <-
afex_plot(
painEmpathy_p3_rep_anova,
x = "electrode",
trace = "stimulus",
panel = "sex",
error = "within",
error_arg = list(width = .25),
dodge = -.5,
mapping = c("color"),
point_arg = list(size = 4)
)
nice(painEmpathy_p3_rep_anova)Anova Table (Type 3 tests)
Response: amplitude
Effect df MSE F ges p.value
1 sex 1, 55 107.93 0.59 .010 .445
2 sass_centered 1, 55 107.93 0.74 .012 .393
3 stimulus 1, 55 1.80 10.01 ** .003 .003
4 sex:stimulus 1, 55 1.80 0.43 <.001 .514
5 sass_centered:stimulus 1, 55 1.80 0.74 <.001 .392
6 electrode 1, 55 7.98 16.43 *** .020 <.001
7 sex:electrode 1, 55 7.98 0.00 <.001 .989
8 sass_centered:electrode 1, 55 7.98 0.06 <.001 .803
9 stimulus:electrode 1, 55 0.53 1.38 <.001 .245
10 sex:stimulus:electrode 1, 55 0.53 1.52 <.001 .223
11 sass_centered:stimulus:electrode 1, 55 0.53 0.30 <.001 .589
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘+’ 0.1 ‘ ’ 1
____________________________________________________________________________________________________
$emmeans
stimulus emmean SE df lower.CL upper.CL
Pain 6.61 0.704 55 5.2 8.02
NoPain 7.17 0.685 55 5.8 8.54
Results are averaged over the levels of: sex, electrode
Confidence level used: 0.95
$contrasts
contrast estimate SE df t.ratio p.value
Pain - NoPain -0.563 0.178 55 -3.164 0.0025
Results are averaged over the levels of: sex, electrode
____________________________________________________________________________________________________
$emmeans
electrode emmean SE df lower.CL upper.CL
PO7 6.13 0.675 55 4.78 7.48
PO8 7.65 0.750 55 6.14 9.15
Results are averaged over the levels of: sex, stimulus
Confidence level used: 0.95
$contrasts
contrast estimate SE df t.ratio p.value
PO7 - PO8 -1.52 0.374 55 -4.054 0.0002
Results are averaged over the levels of: sex, stimulus
Occipital channels:
PO7, PO3, POz, PO4, PO8
O1, Oz, O2
Iz
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