rating_analysis_bothDays.R 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #.______ ___ .___________. __ .__ __. _______ _______.
  2. #| _ \ / \ | || | | \ | | / _____| / |
  3. #| |_) | / ^ \ `---| |----`| | | \| | | | __ | (----`
  4. #| / / /_\ \ | | | | | . ` | | | |_ | \ \
  5. #| |\ \----./ _____ \ | | | | | |\ | | |__| | .----) |
  6. #| _| `._____/__/ \__\ |__| |__| |__| \__| \______| |_______/
  7. #Analysis script Rating data "Fear/Discomfort/Physical Arousal"-Ratings (day1 and day 2) and "danger/safe" ratings (day 2)
  8. #written by Madeleine Mueller, January 2024
  9. #libraries
  10. library("lme4")
  11. library("car")
  12. library("effects")
  13. library("emmeans")
  14. library("dplyr")
  15. # _______ ___ ____ ____ __
  16. # | \ / \ \ \ / / /_ |
  17. # | .--. | / ^ \ \ \/ / | |
  18. # | | | | / /_\ \ \_ _/ | |
  19. # | '--' | / _____ \ | | | |
  20. # |_______/ /__/ \__\ |__| |_|
  21. ##########################
  22. #FEAR/DISCOMFORT/PHYSICAL AROUSAL
  23. ##########################
  24. #read in data
  25. O_D1<-read.csv(file='/Users/mmueller/Documents/socialLearn/ana/R/d1_FearRating.csv',head=TRUE,sep=",")
  26. O_D1$stim<-as.factor(O_D1$stim)
  27. O_D1$time<-as.factor(O_D1$prepost)
  28. O_D1$sub<-O_D1$sub
  29. #create models
  30. d1.discomfort<-(lmer(disc_rat~(1|sub)+stim*time, data=O_D1,control = lmerControl(optimizer = "bobyqa")))
  31. d1.fear<-(lmer(fear_rat~(1|sub)+stim*time, data=O_D1,control = lmerControl(optimizer = "bobyqa")))
  32. d1.phys<-(lmer(phys_rat~(1|sub)+stim*time, data=O_D1,control = lmerControl(optimizer = "bobyqa")))
  33. #ANOVAs based on models
  34. Anova(d1.discomfort, type="3", test="F" )
  35. Anova(d1.fear, type="3", test="F" )
  36. Anova(d1.phys, type="3", test="F" )
  37. #post-hoc tests
  38. emmeans(d1.discomfort, list(pairwise ~ stim:time), adjust = "none")
  39. emmeans(d1.fear, list(pairwise ~ stim:time), adjust = "none")
  40. emmeans(d1.phys, list(pairwise ~ stim:time), adjust = "none")
  41. #plot interaction effect
  42. plot(effect("stim*time",d1.discomfort))
  43. plot(effect("stim*time",d1.fear))
  44. plot(effect("stim*time",d1.phys))
  45. ##########################
  46. #FEAR/DISCOMFORT/PHYSICAL AROUSAL --> CS DISCRIMINATION CS+ - CS-
  47. ##########################
  48. # Create a new data frame with the differences
  49. csdiff <- O_D1 %>%
  50. # Group by subject and prepost
  51. group_by(sub, prepost) %>%
  52. # Calculate the differences for each variable
  53. summarise(diff_disc_rat = diff(disc_rat),
  54. diff_fear_rat = diff(fear_rat),
  55. diff_phys_rat = diff(phys_rat))%>%
  56. # Adjust the differences to be CSP - CSM
  57. mutate(across(starts_with("diff"), ~ -1 * .))
  58. csdiff$time<-as.factor(csdiff$prepost)
  59. #create model, now with CS differences as outcome measure, therefore only time as fixed effect
  60. d1.ddiff<-(lmer(diff_disc_rat~(1|sub)+time, data=csdiff,control = lmerControl(optimizer = "bobyqa")))
  61. d1.fdiff<-(lmer(diff_fear_rat~(1|sub)+time, data=csdiff,control = lmerControl(optimizer = "bobyqa")))
  62. d1.pdiff<-(lmer(diff_phys_rat~(1|sub)+time, data=csdiff,control = lmerControl(optimizer = "bobyqa")))
  63. #ANOVAs based on models
  64. Anova(d1.ddiff, type="3", test="F" )
  65. Anova(d1.fdiff, type="3", test="F" )
  66. Anova(d1.pdiff, type="3", test="F" )
  67. #post-hoc tests
  68. emmeans(d1.ddiff, list(pairwise ~ time), adjust = "none")
  69. emmeans(d1.fdiff, list(pairwise ~ time), adjust = "none")
  70. emmeans(d1.pdiff, list(pairwise ~ time), adjust = "none")
  71. #plot effect
  72. plot(effect("time",d1.ddiff))
  73. plot(effect("time",d1.fdiff))
  74. plot(effect("time",d1.pdiff))
  75. # _______ ___ ____ ____ ___
  76. # | \ / \ \ \ / / |__ \
  77. # | .--. | / ^ \ \ \/ / ) |
  78. # | | | | / /_\ \ \_ _/ / /
  79. # | '--' | / _____ \ | | / /_
  80. # |_______/ /__/ \__\ |__| |____|
  81. ##########################
  82. #FEAR/DISCOMFORT/PHYSICAL AROUSAL
  83. ##########################
  84. #read in data
  85. O_D2<-read.csv(file='/Users/mmueller/Documents/socialLearn/ana/R/d2_FearRating.csv',head=TRUE,sep=",")
  86. O_D2$stim<-as.factor(O_D2$stim)
  87. O_D2$time<-as.factor(O_D2$prepost)
  88. O_D2$sub<-O_D2$sub
  89. #create models
  90. d2.disc<-(lmer(disc_rat~(1|sub)+stim*time, data=O_D2,control = lmerControl(optimizer = "bobyqa")))
  91. d2.fear<-(lmer(fear_rat~(1|sub)+stim*time, data=O_D2,control = lmerControl(optimizer = "bobyqa")))
  92. d2.phys<-(lmer(phys_rat~(1|sub)+stim*time, data=O_D2,control = lmerControl(optimizer = "bobyqa")))
  93. #ANOVAs based on models
  94. Anova(d2.disc, type="3", test="F" )
  95. Anova(d2.fear, type="3", test="F" )
  96. Anova(d2.phys, type="3", test="F" )
  97. #post-hoc tests
  98. emmeans(d2.disc, list(pairwise ~ stim:time), adjust = "none")
  99. emmeans(d2.fear, list(pairwise ~ stim:time), adjust = "none")
  100. emmeans(d2.phys, list(pairwise ~ stim:time), adjust = "none")
  101. #plot interaction effect
  102. plot(effect("stim*time",d2.disc))
  103. plot(effect("stim*time",d2.fear))
  104. plot(effect("stim*time",d2.phys))
  105. ################
  106. ##SAFE/DANGER Ratings
  107. ################
  108. #now we have generalised stimuli
  109. #read in data
  110. O_D2s<-read.csv(file='/Users/mmueller/Documents/socialLearn/ana/R/d2_Safe_Ratings.csv',head=TRUE,sep=",")
  111. O_D2s$stim<-as.factor(O_D2s$stim)
  112. O_D2s$block<-as.factor(O_D2s$block)
  113. O_D2s$sub<-O_D2s$sub
  114. #create model
  115. d2.safe<-(lmer(safe_rat~(1|sub)+stim*block, data=O_D2s,control = lmerControl(optimizer = "bobyqa")))
  116. #ANOVA based on model
  117. Anova(d2.safe, type="3", test="F" )
  118. #post-hoc tests
  119. emmeans(d2.safe, list(pairwise ~ stim*block), adjust = "none")
  120. #options(max.print=2000)
  121. #plot effects
  122. plot(effect("stim",d2.safe))
  123. plot(effect("stim*block",d2.safe))