-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
512 lines (512 loc) · 28.3 KB
/
.Rhistory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# Compare different DVs and make preprocessing visual ---------------------------------------------------
tumor.baby.redux2 <- tumor.baby[which(tumor.baby$last.sync==0),]
models <- list()
dvs <- c("cor.increase.raw", "cor.increase.pc", "cor.z", "cor.hampel.increase")
samples <- c("all", "cor.error.bigchange<1", "cor.error.nochange<1", "cor.n.errors<2", "cor.n.errors<1", "winsorized", "outlier removal")
options <- expand.grid(dvs, samples)
do.model <- function(sample.type, dv){
if(sample.type=="all"){sample <- tumor.baby.redux2
} else if(sample.type%in%c("winsorized", "outlier removal")){sample <- tumor.baby.redux2[which(tumor.baby.redux2$cor.n.errors<1),]
} else {sample <- eval(parse(text=paste("tumor.baby.redux2[tumor.baby.redux2$",sample.type,",]")))}
sample$x <- eval(parse(text=paste0("sample$", dv)))
if(sample.type=="winsorized"){ sample$x <- winsorize(sample$x,95) }
if(sample.type=="outlier removal") {sample <- sample[-which(scale(sample$x)>4),]}
# note the model with sync.units as random effect doesn't converge
model <- lmer(x ~ 1 + condition2 + sync.units + syncs + (1|study_respondent), control=strictControl, REML=T, data=sample)
output <- coef(summary(model))[2,]
n <- dim(model@frame)[1]
return(c(output,n))
}
# standard models
models.cor <- lapply(1:dim(options)[1], function(x) do.model(sample.type=options[x,2],dv=options[x,1]))
# winsorized models.cor
models.cor <- do.call("rbind", models.cor)
models.cor <- data.frame(models.cor, options)
models.cor$lo <- models.cor$Estimate - 1.96*models.cor$Std..Error
models.cor$hi <- models.cor$Estimate + 1.96*models.cor$Std..Error
models.cor$samples2 <- ifelse(models.cor$Var2=="all","None\nN=1579",
ifelse(models.cor$Var2=="cor.error.bigchange<1","Cliffs\n1440",
ifelse(models.cor$Var2=="cor.error.nochange<1", "Flat\n1517",
ifelse(models.cor$Var2=="cor.n.errors<2", "Errors\n1455",
ifelse(models.cor$Var2=="cor.n.errors<1", "1 error\n1393",
ifelse(models.cor$Var2=="winsorized", "Winsor\n1393",
ifelse(models.cor$Var2=="outlier removal", "Outliers\n1384", models.cor$Var2)))))))
models.cor$samples2 <- factor(models.cor$samples2, levels=c("None\nN=1579", "Cliffs\n1440", "Flat\n1517", "Errors\n1455", "1 error\n1393", "Winsor\n1393", "Outliers\n1384"))
models.cor$dv <- ifelse(models.cor$Var1=="cor.increase.raw", "Raw",
ifelse(models.cor$Var1=="cor.increase.pc", "Percent",
ifelse(models.cor$Var1=="cor.z", "Z-score",
ifelse(models.cor$Var1=="cor.hampel.increase", "Hampel", models.cor$Var1))))
tumor.baby.redux2 <- tumor.baby[which(tumor.baby$last.sync==0),]
do.model <- function(sample.type, dv){
if(sample.type=="all"){sample <- tumor.baby.redux2
} else if(sample.type%in%c("winsorized", "outlier removal")){sample <- tumor.baby.redux2[which(tumor.baby.redux2$zyg.n.errors<1),]
} else {sample <- eval(parse(text=paste("tumor.baby.redux2[tumor.baby.redux2$",sample.type,",]")))}
sample$x <- eval(parse(text=paste0("sample$", dv)))
if(sample.type=="winsorized"){ sample$x <- winsorize(sample$x,95) }
if(sample.type=="outlier removal") {sample <- sample[-which(scale(sample$x)>4),]}
# note the model with sync.units as random effect doesn't converge
model <- lmer(x ~ 1 + condition2 + sync.units + syncs + (1|study_respondent), control=strictControl, REML=T, data=sample)
output <- coef(summary(model))[2,]
n <- dim(model@frame)[1]
return(c(output,n))
}
models.zyg <- list()
dvs <- c("zyg.increase.raw", "zyg.increase.pc", "zyg.z", "zyg.hampel.increase")
samples <- c("all", "zyg.error.bigchange<1", "zyg.error.nochange<1", "zyg.n.errors<2", "zyg.n.errors<1", "winsorized", "outlier removal")
options <- expand.grid(dvs, samples)
models.zyg <- lapply(1:dim(options)[1], function(x) do.model(sample.type=options[x,2],dv=options[x,1]))
models.zyg <- do.call("rbind", models.zyg)
models.zyg <- data.frame(models.zyg, options)
models.zyg$lo <- models.zyg$Estimate - 1.96*models.zyg$Std..Error
models.zyg$hi <- models.zyg$Estimate + 1.96*models.zyg$Std..Error
models.zyg$samples2 <- ifelse(models.zyg$Var2=="all","None\nN=1579",
ifelse(models.zyg$Var2=="zyg.error.bigchange<1","Cliffs\n1440",
ifelse(models.zyg$Var2=="zyg.error.nochange<1", "Flat\n1486",
ifelse(models.zyg$Var2=="zyg.n.errors<2", "Errors\n1409",
ifelse(models.zyg$Var2=="zyg.n.errors<1", "1 error\n1333",
ifelse(models.zyg$Var2=="winsorized", "Winsor\n1333",
ifelse(models.zyg$Var2=="outlier removal", "Outliers\n1324", models.zyg$Var2)))))))
models.zyg$samples2 <- factor(models.zyg$samples2, levels=c("None\nN=1579", "Cliffs\n1440", "Flat\n1486", "Errors\n1409", "1 error\n1333", "Winsor\n1333", "Outliers\n1324"))
models.zyg$dv <- ifelse(models.zyg$Var1=="zyg.increase.raw", "Raw",
ifelse(models.zyg$Var1=="zyg.increase.pc", "Percent",
ifelse(models.zyg$Var1=="zyg.z", "Z-score",
ifelse(models.zyg$Var1=="zyg.hampel.increase","Hampel", models.zyg$Var1))))
models <- rbind(models.cor, models.zyg)
models <- data.frame(models)
models <- models[c(2,18,20,24,28,30,46,50,54),]
models$activity <- c(rep("Corrugator",5), rep("Zygomaticus",4))
models$samples3 <- as.character(models$samples2)
models$samples3[3] <- "Hampel\n1393"
models$samples3 <- factor(models$samples3, levels=c("None\nN=1579", "1 error\n1393", "Hampel\n1393","Winsor\n1393", "Outliers\n1384",
"1 error\n1333", "Winsor\n1333", "Outliers\n1324"))
#levels(models$samples2) <- levels(models$samples2)[c(1,)]
preprocessing <- ggplot(models, aes(x=samples3, y=Estimate)) +
geom_point() +
geom_errorbar(data=models, aes(ymin=lo, ymax=hi), width=.2) +
facet_grid(.~activity, scales="free") +
xlab("Data removal or transformation") +
ylab("Estimated change in corrugator activity (microVolt)") +
theme(strip.text=element_text(size=12),
axis.title.x = element_text(size=12),
axis.text.x=element_text(size=10),
axis.title.y=element_text(size=12),
axis.text.y=element_text(size=10)) +
geom_hline(yintercept=0)
ggsave(preprocessing, file=paste0(outdir, "/Figures/preprocessing.pdf"), dpi=900)
ggsave(preprocessing, file=paste0(outdir, "/Figures/preprocessing.pdf"), dpi=900)
ggsave(preprocessing, file=paste0(outdir, "/Figures/preprocessing.pdf"), dpi=900)
# In this script we run different tests and simulations to address the question how to model fEMG data
rm(list=ls())
#outdir <- paste0("C:/Users/gschuma1/Dropbox/Apps/Overleaf/Doing Psychophysiology research in political science")
#change the outdir to a local folder to store the output folders there
outdir <- "outdir"
source("Analysis/descriptives.R") # print and display the data's descriptive statistics
load("All Syncs/emg.RData")
source("Analysis/descriptives.R") # print and display the data's descriptive statistics
library(stargazer)
emg_by_p <- emg[which(emg$syncs==1 & emg$sync.units==1),]
stargazer(data.frame(
emg_by_p$female,
emg_by_p$polknow,
emg_by_p$age,
emg_by_p$alcohol,
emg_by_p$student,
emg_by_p$leftright,
emg_by_p$cynicism,
emg_by_p$political_interest,
emg_by_p$partisanship.strength), summary.stat=c("n", "mean","sd","min", "max")
)
ata.frame(
emg_by_p$female,
emg_by_p$polknow,
emg_by_p$age,
emg_by_p$alcohol,
emg_by_p$student,
emg_by_p$leftright,
emg_by_p$cynicism,
emg_by_p$political_interest,
emg_by_p$partisanship.strength)
emg_by_p$education.label <- factor(ifelse(emg_by_p$education=="Secondary Vocational","Secondary\nVocational",
ifelse(emg_by_p$education=="Higher Vocational","Higher\nVocational",as.character(emg_by_p$education))),
labels=c("Secondary\nVocational", "Secondary", "Higher\nVocational", "University"))
desc.edu <- ggplot(emg_by_p[-which(is.na(emg_by_p$education)),], aes(x=education.label, fill=education.label)) +
geom_bar(aes(y = (..count..)/sum(..count..))) +
scale_y_continuous(labels=scales::percent) +
ylab("Percentage") + xlab("Finished Education level") +
theme(legend.position = "none")
emg_by_p$votechoice_lr <- factor(emg_by_p$votechoice_lr, levels=c("Left","Center","Right","Other"))
desc.vote <- ggplot(emg_by_p, aes(x=votechoice_lr, fill=votechoice_lr)) +
geom_bar(aes(y = (..count..)/sum(..count..))) +
scale_y_continuous(labels=scales::percent) +
ylab("Percentage") + xlab("Vote at last election") +
theme(legend.position = "none")
ggsave(desc.vote, file=paste0(outdir,"/Figures/desc_vote.jpg"))
ggsave(desc.edu, file=paste0(outdir,"/Figures/desc_edu.jpg"))
library(stargazer)
data.frame(
emg_by_p$female,
emg_by_p$polknow,
emg_by_p$age,
emg_by_p$alcohol,
emg_by_p$student,
emg_by_p$leftright,
emg_by_p$cynicism,
emg_by_p$political_interest,
emg_by_p$partisanship.strength)
source("Analysis/modelspecification.R") # run another set of simulations using different variables in the specification.
source("Analysis/iaps_means_comparision.R") # this visualizes corrugator and zygomaticus responses to IAPS images.
library(plyr)
library(ggplot2)
library(dplyr)
library(ggthemes)
cbbPalette <- c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
load( "All Syncs/emg.RData")
load( "All Syncs/scl.RData")
selection <- c("lab replication", "Lab 20")
cor.subset <- emg[is.na(emg$positive)==FALSE & emg$cor.n.errors<1 & emg$last.sync==0 & emg$cor.stat.outlier==0 & emg$study%in%selection & emg$datacollection%in%c("iaps", "threat", "congruence"),]
zyg.subset <- emg[is.na(emg$positive)==FALSE & emg$zyg.n.errors<1 & emg$last.sync==0 & emg$zyg.stat.outlier==0 & emg$study%in%selection & emg$datacollection%in%c("iaps", "threat", "congruence"),]
scl.subset <- scl[is.na(scl$positive)==FALSE & scl$scl.n.errors<1 & scl$last.sync==0 & is.na(scl$scl.increase.pc)==FALSE & scl$condition2!="birds" & scl$scl.increase.pc%in%c("Inf","-Inf")==FALSE & scl$study%in%selection & scl$datacollection%in%c("iaps", "threat", "congruence"),]
scl.subset$scl.stat.outlier <- ifelse(abs(scale(scl.subset$scl.increase.pc))>3, 1, 0)
scl.subset <- scl.subset[scl.subset$scl.stat.outlier==0,]
cor.subset <- cor.subset[order(cor.subset$study_respondent, cor.subset$syncs, cor.subset$sync.units),]
zyg.subset <- zyg.subset[order(zyg.subset$study_respondent, zyg.subset$syncs, zyg.subset$sync.units),]
scl.subset <- scl.subset[order(scl.subset$study_respondent, scl.subset$syncs, scl.subset$sync.units),]
cor <- ddply(cor.subset, .(condition2), summarize,
mean=mean(cor.hampel.increase),
sd=sd(cor.hampel.increase),
length=length(cor.hampel.increase),
se=sd / sqrt(length),
lo=mean - 1.96*se,
hi=mean + 1.96*se)
colnames(cor) <- paste0("cor", colnames(cor))
zyg <- ddply(zyg.subset, .(condition2), summarize,
mean=mean(zyg.increase.pc),
sd=sd(zyg.increase.pc),
length=length(zyg.increase.pc),
se=sd / sqrt(length),
lo=mean - 1.96*se,
hi=mean + 1.96*se)
colnames(zyg) <- paste0("zyg", colnames(zyg))
scl <- ddply(scl.subset, .(condition2), summarize,
mean=mean(scl.increase.pc),
sd=sd(scl.increase.pc),
length=length(scl.increase.pc),
se=sd / sqrt(length),
lo=mean - 1.96*se,
hi=mean + 1.96*se)
colnames(scl) <- paste0("scl", colnames(scl))
joint.data <- data.frame(cor, zyg, scl)
colnames(joint.data)[1] <- "visual"
iaps <- openxlsx::read.xlsx("Markers/overview conditions.xlsx")
selection <- c("A", "B", "C", "D", "E", "F",2)
iaps$valence[iaps$condition%in%selection] <- (iaps$valence[iaps$condition%in%selection]-1) * (100/8)
iaps$arousal[iaps$condition%in%selection] <- (iaps$arousal[iaps$condition%in%selection]-1) * (100/8)
joint.data <- left_join(joint.data, iaps, by="visual")
# A few values are missing; add them here
joint.data$valence[joint.data$visual=="baby3"] <- (8.2-1) * (100/8)
joint.data$valence[joint.data$visual=="basket"] <- (4.94-1) * (100/8)
joint.data$valence[joint.data$visual=="dog1"] <- (3.55-1) * (100/8)
joint.data$valence[joint.data$visual=="dog2"] <- (4.21-1) * (100/8)
joint.data$valence[joint.data$visual=="grandpa"] <- (8.03-1) * (100/8)
joint.data$valence[joint.data$visual=="knive"] <- (2.73-1) * (100/8)
joint.data$valence[joint.data$visual=="spoon"] <- (5.04-1) * (100/8)
joint.data$valence[joint.data$visual=="tumor"] <- (1.46-1) * (100/8)
joint.data$valence.lab <- ifelse(joint.data$valence<40,"Negative",
ifelse(joint.data$valence>60, "Positive", "Neutral"))
ggplot(joint.data, aes(x=cormean, y=sclmean, fill=valence.lab, colour=valence.lab)) +
geom_point() +
geom_errorbarh(aes(xmin=corlo, xmax=corhi)) +
geom_errorbar(aes(ymin=scllo, ymax=sclhi)) +
geom_hline(yintercept=100, colour="red") +
geom_vline(xintercept=100, colour="red") +
xlab("Negative-Positive (corrugator only)") +
ylab("Low-High Arousal") +
ggtitle("Valence-Arousal model with IAPS pictures")
cor.zyg <- ggplot(joint.data, aes(x=cormean, y=zygmean, fill=valence.lab, colour=valence.lab)) +
geom_point(aes(shape=valence.lab)) +
geom_errorbarh(aes(xmin=corlo, xmax=corhi)) +
geom_errorbar(aes(ymin=zyglo, ymax=zyghi)) +
geom_hline(yintercept=100, colour="red") +
geom_vline(xintercept=100, colour="red") +
scale_colour_manual(values=cbbPalette[c(1,2,6)]) +
xlab("Corrugator") +
ylab("Zygomaticus") +
#ggtitle("Positive-Negative with IAPS pictures") +
theme(legend.title = element_blank(),
legend.position=c(0.8,0.8))
#outdir <- "C:/Users/gschuma1/Dropbox/Apps/Overleaf/Doing Psychophysiology research in political science"
ggsave(cor.zyg, file=paste0(outdir, "/Figures/cor_zyg.jpg"), dpi=900)
cor(joint.data$zygmean, joint.data$valence, use="pairwise.complete.obs")
cor(joint.data$cormean, joint.data$valence, use="pairwise.complete.obs")
# Add comparison data -------
selection <- c("lab replication", "Lab 20")
cor.subset2 <- emg[is.na(emg$positive)==FALSE & emg$last.sync==0 & emg$study%in%selection & emg$datacollection%in%c("iaps", "threat", "congruence"),]
zyg.subset2 <- emg[is.na(emg$positive)==FALSE & emg$last.sync==0 & emg$study%in%selection & emg$datacollection%in%c("iaps", "threat", "congruence"),]
cor.subset2 <- cor.subset2[order(cor.subset2$study_respondent, cor.subset2$syncs, cor.subset2$sync.units),]
zyg.subset2 <- zyg.subset2[order(zyg.subset2$study_respondent, zyg.subset2$syncs, zyg.subset2$sync.units),]
cor2 <- ddply(cor.subset2, .(condition2), summarize,
mean=mean(cor.hampel.increase),
sd=sd(cor.hampel.increase),
length=length(cor.hampel.increase),
se=sd / sqrt(length),
lo=mean - 1.96*se,
hi=mean + 1.96*se)
colnames(cor2) <- paste0("cor2", colnames(cor2))
zyg2 <- ddply(zyg.subset2, .(condition2), summarize,
mean=mean(zyg.increase.pc),
sd=sd(zyg.increase.pc),
length=length(zyg.increase.pc),
se=sd / sqrt(length),
lo=mean - 1.96*se,
hi=mean + 1.96*se)
colnames(zyg2) <- paste0("zyg2", colnames(zyg2))
colnames(cor2)[1] <- "visual"
colnames(zyg2)[1] <- "visual"
joint.data <- left_join(joint.data, cor2, by="visual")
joint.data <- left_join(joint.data, zyg2, by="visual")
cor(joint.data$zyg2mean, joint.data$valence, use="pairwise.complete.obs")
cor(joint.data$cor2mean, joint.data$valence, use="pairwise.complete.obs")
cor(joint.data$cor2mean, joint.data$cormean, use="pairwise.complete.obs")
cor(joint.data$zyg2mean, joint.data$zygmean, use="pairwise.complete.obs")
outdir
outdir <- "outdir"
ggsave(cor.zyg, file=paste0(outdir, "/Figures/cor_zyg.jpg"), dpi=900)
source("Analysis/analysis.R") # this runs the main set of preregistered analyses
library(stargazer)
library(stringr)
library(lme4)
library(lmerTest)
library(ggplot2)
library(ggthemes)
load( "All Syncs/emg.RData")
# Basic setup -------------------------------------------------------------
strictControl <- lmerControl(optCtrl = list(
algorithm = "NLOPT_LN_NELDERMEAD",
xtol_abs = 1e-12,
ftol_abs = 1e-12))
winsorize <- function(var, prob){
low <- paste0(100-prob,"%")
high <- paste0(prob,"%")
quantiles <- quantile(var, probs=seq(0,1,0.05), na.rm=TRUE)
new.var <- ifelse(var<quantiles[low],quantiles[low],
ifelse(var>quantiles[high],quantiles[high],var))
return(new.var)
}
cor.analysis <- emg[emg$treatment==1 & emg$cor.n.errors<1 & emg$last.sync==0,]
zyg.analysis <- emg[emg$treatment==1 & emg$zyg.n.errors<1 & emg$last.sync==0,]
cor.analysis$lab <- ifelse(cor.analysis$location=="lab",1,0)
cor.analysis$emotion <- factor(ifelse(cor.analysis$positive==1, 1,
ifelse(cor.analysis$negative==1, 2,
ifelse(cor.analysis$negative==0 & cor.analysis$positive==0,0,NA))))
cor.analysis$cor.increase.pc.win <- winsorize(cor.analysis$cor.increase.pc, 95)
cor.analysis$cor.hampel.increase <- cor.analysis$cor.hampel.increase
cor.analysis$cor.increase.pc.win <- cor.analysis$cor.increase.pc.win
zyg.analysis$zyg.increase.pc.win <- winsorize(zyg.analysis$zyg.increase.pc, 95)
zyg.analysis$lab <- ifelse(zyg.analysis$location=="lab",1,0)
zyg.analysis$emotion <- factor(ifelse(zyg.analysis$positive==1, 1,
ifelse(zyg.analysis$negative==1, 2,
ifelse(zyg.analysis$negative==0 & zyg.analysis$positive==0,0,NA))))
zyg.analysis$zyg.increase.pc <- zyg.analysis$zyg.increase.pc
zyg.analysis$zyg.increase.pc.win <- zyg.analysis$zyg.increase.pc.win
cor.analysis$female[which(cor.analysis$female==2)] <- 1
zyg.analysis$female[which(zyg.analysis$female==2)] <- 1
cor.analysis$leftright <- scale(cor.analysis$leftright)
cor.analysis$age <- scale(cor.analysis$age)
zyg.analysis$leftright <- scale(zyg.analysis$leftright)
zyg.analysis$age <- scale(zyg.analysis$age)
robustness <- function(dv, treatment){
if(substr(dv,1,3)=="zyg"){
formula <- as.formula(paste0(dv, "~ 1 +", treatment, "+ sync.units + syncs + temperature + factor(computer) + (sync.units|study_respondent)"))
if(str_sub(dv,-3, -1)=="win"){model <- lmer(formula, REML=T, control=strictControl,data=zyg.analysis)
} else {model <- lmer(formula, REML=T, control=strictControl,data=zyg.analysis, subset=zyg.stat.outlier==0)
}
return(model)
}}
robustness.analyses <- lapply(1:dim(options)[1], function(x) robustness(dv=options[x,1],treatment=options[x,2]))
# Correction to preregistered model Q1: -----------------
# Because the preregistered models didn't converge. We ran slightly simpler models (taking out the time higher level) and report them here. The code that was commented out was the original preregistered code
treatment.chars <- c("political", "label", "face","emotion", "sound", "lab")
DVs <- c("cor.hampel.increase", "cor.increase.pc.win", "zyg.increase.pc", "zyg.increase.pc.win")
options <- expand.grid(DVs, treatment.chars)
main.analysis <- function(dv, treatment){
if(substr(dv,1,3)=="zyg" & treatment=="emotion"){formula <- as.formula(paste0(dv, "~ 1 +", treatment, "+ sync.units + syncs + (1|study_respondent)"))
} else{formula <- as.formula(paste0(dv, "~ 1 +", treatment, "+ sync.units + syncs + location + temperature + (1|study_respondent)"))
}
if(substr(dv,1,3)=="cor"){
if(str_sub(dv,-3, -1)=="win"){model <- lmer(formula, REML=T, control=strictControl,data=cor.analysis)
} else {model <- lmer(formula, REML=T, control=strictControl,data=cor.analysis, subset=cor.stat.outlier==0)
}
} else if(str_sub(dv,-3, -1)=="win"){model <- lmer(formula, REML=T, control=strictControl,data=zyg.analysis)
} else {model <- lmer(formula, REML=T, control=strictControl,data=zyg.analysis, subset=zyg.stat.outlier==0)
}
return(model)
}
robustness.check2 <- list()
for(i in 1:24){
robustness.check2[[i]] <- main.analysis(dv=options[i,1],treatment=options[i,2])
print(i)
}
save(robustness.check2, file="main.analyses_correction.RData")
save(robustness.check2, file="Analysis/main.analyses_correction.RData")
# Q1 make figure ----------------------------------------------------------
load("Analysis/main.analyses_correction.RData")
coefs <- lapply(robustness.check2, function(x){
if(colnames(x@frame)[2]%in%c("label", "emotion")){
output <- rbind(c(coef(summary(x))[2,1],coef(summary(x))[2,2],coef(summary(x))[2,5]),
c(coef(summary(x))[3,1],coef(summary(x))[3,2],coef(summary(x))[3,5]))
} else {output <- c(coef(summary(x))[2,1],coef(summary(x))[2,2],coef(summary(x))[2,5])}
return(output)
})
coefs <- do.call("rbind", coefs)
coefs <- data.frame(coefs)
colnames(coefs) <- c("b", "se", "p")
ex.options <- rbind(options[1:4,],
options[c(5,5,6,6,7,7,8,8),],
options[9:12,],
options[c(13,13,14,14,15,15,16,16),], # squeeze in two factors
options[17:24,])
coefs <- data.frame(coefs, ex.options)
rownames(coefs) <- NULL
coefs$temp1 <- ifelse(rownames(coefs)%in%c("18","20","22","24"),1,0)
coefs$temp2 <- ifelse(rownames(coefs)%in%c("6","8","10","12"),1,0)
coefs$lo <- coefs$b - 1.96*coefs$se
coefs$hi <- coefs$b + 1.96*coefs$se
coefs$dv <- ifelse(str_sub(as.character(coefs$Var1),1,3)=="cor","DV: Corrugator", "DV: Zygomaticus")
coefs$transform <- ifelse(str_sub(as.character(coefs$Var1),-3,-1)=="win", "Win", "Out")
coefs$labels <- ifelse(coefs$Var2=="political", "IV: Political/\nNot political",
ifelse(coefs$Var2=="label" & coefs$temp2==0, "IV: Video/\nImage",
ifelse(coefs$Var2=="label" & coefs$temp2==1, "IV: Word/\nImage",
ifelse(coefs$Var2=="face", "IV: Face/\nIssue",
ifelse(coefs$Var2=="sound", "IV: Sound/\nNo sound",
ifelse(coefs$Var2=="lab", "IV: Lab/\nFieldlab",
ifelse(coefs$Var2=="emotion" & coefs$temp1==0, "IV: Positive/\nNeutral",
ifelse(coefs$Var2=="emotion" & coefs$temp1==1, "IV:Negative/\nNeutral",coefs$Var2))))))))
coefs$labels <- factor(coefs$labels, levels=c("IV: Lab/\nFieldlab", "IV:Negative/\nNeutral", "IV: Positive/\nNeutral", "IV: Video/\nImage", "IV: Word/\nImage", "IV: Sound/\nNo sound", "IV: Political/\nNot political","IV: Face/\nIssue"))
main.analyses.plot <- ggplot(coefs, aes(x=transform, y=b)) +
geom_point() +
geom_errorbar(aes(ymin=lo, ymax=hi), width=.2) +
facet_grid(dv~labels, scales="free") + ylab("Character effect") + xlab("")
ggsave(main.analyses.plot, file=paste0(outdir,"/Figures/main.analyses.plot.jpg"), dpi=900)
coefs.subset <- coefs[1:28,]
subset.analyses.plot <- ggplot(coefs.subset, aes(x=transform, y=b)) +
geom_point() +
geom_errorbar(aes(ymin=lo, ymax=hi), width=.2) + geom_hline(yintercept=0, colour="red") +
facet_grid(dv~labels, scales="free") + ylab("Characteristic effect expressed as microVolt change from baseline") + xlab("")
ggsave(subset.analyses.plot, file=paste0(outdir,"/Figures/subset.analyses.plot.pdf"))
load("Analysis/moderator.analyses_selection.RData")
result <- list()
for(i in 1:dim(selection)[1]){
if(i<3){
means <- emmeans(moderator.analyses.selection[[i]],~female*emotion)
result[[i]] <- data.frame(contrast(means,"revpairwise",by="emotion"))[,c(2,3,4)]}
if(i%in%c(3,4)){result[[i]] <- data.frame(emtrends(moderator.analyses.selection[[i]],~emotion, var="age"))[,c(1,2,3)]}
if(i%in%c(5,6)){result[[i]] <- data.frame(emtrends(moderator.analyses.selection[[i]],~emotion, var="leftright"))[,c(1,2,3)]}
if(i%in%c(7,8)){
means <- emmeans(moderator.analyses.selection[[i]],~education*emotion)
result[[i]] <- data.frame(contrast(means,"revpairwise",by="emotion"))[c(1,3,5,7,9,11,13,15,17),c(2,3,4)]}
colnames(result[[i]]) <- c("emotion","estimate","se")
result[[i]]$var <- selection[i,3]
}
# Q3: Moderators ---------------------------------------------------
load("Analysis/moderator.analyses_selection.RData")
result <- list()
# Correction to preregistered model Q1: -----------------
# Because the preregistered models didn't converge. We ran slightly simpler models (taking out the time higher level) and report them here. The code that was commented out was the original preregistered code
treatment.chars <- c("political", "label", "face","emotion", "sound", "lab")
moderators <- c("female","polknow","age","alcohol","student","leftright","cynicism",
"political_interest","votechoice_lr","education")
DVs <- c("cor.hampel.increase", "cor.increase.pc.win", "zyg.increase.pc", "zyg.increase.pc.win")
options <- expand.grid(DVs,treatment.chars, moderators)
selection <- options[which(options$Var2%in%c("emotion")),]
selection <- selection[which(selection$Var3%in%c("female", "age", "leftright", "education")),]
selection <- selection[which(selection$Var1%in%c("cor.hampel.increase", "zyg.increase.pc")),]
length(moderator.analyses.selection)
for(i in 1:length(moderator.analyses.selection)){
if(i<3){
means <- emmeans(moderator.analyses.selection[[i]],~female*emotion)
result[[i]] <- data.frame(contrast(means,"revpairwise",by="emotion"))[,c(2,3,4)]}
if(i%in%c(3,4)){result[[i]] <- data.frame(emtrends(moderator.analyses.selection[[i]],~emotion, var="age"))[,c(1,2,3)]}
if(i%in%c(5,6)){result[[i]] <- data.frame(emtrends(moderator.analyses.selection[[i]],~emotion, var="leftright"))[,c(1,2,3)]}
if(i%in%c(7,8)){
means <- emmeans(moderator.analyses.selection[[i]],~education*emotion)
result[[i]] <- data.frame(contrast(means,"revpairwise",by="emotion"))[c(1,3,5,7,9,11,13,15,17),c(2,3,4)]}
colnames(result[[i]]) <- c("emotion","estimate","se")
result[[i]]$var <- selection[i,3]
}
library(emmeans)
for(i in 1:length(moderator.analyses.selection)){
if(i<3){
means <- emmeans(moderator.analyses.selection[[i]],~female*emotion)
result[[i]] <- data.frame(contrast(means,"revpairwise",by="emotion"))[,c(2,3,4)]}
if(i%in%c(3,4)){result[[i]] <- data.frame(emtrends(moderator.analyses.selection[[i]],~emotion, var="age"))[,c(1,2,3)]}
if(i%in%c(5,6)){result[[i]] <- data.frame(emtrends(moderator.analyses.selection[[i]],~emotion, var="leftright"))[,c(1,2,3)]}
if(i%in%c(7,8)){
means <- emmeans(moderator.analyses.selection[[i]],~education*emotion)
result[[i]] <- data.frame(contrast(means,"revpairwise",by="emotion"))[c(1,3,5,7,9,11,13,15,17),c(2,3,4)]}
colnames(result[[i]]) <- c("emotion","estimate","se")
result[[i]]$var <- selection[i,3]
}
emotion.analysis <- dplyr::bind_rows(result)
emotion.analysis$x <- factor(ifelse(emotion.analysis$emotion==0, "Neutral",
ifelse(emotion.analysis$emotion==1, "Positive", "Negative")), levels=c("Negative", "Neutral", "Positive"))
emotion.analysis$group <- c(rep("Female\nvs Male",6),
rep("Age",6),rep("Left-\nRight",6),
rep(c("Sec vs\nSec Voc","High vs\nSec Voc","Uni vs\nSec Voc"),6))
emotion.analysis$conf.lo <- emotion.analysis$estimate - 1.96*emotion.analysis$se
emotion.analysis$conf.hi <- emotion.analysis$estimate + 1.96*emotion.analysis$se
emotion.analysis$group <- factor(emotion.analysis$group,
levels=c("Age","Female\nvs Male","Left-\nRight", "Sec vs\nSec Voc","High vs\nSec Voc","Uni vs\nSec Voc"))
emotion.analysis$x <- ifelse(emotion.analysis$x=="Negative","neg",
ifelse(emotion.analysis$x=="Positive","pos","neu"))
emotion.analysis$dv <- c(rep("Corrugator",3),rep("Zygomaticus",3),
rep("Corrugator",3),rep("Zygomaticus",3),
rep("Corrugator",3),rep("Zygomaticus",3),
rep("Corrugator",9),rep("Zygomaticus",9))
#emotion.analysis$iv <- c(rep("female",12), rep("age",12), rep("ideology",8), rep("education",24))
#emotion.analysis$x2 <- paste0(emotion.analysis$group,"\n",emotion.analysis$x)
emotion <- ggplot(data=emotion.analysis, aes(x=x, y=estimate)) +
geom_point(position = position_dodge(width=1)) +
geom_errorbar(data=emotion.analysis, aes(ymin=conf.lo, ymax=conf.hi), position = position_dodge(width=1), width=.2) +
facet_grid(dv~group, scales="free") +
xlab("Treatment characteristics") +
ylab("Marginal effect") +
geom_hline(yintercept=0, colour="red") +
theme(legend.position="bottom",
legend.title=element_blank(),
axis.text.x=element_text(size=6)
)
ggsave(emotion, file=paste0(outdir,"/Figures/emotion.pdf"), dpi=900)
# Load data ---------------------------------------------------------------
coder1_cor <- read_excel("Annotations/codingalltreatments_coder1.xlsx", sheet="cor")
library(readxl)
library(icr)
library(stringr)
library(dplyr)
# Load data ---------------------------------------------------------------
coder1_cor <- read_excel("Annotations/codingalltreatments_coder1.xlsx", sheet="cor")
coder1_labii <- read_excel("Annotations/codingalltreatments_coder1.xlsx", sheet="labii")
coder2_labii <- read_excel("Annotations/codingalltreatments_coder2.xlsx", sheet="labii")
coder2_cor <- read_excel("Annotations/codingalltreatments_coder2.xlsx", sheet="cor")
coder2_zyg <- read_excel("Annotations/codingalltreatments_coder2.xlsx", sheet="zyg")
coder2_cor <- read_excel("Annotations/codingalltreatments_coder2.xlsx", sheet="cor")
coder2_zyg <- read_excel("Annotations/codingalltreatments_coder2.xlsx", sheet="zyg")
coder2_scl <- read_excel("Annotations/codingalltreatments_coder2.xlsx", sheet="scl_new")
coder3_zyg <- read_excel("Annotations/codingalltreatments_coder3.xlsx", sheet="zyg")
coder3_scl <- read_excel("Annotations/codingalltreatments_coder3.xlsx", sheet="scl")
coder4_zyg <- read_excel("Annotations/codingalltreatments_coder4.xlsx", sheet="zyg")
coder4_scl <- read_excel("Annotations/codingalltreatments_coder4.xlsx", sheet="scl new")
# Comparison corrugator ---------------------------------------------------
respondents <- str_sub(list.files(path="timeplots/cor/"),start=6, end=-5)
# Comparison corrugator ---------------------------------------------------
respondents <- str_sub(list.files(path="C:/Users/gschuma1/surfdrive/Papers/Multimodal Physiology/Annotations/timeplots/cor/"),start=6, end=-5)
respondents
save(respondents, file="Annotations/coded_respondents.RData")
# Comparison corrugator ---------------------------------------------------
#respondents <- str_sub(list.files(path="timeplots/cor/"),start=6, end=-5)
load("Annotations/coded_respondents.RData")
respondents
coder1 <- ifelse(respondents%in%coder1_cor$study_respondent,1,0)
coder2 <- ifelse(respondents%in%gijs_cor$study_respondent,1,0)
coder2 <- ifelse(respondents%in%coder2_cor$study_respondent,1,0)
cor.coding <- rbind(coder1, coder2)
krippalpha(cor.coding, metric="nominal") # alpha = 0.864, n=585