EEG_analysis.Rmd 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. ---
  2. title: "Blocked vs. Interleaved: How range contexts modulate time perception and its EEG signatures"
  3. author: "Cemre Baykan, Xiuna Zhu, Artyom Zinchenko, Zhuanghua Shi"
  4. date: "August 2023"
  5. ---
  6. ```{r setup, include=FALSE}
  7. knitr::opts_chunk$set(echo = TRUE)
  8. source('preproc_data.R')
  9. ```
  10. ### 1. Perception
  11. ```{r}
  12. # plot
  13. p1t_Perception_all <- ggplot(m_validAverageDat_short %>% filter(phase=="Perception") ,aes(x = t,y = CNV_m, colour = as.factor(targetDur), linetype=cond)) +geom_vline(xintercept = 300, linetype="dashed")+geom_vline(xintercept = 600, linetype="dashed")+
  14. geom_line(na.rm = FALSE, size=0.5) + labs(x = "Time (s)", y = "Amplitude(\u03BCV)", color='', linetype='') +
  15. geom_vline(xintercept = 0, size=0.2, color="grey") +
  16. geom_hline(yintercept = 0, size=0.2,color="grey") +
  17. coord_cartesian(ylim = c(8, -8), xlim = c(-200, 3600), expand = c(0,0))+theme_new2+ theme(legend.position = "right" )+ scale_x_continuous(breaks=c(0,400,1200,2400,3600), labels=c(0,0.4,1.2,2.4,3.6))+scale_color_manual(values = colors3,labels=c("0.4","0.566","0.8","1.2", "1.697","2.4"))
  18. p1t_Perception_all
  19. ```
  20. ###1.1 CNV buildup rate
  21. ```{r}
  22. allAverageDat1 = allAverageDat%>% filter( (t>= 300 & t <= 600)) %>% dplyr::group_by(cond,group, phase, SubName) %>% dplyr::summarise(n= n(), m_cnv = mean(CNV))
  23. allAverageDat1_sub = allAverageDat1 %>% dplyr::group_by(cond,SubName, phase) %>% dplyr::summarise(n= n(), mm_cnv = mean(m_cnv))
  24. allAverageDat1_sub %>% dplyr::group_by(cond, phase) %>% dplyr::summarise(n= n(), mmm_cnv = mean(mm_cnv), se_cnv= sd(mm_cnv)/sqrt(n-1))
  25. ```
  26. ```{r}
  27. mallAverageDat1 <- allAverageDat1 %>% dplyr::group_by(phase,cond,group) %>% dplyr::summarise(mm_cnv = mean(m_cnv), se_cnv = sd(m_cnv)/sqrt(n-1), n= n())
  28. mallAverageDat1=unique(mallAverageDat1)
  29. ```
  30. ```{r}
  31. mallAverageDat1_per= mallAverageDat1%>% filter(phase=="Perception")
  32. targ_labeller2=c("Lower","Upper")
  33. p_cnv1_target= ggplot(data=mallAverageDat1_per%>% mutate(cond = factor(cond, levels = c("BR", "IR")))%>% mutate(group = factor(group, levels = c("Lower", "Upper"))), aes(x=group, y=mm_cnv, color=group,fill=group,linetype=cond))+ geom_hline(yintercept = -0.5, linetype="dashed") +geom_hline(yintercept = -1, linetype="dashed")+ geom_hline(yintercept = 0, linetype="dashed") +geom_bar(stat="identity",position=position_dodge(), width=0.6, fill="white", alpha=0.9)+ theme_new + scale_color_manual(values=col11)+ theme(legend.position ="bottom") + labs(x="", y="Amplitude (\u03BCV)",color="",linetype="") + geom_errorbar(aes(ymin = mm_cnv-se_cnv,ymax = mm_cnv+ se_cnv),width = 0.1, position = position_dodge(width=0.6))+ scale_y_reverse()+ ggtitle("CNV buildup")+guides(colour = "none")+ coord_cartesian(ylim=c(0,-3)) +theme(legend.text=element_text(size=9))+theme(legend.key.size = unit(0.5, "cm"))+theme(legend.box.spacing = unit(-10, "pt"))
  34. p_cnv1_target
  35. ```
  36. # anova
  37. ```{r}
  38. mallAverageDat1_sub <- allAverageDat1 %>% group_by(phase,cond,SubName,group) %>% dplyr::summarise(mm_cnv = mean(m_cnv))
  39. ezANOVA(data = allAverageDat1%>% filter(phase=="Perception"), dv= m_cnv, wid=SubName, within=.(cond, group))
  40. ```
  41. ### 1.2 CNV mean ampl.
  42. ```{r}
  43. # calculate average CNV until offsets
  44. cnv_build2 <- data.frame()
  45. targetList <- c(400,566,800,1200,1697,2400)
  46. for (target in targetList){
  47. m_cnv_1 <- allAverageDat %>%filter(t>= 300 & t <= (target+300))%>% dplyr::group_by(cond,group, phase, SubName, targetDur) %>% dplyr::summarise(n= n(), m_CNV = mean(CNV))
  48. m_cnv_1= m_cnv_1%>% filter(targetDur==target)
  49. cnv_build2 <- rbind2(cnv_build2,m_cnv_1, by=c("SubName", "group","phase","targetDur","cond"))
  50. }
  51. ```
  52. ```{r}
  53. mallAverageDat2 <- cnv_build2 %>% dplyr::group_by(phase,cond, group,targetDur) %>% dplyr::summarise(mm_cnv = mean(m_CNV), se_cnv = sd(m_CNV)/sqrt(n-1), n= n())
  54. mallAverageDat2= unique(mallAverageDat2)
  55. tallAverageDat2 <- mallAverageDat2 %>% dplyr::group_by(phase,cond, group) %>% dplyr::summarise(mmm_cnv = mean(mm_cnv), se_cnv = sd(mm_cnv)/sqrt(n-1), n= n())
  56. tallAverageDat2= unique(tallAverageDat2)
  57. ```
  58. ```{r}
  59. targ_labeller= c('400'='0.4', '566'='0.566','800'='0.8','1200'='1.2','1697'='1.697','2400'='2.4')
  60. mallAverageDat2$targetDur= as.factor(mallAverageDat2$targetDur)
  61. p_cnv2_target= ggplot(data=mallAverageDat2%>% filter(phase=="Perception")%>% mutate(cond = factor(cond, levels = c("BR", "IR")))%>% mutate(group = factor(group, levels = c("Lower", "Upper"))), aes(x=cond, y=mm_cnv, color=group,fill=group, linetype=cond))+ geom_hline(yintercept = -1, linetype="dashed")+geom_hline(yintercept = -2, linetype="dashed")+geom_hline(yintercept = -3, linetype="dashed")+ geom_hline(yintercept = 0, linetype="dashed") +geom_bar(stat="identity",position=position_dodge(), width=0.6, fill="white", alpha=0.9)+ theme_new + theme(legend.position = "bottom") + labs(x="", y="Amplitude (\u03BCV)",color="", linetype="") + geom_errorbar(aes(ymin = mm_cnv-se_cnv,ymax = mm_cnv+ se_cnv),width = 0.1, position = position_dodge(width=0.6)) + scale_y_reverse() + scale_color_manual(values=col11) + ggtitle("Mean CNV")+ scale_x_discrete(labels=c(0.4,0.566,0.8, 1.2,1.697,2.4))+facet_grid(~targetDur,labeller=as_labeller(targ_labeller))+
  62. theme(axis.title.x=element_blank(),
  63. axis.text.x=element_blank(),
  64. axis.ticks.x = element_blank())+theme(legend.box.spacing = unit(3, "pt"))
  65. p_cnv2_target
  66. ```
  67. # lmm
  68. ```{r}
  69. cnv_build2_p= cnv_build2 %>% filter(phase=="Perception")
  70. cnv_build2_p= cnv_build2_p%>% dplyr::mutate(cond= factor(cond,levels=c("IR","BR")))
  71. cnv_build2_p= cnv_build2_p%>% dplyr::mutate(group= factor(group,levels=c("Lower","Upper")))
  72. cnv_build2_p$targetDur_n= cnv_build2_p$targetDur-1200
  73. cnv_build2_p$targetDur_n= cnv_build2_p$targetDur_n/1000
  74. cnv_build2$cond = as.factor(cnv_build2$cond)
  75. cnv_build2$group = as.factor(cnv_build2$group)
  76. contrasts(cnv_build2$cond) = contr.Sum(levels(cnv_build2$cond))
  77. contrasts(cnv_build2$group) = contr.Sum(levels(cnv_build2$group))
  78. ```
  79. ```{r}
  80. # fit the linear mixed model
  81. fit_m2 = lmer(m_CNV ~
  82. cond*targetDur_n +group+ # fixed effect, covariate and interaction
  83. (targetDur_n|SubName), # random effect
  84. data = cnv_build2_p)
  85. #output as table
  86. tab_model(fit_m2, p.val = 'kr') # Kenward-Roger approximation, alternative 'satterthwaite' approxmiation
  87. ```
  88. ## 1.3 CNV width
  89. ```{r}
  90. mcnv_width_per <- cnv_width_per %>% group_by(cond, targetDur) %>% dplyr::summarise(n= n(),mlat = mean(time), se_lat = sd(time)/sqrt(n-1))
  91. ```
  92. #lmm
  93. ```{r}
  94. cnv_width_per$cond = as.factor(cnv_width_per$cond)
  95. contrasts(cnv_width_per$cond) = contr.Sum(levels(cnv_width_per$cond))
  96. cnv_width_per$targetDur_n = cnv_width_per$targetDur - 1200
  97. ```
  98. ```{r}
  99. scnv_width_per <- cnv_width_per %>% group_by(cond, targetDur,targetDur_n, SubName,group) %>% dplyr::summarise(n= n(),time = mean(time))
  100. ```
  101. ```{r}
  102. # fit the linear mixed model
  103. fit_m2_p = lmer(time ~
  104. cond*targetDur_n+group + # fixed effect, covariate and interaction
  105. (targetDur_n|SubName), # random effect
  106. data = scnv_width_per)
  107. #output as table
  108. tab_model(fit_m2_p, p.val = 'kr') # Kenward-Roger approximation, alternative 'satterthwaite' approxmiation
  109. ```
  110. ```{r}
  111. m_scnv_width_per = scnv_width_per%>% group_by(cond, SubName) %>% dplyr::summarise(n= n(),time = mean(time))
  112. m_scnv_width_per %>% group_by(cond) %>% dplyr::summarise(time = mean(time))
  113. ```
  114. ######figure 3
  115. ```{r}
  116. frow <- plot_grid(p_cnv1_target,p_cnv2_target, nrow=1, rel_widths = c(1, 3), labels=c("c","d"))
  117. srow <- plot_grid(NULL,p1t_Perception_all,frow, nrow=3, rel_heights = c(0.5,1,0.8), labels=c("a","b",""))
  118. ggsave("figures/figure3.png", srow, width = 8.5, height = 6.5)
  119. srow
  120. ```
  121. ## 2. Offset-P2
  122. ```{r}
  123. mallAverageDat_p2=allAverageDat_p2%>% group_by(phase, group, cond, targetDur,t) %>% dplyr::summarise(mP2 = mean(P2), sd_P2 = sd(P2),n= n())
  124. ```
  125. ```{r}
  126. #mix condition
  127. p1t_Perception_p2 <- ggplot(mallAverageDat_p2 %>% filter(phase=="Perception") ,aes(x = t,y = mP2, colour = as.factor(targetDur), linetype=cond))+
  128. geom_line(na.rm = FALSE) + labs(x = "Time (s)", y = "Amplitude(\u03BCV)", color='', linetype='') +
  129. geom_vline(xintercept = 0, size=0.2, color="grey") +
  130. geom_hline(yintercept = 0, size=0.2,color="grey") +
  131. scale_x_continuous(breaks= c(0, 200, 600,1000), labels=c("0","0.2","0.6","1.0")) +theme_new2+ theme(legend.key.size = unit(0.5, "cm"))+scale_color_manual(values = colors3,labels=c("0.4","0.566","0.8","1.2", "1.697","2.4"))+
  132. scale_y_reverse()+coord_cartesian(ylim = c(8, -5), xlim = c(-100, 1000), expand = c(0,0))+theme(legend.position = "right")
  133. p1t_Perception_p2
  134. ```
  135. ### 2.1 P2 mean ampl.
  136. ```{r}
  137. m_validAverageDat_p2 = allAverageDat_p2%>% filter( (t>= 200 & t <= 300)) %>% group_by(cond,group,targetDur, phase, SubName) %>% dplyr::summarise(n= n(), m_P2 = mean(P2),se_P2 = sd(P2)/sqrt(n-1))
  138. m_validAverageDat_p2_percep= m_validAverageDat_p2 %>% filter(phase=="Perception")
  139. m_validAverageDat_p2_reprod= m_validAverageDat_p2 %>% filter(phase=="Reproduction")
  140. ```
  141. ```{r}
  142. mm_validAverageDat_p2 <- m_validAverageDat_p2 %>% group_by(phase,cond, group,targetDur) %>% dplyr::summarise(mm_P2 = mean(m_P2), se_P2 = sd(m_P2)/sqrt(n-1), n= n())
  143. mm_validAverageDat_p2_percep= mm_validAverageDat_p2 %>% filter(phase=="Perception")
  144. mm_validAverageDat_p2_reprod= mm_validAverageDat_p2 %>% filter(phase=="Reproduction")
  145. ```
  146. ```{r}
  147. targ_labeller2= c('0.4'='0.4', '0.566'='0.566','0.8'='0.8','1.2'='1.2','1.697'='1.697','2.4'='2.4')
  148. p_cnv2_target_p2= ggplot(data=mm_validAverageDat_p2_percep%>% filter(phase=="Perception")%>% mutate(cond = factor(cond, levels = c("BR", "IR")))%>% mutate(group = factor(group, levels = c("Lower", "Upper"))), aes(x=cond, y=mm_P2, color=group,fill=group,linetype=cond)) +geom_bar(stat="identity",position=position_dodge(), width=0.6, fill="white", alpha=0.9)+ theme_new2+ scale_color_manual(values=col11)+ theme(legend.position = "bottom") + labs(x="", y="Amplitude (\u03BCV)",color="",linetype="") + geom_errorbar(aes(ymin = mm_P2-se_P2,ymax = mm_P2+ se_P2),width = 0.1, position = position_dodge(width=0.6)) + ggtitle("Mean P2") +facet_grid(~targetDur,labeller=as_labeller(targ_labeller2))+
  149. theme(axis.title.x=element_blank(),
  150. axis.text.x=element_blank(),
  151. axis.ticks.x=element_blank())+theme(legend.key.size = unit(0.3, "cm"))
  152. p_cnv2_target_p2
  153. ```
  154. #lmm
  155. ```{r}
  156. m_validAverageDat_p2_percep$cond = as.factor(m_validAverageDat_p2_percep$cond)
  157. m_validAverageDat_p2_percep$group = as.factor(m_validAverageDat_p2_percep$group)
  158. contrasts(m_validAverageDat_p2_percep$cond) = contr.Sum(levels(m_validAverageDat_p2_percep$cond))
  159. contrasts(m_validAverageDat_p2_percep$group) = contr.Sum(levels(m_validAverageDat_p2_percep$group))
  160. m_validAverageDat_p2_percep$targetDur_n = m_validAverageDat_p2_percep$targetDur - 1.2
  161. ```
  162. ```{r}
  163. # fit the linear mixed model
  164. fit_m7 = lmer(m_P2 ~
  165. cond*targetDur_n+group + # fixed effect, covariate and interaction
  166. (targetDur_n|SubName), # random effect
  167. data = m_validAverageDat_p2_percep)
  168. #output as table
  169. tab_model(fit_m7, p.val = 'kr') # Kenward-Roger approximation, alternative 'satterthwaite' approxmiation
  170. ```
  171. ```{r}
  172. mm_validAverageDat_p2_percep %>% group_by(cond) %>% dplyr::summarise(mm_P2 = mean(mm_P2),n=n())
  173. ```
  174. ###### figure 4
  175. ```{r}
  176. frow_31 <- plot_grid(NULL,p1t_Perception_p2,nrow=2,ncol = 1, rel_heights = c(0.35,1), labels=c("a",""))
  177. frow_3 <- plot_grid(frow_31,p_cnv2_target_p2,nrow=1,ncol = 2, rel_widths = c(3.2, 1.7), labels=c("","b"))
  178. ggsave("figures/figure4.png", frow_3, width = 8.5, height = 2.7)
  179. frow_3
  180. ```
  181. # 3. LPCt
  182. ```{r}
  183. m_allAverageDat_resp <- allAverageDat_resp %>% group_by(phase, group, cond, targetDur,t) %>% dplyr::summarise(m_p3 = mean(P3), sd_p3 = sd(P3),n= n())
  184. ```
  185. ```{r}
  186. plt_resp <- ggplot(m_allAverageDat_resp %>% filter(phase=="Perception") ,aes(x = t,y = m_p3, colour = as.factor(targetDur), linetype=cond)) +
  187. geom_line(na.rm = FALSE, size=0.5) + labs(x = "Time (s)", y = "Amplitude(\u03BCV)", color='', linetype='') +
  188. geom_vline(xintercept = 0, size=0.2, color="grey") +
  189. geom_hline(yintercept = 0, size=0.2,color="grey") +theme_new2+ theme(legend.position = "right" )+theme(legend.key.size = unit(0.5, "cm"))+ scale_x_continuous(breaks=c(-1300,-800,-300,0,300), labels=c(-1.3,-0.8,-0.3,0,0.3)) + scale_y_reverse() +coord_cartesian(ylim = c(8, -8), expand = c(0,0)) + scale_color_manual(values = colors3,labels=c("0.4","0.566","0.8","1.2", "1.697","2.4"))+ coord_cartesian(xlim=c(-1000,500), ylim=c(7,-5))
  190. plt_resp
  191. ```
  192. ```{r}
  193. allAverageDat_resp$targetDur = allAverageDat_resp$targetDur* 1000
  194. ```
  195. ```{r}
  196. # calculate average amplitude in pre-reproduction
  197. cnv_build3 <- data.frame()
  198. targetList <- c(400,566,800,1200,1697,2400)
  199. for (target in targetList){
  200. m_cnv_1 <- allAverageDat_resp %>%filter(t>= (-800) & t <= (-600))%>% group_by(cond,group, phase, SubName, targetDur) %>% dplyr::summarise(n= n(), m_P3 = mean(P3),se_P3 = sd(P3)/sqrt(n-1))
  201. m_cnv_1= m_cnv_1%>% filter(targetDur==target)
  202. cnv_build3 <- rbind2(cnv_build3,m_cnv_1, by=c("SubName", "group","phase","targetDur","cond"))
  203. }
  204. ```
  205. ```{r}
  206. mallAverageDat3 <- cnv_build3 %>% group_by(phase,cond, group,targetDur) %>% dplyr::summarise(mm_p3 = mean(m_P3), se_p3 = sd(m_P3)/sqrt(n-1), n= n())
  207. mallAverageDat3= unique(mallAverageDat3)
  208. tallAverageDat3 <- mallAverageDat3 %>% group_by(phase,cond, group) %>% dplyr::summarise(mmm_p3 = mean(mm_p3), se_p3 = sd(mm_p3)/sqrt(n-1), n= n())
  209. tallAverageDat3= unique(tallAverageDat3)
  210. ```
  211. ```{r}
  212. targ_labeller= c('400'='0.4', '566'='0.566','800'='0.8','1200'='1.2','1697'='1.697','2400'='2.4')
  213. p_cnv3_target= ggplot(data=mallAverageDat3%>% filter(phase=="Perception")%>% mutate(cond = factor(cond, levels = c("BR", "IR")))%>% mutate(group = factor(group, levels = c("Lower", "Upper"))), aes(x=cond, y=mm_p3, color=group,fill=group,linetype=cond)) +geom_bar(stat="identity",position=position_dodge(), width=0.6, fill="white", alpha=0.9)+ theme_new2+ scale_color_manual(values=col11)+ theme(legend.position = "bottom") + labs(x="", y="Amplitude (\u03BCV)",color="",linetype="") + geom_errorbar(aes(ymin = mm_p3-se_p3,ymax = mm_p3+ se_p3),width = 0.1, position = position_dodge(width=0.6)) + ggtitle("Mean LPCt") +facet_grid(~targetDur,labeller=as_labeller(targ_labeller))+
  214. theme(axis.title.x=element_blank(),
  215. axis.text.x=element_blank(),
  216. axis.ticks.x=element_blank())+theme(legend.key.size = unit(0.3, "cm"))
  217. p_cnv3_target
  218. ```
  219. #lmm
  220. ```{r}
  221. cnv_build3_p = cnv_build3%>%filter(phase=='Perception')
  222. cnv_build3_p$cond = as.factor(cnv_build3_p$cond)
  223. contrasts(cnv_build3_p$cond) = contr.Sum(levels(cnv_build3_p$cond))
  224. cnv_build3_p$targetDur=cnv_build3_p$targetDur/1000
  225. cnv_build3_p$targetDur_n = cnv_build3_p$targetDur - 1.2
  226. cnv_build3_p$group = as.factor(cnv_build3_p$group)
  227. ```
  228. ```{r}
  229. # fit the linear mixed model
  230. fit_m2 = lmer(m_P3 ~
  231. cond*targetDur_n+group+ # fixed effect, covariate and interaction
  232. (targetDur_n|SubName), # random effect
  233. data = cnv_build3_p)
  234. #output as table
  235. tab_model(fit_m2, p.val = 'kr') # Kenward-Roger approximation, alternative 'satterthwaite' approxmiation
  236. ```
  237. ```{r}
  238. tallAverageDat3%>% filter(phase=="Perception")%>% group_by(cond)%>% dplyr::summarise(m_p3= mean(mmm_p3))
  239. ```
  240. ```{r}
  241. tallAverageDat3%>% filter(phase=="Perception")%>% group_by(group,cond)%>% dplyr::summarise(m_p3= mean(mmm_p3))
  242. ```
  243. ######figure 5
  244. ```{r}
  245. frow_21 <- plot_grid(NULL,plt_resp,nrow=2,ncol = 1, rel_heights = c(0.35, 1), labels=c("a",""))
  246. frow_2 <- plot_grid(frow_21,p_cnv3_target,nrow=1,ncol = 2, rel_widths = c(3.2, 1.8), labels=c("","b"))
  247. ggsave("figures/figure5.png", frow_2, width = 8.5, height = 2.7)
  248. frow_2
  249. ```
  250. ### 4. Reproduction
  251. ```{r}
  252. m_validAverageDat <- allAverageDat %>% group_by(phase, group, cond, targetDur,t) %>% dplyr::summarise(CNV_m = mean(CNV), CNV_sd = sd(CNV),n= n())
  253. m_validAverageDat_split <- split(m_validAverageDat, f = list(m_validAverageDat$phase,m_validAverageDat$cond))
  254. #full range condition
  255. p1t_Reproduction_all <- ggplot(m_validAverageDat_short %>% filter(phase=="Reproduction") ,aes(x = t,y = CNV_m, colour = as.factor(targetDur), linetype=cond)) +
  256. geom_line(na.rm = FALSE, size=0.5) + labs(x = "Time (s)", y = "Amplitude(\u03BCV)", color='', linetype='') +geom_vline(xintercept = 300, linetype="dashed")+geom_vline(xintercept = 600, linetype="dashed")+
  257. geom_vline(xintercept = 0, size=0.2, color="grey") +
  258. geom_hline(yintercept = 0, size=0.2,color="grey") +
  259. scale_x_continuous(breaks= c(0, 400, 1400, 2400)) +
  260. coord_cartesian(ylim = c(8, -8), xlim = c(-200, 3600), expand = c(0,0))+theme_new2+ scale_x_continuous(breaks=c(0,400,1200,2400,3600), labels=c(0,0.4,1.2,2.4, 3.6))+ scale_color_manual(values = colors3, labels=c("0.4", "0.566","0.8", "1.2","1.697","2.4"))
  261. p1t_Reproduction_all
  262. ```
  263. ## 4.1 CNV buildup rate
  264. ```{r}
  265. p_cnv1_target_p= ggplot(data=mallAverageDat1%>% filter(phase=="Reproduction")%>% mutate(cond = factor(cond, levels = c("BR", "IR")))%>% mutate(group = factor(group, levels = c("Lower", "Upper"))), aes(x=group, y=mm_cnv, color=group,fill=group,linetype=cond))+ geom_hline(yintercept = -2, linetype="dashed") +geom_hline(yintercept = -1, linetype="dashed")+ geom_hline(yintercept = 0, linetype="dashed") +geom_bar(stat="identity",position=position_dodge(), width=0.6, fill="white", alpha=0.9)+ theme_new + scale_color_manual(values=col11)+ theme(legend.position = "top") + labs(x="", y="Amplitude (\u03BCV)",color="", linetype='') + geom_errorbar(aes(ymin = mm_cnv-se_cnv,ymax = mm_cnv+ se_cnv),width = 0.1, position = position_dodge(width=0.6)) + scale_y_reverse() +theme(legend.key.size = unit(0.3, "cm"))+ coord_cartesian(ylim=c(0,-3.5)) + ggtitle("CNV buildup") +guides(color = "none")+ guides(linetype=guide_legend(nrow=2,byrow=TRUE))+ theme(legend.position = c(0.75,0.9))
  266. p_cnv1_target_p
  267. ```
  268. ```{r}
  269. allAverageDat1_sub2 = allAverageDat1 %>% dplyr::group_by(group,SubName, phase) %>% dplyr::summarise(n= n(), mm_cnv = mean(m_cnv))
  270. allAverageDat1_sub2 %>% dplyr::group_by(group, phase) %>% dplyr::summarise(n= n(), mmm_cnv = mean(mm_cnv), se_cnv= sd(mm_cnv)/sqrt(n-1))
  271. ```
  272. #anova
  273. ```{r}
  274. ezANOVA(data = allAverageDat1%>% filter(phase=="Reproduction"), dv= m_cnv, wid=SubName, within=.(cond, group))
  275. ```
  276. ###4.2 CNV mean ampl.
  277. #lmm
  278. ```{r}
  279. cnv_build2_r= cnv_build2%>% filter(phase=="Reproduction")
  280. cnv_build2_r$targetDur=cnv_build2_r$targetDur/1000
  281. cnv_build2_r$targetDur_n = cnv_build2_r$targetDur - 1.2
  282. ```
  283. ```{r}
  284. # fit the linear mixed model
  285. fit_m2 = lmer(m_CNV ~
  286. cond*targetDur_n+group + # fixed effect, covariate and interaction
  287. (targetDur_n|SubName), # random effect
  288. data = cnv_build2_r)
  289. #output as table
  290. tab_model(fit_m2, p.val = 'kr') # Kenward-Roger approximation, alternative 'satterthwaite' approxmiation
  291. ```
  292. ### 4.3 CNV width
  293. ```{r}
  294. mcnv_width_rep <- cnv_width_rep %>% group_by(cond, targetDur) %>% dplyr::summarise(n= n(),mlat = mean(time), se_lat = sd(time)/sqrt(n-1))
  295. ```
  296. ```{r}
  297. mcnv_width_rep %>% group_by(cond) %>% dplyr::summarise(n= n(),time = mean(mlat))
  298. ```
  299. ```{r}
  300. cnv_width_rep$cond = as.factor(cnv_width_rep$cond)
  301. contrasts(cnv_width_rep$cond) = contr.Sum(levels(cnv_width_rep$cond))
  302. cnv_width_rep$targetDur=cnv_width_rep$targetDur*1000
  303. cnv_width_rep$targetDur_n = cnv_width_rep$targetDur - 1200
  304. ```
  305. ```{r}
  306. scnv_width_rep <- cnv_width_rep %>% group_by(cond, targetDur,targetDur_n, SubName,group) %>% dplyr::summarise(n= n(),time = mean(time))
  307. scnv_width_rep$targetDur_n = scnv_width_rep$targetDur- 1200
  308. ```
  309. #lmm
  310. ```{r}
  311. scnv_width_rep= scnv_width_rep%>% dplyr::mutate(cond= factor(cond,levels=c("IR","BR")))
  312. scnv_width_rep= scnv_width_rep%>% dplyr::mutate(group= factor(group,levels=c("Lower","Upper")))
  313. # fit the linear mixed model
  314. fit_m2_p = lmer(time ~
  315. cond*targetDur_n +group+ # fixed effect, covariate and interaction
  316. (targetDur_n|SubName), # random effect
  317. data = scnv_width_rep)
  318. #output as table
  319. tab_model(fit_m2_p, p.val = 'kr') # Kenward-Roger approximation, alternative 'satterthwaite' approxmiation
  320. ```
  321. # figure 6
  322. ```{r}
  323. frow_r <- plot_grid(NULL,p1t_Reproduction_all,nrow=2,ncol = 1, rel_heights = c(0.35,1), labels=c("a",""))
  324. frow_r2 <- plot_grid(frow_r,p_cnv1_target_p,nrow=1,ncol = 2, rel_widths = c(4.5,1), labels=c("","b"))
  325. ggsave("figures/figure6.png", frow_r2, width = 8.5, height = 3.2)
  326. frow_r2
  327. ```