-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis-LTD.R
274 lines (229 loc) · 20.6 KB
/
analysis-LTD.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
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
###########################################################################################################################################################################################################################################################################################################################################################################################################################################
# Moodmecodynamics: Roel van Dooren, Roy de Kleijn, Bernhard Hommel, & Zsuzsika Sjoerds
###########################################################################################################################################################################################################################################################################################################################################################################################################################################
### Last adjustment on: 27-01-2020
### r.van.dooren@fsw.leidenuniv.nl
options(scipen = 20)
rm(list = ls()) # Clean up workspace.
###########################################################################################################################################################################################################################################################################################################################################################################################################################################
# Import libraries --------------------------------------------------------
###########################################################################################################################################################################################################################################################################################################################################################################################################################################
ipak <- function(pkg) {
new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if (length(new.pkg))
install.packages(new.pkg, dependencies = T)
sapply(pkg, require, character.only = T)
}
packages <- c("plyr", "zeallot", "reshape2", "data.table", "car", "Rmisc", "report", "effsize", "BayesFactor",
"ggplot2", "ez", "apaTables", "dplyr") # Make sure to load dplyr after plyr and ggplot2
ipak(packages)
###########################################################################################################################################################################################################################################################################################################################################################################################################################################
# Load the main workspace -------------------------------------------------
###########################################################################################################################################################################################################################################################################################################################################################################################################################################
load("./workspaces/mood-workspace.RData")
###########################################################################################################################################################################################################################################################################################################################################################################################################################################
# Merge data of the foraging runs -----------------------------------------
###########################################################################################################################################################################################################################################################################################################################################################################################################################################
leavetimes_baseline <- read.csv2('./data-processed/baseline-optimal-actual-leavetimes.txt', header = T, sep = ' ', dec = '.', stringsAsFactors = TRUE)
leavetimes_postmanipulation <- read.csv2('./data-processed/post-manipulation-optimal-actual-leavetimes.txt',
header = T, sep = ' ', dec = '.', stringsAsFactors = TRUE)
df.list <- list(baseline = leavetimes_baseline, post = leavetimes_postmanipulation)
c(leavetimes_baseline, leavetimes_postmanipulation) %<-%
lapply(names(df.list), function(df) {
dataframe_name = df # Extract the name of the dataframe
df <- df.list[[dataframe_name]] # Extract the data
if (dataframe_name == 'baseline') { df$phase = 'baseline' } else { df$phase = 'post-manipulation' }
df <- df %>% mutate(diff = optimal - actual) %>%
select(-c(actual, optimal)) %>% group_by(subjectID, condition, phase) %>%
summarise(MU = mean(diff),
SD = sd(diff, na.rm = T),
N = sum(!is.na(diff))
) %>% ungroup() %>% arrange(MU)
; return(df)})
# Bind the two dataframes
merged_leavetimes <- rbind(leavetimes_baseline, leavetimes_postmanipulation) %>% arrange(subjectID)
# Let's remove participants which don't show any variance in the baseline trial
if (length(subj_to_remove) > 0) {
merged_leavetimes <- merged_leavetimes[-which(merged_leavetimes$subjectID %in% subj_to_remove),]
}
# Check whether there are baseline differences in foraging behavior ~ mood induction condition
model <- ezANOVA(data = leavetimes_baseline[-which(leavetimes_baseline$subjectID %in% subj_to_remove),],
dv = MU, wid = .(subjectID), between = .(condition), type = 3, detailed = TRUE, return_aov = T)
apa.ezANOVA.table(model); calculateEtaSquared(model)$ANOVA$partialetasquared
###########################################################################################################################################################################################################################################################################################################################################################################################################################################
# Perform regression analyses ---------------------------------------------
###########################################################################################################################################################################################################################################################################################################################################################################################################################################
# Detect whether there are outliers in the data
ggplot(merged_leavetimes) + geom_boxplot(aes(condition, MU, color = phase))
get.outliers <- function(df) { data.frame(MU = boxplot.stats(df$MU, coef = 2.2)$out) }
outliers <- merged_leavetimes %>% group_by(phase, condition) %>% do(get.outliers(.))
outliers.participants <- merged_leavetimes %>% inner_join(outliers) %>% pull(subjectID)
length(unique(outliers.participants))
merged_leavetimes <- merged_leavetimes[-which(merged_leavetimes$subjectID %in% outliers.participants),]
# One of the questions we would like to answer, is whether or not valence and/or arousal ratings are correlated with foraging
# behavior. As a first step, we want to check whether the mean valence/arousal ratings during foraging (pre and post
# induction manipulation) predict optimal foraging behavior. Therefore, we can define two regression models:
# 1. Baseline behavior: Optimal foraging score ~ meanValence (= mean(1, 2)) * meanArousal (= mean(1, 2))
# 2. Post-induction behavior: Optimal foraging score ~ meanValence (= mean(5, 6)) * meanArousal (= mean(5, 6))
lm_dat_format <- data.table::dcast(setDT(merge(merged_leavetimes, mood_data)),
subjectID + condition + phase + MU + SD ~ block, value.var = c("pleasurerating", "arousalrating")) %>%
group_by(subjectID, phase) %>% mutate(
pleasurerating = ifelse(phase == 'baseline', mean(c(pleasurerating_1, pleasurerating_2)),
mean(c(pleasurerating_5, pleasurerating_6 ))),
arousalrating = ifelse(phase == 'baseline', mean(c(arousalrating_1, arousalrating_2)),
mean(c(arousalrating_5, arousalrating_6 )))
) %>%
select(subjectID, condition, phase, MU, SD, pleasurerating, arousalrating) %>%
ungroup() %>% group_by(phase) %>% mutate(
pleasurerating = scale(pleasurerating, center = T, scale = F),
arousalrating = scale(arousalrating, center = T, scale = F),
MU = scale(MU, center = T, scale = F),
SD = scale(SD, center = T, scale = F))
# First of all, let's check whether the newly created arousal and pleasure values were sign different for the conditions:
lm_dat_format %>% group_by(phase) %>%
do(wrapper.t.test(., formula = arousalrating ~ condition) ) %>%
mutate(t.rounded = round(t, 2), p.rounded = round(p, 3), direction = 'greater') %>%
ungroup() %>% mutate(p.adjusted = round(p.adjust(p.rounded, method = "holm"), 3))
lm_dat_format %>% group_by(phase) %>%
do(wrapper.t.test(., formula = pleasurerating ~ condition, direction = 'greater') ) %>%
mutate(t.rounded = round(t, 2), p.rounded = round(p, 3)) %>%
ungroup() %>% mutate(p.adjusted = round(p.adjust(p.rounded, method = "holm"), 3))
#########################
# 1) Baseline foraging ~ mood
#########################
with(lm_dat_format %>% filter(phase == 'baseline'), cor.test(as.numeric(condition), arousalrating, alternative = 'two.sided', method='pearson'))
with(lm_dat_format %>% filter(phase == 'baseline'), correlationBF(as.numeric(condition), arousalrating))
with(lm_dat_format %>% filter(phase == 'baseline'), cor.test(as.numeric(condition), pleasurerating, alternative = 'two.sided', method='pearson'))
with(lm_dat_format %>% filter(phase == 'baseline'), correlationBF(as.numeric(condition), pleasurerating))
simple <- lm(MU ~ arousalrating + pleasurerating, data = lm_dat_format %>% filter(phase == 'baseline'))
#simple <- lm(MU ~ arousalrating + pleasurerating + condition, data = lm_dat_format %>% filter(phase == 'baseline'))
twoway <- update(simple, .~. + arousalrating : pleasurerating)
#twoway <- update(simple, .~. + arousalrating : pleasurerating + arousalrating : condition + pleasurerating : condition)
#threeway <- update(twoway, .~. + arousalrating : pleasurerating : condition)
anova(simple, twoway); BIC(simple); BIC(twoway)#; BIC(threeway)
summary(simple)
simple %>% report()# %>% table_long()
### Check model assumptions
# Assess assumption of independent errors
durbinWatsonTest(simple) # Note that p is bootstrapped, and will therefore always return slightly different results
# Assess the assumption of no multicollinearity
vif(simple) # No predictor's VIF > 10, all fine
1 / vif(simple) # Tolerance > 0.2, all fine
# Visually assess the assumptions of normality of residuals, linearity of residuals and homoscedasticity
par(mfrow = c(2, 2))
plot(simple)
# Pre-mood induction: regress MU on pleasure and arousal measurements
lmPleasureArousal <- lmBF(MU ~ pleasurerating + arousalrating, data = lm_dat_format %>% filter(phase=="baseline") )
#lmPleasureArousal <- lmBF(MU ~ pleasurerating + arousalrating + condition, data = lm_dat_format %>% filter(phase=="baseline") )
lmPleasureArousal # BF10
1/lmPleasureArousal # BF01 (= 1/BF10)
#########################
# 2) Post-manipulation foraging ~ mood
#########################
with(lm_dat_format %>% filter(phase == 'post-manipulation'), cor.test(as.numeric(condition), arousalrating, alternative = 'two.sided', method='pearson'))
with(lm_dat_format %>% filter(phase == 'post-manipulation'), correlationBF(as.numeric(condition), arousalrating))
with(lm_dat_format %>% filter(phase == 'post-manipulation'), cor.test(as.numeric(condition), pleasurerating, alternative = 'two.sided', method='pearson'))
with(lm_dat_format %>% filter(phase == 'post-manipulation'), correlationBF(as.numeric(condition), pleasurerating))
simple <- lm(MU ~ arousalrating + pleasurerating, data = lm_dat_format %>% filter(phase == 'post-manipulation'))
#simple <- lm(MU ~ arousalrating + pleasurerating + condition, data = lm_dat_format %>% filter(phase == 'post-manipulation'))
twoway <- update(simple, .~. + arousalrating : pleasurerating)
#twoway <- update(simple, .~. + arousalrating : pleasurerating + arousalrating : condition + pleasurerating : condition)
#threeway <- update(twoway, .~. + arousalrating : pleasurerating : condition)
anova(simple, twoway); BIC(simple); BIC(twoway)#; BIC(threeway)
summary(simple)
simple %>% report() #%>% table_long()
### Check model assumptions
# Assess assumption of independent errors
durbinWatsonTest(simple) # Note that p is bootstrapped, and will therefore always return slightly different results
# Assess the assumption of no multicollinearity
vif(simple) # No predictor's VIF > 10, all fine
1 / vif(simple) # Tolerance > 0.2, all fine
# Visually assess the assumptions of normality of residuals, linearity of residuals and homoscedasticity
par(mfrow = c(2, 2))
plot(simple)
# Post-mood induction: regress MU on pleasure and arousal measurements
lmPleasureArousal <- lmBF(MU ~ pleasurerating + arousalrating, data = lm_dat_format %>% filter(phase=="post-manipulation"))
#lmPleasureArousal <- lmBF(MU ~ pleasurerating + arousalrating + condition, data = lm_dat_format %>% filter(phase=="post-manipulation"))
lmPleasureArousal # BF10
1/lmPleasureArousal # BF01 (= 1/BF10)
#########################
# 3) Delta foraging ~ delta mood
#########################
# Next, to have a look at more dynamic components, we look at how changes in valence and/or arousal ratings (delta-scores)
# correlate with changes in optimal foraging behavior (delta-score). In other words, we can define the following regression model:
# 1. Delta optimal foraging score (post - baseline) ~ deltaValence (= mean(5, 6) - mean(1, 2)) * deltaArousal (= mean(5, 6) - mean(1, 2))
lm_dat_format_delta <- lm_dat_format %>% group_by(subjectID) %>% mutate(
deltaMU = MU[phase == 'post-manipulation'] - MU[phase == 'baseline'],
deltaSD = SD[phase == 'post-manipulation'] - SD[phase == 'baseline'],
deltaPleasure = pleasurerating[phase == 'post-manipulation'] - pleasurerating[phase == 'baseline'],
deltaArousal = arousalrating[phase == 'post-manipulation'] - arousalrating[phase == 'baseline']
) %>% select(subjectID, condition, deltaMU, deltaSD, deltaPleasure, deltaArousal) %>% distinct()
with(lm_dat_format_delta, cor.test(as.numeric(condition), deltaArousal, alternative = 'two.sided', method='pearson'))
with(lm_dat_format_delta, correlationBF(as.numeric(condition), deltaArousal))
with(lm_dat_format_delta, cor.test(as.numeric(condition), deltaPleasure, alternative = 'two.sided', method='pearson'))
with(lm_dat_format_delta, correlationBF(as.numeric(condition), deltaPleasure))
simple <- lm(deltaMU ~ deltaArousal + deltaPleasure, data = lm_dat_format_delta )
#simple <- lm(deltaMU ~ deltaArousal + deltaPleasure + condition, data = lm_dat_format_delta )
twoway <- update(simple, .~. + deltaPleasure : deltaArousal)
#twoway <- update(simple, .~. + deltaPleasure : deltaArousal + condition : deltaPleasure + condition : deltaArousal)
#threeway <- update(twoway, .~. + deltaArousal : deltaPleasure : condition)
anova(simple, twoway); BIC(simple); BIC(twoway)#; BIC(threeway)
summary(simple)
simple %>% report() #%>% table_long()
### Check model assumptions
# Assess assumption of independent errors
dw_pval <- c()
for (i in seq(1, 1000)) {
dw_pval <- c(dw_pval, durbinWatsonTest(simple, exact = T)$p) # Note that p is bootstrapped, and will therefore always return slightly different results)
}; mean(dw_pval) # As pvalue is close to .05, we ran multiple iterations. Bootstrapped pvalue > .05.
# Assess the assumption of no multicollinearity.
vif(simple) # No predictor's VIF > 10, all fine
1 / vif(simple) # Tolerance > 0.2, all fine
# Regress deltaMU on deltaPleasure and deltaArousal
lmPleasureArousal <- lmBF(deltaMU ~ deltaPleasure + deltaArousal, data = lm_dat_format_delta)
#lmPleasureArousal <- lmBF(deltaMU ~ deltaPleasure + deltaArousal + condition, data = lm_dat_format_delta)
lmPleasureArousal # BF10
1/lmPleasureArousal # BF01 (= 1/BF10)
###########################################################################################################################################################################################################################################################################################################################################################################################################################################
# Save the current workspace ----------------------------------------------
###########################################################################################################################################################################################################################################################################################################################################################################################################################################
save.image("./workspaces/LTD-workspace.RData")
###########################################################################################################################################################################################################################################################################################################################################################################################################################################
# Create plots ------------------------------------------------------------
###########################################################################################################################################################################################################################################################################################################################################################################################################################################
lm_dat_format_long <- reshape(
lm_dat_format_delta %>% select(-deltaSD), varying = c('deltaPleasure', 'deltaArousal'), direction = 'long', idvar = c('subjectID', 'condition'),
v.name = 'rating', timevar = 'ratingtype', times = c('pleasurerating', 'arousalrating'), new.row.names = NULL
) %>% arrange(subjectID) %>% mutate(rating = as.numeric(rating))
lm_dat_format_long$ratingtype <- factor(lm_dat_format_long$ratingtype, labels = c('Arousal', 'Valence'), levels = c('arousalrating', 'pleasurerating'))
textSize = 20
ggplot(data = lm_dat_format_long, aes(x = rating, y = deltaMU, group = ratingtype)) +
geom_point(color = 'grey') + facet_wrap(~ ratingtype) +
geom_smooth(method = "lm", se = TRUE, color = 'black', linetype = 'dashed', aes(group = ratingtype)) +
coord_cartesian(ylim = c(-40, 40), xlim = c(-4, 4), clip = 'off') +
scale_y_continuous(expand = c(0, 0), breaks = seq(-40, 40, 10)) +
scale_x_continuous(expand = c(0, 0), breaks = seq(-4, 4, 1)) +
labs(y = "Delta Leave Time Difference (LTD)", x = "Delta rating") +
theme( plot.margin=unit(c(1, 1, 1, 1), 'cm'),
legend.position = c(0.5, 1.05),
legend.direction = 'horizontal',
axis.line = element_line(linetype = "solid"),
axis.title = element_text(size = textSize, colour = "black"),
axis.title.x = element_text(margin=margin(15,0,0,0)),
axis.title.y = element_text(margin=margin(0,15,0,0)),
legend.title = element_text(size = textSize, colour = "black"),
axis.text = element_text(size = textSize, colour = "black"),
axis.text.x = element_text(margin=margin(10,0,0,0)),
axis.text.y = element_text(margin=margin(0,10,0,0)),
legend.text = element_text(size = textSize, colour = "black"),
legend.key = element_rect(fill = NA),
legend.background = element_rect(fill = NA),
axis.ticks = element_line(colour = "black", size = 0.5),
panel.grid.major = element_line(colour = NA, linetype = "blank"),
panel.grid.minor = element_line(colour = NA, linetype = "blank"),
panel.background = element_rect(fill = NA),
panel.spacing = unit(5, "lines"),
strip.text.x = element_text(size = textSize, colour = 'black'),
strip.background = element_rect(colour = 'white', fill = 'white'),
plot.background = element_rect(fill = "white", colour = NA))
ggsave('./plots/Figure-4.png', antialias ='none', width = 13.3)