avoidance_analysis.R 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # ___ ____ ____ ______ __ _______ ___ .__ __. ______ _______
  2. # / \ \ \ / / / __ \ | | | \ / \ | \ | | / || ____|
  3. # / ^ \ \ \/ / | | | | | | | .--. | / ^ \ | \| | | ,----'| |__
  4. # / /_\ \ \ / | | | | | | | | | | / /_\ \ | . ` | | | | __|
  5. # / _____ \ \ / | `--' | | | | '--' | / _____ \ | |\ | | `----.| |____
  6. #/__/ \__\ \__/ \______/ |__| |_______/ /__/ \__\ |__| \__| \______||_______|
  7. #Analysis script avoidance data general/stimulus difference analysis, mediation analysis and reactiontime
  8. #written by Madeleine Mueller, January 2024
  9. #libraries
  10. library("tidyverse")
  11. library("lme4")
  12. library("car")
  13. library("effects")
  14. library("emmeans")
  15. library("dplyr")
  16. ######################################
  17. #AVOIDANCE MAIN ANALYSIS
  18. #steps as outcome measure
  19. ######################################
  20. #read in data
  21. OSLO_grid<-read.csv(file='/Users/mmueller/Documents/socialLearn/ana/R/d2_avoidance.csv',head=TRUE,sep=",")
  22. #exclude fence trials
  23. clearFence<-OSLO_grid[OSLO_grid$CS != "3", ]
  24. #exclude both CS door trials
  25. onlyCS<-clearFence[clearFence$CS != "4", ]
  26. onlyCS$trial<-as.factor(onlyCS$trial)
  27. onlyCS$CS<-as.factor(onlyCS$CS)
  28. onlyCS$pathlength<-as.factor(onlyCS$pathlength)
  29. onlyCS$code<-onlyCS$code
  30. #create model
  31. totalsteps<-(lmer(steps~(1|code)+CS*pathlength+trial, data=onlyCS,control = lmerControl(optimizer = "bobyqa")))
  32. #ANOVA based on model
  33. Anova(totalsteps, type="3", test="F" )
  34. #post-hoc tests
  35. emmeans(totalsteps, list(pairwise ~ CS:pathlength), adjust = "none")
  36. #plot effect
  37. plot(effect("CS*pathlength",totalsteps))
  38. plot(effect("CS",totalsteps))
  39. ######################################
  40. #AVOIDANCE STIMULUS DIFFERENCE ANALYSIS
  41. #steps as outcome measure
  42. ######################################
  43. # Calculate the mean steps per participant for each combination of CS and pathlength
  44. mean_steps <- onlyCS %>%
  45. group_by(code, CS, pathlength) %>%
  46. summarise(mean_steps = mean(steps))
  47. # Calculate the difference between mean steps for CS=1 and CS=2 for each pathlength
  48. csdiff <- mean_steps %>%
  49. pivot_wider(names_from = CS, values_from = mean_steps) %>%
  50. mutate(diff_mean_steps = `1` - `2`)
  51. csdiff$pathlength<-as.factor(csdiff$pathlength)
  52. #create model
  53. csdiff_model<-(lmer(diff_mean_steps~(1|code)+pathlength, data=csdiff,control = lmerControl(optimizer = "bobyqa")))
  54. #ANOVA based on model
  55. Anova(csdiff_model, type="3", test="F" )
  56. #post-hoc test
  57. emmeans(csdiff_model, list(pairwise ~ pathlength), adjust = "none")
  58. #plot effect
  59. plot(effect("pathlength",csdiff_model))
  60. ######################################
  61. #AVOIDANCE WALLFACTOR ANALYSIS
  62. #wallfactor as outcome measure (mean wallfactor over whole trial)
  63. ######################################
  64. #create model
  65. wall2<-(lmer(wallfactor~(1|code)+CS*pathlength+trial, data=onlyCS, control = lmerControl(optimizer = "bobyqa")))
  66. #ANOVA based on model
  67. Anova(wall2, type="3", test="F" )
  68. #post-hoc test
  69. emmeans(wall2, list(pairwise ~ trial), adjust = "none")
  70. #plot effects
  71. plot(effect("CS*pathlength",wall2))
  72. plot(effect("pathlength",wall2))
  73. plot(effect("CS",wall2))
  74. ######################################
  75. #AVOIDANCE MEDIATION ANALYSIS
  76. ######################################
  77. #libraries
  78. library(mediation)
  79. library(lme4)
  80. set.seed(2014)
  81. data("framing", package = "mediation")
  82. t.test(onlyCS$wallfactor~onlyCS$CS, var.equal = TRUE, alternative = "two.sided")
  83. #example:
  84. #1. MEDIATOR MODEL:
  85. #lmer=mediator model type, as linear model "lm" would also be possible. As GLM, we could use glm/bayesglm/glmer.
  86. #wallmean is the mediator of this model
  87. #then after~ comes the treatment, which is CS for us, we would need a binary treatment (0=CS+, 1=CS-)
  88. #this translates to "With treatment of CS- or without"
  89. #following that, we can add pre-treatment covariates, e.g. age/gender...
  90. medoslo.fit<- lmer(wallfactor~(1|code)+CS, data=onlyCS)
  91. summary(medoslo.fit)
  92. #2. OUTCOME MODEL:
  93. #here again lm, because our outcome variable "steps" is not binary, for binary, we would need glmer
  94. #with this model, we expect that CS- decision is increasing wallfactor, which leads to more steps
  95. outoslo.fit <- lmer(steps ~ (1|code)+CS+wallfactor , data = onlyCS)
  96. summary(outoslo.fit)
  97. #
  98. #ACME=average causal mediation effects ("indirect effect")
  99. #ADE=average direct effects
  100. #1000 simulations is default, but it is recommended, but one can choose a higher number, if results vary too much
  101. #it is recommended to use bootstrap, if sims>1000 and computing power is not a problem/results should be very similar without boot
  102. #if boot='FALSE' a quasi-Bayesian approximation is used for confidence intervals; if 'TRUE' nonparametric bootstrap will be used.
  103. medoslo.out <- mediate(medoslo.fit, outoslo.fit, treat = "CS", mediator = "wallfactor",boot=FALSE, sims = 1000)
  104. summary(medoslo.out)
  105. plot(medoslo.out)
  106. ######################################
  107. #AVOIDANCE REACTIONTIME ANALYSIS
  108. ######################################
  109. #read in data
  110. OSLOrt<-read.csv(file='/Users/mmueller/Documents/socialLearn/ana/R/d2_avoidance_reactiontime.csv',head=TRUE,sep=",")
  111. #PRE DECISION
  112. #exclude steps during decision
  113. OSLOrtwodec <- subset(OSLOrt, !grepl("dec", prepost))
  114. #exclude steps after decision
  115. OSLOrtwodec <- subset(OSLOrtwodec, !grepl("post", prepost))
  116. df <- subset(OSLOrtwodec, reactiontime <= 4)
  117. df <- subset(df, reactiontime >0)
  118. rm(OSLOrt)
  119. rm(OSLOrtwodec)
  120. #create model
  121. pre_rt_single<-(lmer(reactiontime~(1|code)+Csdoor*pathlength+steps+trial, data=df,control = lmerControl(optimizer = "bobyqa")))
  122. #ANOVA based on model
  123. Anova(pre_rt_single, type="3", test="F" )
  124. #post-hoc tests
  125. emmeans(pre_rt_single, list(pairwise ~ trial), adjust = "none")
  126. #plot effect
  127. plot(effect("steps",pre_rt_single))
  128. #POST DECISION
  129. #exclude steps during decision
  130. OSLOrtwodec <- subset(OSLOrt, !grepl("dec", prepost))
  131. #exclude steps before decision
  132. OSLOrtwodec <- subset(OSLOrtwodec, !grepl("pre", prepost))
  133. dfpost <- subset(OSLOrtwodec, reactiontime <= 4)
  134. dfpost <- subset(dfpost, reactiontime >0)
  135. rm(OSLOrt)
  136. rm(OSLOrtwodec)
  137. #create model
  138. post_rt_single<-(lmer(reactiontime~(1|code)+Csdoor*pathlength+steps+trial, data=dfpost,control = lmerControl(optimizer = "bobyqa")))
  139. #ANOVA based on model
  140. Anova(post_rt_single, type="3", test="F" )
  141. #plot effect
  142. plot(effect("steps",post_rt_single))