R-Skript_FrameBetween_publication.R 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. #### FrameBetween ####
  2. # # Sind alle notwendigen Pakete installiert?
  3. # install.packages("car")
  4. # install.packages("ez")
  5. # install.packages("ggplot2")
  6. # install.packages("pastecs")
  7. # install.packages("tidyr")
  8. # install.packages("Hmisc")
  9. # install.packages("dplyr")
  10. # install.packages("tidyverse")
  11. # install.packages("gdata")
  12. # install.packages("lme4")
  13. # install.packages("psych")
  14. # install.packages("psychReport")
  15. # install.packages("rcompanion")
  16. # install.packages("data.table")
  17. # install.packages("hrbrthemes")
  18. # install.packages("viridis")
  19. library(ggplot2)
  20. library(psychReport)
  21. library(dplyr)
  22. library(apa)
  23. library(afex)
  24. library(dplyr)
  25. library(forcats)
  26. library(data.table)
  27. library(psych)
  28. library(doBy)
  29. library(rcompanion)
  30. library(effectsize)
  31. #### Read data ####
  32. setwd("Your Directory")
  33. list_of_files <- list.files(path = "Your Directory", recursive = TRUE,
  34. pattern = "\\.dat$",
  35. full.names = TRUE)
  36. FrameBetween <- vroom(list_of_files)
  37. FrameBetween <- subset(FrameBetween, FrameBetween$subjID !=2) # MEG artifacts
  38. # stim: 1 = anodal /excitatory 2 = Sham/Placebo
  39. FrameBetween$stim <- ifelse(FrameBetween$stim == 1, "Excitatory", "Sham")
  40. #### Check of Programming ####
  41. table (FrameBetween$InitRew)
  42. table (FrameBetween$RisktoLoose)
  43. table (FrameBetween$GainLossTr)
  44. table (FrameBetween$subjID)
  45. table (FrameBetween$choice)
  46. describe(FrameBetween$choice)
  47. table (FrameBetween$stim)
  48. kreuztabelle <- xtabs (~ FrameBetween$RisktoLoose + FrameBetween$GainLossTr)
  49. xtabs (~ FrameBetween$stim + FrameBetween$subjID)
  50. print(kreuztabelle)
  51. table(FrameBetween$subjID)
  52. # Half of the trials risky choices?
  53. observed_proportion <- c(10808, 12232)
  54. theoretical_proportion <- c(1/2, 1/2)
  55. chisq.test(observed_proportion, p=theoretical_proportion)
  56. FrameBetween = FrameBetween %>%
  57. dplyr::group_by(subjID) %>%
  58. dplyr::mutate(TrialNumber = (row_number()-1) %% 320 +1) %>%
  59. ungroup()
  60. #### Add expected value ####
  61. FrameBetween <- FrameBetween %>%
  62. mutate(expectation_valueRisk = case_when(RisktoLoose == 20 & InitRew == 100 ~ 80,
  63. RisktoLoose == 40 & InitRew == 100 ~ 60,
  64. RisktoLoose == 60 & InitRew == 100 ~ 40,
  65. RisktoLoose == 80 & InitRew == 100 ~ 20,
  66. RisktoLoose == 20 & InitRew == 75 ~ 60,
  67. RisktoLoose == 40 & InitRew == 75 ~ 45,
  68. RisktoLoose == 60 & InitRew == 75 ~ 30,
  69. RisktoLoose == 80 & InitRew == 75 ~ 15,
  70. RisktoLoose == 20 & InitRew == 50 ~ 40,
  71. RisktoLoose == 40 & InitRew == 50 ~ 30,
  72. RisktoLoose == 60 & InitRew == 50 ~ 20,
  73. RisktoLoose == 80 & InitRew == 50 ~ 10,
  74. RisktoLoose == 20 & InitRew == 25 ~ 20,
  75. RisktoLoose == 40 & InitRew == 25 ~ 15,
  76. RisktoLoose == 60 & InitRew == 25 ~ 10,
  77. RisktoLoose == 80 & InitRew == 25 ~ 5,
  78. ))
  79. FrameBetween <- FrameBetween %>%
  80. mutate(expectation_valueSafe = case_when(InitRew == 100 ~ 40,
  81. InitRew == 75 ~ 30,
  82. InitRew == 50 ~ 20,
  83. InitRew == 25 ~ 10,
  84. ))
  85. FrameBetween <- FrameBetween %>%
  86. mutate(expectation_valueChoice = case_when(choice == 1 ~ expectation_valueRisk,
  87. choice == 0 ~ expectation_valueSafe,
  88. ))
  89. FrameBetween <- FrameBetween %>%
  90. mutate(RationalDecision = case_when(choice == 1 & expectation_valueRisk >= expectation_valueSafe ~ 1,
  91. choice == 0 & expectation_valueSafe >= expectation_valueRisk ~ 1,
  92. choice == 1 & expectation_valueRisk < expectation_valueSafe ~ 0,
  93. choice == 0 & expectation_valueSafe < expectation_valueRisk ~ 0,
  94. ))
  95. FrameBetween <- FrameBetween %>%
  96. mutate(Outcome = case_when(choice == 1 & RiskFeedback == 22 | choice == 1 & RiskFeedback == 52 | choice == 1 & RiskFeedback == 72 | choice == 1 & RiskFeedback == 102 ~ 1,
  97. choice == 1 & RiskFeedback == 21 | choice == 1 & RiskFeedback == 51 | choice == 1 & RiskFeedback == 71 | choice == 1 & RiskFeedback == 101 ~ 0,
  98. choice == 0 & GainLossTr == 0 & RiskFeedback == 0 ~ 1, # 99 oder 1
  99. choice == 0 & GainLossTr == 1 & RiskFeedback == 0 ~ 0 # 99 oder 0
  100. ))
  101. FrameBetween$choicepercent <- FrameBetween$choice*100
  102. #FrameBetween <- FrameBetween %>%
  103. # mutate(Outcome4 = case_when(choice == 1 & RiskFeedback == 22 | choice == 1 & RiskFeedback == 52 | choice == 1 & RiskFeedback == 72 | choice == 1 & RiskFeedback == 102 ~ 4,
  104. # choice == 1 & RiskFeedback == 21 | choice == 1 & RiskFeedback == 51 | choice == 1 & RiskFeedback == 71 | choice == 1 & RiskFeedback == 101 ~ 1,
  105. # choice == 0 & GainLossTr == 0 & RiskFeedback == 0 ~ 3,
  106. # choice == 0 & GainLossTr == 1 & RiskFeedback == 0 ~ 2
  107. # ))
  108. FrameBetween <- data.table(FrameBetween)
  109. FrameBetween[ , previousTr := shift(Outcome)]
  110. #FrameBetween[ , previousTr4 := shift(Outcome4)]
  111. PrevTrial <- subset(FrameBetween, FrameBetween$previousTr !=99)
  112. PrevTrial <- subset(PrevTrial, PrevTrial$previousTr !="NA")
  113. PrevTrial <- subset(PrevTrial, PrevTrial$TrialNumber !=1)
  114. #PrevTrial4 <- subset(FrameBetween, FrameBetween$previousTr4 !=99)
  115. #PrevTrial4 <- subset(PrevTrial, PrevTrial$previousTr4 !="NA")
  116. PrevTrial$previousTr <- ifelse(PrevTrial$previousTr == 1, "Gain", "Loss")
  117. # Choice = 0 --> no risk, Choice = 1 --> risk
  118. # GainLossTr: 0 = GainTrial 1 = LossTrial
  119. FrameRat <- subset(FrameBetween, FrameBetween$RisktoLoose !=60)
  120. GLMRat <- glm(formula = RationalDecision ~ stim, family = binomial(logit), data = FrameRat)
  121. summary(GLMRat)
  122. GLMFrame <- glm(formula = choice ~ GainLossTr, family = binomial(logit), data = FrameBetween)
  123. summary(GLMFrame)
  124. effectsizes <- standardize_parameters(GLMFrame, exp = TRUE)
  125. GLMIntFrame <- glm(formula = choice ~ stim * GainLossTr, family = binomial(logit), data = FrameBetween)
  126. summary(GLMIntFrame)
  127. effectsizes <- standardize_parameters(GLMIntFrame, exp = TRUE)
  128. GLMALL <- glm(formula = choice ~ stim * GainLossTr * RisktoLoose, family = binomial(logit), data = FrameBetween)
  129. summary(GLMALL)
  130. effectsizes <- standardize_parameters(GLMALL, exp = TRUE)
  131. GLMInitRew <- glm(formula = choice ~ stim * InitRew, family = binomial(logit), data = FrameBetween)
  132. summary(GLMInitRew)
  133. Frame_Sham <- subset(FrameBetween, FrameBetween$stim !="Excitatory")
  134. Frame_Ano <- subset(FrameBetween, FrameBetween$stim !="Sham")
  135. GLMInitRew <- glm(formula = choice ~ InitRew, family = binomial(logit), data = Frame_Sham)
  136. summary(GLMInitRew)
  137. effectsizes <- standardize_parameters(GLMInitRew, exp = TRUE)
  138. GLMInitRew <- glm(formula = choice ~ InitRew, family = binomial(logit), data = Frame_Ano)
  139. summary(GLMInitRew)
  140. GLMprevTr <- glm(formula = choice ~ stim * previousTr, family = binomial(logit), data = PrevTrial)
  141. summary(GLMprevTr)
  142. effectsizes <- standardize_parameters(GLMprevTr, exp = TRUE)
  143. Frame_Sham <- subset(PrevTrial, PrevTrial$stim !="Excitatory")
  144. Frame_Ano <- subset(PrevTrial, PrevTrial$stim !="Sham")
  145. Frame_prevLoss <- subset(PrevTrial, PrevTrial$previousTr !="Gain")
  146. Frame_prevGain <- subset(PrevTrial, PrevTrial$previousTr !="Loss")
  147. GLMprevTr <- glm(formula = choice ~ previousTr, family = binomial(logit), data = Frame_Ano)
  148. summary(GLMprevTr)
  149. effectsizes <- standardize_parameters(GLMprevTr, exp = TRUE)
  150. GLMprevTr <- glm(formula = choice ~ previousTr, family = binomial(logit), data = Frame_Sham)
  151. summary(GLMprevTr)
  152. effectsizes <- standardize_parameters(GLMprevTr, exp = TRUE)
  153. GLMprevTr <- glm(formula = choice ~ stim, family = binomial(logit), data = Frame_prevLoss)
  154. summary(GLMprevTr)
  155. GLMprevTr <- glm(formula = choice ~ stim, family = binomial(logit), data = Frame_prevGain)
  156. summary(GLMprevTr)
  157. GLMNull <- glm(formula = choice ~ 1, family = binomial(logit), data = FrameBetween)
  158. summary(GLMNull)
  159. anova(GLMFrame, GLMNull, test = "Chisq")
  160. anova(GLMRat, GLMNull, test = "Chisq")
  161. anova(GLMALL, GLMNull, test = "Chisq")
  162. #### Post-Hocs Interaction Risk and Expected Value ####
  163. apa(t.test(expectation_valueChoice ~ stim, data = FrameBetween, var.equal = TRUE, paired = FALSE,
  164. alternative = "greater")) # two.sided
  165. groupwiseMean(expectation_valueChoice ~ stim,
  166. data = FrameBetween,
  167. conf = 0.95,
  168. digits = 3)
  169. FrameAgg <- aggregate(choice ~ subjID + stim + RisktoLoose,
  170. data = FrameBetween, FUN = mean)
  171. Frame20 <- subset(FrameBetween, FrameBetween$RisktoLoose !=80)
  172. Frame20 <- subset(Frame20, Frame20$RisktoLoose !=60)
  173. Frame20 <- subset(Frame20, Frame20$RisktoLoose !=40)
  174. apa(t.test(expectation_valueChoice ~ stim, data = Frame20, var.equal = TRUE, paired = FALSE,
  175. alternative = "greater")) # two.sided
  176. GLM20 <- glm(formula = choice ~ stim, family = binomial(logit), data = Frame20)
  177. summary(GLM20)
  178. effectsizes <- standardize_parameters(GLM20, exp = TRUE)
  179. xtabs (~ Frame20$choice + Frame20$stim)
  180. prop.test(x = c(1089, 1001), n = c(1280, 1280),alternative = "two.sided")
  181. Frame40 <- subset(FrameBetween, FrameBetween$RisktoLoose !=80)
  182. Frame40 <- subset(Frame40, Frame40$RisktoLoose !=60)
  183. Frame40 <- subset(Frame40, Frame40$RisktoLoose !=20)
  184. apa(t.test(expectation_valueChoice ~ stim, data = Frame40, var.equal = TRUE, paired = FALSE,
  185. alternative = "greater")) # two.sided
  186. xtabs (~ Frame40$choice + Frame40$stim)
  187. GLM40 <- glm(formula = choice ~ stim, family = binomial(logit), data = Frame40)
  188. summary(GLM40)
  189. prop.test(x = c(852, 867), n = c(1280, 1280),alternative = "two.sided")
  190. Frame60 <- subset(FrameBetween, FrameBetween$RisktoLoose !=80)
  191. Frame60 <- subset(Frame60, Frame60$RisktoLoose !=20)
  192. Frame60 <- subset(Frame60, Frame60$RisktoLoose !=40)
  193. apa(t.test(expectation_valueChoice ~ stim, data = Frame60, var.equal = TRUE, paired = FALSE,
  194. alternative = "greater"))
  195. xtabs (~ Frame60$choice + Frame60$stim)
  196. GLM60 <- glm(formula = choice ~ stim, family = binomial(logit), data = Frame60)
  197. summary(GLM60)
  198. effectsizes <- standardize_parameters(GLM60, exp = TRUE)
  199. prop.test(x = c(468, 637), n = c(1280, 1280),alternative = "two.sided")
  200. Frame80 <- subset(FrameBetween, FrameBetween$RisktoLoose !=60)
  201. Frame80 <- subset(Frame80, Frame80$RisktoLoose !=20)
  202. Frame80 <- subset(Frame80, Frame80$RisktoLoose !=40)
  203. apa(t.test(expectation_valueChoice ~ stim, data = Frame80, var.equal = TRUE, paired = FALSE,
  204. alternative = "greater"))
  205. xtabs (~ Frame80$choice + Frame80$stim)
  206. GLM80 <- glm(formula = choice ~ stim, family = binomial(logit), data = Frame80)
  207. summary(GLM80)
  208. effectsizes <- standardize_parameters(GLM80, exp = TRUE)
  209. interpret_oddsratio(1.88, rules = "cohen1988")
  210. prop.test(x = c(223, 361), n = c(1280, 1280),alternative = "two.sided")
  211. apa(t.test(expectation_valueChoice ~ stim, data = FrameRat, var.equal = TRUE, paired = FALSE,
  212. alternative = "greater")) # two.sided
  213. #### Post-hoc Tests Interaction Frame ####
  214. LossFrame <- subset(FrameBetween, FrameBetween$GainLossTr !="GainTr")
  215. GainFrame <- subset(FrameBetween, FrameBetween$GainLossTr !="LossTr")
  216. GainFrame$Diff <- GainFrame$choice - LossFrame$choice
  217. bar <- ggplot(GainFrame, aes(stim, Diff, fill = stim))
  218. bar +
  219. stat_summary(fun = mean, geom = "bar", position = "dodge") +
  220. stat_summary(fun.data = mean_cl_normal, geom = "errorbar", position = position_dodge(width = 0.90),width = 0.2) +
  221. labs (x = "Frame", y = "Risky Choice", fill = "Stim") +
  222. scale_fill_brewer(palette = "Set1")
  223. theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
  224. panel.background = element_blank(), axis.line = element_line(colour = "black"))
  225. xtabs (~ GainFrame$choice + GainFrame$stim)
  226. prop.test(x = c(1293, 1384), n = c(2560, 2560),alternative = "two.sided")
  227. xtabs (~ LossFrame$choice + LossFrame$stim)
  228. prop.test(x = c(1339, 1482), n = c(2560, 2560),alternative = "two.sided")
  229. GainFrameexc <- subset(GainFrame, GainFrame$stim !="Sham")
  230. GainFrameSham <- subset(GainFrame, GainFrame$stim !="Excitatory")
  231. table(GainFrameexc$Diff)
  232. table(GainFrameSham$Diff)
  233. prop.test(x = c(640, 680), n = c(2560, 2560),alternative = "less")
  234. #### Post-Hoc Interaction InitRew ####
  235. Frame25 <- subset(FrameBetween, FrameBetween$InitRew !=50)
  236. Frame25 <- subset(Frame25, Frame25$InitRew !=75)
  237. Frame25 <- subset(Frame25, Frame25$InitRew !=100)
  238. xtabs (~ Frame25$choice + Frame25$stim)
  239. prop.test(x = c(652, 647), n = c(1280, 1280),alternative = "two.sided")
  240. Frame50 <- subset(FrameBetween, FrameBetween$InitRew !=25)
  241. Frame50 <- subset(Frame50, Frame50$InitRew !=75)
  242. Frame50 <- subset(Frame50, Frame50$InitRew !=100)
  243. xtabs (~ Frame50$choice + Frame50$stim)
  244. prop.test(x = c(658, 692), n = c(1280, 1280),alternative = "two.sided")
  245. Frame75 <- subset(FrameBetween, FrameBetween$InitRew !=50)
  246. Frame75 <- subset(Frame75, Frame75$InitRew !=25)
  247. Frame75 <- subset(Frame75, Frame75$InitRew !=100)
  248. xtabs (~ Frame75$choice + Frame75$stim)
  249. prop.test(x = c(652, 728), n = c(1280, 1280),alternative = "two.sided")
  250. GLM75 <- glm(formula = choice ~ stim, family = binomial(logit), data = Frame75)
  251. summary(GLM75)
  252. effectsizes <- standardize_parameters(GLM75, exp = TRUE)
  253. Frame100 <- subset(FrameBetween, FrameBetween$InitRew !=25)
  254. Frame100 <- subset(Frame100, Frame100$InitRew !=50)
  255. Frame100 <- subset(Frame100, Frame100$InitRew !=75)
  256. xtabs (~ Frame100$choice + Frame100$stim)
  257. prop.test(x = c(670, 799), n = c(1280, 1280),alternative = "two.sided")
  258. GLM100 <- glm(formula = choice ~ stim, family = binomial(logit), data = Frame100)
  259. summary(GLM100)
  260. effectsizes <- standardize_parameters(GLM100, exp = TRUE)
  261. #### Post-Hoc previous Trial ####
  262. prvLoss <- subset(PrevTrial, PrevTrial$previousTr !="Gain")
  263. prvGain <- subset(PrevTrial, PrevTrial$previousTr !="Loss")
  264. Exc <- subset(PrevTrial, PrevTrial$stim !="Sham")
  265. Sham <- subset(PrevTrial, PrevTrial$stim !="Excitatory")
  266. xtabs (~ prvLoss$choice + prvLoss$stim)
  267. prop.test(x = c(1126, 1262), n = c(2217, 2181),alternative = "two.sided")
  268. xtabs (~ prvGain$choice + prvGain$stim)
  269. prop.test(x = c(1454, 1495), n = c(2787, 2758),alternative = "two.sided")
  270. xtabs (~ Exc$choice + Exc$previousTr)
  271. prop.test(x = c(1454, 1126), n = c(2787, 2217),alternative = "two.sided")
  272. xtabs (~ Sham$choice + Sham$previousTr)
  273. prop.test(x = c(1496, 1260), n = c(2758, 2178),alternative = "two.sided")