Spaces:
Running
Running
Download IS_R_code.Rmd from princessamygdala/HarvestForTwo: direct link, hf CLI and curl.
- Browser
- Download file 12.7 kB
-
https://huggingface.co/spaces/princessamygdala/HarvestForTwo/resolve/main/IS_R_code.Rmd
- Command line
-
hf download hf://spaces/princessamygdala/HarvestForTwo/IS_R_code.Rmd
-
curl -L -o IS_R_code.Rmd https://huggingface.co/spaces/princessamygdala/HarvestForTwo/resolve/main/IS_R_code.Rmd
12.7 kB
| 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('< %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('< %s', format(sig.limit))) | |
| } else {return(sprintf('<%s', format(sig.limit)))} | |
| } | |
| if (x > .1){ | |
| if (html) {return(sprintf('=%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('=%s', format(roundr(x, digits = digits + 1)))) | |
| } else {return(sprintf('=%s', format(roundr(x, digits = digits + 1))))} | |
| } else { | |
| if (html) {return(sprintf('=%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) | |
| ``` | |