-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07_anovas.R
executable file
·110 lines (85 loc) · 3.93 KB
/
07_anovas.R
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
# ==================
# === Run anovas ===
# ==================
# set working directory
setwd('/Users/philippe/Desktop/projects/emotion')
# detach 'other packages' if there are any
if (!is.null(names(sessionInfo()$otherPkgs))) {
invisible(lapply(paste('package:',names(sessionInfo()$otherPkgs),sep=""),detach,character.only=TRUE,unload=TRUE))
}
# activate R environment
if (exists('.rs.restartR', mode = 'function')) { .rs.restartR() }
source('renv/activate.R')
renv::activate(getwd())
renv::restore(prompt = FALSE)
# attach packages to current R session
library(reshape2)
library(rstatix)
library(dplyr)
# ------------------------------------------------
# --- ER success - picture x strategy rm-ANOVA ---
# ------------------------------------------------
# load data
df = read.delim('code/derivatives/main_winsor_longlist.txt', sep = '\t', header = TRUE)
# create list of variables
measure = c('ER_valence', 'ER_arousal', 'Corru', 'HP', 'SCR', 'Wins_ER_valence', 'Wins_ER_arousal', 'Wins_Corru', 'Wins_HP', 'Wins_SCR')
for (i in measure) {
vars = paste0(i, c('_neg_permit', '_neg_distancing', '_neu_permit', '_neu_distancing'))
# create data.frame for anova
df.anova = df[, c('id', vars)]
df.anova = df.anova[complete.cases(df.anova),]
df.anova = melt(df.anova, id="id", variable.name="condition", value.name="score", na.rm = F)
df.anova$picture = ""
df.anova$strategy = ""
df.anova$picture[grep('neg',df.anova$condition)] = 'negative'
df.anova$picture[grep('neu',df.anova$condition)] = 'neutral'
df.anova$strategy[grep('permit',df.anova$condition)] = 'permit'
df.anova$strategy[grep('distancing',df.anova$condition)] = 'distancing'
# carry out repeated-measures anova and save results
tmp = anova_test( data = df.anova, dv = score, wid = id, within = c(picture,strategy), effect.size = 'pes')
assign(paste0(i, '.anova'), tmp)
}
# create results data frame
results = data.frame(matrix(NA, nrow = length(measure), ncol = 15))
colnames(results) = apply(expand.grid(c('F', 'dfn', 'dfd', 'p', 'eta2'),c('picture', 'strategy', 'picture_by_strategy'))[c(2,1)], 1, paste, collapse=".")
k = 0
for (i in measure) {
k = k +1
tmp = get(paste0(i,'.anova'))
results[k,c(1,6,11,2,7,12,3,8,13,4,9,14,5,10,15)] = c(tmp$F,tmp$DFn, tmp$DFd, tmp$p, tmp$pes)
rownames(results)[k] = i
}
# write.table
write.table(data.frame(measure = rownames(results), results), 'code/tables/anova_ERsucc.txt', sep = '\t', row.names = F, quote = F)
# -------------------------------------------------
# --- ER success (negative) - strategy rm-ANOVA ---
# -------------------------------------------------
# load data
df = read.delim('code/derivatives/main_winsor_longlist.txt', sep = '\t', header = TRUE)
# create list of variables
measure = c('ER_valence', 'ER_arousal', 'Corru', 'HP', 'SCR', 'Wins_ER_valence', 'Wins_ER_arousal', 'Wins_Corru', 'Wins_HP', 'Wins_SCR')
for (i in measure) {
vars = paste0(i, c('_neg_permit', '_neg_distancing'))
# create data.frame for anova
df.anova = df[, c('id', vars)]
df.anova = df.anova[complete.cases(df.anova),]
df.anova = melt(df.anova, id="id", variable.name="condition", value.name="score", na.rm = F)
df.anova$strategy = ""
df.anova$strategy[grep('permit',df.anova$condition)] = 'permit'
df.anova$strategy[grep('distancing',df.anova$condition)] = 'distancing'
# carry out repeated-measures anova and save results
tmp = anova_test( data = df.anova, dv = score, wid = id, within = c(strategy), effect.size = 'pes')
assign(paste0(i, '.anova'), tmp)
}
# create results data frame
results = data.frame(matrix(NA, nrow = length(measure), ncol = 5))
colnames(results) = paste0('strategy_', c('F', 'dfn', 'dfd', 'p', 'eta2'))
k = 0
for (i in measure) {
k = k +1
tmp = get(paste0(i,'.anova'))
results[k,] = c(tmp$F,tmp$DFn, tmp$DFd, tmp$p, tmp$pes)
rownames(results)[k] = i
}
# write.table
write.table(data.frame(measure = rownames(results), results), 'code/tables/anova_ERsucc_neg.txt', sep = '\t', row.names = F, quote = F)