File size: 12,749 Bytes
3cc82da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
title: "IS_R_code"
author: "Jo Cutler"
date: "26/04/2024"
output: html_document
---

```{r setup, include=FALSE}

knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)

require(pacman)
pacman::p_load(afex, # mixed models with extra capability for factors
  # BayesFactor, # for BF
  # buildmer, # optimise mixed models
  # boot, # bootstrapping
  # coin, # used by other packages
  cowplot, # arrange plots
  # effects,
  emmeans, # post-hoc contrasts
  english, # numbers as words
  # ggpubr, # extra plot functions
  ggforce, # extra plot functions
  ggsignif, # significance stars
  ggtext, # formatting text
  # grid, # plot dimensions
  # gridExtra,
  glmmTMB, # mixed models
  # insight, # for performance to work
  kableExtra, # table formatting
  lme4, # mixed models
  lmerTest, 
  # MASS,
  # magick, # image manipulation
  parameters, # format and standardise
  performance, # check model assumptions
  # ppcor,
  psych, # for statistical tests
  Rmisc, # for summarySEwithin function
  # robustlmm, 
  # rstatix, # for Wilcoxon effect sizes
  # sjstats,
  tidyverse, # ggplot, dplyr packages
  tidyselect)

### Function to format p values taken from https://stackoverflow.com/questions/23018256/printing-p-values-with-0-001

## version without = sign

pvalr <- function(pvals, sig.limit = .001, digits = 3, html = FALSE) {
  
  roundr <- function(x, digits = 1) {
    res <- sprintf(paste0('%.', digits, 'f'), x)
    zzz <- paste0('0.', paste(rep('0', digits), collapse = ''))
    res[res == paste0('-', zzz)] <- zzz
    res
  }
  
  sapply(pvals, function(x, sig.limit) {
    if (is.na(x))
      return(NA)
    if (x < sig.limit)
      if (html)
        return(sprintf('&lt; %s', format(sig.limit))) else
          return(sprintf('<%s', format(sig.limit)))
    
    if (x > .1) {
      return(roundr(x, digits = 2))} 
    else if (x > 0.04905 & x < 0.0505) {
      return(roundr(x, digits = digits + 1))} 
    else {
      return(roundr(x, digits = digits))}
    
  }, sig.limit = sig.limit)
}

## version with sign

pvalrf <- function(pvals, sig.limit = .001, digits = 3, html = FALSE) {
  
  roundr <- function(x, digits = 1) {
    res <- sprintf(paste0('%.', digits, 'f'), x)
    zzz <- paste0('0.', paste(rep('0', digits), collapse = ''))
    res[res == paste0('-', zzz)] <- zzz
    res
  }
  
  sapply(pvals, function(x, sig.limit) {
    if (x < sig.limit){
      if (html) {return(sprintf('&lt; %s', format(sig.limit)))
      } else {return(sprintf('<%s', format(sig.limit)))}
    }
    if (x > .1){
      if (html) {return(sprintf('&equals;%s', format(roundr(x, digits = 2)))) 
      } else {return(sprintf('=%s', format(roundr(x, digits = 2))))}
    } else if (x > 0.04905 & x < 0.0505) {
      if (html) {return(sprintf('&equals;%s', format(roundr(x, digits = digits + 1))))
      } else {return(sprintf('=%s', format(roundr(x, digits = digits + 1))))}
    } else {
      if (html) {return(sprintf('&equals;%s', format(roundr(x, digits = digits))))
      } else {return(sprintf('=%s', format(roundr(x, digits = digits))))}
    }
  }, sig.limit = sig.limit)
}

```

```{r settings}

## settings

axtext <- 16
axtitle <- 20
pd <- position_jitter(height = 0, width = 0.2, seed = 123)

# whether to write csv files of results & save plot files

writecsvs <- 0
writebigcsvs <- 0
saveplots <- 0

agent.lims <- c('Self', 'Other')
agent.labs <- c('Self', 'Other')
agent.fills <- c('#8A29E4', '#46EFDE')
agent.cols <- c('#4B157F', '#247D75')  
agent.lines = c("dashed", "solid")

paper.theme = list(theme(panel.background = element_rect(fill='transparent'),
                         panel.grid.minor = element_line(color='transparent'),
                         axis.line = element_line(color='black'),
                         axis.text=element_text(size = axtext),
                         axis.title.x=element_text(size = axtitle),
                         axis.title.y=element_markdown(size = axtitle),
                         plot.title = element_text(size = axtitle, face = "bold", hjust = 0.5),
                         strip.text=element_text(size = axtext),
                         legend.title=element_text(size = axtext, colour = 'black', hjust = 0),
                         legend.text=element_text(size = axtext, colour = 'black'),
                         legend.key = element_rect(color = NA),
                         legend.spacing.x = unit(0.25,'cm'),
                         legend.spacing.y = unit(0.25,'cm'),
                         legend.key.height = unit(0.75,'cm'),
                         legend.key.width = unit(1,'cm')))

recip.lab <- " "
info.lab <- "Choices to seek"
discount.lab <- "Choices to work"
cost.lab <- "Effort cost (%)"
prob.lab <- "Probability"
uncertainty.lab <- "Uncertainty"
valence.lab <- "Valence"
credits.lab <- "Credits available"
kcost.lab <- 'Discounting (*\u039A*)'
outw.lab <- 'Earn > seek'
unc.lab <- 'Uncertainty (*U*)'
val.lab <- 'Valence (*V*)'
ren.lab <- 'Rényi (*R*)'
beta.lab <- "Consistency (*\u03B2*)"

```

## Load data

```{r load-data}

df.discount <- read.csv("data_exp_232929-v5/data_exp_232929-v5_task-vhka.csv") # discounting choice task
                     
columns <- names(df.discount)
columns <- tibble(cols = columns)
df.discount <- df.discount %>% select(-one_of(columns$cols[c(1:11, 13:30)])) %>% subset(Screen == "Choice" & Response.Type != "tooEarly")

```

## Discounting plots

```{r plot-discount-choices, fig.width=2.3, fig.height=4}

discount.choices <- df.discount %>% group_by(Participant.Public.ID, recipient, credits, cost) %>% dplyr::summarise(accept = mean(accept))

discount.choices.recipient.mean <- summarySEwithin(data = discount.choices, measurevar = "accept", withinvars = "recipient", idvar = "Participant.Public.ID")
discount.choices.recipient <- discount.choices %>% group_by(Participant.Public.ID, recipient) %>% dplyr::summarise(accept = mean(accept))

discount.choices.recipient.wide <- pivot_wider(discount.choices.recipient, names_from = "recipient", values_from = "accept", id_cols = "Participant.Public.ID", names_prefix = "discount_") %>% 
  mutate(discount_diff = discount_Self - discount_Other)

choices.recipient.wide <- left_join(info.choices.recipient.wide, discount.choices.recipient.wide, by = "Participant.Public.ID")

df.id <- left_join(df.id, choices.recipient.wide)

df.id <- df.id %>% 
  subset(!Participant.Public.ID %in% checks$Participant.Public.ID[checks$exclude == 1]) %>% # remove excluded participants
  droplevels()

corr.id <- corr.test(df.id %>% select(contains(c("_info", "_discount", "_kcost", "_unc", "_val", "AMI", "DASS", "IUS", "QCAE", "SHAPS"))), adjust = "none")
corr.id.r <- corr.id$r
corr.id.r[corr.id$p>0.05] <- NA
# kable(corr.id.r) %>% kable_styling() 

plot.discount.recipient <- ggplot(data = discount.choices.recipient, aes(x = recipient, y = accept, colour = recipient, fill = recipient, group = recipient)) +
  geom_line(aes(group = Participant.Public.ID), position = pd, colour = "grey", alpha = 0.2) +
  geom_point(aes(group = Participant.Public.ID), position = pd, size = 3, alpha = 0.2, show.legend = FALSE, shape = 16) +
  geom_line(aes(group = "none"), data = discount.choices.recipient.mean, colour = "black", alpha = 0.5) +
  geom_point(data = discount.choices.recipient.mean, size = 5, position = position_dodge(width = 0.5), shape = 21, colour = 'black') +
  geom_errorbar(data = discount.choices.recipient.mean, aes(ymin = accept - se, ymax = accept + se), width = 0, position = position_dodge(width = 0.5), colour = 'black') +
  scale_fill_manual(limits=agent.lims, values=agent.fills) +
  scale_color_manual(limits=agent.lims, values=agent.cols) +
  # coord_cartesian(ylim = c(0,1)) + 
  labs(x = recip.lab, y = discount.lab) +
  theme_classic() +
  paper.theme +
  theme(legend.position = "bottom",
        axis.text.x =element_text(size = (axtitle + axtext) / 2, colour = "black"),
        legend.title = element_blank()) + #         axis.title.x = element_blank()
  geom_signif(comparisons = list(c("Self", "Other")), colour = 'black', map_signif_level = TRUE)

print(plot.discount.recipient)

# plot.ppt.discount.recipient <- ggplot(data = discount.choices.recipient, aes(x = recipient, y = accept, colour = recipient, group = recipient)) +
#   geom_point(size = 2, position = position_dodge(width = 0.5)) +
#   scale_fill_manual(limits=agent.lims, values=agent.fills) +
#   scale_color_manual(limits=agent.lims, values=agent.cols) +
#   coord_cartesian(ylim = c(0,1)) + 
#   labs(y = discount.lab) +
#   theme_classic() +
#   paper.theme +
#   theme(legend.title = element_blank(),
#         axis.title.x = element_blank()) +
#   facet_wrap("Participant.Public.ID")
# 
# print(plot.ppt.discount.recipient)

discount.choices.credits.mean <- summarySEwithin(data = discount.choices, measurevar = "accept", withinvars = c("recipient", "credits"), idvar = "Participant.Public.ID")
discount.choices.credits <- discount.choices %>% mutate(credits = factor(credits)) %>% group_by(Participant.Public.ID, recipient, credits) %>% dplyr::summarise(accept = mean(accept))

plot.discount.credits <- ggplot(data = discount.choices.credits, aes(x = credits, y = accept, colour = recipient, fill = recipient, group = recipient)) +
  geom_smooth(width = 0.5, alpha = 0.1, method = "lm") +
  # geom_point(size = 1.5, alpha = 0.2) +
  # geom_errorbar(data = discount.choices.credits.mean, aes(ymin = accept - se, ymax = accept + se), width = 0, position = position_dodge(width = 0.5)) +
  # geom_jitter(position = position_jitterdodge(dodge.width = 0.5, jitter.height = 0.05, jitter.width = 0.3), size = 1.5, alpha = 0.2, show.legend = FALSE) +
  geom_jitter(position = position_jitternormal(sd_x = 0.1, sd_y = 0.025), size = 1.5, alpha = 0.02, show.legend = FALSE) +
  scale_fill_manual(limits=agent.lims, values=agent.fills) +
  scale_color_manual(limits=agent.lims, values=agent.cols) +
  coord_cartesian(ylim = c(0,1)) + 
  labs(x = credits.lab, y = discount.lab) +
  theme_classic() +
  paper.theme +
  theme(legend.position = "bottom",
        axis.title.y = element_blank(),
        legend.title = element_blank())

print(plot.discount.credits)

# plot.ppt.discount.credits <- ggplot(data = discount.choices.credits, aes(x = credits, y = accept, colour = recipient, group = recipient)) +
#   geom_smooth(alpha = 0, method = "lm", se = FALSE) +
#   # geom_smooth(alpha = 0, se = FALSE) +
#   scale_fill_manual(limits=agent.lims, values=agent.fills) +
#   scale_color_manual(limits=agent.lims, values=agent.cols) +
#   coord_cartesian(ylim = c(0,1)) + 
#   labs(x = credits.lab, y = discount.lab) +
#   theme_classic() +
#   paper.theme +
#   theme(legend.title = element_blank()) +
#   facet_wrap("Participant.Public.ID")
# 
# print(plot.ppt.discount.credits)

discount.choices.cost.mean <- summarySEwithin(data = discount.choices, measurevar = "accept", withinvars = c("recipient", "cost"), idvar = "Participant.Public.ID")
discount.choices.cost <- discount.choices %>% mutate(cost = factor(cost)) %>% group_by(Participant.Public.ID, recipient, cost) %>% dplyr::summarise(accept = mean(accept))

plot.discount.cost <- ggplot(data = discount.choices.cost, aes(x = cost, y = accept, colour = recipient, fill = recipient, group = recipient)) +
  geom_smooth(width = 0.5, alpha = 0.1, method = "lm") +
  # geom_point(size = 1.5, alpha = 0.2) +
  # geom_errorbar(data = discount.choices.credits.mean, aes(ymin = accept - se, ymax = accept + se), width = 0, position = position_dodge(width = 0.5)) +
  # geom_jitter(position = position_jitterdodge(dodge.width = 0.5, jitter.height = 0.05, jitter.width = 0.3), size = 1.5, alpha = 0.2, show.legend = FALSE) +
  geom_jitter(position = position_jitternormal(sd_x = 0.1, sd_y = 0.02), size = 1.5, alpha = 0.02, show.legend = FALSE) +
  scale_fill_manual(limits=agent.lims, values=agent.fills) +
  scale_color_manual(limits=agent.lims, values=agent.cols) +
  coord_cartesian(ylim = c(0,1)) + 
  labs(x = cost.lab, y = discount.lab) +
  theme_classic() +
  paper.theme +
  theme(legend.position = "bottom",
        axis.title.y = element_blank(),
        legend.title = element_blank())

print(plot.discount.cost)

# plot.ppt.discount.cost <- ggplot(data = discount.choices.cost, aes(x = cost, y = accept, colour = recipient, group = recipient)) +
#   geom_smooth(alpha = 0, method = "lm", se = FALSE) +
#   # geom_smooth(alpha = 0, se = FALSE) +
#   scale_fill_manual(limits=agent.lims, values=agent.fills) +
#   scale_color_manual(limits=agent.lims, values=agent.cols) +
#   coord_cartesian(ylim = c(0,1)) + 
#   labs(x = cost.lab, y = discount.lab) +
#   theme_classic() +
#   paper.theme +
#   theme(legend.title = element_blank()) +
#   facet_wrap("Participant.Public.ID")
# 
# print(plot.ppt.discount.cost)

```