EEG_analysis.Rmd 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. ---
  2. title: "Electrophysiological signatures of temporal context in the bisection task"
  3. author: "Cemre Baykan, Xiuna Zhu, Artyom Zinchenko, Hermann Muller, Zhuanghua Shi"
  4. date: "February 2023"
  5. ---
  6. ```{r setup, include=FALSE}
  7. knitr::opts_chunk$set(echo = TRUE)
  8. source('dataana.R')
  9. ```
  10. #EXPERIMENT 1
  11. ```{r}
  12. # to see GM and AM of the sets
  13. mDist_exp= eeg_dat_all %>% dplyr::group_by(Exp, cond) %>%
  14. dplyr::summarise(n= n(),
  15. am_Dur = mean(targetDur),
  16. sd_Dur = sd(targetDur),
  17. gm_Dur = gm_mean(targetDur),
  18. se_Dur = sd(targetDur)/sqrt(n-1))
  19. mDist_exp
  20. ```
  21. ```{r}
  22. cnv_fit <- function(df){
  23. fit = lm(CNV ~ t, data = df)
  24. return(tibble(B = fit$coefficients[1], slope= fit$coefficients[2]))
  25. }
  26. ```
  27. # 1.CNV
  28. ## Slopes
  29. ```{r}
  30. allAverageDat_ica_exp1_ns_n <- allAverageDat_exp1 %>% filter(cond == 'NS')
  31. allAverageDat_ica_exp1_ns_n$targetDur = allAverageDat_ica_exp1_ns_n$targetDur /1000
  32. allAverageDat_ica_exp1_ns_n$t = allAverageDat_ica_exp1_ns_n$t /1000
  33. allAverageDat_ica_exp1_ns_n %>% filter(t <= 0.65& t >= 0.25) %>%
  34. group_by(SubName) %>% nest() %>%
  35. mutate(model = map(data, cnv_fit)) %>%
  36. unnest(model, .drop = TRUE) -> lm_exp1_ns_slope1
  37. lm_exp1_ns_slope1$cond <- "NS"
  38. t.test(lm_exp1_ns_slope1$slope, mu = 0, alternative = "two.sided")
  39. ```
  40. ```{r}
  41. #in ps
  42. allAverageDat_ica_exp1_ps_n <- allAverageDat_exp1 %>% filter(cond == 'PS')
  43. allAverageDat_ica_exp1_ps_n$targetDur = allAverageDat_ica_exp1_ps_n$targetDur /1000
  44. allAverageDat_ica_exp1_ps_n$t = allAverageDat_ica_exp1_ps_n$t /1000
  45. allAverageDat_ica_exp1_ps_n %>% filter(t <= 0.65 & t >= 0.25) %>%
  46. group_by(SubName) %>% nest() %>%
  47. mutate(model = map(data, cnv_fit)) %>%
  48. unnest(model, .drop = TRUE) -> lm_exp1_ps_slope1
  49. lm_exp1_ps_slope1$cond <- "PS"
  50. t.test(lm_exp1_ps_slope1$slope, mu = 0, alternative = "two.sided")
  51. ```
  52. ```{r}
  53. #based on condition?
  54. lm_exp1_slope1 <- rbind(lm_exp1_ps_slope1, lm_exp1_ns_slope1)
  55. lm_exp1_slope1 %>% group_by(cond) %>%dplyr::summarise(n=n(), m_slope=mean(slope), se_slope=sd(slope)/sqrt(n-1))
  56. ```
  57. ```{r}
  58. ezANOVA(data = lm_exp1_slope1, dv= slope, wid=SubName, within=.(cond))
  59. ```
  60. # 1.1 CNV activity
  61. ```{r}
  62. m_allAverageDat_exp1 <- allAverageDat_exp1 %>% group_by(targetDur,t, cond) %>%
  63. dplyr::summarise(n= n(), m_CNV = mean(CNV),se_CNV = sd(CNV)/sqrt(n-1))
  64. ```
  65. ###separate plots
  66. ```{r}
  67. c1 <- c("#000333", "#0000CC", "#3399CC", "#33FFFF","#66CC00", "#CC0066", "#FF3333")
  68. p1 <- ggplot(m_allAverageDat_exp1 %>% filter(cond=="PS"), aes(x=t, y=m_CNV, color=as.factor(targetDur)))+geom_line(size=0.5)+
  69. scale_color_manual(values=c1) + mytheme+
  70. scale_y_reverse() + labs(x = "Time (ms)", y = "Amplitude (\u03BCV)", color='') +
  71. scale_x_continuous(breaks= c(0, 400,800,1200, 1600, 2000), labels = c(0, 400,800,1200, 1600,2000)) +coord_cartesian(ylim = c(7.5, -6),xlim = c(-200, 2400), expand = c(0,0))+ ggtitle("Short context (PS)")+geom_hline(yintercept=0, linetype="dashed")+theme(legend.key.size = unit(0.5, "cm"))
  72. p1<-p1+theme(axis.title.y = element_text(angle=90))
  73. p1
  74. ```
  75. ```{r}
  76. c11<-c("#000333", "#3833cc", "#33a1cc","#33cca6","#c2cc33", "#b233cc", "#FF3333")
  77. p11<-ggplot(m_allAverageDat_exp1 %>% filter(cond=="NS" ), aes(x=t, y=m_CNV, color=as.factor(targetDur)))+geom_line(size=0.5)+
  78. scale_color_manual(values=c11)+
  79. scale_y_reverse() + mytheme+ labs(x = "Time (ms)", y = "Amplitude (\u03BCV)", color='') +
  80. scale_x_continuous(breaks= c(0, 400,800,1200, 1600,2000), labels = c(0, 400,800,1200, 1600,2000)) +coord_cartesian(ylim = c(7.5, -6),xlim = c(-200, 2400), expand = c(0,0))+ ggtitle("Long context (NS)")+theme(legend.key.size = unit(0.5, "cm"))+
  81. geom_hline(yintercept=0, linetype="dashed")
  82. p11<-p11 +theme(axis.title.y = element_text(angle=90))
  83. p11
  84. ```
  85. # 1.2 CNV-Peak
  86. We check the negativity peak latency within 0-1600 ms using a custom peak detection function.
  87. ```{r}
  88. mexp1_negPeak_lat <- exp1_negPeak_lat %>% group_by(cond, SubName) %>% dplyr::summarise(m_lat = mean(Latency), m_amp= mean(Amplitude))
  89. mmexp1_negPeak_lat <-mexp1_negPeak_lat %>% group_by(cond) %>% dplyr::summarise(n=n(),mm_lat = mean(m_lat), se_lat= sd(m_lat)/sqrt(n-1), mm_amp=mean(m_amp), se_amp=sd(m_amp)/sqrt(n-1))
  90. mmexp1_negPeak_lat
  91. ```
  92. ```{r}
  93. ezANOVA(data = mexp1_negPeak_lat, dv= m_lat, wid=SubName, within=.(cond))
  94. ```
  95. ```{r}
  96. ezANOVA(data = mexp1_negPeak_lat, dv= m_amp, wid=SubName, within=.(cond))
  97. ```
  98. ##barplot
  99. ```{r}
  100. exp1_lat= ggplot(data=mmexp1_negPeak_lat%>% mutate(cond = factor(cond, levels = c("PS", "NS"))), aes(x=cond, y=mm_lat, color=cond,fill=cond)) +geom_bar(stat="identity",position=position_dodge(), width=0.6, fill="white", alpha=0.9)+ mytheme+ scale_color_manual(values=colors_plot)+ theme(legend.position = "") + labs(x="", y="Peak latency (ms)") + geom_errorbar(data=mmexp1_negPeak_lat%>% mutate(cond = factor(cond, levels = c("PS", "NS"))),aes(ymin = mm_lat-se_lat,ymax = mm_lat+ se_lat),width = 0.1, position = position_dodge(width=0.6))+ coord_cartesian(ylim=c(0,1000)) +theme(axis.text.y= element_text(angle=90))+ggtitle("")+theme(plot.title = element_text(color = "black", size = 10))
  101. exp1_lat
  102. ```
  103. ```{r}
  104. exp1_amp= ggplot(data=mmexp1_negPeak_lat%>% mutate(cond = factor(cond, levels = c("PS", "NS"))), aes(x=cond, y=mm_amp, color=cond,fill=cond)) +geom_bar(stat="identity",position=position_dodge(), width=0.6, fill="white", alpha=0.9)+ scale_y_reverse() +mytheme+ scale_color_manual(values=colors_plot)+ theme(legend.position = "") + labs(x="", y="Peak amplitude (\u03BCV)") + geom_errorbar(aes(ymin = mm_amp-se_amp,ymax = mm_amp+ se_amp),width = 0.1, position = position_dodge(width=0.6))+coord_cartesian(ylim=c(0,-7))+ggtitle("")+theme(plot.title = element_text(color = "black", size = 10))
  105. exp1_amp
  106. ```
  107. ```{r}
  108. m_peak_exp1_targets <- exp1_negPeak_lat %>% group_by(targetDur,cond) %>% dplyr::summarise(n=n(), m_lat = mean(Latency),
  109. se_lat = sd(Latency)/sqrt(n-1),
  110. m_amp = mean(Amplitude),
  111. se_amp = sd(Amplitude)/sqrt(n-1))
  112. ```
  113. ##plot
  114. ```{r}
  115. p_exp1_lat <- m_peak_exp1_targets%>% mutate(cond = factor(cond, levels = c("PS", "NS")))%>%
  116. ggplot(aes(x=targetDur, y=m_lat, color=cond))+geom_point()+
  117. geom_abline(slope=1, linetype="dashed", color='black')+
  118. geom_errorbar(aes(ymin = m_lat-se_lat,ymax = m_lat+se_lat),width = 40, position = position_dodge(width=0.5))+mytheme+
  119. theme(axis.text.y= element_text(angle=90)) +
  120. geom_line()+theme(legend.position = c(0.9,0.9))+
  121. labs(x = "Target interval (ms)", y = "Peak latency (ms)") +scale_color_manual(values = colors_plot) + theme(legend.title = element_blank())+ #scale_x_continuous(breaks=c(400,800,1200,1600), labels = c("0.4","0.8","1.2","1.6"))
  122. coord_cartesian(ylim = c(500,1200)) +theme(axis.text.y= element_text(angle=90))+ggtitle("CNV")
  123. p_exp1_lat
  124. ```
  125. #1.3 CNV-mean amplitude
  126. ```{r}
  127. # calculate average CNV
  128. t_ps <- data.frame()
  129. targetList <- c(400,504,636,800,1008,1270, 1600)
  130. for (target in targetList){
  131. m_cnv_1 <- allAverageDat_exp1%>% filter( (t>= 250 & t <= (target+250))) %>% group_by(cond,targetDur,SubName) %>% dplyr::summarise(n= n(), m_CNV = mean(CNV),se_CNV = sd(CNV)/sqrt(n-1))
  132. m_cnv_1= m_cnv_1%>% filter(targetDur== target & cond=="PS")
  133. t_ps <- rbind2(t_ps,m_cnv_1, by=c("SubName","targetDur","cond"))
  134. }
  135. ```
  136. ```{r}
  137. # calculate average CNV
  138. t_ns <- data.frame()
  139. targetList <- c(400,730,992,1200,1366,1496, 1600)
  140. for (target in targetList){
  141. m_cnv_2 <- allAverageDat_exp1%>% filter( (t>= 250 & t <= (target+250))) %>% group_by(cond,targetDur,SubName) %>% dplyr::summarise(n= n(), m_CNV = mean(CNV),se_CNV = sd(CNV)/sqrt(n-1))
  142. m_cnv_2= m_cnv_2%>% filter(targetDur== target & cond=="NS")
  143. t_ns <- rbind2(t_ns,m_cnv_2, by=c("SubName","targetDur","cond"))
  144. }
  145. ```
  146. ```{r}
  147. exp1_t <- rbind(t_ps, t_ns)
  148. t_targets <- exp1_t %>% group_by(targetDur, cond)%>% dplyr::summarise(n=n(), mm_CNV =mean(m_CNV), se_CNV=sd(m_CNV)/sqrt(n-1))
  149. m_exp1_t <- exp1_t %>% group_by(SubName, cond)%>% dplyr::summarise(n=n(), mm_CNV =mean(m_CNV), se_CNV=sd(m_CNV)/sqrt(n-1))
  150. m_exp1_t %>% group_by(cond)%>% dplyr::summarise(n=n(), mmm_CNV =mean(mm_CNV))
  151. ```
  152. ##plot
  153. ```{r}
  154. exp1_ave <- t_targets%>% mutate(cond = factor(cond, levels = c("PS", "NS")))%>%
  155. ggplot(aes(x=targetDur, y=mm_CNV, color=cond)) +
  156. geom_point(position = position_dodge(width=30))+
  157. geom_errorbar(aes(ymin = mm_CNV-se_CNV,ymax = mm_CNV+se_CNV),width = 80, position = position_dodge(width=30),size=0.5)+mytheme+
  158. geom_line()+
  159. labs(x = "Target interval (ms)", y = "Mean amplitude (\u03BCV)") +scale_y_reverse()+
  160. scale_color_manual(values = colors_plot)+ theme(legend.position = c(0.9,0.25), legend.title = element_blank()) + scale_x_continuous(breaks = c(400,800,1200,1600), labels = c(400,800,1200,1600))+coord_cartesian(ylim = c(2.5, -3)) + ggtitle("")
  161. exp1_ave
  162. ```
  163. ##mixed linear model
  164. ```{r}
  165. exp1_t$targetDur = exp1_t$targetDur /1000
  166. exp1_t$cond = as.factor(exp1_t$cond)
  167. contrasts(exp1_t$cond) = contr.Sum(levels(exp1_t$cond))
  168. ```
  169. ```{r}
  170. # fit the linear mixed model
  171. exp1_mean_cnv = lmer(m_CNV ~
  172. cond*targetDur + # fixed effect, covariate and interaction
  173. (1+targetDur|SubName), # random effect
  174. data = exp1_t)
  175. #output as table
  176. tab_model(exp1_mean_cnv, p.val = 'kr') # Kenward-Roger approximation, alternative 'satterthwaite' approxmiation
  177. ```
  178. ###Figure 3
  179. ```{r}
  180. fig2_11 <- cowplot::plot_grid(p1,exp1_lat, exp1_amp,
  181. nrow = 1, labels = c("a", "b","c"), rel_widths = c(2.5,0.5,0.5))
  182. fig2_12 <- cowplot::plot_grid(p11,exp1_ave,
  183. nrow = 1, labels = c("d", "e"), rel_widths = c(2.5,1))
  184. fig2_13 <- cowplot::plot_grid(fig2_11, fig2_12, nrow=2, rel_heights = c(1,1))
  185. ggsave("figures/figure3.png", fig2_13, width = 8, height = 4.5)
  186. fig2_13
  187. ```
  188. # 2.Offset-P2
  189. ```{r}
  190. m_data_ica_p2_exp1 <- data_ica_p2_exp1 %>% group_by(targetDur,t, cond) %>%
  191. dplyr::summarise(n= n(), m_P2 = mean(P2),
  192. se_P2 = sd(P2)/sqrt(n-1))
  193. m_data_ica_p2_exp1$targetDur <- as.factor(m_data_ica_p2_exp1$targetDur)
  194. ```
  195. ```{r}
  196. data_ica_p2_exp1_sub= data_ica_p2_exp1 %>% group_by(SubName, targetDur,t, cond) %>%
  197. dplyr::summarise(n= n(), m_P2 = mean(P2),
  198. se_P2 = sd(P2)/sqrt(n-1))
  199. ```
  200. ##separate plots
  201. ```{r}
  202. colors_4_exp1 <- c("#000333", "#3833cc", "#33a1cc","#33cca6","#c2cc33", "#b233cc", "#FF3333")
  203. plot_4_p2_exp1_ps <- ggplot(m_data_ica_p2_exp1 %>% filter(cond=="PS"), aes(x=t, y=m_P2, color=as.factor(targetDur)))+geom_line(size=0.5)+
  204. scale_color_manual(values=colors_4_exp1)+mytheme+
  205. scale_y_reverse() +labs(x = "Time (ms)", y = "Amplitude (\u03BCV)", color='') +
  206. scale_x_continuous(breaks= c(0, 200,400,600), labels = c(0, 200,400,600)) +coord_cartesian(ylim = c(10,-5),xlim = c(-100, 700), expand = c(0,0))+ ggtitle("Short context (PS)")+theme(legend.position = "right")+theme(axis.title.y = element_text(angle=90))+geom_hline(yintercept=0, linetype="dashed")+theme(legend.key.size = unit(0.5, "cm"))+
  207. annotate("rect", xmin = 140, xmax = 300, ymin = -3, ymax = 9,
  208. alpha = 0.1,fill = "gray", color="darkgray")+annotate("text", x = 225, y = -4, label = "P2")
  209. plot_4_p2_exp1_ps
  210. ```
  211. ```{r}
  212. colors_4_exp1 <- c("#000333", "#0000CC", "#3399CC","#33FFFF","#66CC00", "#CC0066", "#FF3333")
  213. plot_4_p2_exp1_ns <- ggplot(m_data_ica_p2_exp1 %>% filter(cond=="NS"), aes(x=t, y=m_P2, color=as.factor(targetDur)))+geom_line(size=0.5)+
  214. scale_color_manual(values=colors_4_exp1)+mytheme+
  215. scale_y_reverse() + labs(x = "Time (ms)", y = "Amplitude (\u03BCV)", color='') +
  216. scale_x_continuous(breaks= c(0, 200,400,600), labels = c(0, 200,400,600)) +coord_cartesian(ylim = c(10,-5),xlim = c(-100, 700), expand = c(0,0))+ ggtitle("Long context (NS)")+theme(legend.position = "right")+theme(axis.title.y = element_text(angle=90))+geom_hline(yintercept=0, linetype="dashed")+theme(legend.key.size = unit(0.5, "cm"))+
  217. annotate("rect", xmin = 140, xmax = 300, ymin = -3, ymax = 9,
  218. alpha = 0.1,fill = "gray", color="darkgray")+annotate("text", x = 225, y = -4, label = "P2")
  219. plot_4_p2_exp1_ns
  220. ```
  221. #2.1Offset-P2-Peak
  222. ```{r}
  223. m_exp1_p2_peaks <- exp1_pos_peaks %>% group_by(cond, SubName) %>%
  224. dplyr::summarise(n=n(), m_lat =mean(Latency), m_amp = mean(Amplitude))
  225. m_exp1_p2_peaks_targets <- exp1_pos_peaks %>% group_by(cond, targetDur) %>%
  226. dplyr::summarise(n=n(), m_lat =mean(Latency), m_amp = mean(Amplitude),se_lat= sd(Latency)/ sqrt(n-1), se_amp = sd(Amplitude)/ sqrt(n-1))
  227. mm_exp1_p2_peaks <- m_exp1_p2_peaks %>% group_by(cond) %>%
  228. dplyr::summarise(n=n(), mm_lat =mean(m_lat), mm_amp = mean(m_amp), se_lat= sd(m_lat)/ sqrt(n-1), se_amp = sd(m_amp)/ sqrt(n-1))
  229. mm_exp1_p2_peaks
  230. ```
  231. ###mixed linear model
  232. ```{r}
  233. exp1_pos_peaks$targetDur = exp1_pos_peaks$targetDur /1000
  234. #exp1_pos_peaks$Latency = exp1_pos_peaks$Latency /1000
  235. exp1_pos_peaks$cond = as.factor(exp1_pos_peaks$cond)
  236. contrasts(exp1_pos_peaks$cond) = contr.Sum(levels(exp1_pos_peaks$cond))
  237. ```
  238. ```{r}
  239. # fit the linear mixed model
  240. mexp1_p2_peaks = lmer(Latency ~
  241. cond*targetDur + # fixed effect, covariate and interaction
  242. (1+targetDur|SubName), # random effect
  243. data = exp1_pos_peaks)
  244. #output as table
  245. tab_model(mexp1_p2_peaks, p.val = 'kr') # Kenward-Roger approximation, alternative 'satterthwaite' approxmiation
  246. ```
  247. ##plot
  248. ```{r}
  249. p2p3_exp1_lat <- m_exp1_p2_peaks_targets%>% mutate(cond = factor(cond, levels = c("PS", "NS")))%>%
  250. ggplot(aes(x=targetDur, y=m_lat, color=cond))+geom_point()+
  251. geom_errorbar(aes(ymin = m_lat-se_lat,ymax = m_lat+se_lat),width = 40, position = position_dodge(width=0.5))+mytheme+
  252. labs(x = "Target interval (ms)", y = "Peak latency (ms)") +
  253. scale_color_manual(values = colors_plot)+ theme(legend.position = c(0.9,0.9), legend.title = element_blank())+ scale_x_continuous(breaks=c(400,800,1200,1600)) + geom_line() +ggtitle("P2")
  254. #+ coord_cartesian(ylim = c(-3, -8))
  255. p2p3_exp1_lat
  256. ```
  257. #2.2 P2-mean amplitude
  258. ```{r}
  259. p2_amp_exp1 <- data_ica_p2_exp1 %>%filter(between(t, 140, 300))%>% group_by(SubName, cond, targetDur) %>% dplyr::summarise(n=n(), m_p2=mean(P2), se_p2= sd(P2)/sqrt(n-1))
  260. ```
  261. ## mixed linear model
  262. ```{r}
  263. p2_amp_exp1$targetDur = p2_amp_exp1$targetDur /1000
  264. p2_amp_exp1$cond = as.factor(p2_amp_exp1$cond)
  265. contrasts(p2_amp_exp1$cond) = contr.Sum(levels(p2_amp_exp1$cond))
  266. ```
  267. ```{r}
  268. # fit the linear mixed model
  269. mlm = lmer(m_p2 ~
  270. cond*targetDur + # fixed effect, covariate and interaction
  271. (1+targetDur|SubName), # random effect
  272. data = p2_amp_exp1)
  273. #output as table
  274. tab_model(mlm, p.val = 'kr') # Kenward-Roger approximation, alternative 'satterthwaite' approxmiation
  275. ```
  276. ##plot
  277. ```{r}
  278. p2_amp_exp1 <- data_ica_p2_exp1 %>%filter(between(t, 140, 300))%>% group_by(SubName, cond, targetDur) %>% dplyr::summarise(n=n(), m_p2=mean(P2), se_p2= sd(P2)/sqrt(n-1))
  279. p2_amp_exp1_t <- p2_amp_exp1 %>% group_by(cond, targetDur) %>% dplyr::summarise(n=n(), mm_p2=mean(m_p2), se_p2= sd(m_p2)/sqrt(n-1))
  280. p2_amp_exp1_tt <- p2_amp_exp1_t %>% group_by(cond) %>% dplyr::summarise(n=n(), mmm_p2=mean(mm_p2))
  281. p2_amp_exp1_tt
  282. ```
  283. ```{r}
  284. p2p3_exp1_amp_mean <- p2_amp_exp1_t%>% mutate(cond = factor(cond, levels = c("PS", "NS")))%>%
  285. ggplot(aes(x=targetDur, y=mm_p2, color=cond))+geom_point()+
  286. geom_errorbar(aes(ymin = mm_p2-se_p2,ymax = mm_p2+se_p2),width = 40, position = position_dodge(width=0.5))+mytheme+
  287. labs(x = "Target interval (ms)", y = "Mean amplitude (\u03BCV)") +
  288. scale_color_manual(values = colors_plot)+ theme(legend.position = "", legend.title = element_blank())+ scale_x_continuous(breaks=c(400,800,1200,1600)) + geom_line() + coord_cartesian(ylim = c(-2, 9))+ theme(legend.position = c(0.9,0.24), legend.title = element_blank()) +ggtitle("P2")
  289. p2p3_exp1_amp_mean
  290. ```
  291. ###Figure 4
  292. ```{r}
  293. fig_exp1_p2_ns_ps <- cowplot::plot_grid(plot_4_p2_exp1_ps,p2p3_exp1_lat, plot_4_p2_exp1_ns, p2p3_exp1_amp_mean,
  294. nrow = 2, labels = c("a", "b","c","d"), rel_widths = c(3,1.3))
  295. ggsave("figures/figure4.png", fig_exp1_p2_ns_ps, width = 8, height = 4.5)
  296. fig_exp1_p2_ns_ps
  297. ```
  298. #EXPERIMENT 2
  299. ```{r}
  300. allAverageDat_ica_exp2_af <- allAverageDat_exp2 %>% filter(cond== "AF")
  301. allAverageDat_ica_exp2_df <- allAverageDat_exp2 %>% filter(cond== "DF")
  302. ```
  303. # 1.CNV
  304. ```{r}
  305. m_allAverageDat_exp2 <- allAverageDat_exp2 %>% group_by(targetDur,t, cond) %>%
  306. dplyr::summarise(n= n(), m_CNV = mean(CNV),se_CNV = sd(CNV)/sqrt(n-1))
  307. ```
  308. ## separate plots
  309. ```{r}
  310. c2 <- c("#000333", "#0000CC","#3399CC", "#33FFFF","#FF9933", "#CC0066", "#FF3333")
  311. p2 <-ggplot(m_allAverageDat_exp2 %>% filter(cond=="DF" ), aes(x=t, y=m_CNV, color=as.factor(targetDur)))+geom_line(size=0.5 )+
  312. scale_color_manual(values=c2)+ mytheme+
  313. scale_y_reverse() +
  314. theme(axis.ticks.length=unit(.10, "cm"))+ labs(x = "Time (ms)", y = "Amplitude (\u03BCV)", color='') +
  315. scale_x_continuous(breaks= c(0, 400,800,1200, 1600,2000), labels = c(0, 400,800,1200, 1600,2000)) +coord_cartesian(ylim = c(7.5, -6),xlim = c(-200, 2400), expand = c(0,0))+ ggtitle("Short context (DF)")+ theme(legend.position = "right")+ geom_hline(yintercept = 0, linetype="dashed")+theme(legend.key.size = unit(0.5, "cm"))
  316. p2<-p2 +theme(axis.title.y = element_text(angle=90))
  317. p2
  318. ```
  319. ```{r}
  320. c22 <-c("#000333", "#0000CC", "#3399CC","#33FFFF","#FF9933", "#CC0066", "#FF3333")
  321. p22<-ggplot(m_allAverageDat_exp2 %>% filter(cond=="AF" ), aes(x=t, y=m_CNV, color=as.factor(targetDur)))+geom_line(size=0.5)+
  322. scale_color_manual(values=c22)+mytheme+
  323. scale_y_reverse() +
  324. theme(axis.ticks.length=unit(.10, "cm"))+ labs(x = "Time (ms)", y = "Amplitude (\u03BCV)", color='') +
  325. scale_x_continuous(breaks= c(0, 400,800,1200, 1600,2000), labels = c(0, 400,800,1200, 1600,2000)) +coord_cartesian(ylim = c(7.5, -6),xlim = c(-200, 2400), expand = c(0,0))+ ggtitle("Long context (AF)")+theme(legend.position = "right")+ geom_hline(yintercept = 0, linetype="dashed")+theme(legend.key.size = unit(0.5, "cm"))
  326. p22<-p22 +theme(axis.title.y = element_text(angle=90))
  327. p22
  328. ```
  329. ## Slopes
  330. ```{r}
  331. allAverageDat_ica_exp2_af_n = allAverageDat_ica_exp2_af
  332. allAverageDat_ica_exp2_af_n$targetDur = allAverageDat_ica_exp2_af_n$targetDur /1000
  333. allAverageDat_ica_exp2_af_n$t = allAverageDat_ica_exp2_af_n$t /1000
  334. allAverageDat_ica_exp2_af_n %>% filter(t <= 0.65 & t >= 0.25) %>%
  335. group_by(SubName) %>% nest() %>%
  336. mutate(model = map(data, cnv_fit)) %>%
  337. unnest(model, .drop = TRUE) -> lm_exp2_af_slope1
  338. lm_exp2_af_slope1$cond <- "AF"
  339. t.test(lm_exp2_af_slope1$slope, mu = 0, alternative = "two.sided")
  340. ```
  341. ```{r}
  342. #in DF
  343. allAverageDat_ica_exp2_df_n = allAverageDat_ica_exp2_df
  344. allAverageDat_ica_exp2_df_n$targetDur = allAverageDat_ica_exp2_df_n$targetDur /1000
  345. allAverageDat_ica_exp2_df_n$t = allAverageDat_ica_exp2_df_n$t /1000
  346. allAverageDat_ica_exp2_df_n %>% filter(t <= 0.65 & t >= 0.25) %>%
  347. group_by(SubName) %>% nest() %>%
  348. mutate(model = map(data, cnv_fit)) %>%
  349. unnest(model, .drop = TRUE) -> lm_exp2_df_slope1
  350. lm_exp2_df_slope1$cond <- "DF"
  351. t.test(lm_exp2_df_slope1$slope, mu = 0, alternative = "two.sided")
  352. ```
  353. ```{r}
  354. #based on condition?
  355. lm_exp2_slope1 <- rbind(lm_exp2_df_slope1, lm_exp2_af_slope1)
  356. lm_exp2_slope1 %>% group_by(cond) %>%dplyr::summarise(n=n(), m_slope=mean(slope), se_slope=sd(slope)/sqrt(n-1))
  357. ```
  358. ```{r}
  359. #Do they differ in different conditions?
  360. ezANOVA(data = lm_exp2_slope1, dv= slope, wid=SubName, within=.(cond))
  361. ```
  362. # 1.2.CNV-Peak
  363. ```{r}
  364. mexp2_negPeak_lat <- exp2_negPeak_lat %>% group_by(cond, SubName) %>% dplyr::summarise(m_lat = mean(Latency), m_amp= mean(Amplitude))
  365. mmexp2_negPeak_lat <- mexp2_negPeak_lat %>% group_by(cond) %>% dplyr::summarise(n=n(),mm_lat = mean(m_lat), se_lat= sd(m_lat)/sqrt(n-1), mm_amp=mean(m_amp), se_amp= sd(m_amp)/sqrt(n-1))
  366. mmexp2_negPeak_lat
  367. ```
  368. ```{r}
  369. ezANOVA(data = mexp2_negPeak_lat, dv= m_lat, wid=SubName, within=.(cond))
  370. ```
  371. ```{r}
  372. ezANOVA(data = mexp2_negPeak_lat, dv= m_amp, wid=SubName, within=.(cond))
  373. ```
  374. ```{r}
  375. m_peak_exp2_targets <- exp2_negPeak_lat %>% group_by(targetDur,cond) %>% dplyr::summarise(n=n(), m_lat = mean(Latency),
  376. se_lat = sd(Latency)/sqrt(n-1),
  377. m_amp = mean(Amplitude),
  378. se_amp = sd(Amplitude)/sqrt(n-1))
  379. m_peak_exp2_subs <- exp2_negPeak_lat %>% group_by(SubName,cond) %>% dplyr::summarise(n=n(), m_lat = mean(Latency),
  380. se_lat = sd(Latency)/sqrt(n-1),
  381. m_amp = mean(Amplitude),
  382. se_amp = sd(Amplitude)/sqrt(n-1))
  383. ```
  384. ##barplot
  385. ```{r}
  386. exp2_lat= ggplot(data=mmexp2_negPeak_lat%>% mutate(cond = factor(cond, levels = c("DF", "AF"))), aes(x=cond, y=mm_lat, color=cond,fill=cond)) +geom_bar(stat="identity",position=position_dodge(), width=0.6, fill="white", alpha=0.9)+ mytheme+ scale_color_manual(values=colors_plot)+ theme(legend.position = "") + labs(x="", y="Peak latency (ms)") + geom_errorbar(aes(ymin = mm_lat-se_lat,ymax = mm_lat+ se_lat),width = 0.1, position = position_dodge(width=0.6)) +coord_cartesian(ylim=c(0,1000))+ ggtitle("")+theme(axis.text.y= element_text(angle=90))+theme(plot.title = element_text(color = "black", size = 10))
  387. exp2_lat
  388. ```
  389. ```{r}
  390. exp2_amp= ggplot(data=mmexp2_negPeak_lat%>% mutate(cond = factor(cond, levels = c("DF", "AF"))), aes(x=cond, y=mm_amp, color=cond,fill=cond)) +geom_bar(stat="identity",position=position_dodge(), width=0.6, fill="white", alpha=0.9)+ scale_y_reverse() +mytheme+ scale_color_manual(values=colors_plot)+ theme(legend.position = "") + labs(x="", y="Peak amplitude (\u03BCV)") + geom_errorbar(aes(ymin = mm_amp-se_amp,ymax = mm_amp+ se_amp),width = 0.1, position = position_dodge(width=0.6))+coord_cartesian(ylim=c(0,-7))+ggtitle("")+theme(plot.title = element_text(color = "black", size = 10))
  391. exp2_amp
  392. ```
  393. ##plot
  394. ```{r}
  395. p_exp2_lat <- m_peak_exp2_targets%>% mutate(cond = factor(cond, levels = c("DF", "AF")))%>%
  396. ggplot(aes(x=targetDur, y=m_lat, color=cond))+ geom_point(position = position_dodge(width=30))+
  397. geom_errorbar(aes(ymin = m_lat-se_lat,ymax = m_lat+se_lat),width = 30, position = position_dodge(width=30))+mytheme+ geom_abline(slope=1, linetype="dashed", color='black')+
  398. labs(x = "Target interval (ms)", y = "Peak latency (ms)") +
  399. scale_color_manual(values = colors_plot)+ theme(legend.position = c(0.9,0.24), legend.title = element_blank())+
  400. geom_line()+coord_cartesian(ylim = c(500,1200))+theme(axis.text.y= element_text(angle=90)) +ggtitle("CNV")
  401. p_exp2_lat
  402. ```
  403. # 1.3 CNV-mean amplitude
  404. ```{r}
  405. # calculate average CNV
  406. t_df <- data.frame()
  407. targetList <- c(400,600,800,1000,1200,1400, 1600)
  408. for (target in targetList){
  409. m_cnv_1 <- allAverageDat_exp2%>% filter( (t>= 250 & t <= (target+250))) %>% group_by(cond,targetDur,SubName) %>% dplyr::summarise(n= n(), m_CNV = mean(CNV),se_CNV = sd(CNV)/sqrt(n-1))
  410. m_cnv_1= m_cnv_1%>% filter(targetDur== target & cond=="DF")
  411. t_df <- rbind2(t_df,m_cnv_1, by=c("SubName","targetDur","cond"))
  412. }
  413. ```
  414. ```{r}
  415. # calculate average CNV
  416. t_af <- data.frame()
  417. targetList <- c(400,600,800,1000,1200,1400, 1600)
  418. for (target in targetList){
  419. m_cnv_2 <- allAverageDat_exp2%>% filter( (t>= 250 & t <= (target+250))) %>% group_by(cond,targetDur,SubName) %>% dplyr::summarise(n= n(), m_CNV = mean(CNV),se_CNV = sd(CNV)/sqrt(n-1))
  420. m_cnv_2= m_cnv_2%>% filter(targetDur== target & cond=="AF")
  421. t_af <- rbind2(t_af,m_cnv_2, by=c("SubName","targetDur","cond"))
  422. }
  423. ```
  424. ```{r}
  425. t_exp2 <- rbind(t_df, t_af)
  426. t_targets_exp2 <- t_exp2 %>% group_by(targetDur, cond)%>% dplyr::summarise(n=n(), mm_CNV =mean(m_CNV), se_CNV=sd(m_CNV)/sqrt(n-1))
  427. m_t_exp2 <- t_exp2 %>% group_by(SubName, cond)%>% dplyr::summarise(n=n(), mm_CNV =mean(m_CNV), se_CNV=sd(m_CNV)/sqrt(n-1))
  428. m_t_exp2 %>% group_by(cond)%>% dplyr::summarise(n=n(), mmm_CNV =mean(mm_CNV))
  429. ```
  430. ##plot
  431. ```{r}
  432. exp2_ave <- t_targets_exp2%>% mutate(cond = factor(cond, levels = c("DF", "AF")))%>%
  433. ggplot(aes(x=targetDur, y=mm_CNV, color=cond)) +
  434. geom_point(position = position_dodge(width=30))+
  435. geom_errorbar(aes(ymin = mm_CNV-se_CNV,ymax = mm_CNV+se_CNV),width = 80, position = position_dodge(width=30),size=0.5)+mytheme+
  436. geom_line()+
  437. labs(x = "Target intervals (ms)", y = "Mean amplitude (\u03BCV)") +scale_y_reverse()+
  438. scale_color_manual(values = colors_plot)+ theme(legend.position = c(0.9,0.24), legend.title = element_blank()) + scale_x_continuous(breaks = c(400,800,1200,1600), labels = c(400,800,1200,1600))+coord_cartesian(ylim = c(2.5, -3))+ ggtitle("")
  439. exp2_ave
  440. ```
  441. ###mixed linear model
  442. ```{r}
  443. t_exp2$targetDur = t_exp2$targetDur /1000
  444. ```
  445. ```{r}
  446. t_exp2$cond = as.factor(t_exp2$cond)
  447. contrasts(t_exp2$cond) = contr.Sum(levels(t_exp2$cond))
  448. # fit the linear mixed model
  449. m_exp1_cnv_latency = lmer(m_CNV ~
  450. cond*targetDur + # fixed effect, covariate and interaction
  451. (1+targetDur|SubName), # random effect
  452. data = t_exp2)
  453. #output as table
  454. tab_model(m_exp1_cnv_latency, p.val = 'kr') # Kenward-Roger approximation, alternative 'satterthwaite' approxmiation
  455. ```
  456. ###Figure 7
  457. ```{r}
  458. fig5_11 <- cowplot::plot_grid(p2,exp2_lat, exp2_amp,
  459. nrow = 1, labels = c("a", "b","c"), rel_widths = c(2.5,0.5,0.5))
  460. fig5_12 <- cowplot::plot_grid(p22,exp2_ave,
  461. nrow = 1, labels = c("d", "e"), rel_widths = c(2.5,1))
  462. fig5_13 <- cowplot::plot_grid(fig5_11, fig5_12, nrow=2, rel_heights = c(1,1))
  463. ggsave("figures/figure7.png", fig5_13, width = 8, height = 4.5)
  464. fig5_13
  465. ```
  466. # 2. LPCt
  467. ```{r}
  468. m_data_ica_p2 <- data_ica_pos_exp2 %>% group_by(targetDur,t, cond) %>%
  469. dplyr::summarise(n= n(), m_P2 = mean(P2),
  470. se_P2 = sd(P2)/sqrt(n-1))
  471. ```
  472. ## separate plots
  473. ```{r}
  474. colors_1_exp2 <- c("#000333", "#0000CC","#3399CC", "#33FFFF","#FF9933", "#CC0066", "#FF3333")
  475. plot_1_p2_exp2_df <- ggplot(m_data_ica_p2 %>% filter(cond=="DF"), aes(x=t, y=m_P2, color=as.factor(targetDur)))+geom_line(size=0.5)+
  476. scale_color_manual(values=colors_1_exp2)+ mytheme+
  477. scale_y_reverse() +labs(x = "Time (ms)", y = "Amplitude (\u03BCV)", color='') +
  478. #scale_x_continuous(breaks= c(0, 200,400,600), labels = c(0, 200,400,600)) +
  479. coord_cartesian(ylim = c(10,-5), expand = c(0,0))+ ggtitle("Short context (DF)")+theme(legend.position = "right")+theme(axis.title.y = element_text(angle=90)) +theme(legend.position = "right")+theme(axis.title.y = element_text(angle=90))+geom_hline(yintercept=0, linetype="dashed")+theme(legend.key.size = unit(0.5, "cm"))+
  480. annotate("rect", xmin = 300, xmax = 500, ymin = -3, ymax = 9,
  481. alpha = 0.1,fill = "gray", color="darkgray")+annotate("text", x = 400, y = -4, label = "LPCt")
  482. plot_1_p2_exp2_df
  483. ```
  484. ```{r}
  485. colors_1_exp2 <- c("#000333", "#0000CC","#3399CC", "#33FFFF","#FF9933", "#CC0066", "#FF3333")
  486. plot_1_p2_exp2_af <- ggplot(m_data_ica_p2 %>% filter(cond=="AF"), aes(x=t, y=m_P2, color=as.factor(targetDur)))+geom_line(size=0.5)+
  487. scale_color_manual(values=colors_1_exp2)+ mytheme+
  488. scale_y_reverse() +labs(x = "Time (ms)", y = "Amplitude (\u03BCV)", color='') +
  489. scale_x_continuous(breaks= c(0, 200,400,600), labels = c(0, 200,400,600)) +coord_cartesian(ylim = c(10,-5),xlim = c(-100, 700), expand = c(0,0))+ ggtitle("Long context (AF)")+theme(legend.position = "right")+theme(legend.key.size = unit(0.5, "cm"))+theme(axis.title.y = element_text(angle=90))+ geom_hline(yintercept = 0, linetype="dashed")+
  490. annotate("rect", xmin = 300, xmax = 500, ymin = -3, ymax = 9,
  491. alpha = 0.1,fill = "gray", color="darkgray")+annotate("text", x = 400, y = -4, label = "LPCt")
  492. plot_1_p2_exp2_af
  493. ```
  494. # 2.1 LPCt-Peak
  495. ```{r}
  496. m_exp2_p2_peaks <- exp2_pos_peaks %>% group_by(cond, SubName) %>%
  497. dplyr::summarise(n=n(), m_lat =mean(Latency), m_amp = mean(Amplitude))
  498. m_exp2_p2_peaks_targets <- exp2_pos_peaks %>% group_by(cond, targetDur) %>%
  499. dplyr::summarise(n=n(), m_lat =mean(Latency), m_amp = mean(Amplitude), se_lat= sd(Latency)/ sqrt(n-1), se_amp = sd(Amplitude)/ sqrt(n-1))
  500. mm_exp2_p2_peaks <- m_exp2_p2_peaks %>% group_by(cond) %>%
  501. dplyr::summarise(n=n(), mm_lat =mean(m_lat), mm_amp = mean(m_amp), se_lat= sd(m_lat)/ sqrt(n-1), se_amp = sd(m_amp)/ sqrt(n-1))
  502. mm_exp2_p2_peaks
  503. ```
  504. ###mixed linear model
  505. ```{r}
  506. exp2_pos_peaks$targetDur = exp2_pos_peaks$targetDur /1000
  507. #exp2_pos_peaks$Latency = exp2_pos_peaks$Latency /1000
  508. exp2_pos_peaks$cond = as.factor(exp2_pos_peaks$cond)
  509. contrasts(exp2_pos_peaks$cond) = contr.Sum(levels(exp2_pos_peaks$cond))
  510. ```
  511. ```{r}
  512. # fit the linear mixed model
  513. mexp2_p2_peaks = lmer(Latency ~
  514. cond*targetDur + # fixed effect, covariate and interaction
  515. (1+targetDur|SubName), # random effect
  516. data = exp2_pos_peaks)
  517. #output as table
  518. tab_model(mexp2_p2_peaks, p.val = 'kr') # Kenward-Roger approximation, alternative 'satterthwaite' approxmiation
  519. ```
  520. ##plot
  521. ```{r}
  522. p2p3_exp2_lat <- m_exp2_p2_peaks_targets%>% mutate(cond = factor(cond, levels = c("DF", "AF")))%>%
  523. ggplot(aes(x=targetDur, y=m_lat, color=cond))+geom_point()+
  524. geom_errorbar(aes(ymin = m_lat-se_lat,ymax = m_lat+se_lat),width = 40, position = position_dodge(width=0.5))+mytheme+
  525. labs(x = "Target interval (ms)", y = "Peak latency (ms)") +
  526. scale_color_manual(values = colors_plot)+ theme(legend.position = c(0.9,0.9), legend.title = element_blank())+ scale_x_continuous(breaks=c(400,800,1200,1600)) + geom_line() +ggtitle("LPCt")
  527. #+ coord_cartesian(ylim = c(-3, -8))
  528. p2p3_exp2_lat
  529. ```
  530. #2.2- mean amplitude
  531. ```{r}
  532. # test target Dur x condition x mean amplitude
  533. mdata_ica_p2_exp2 <- data_ica_pos_exp2%>% filter( (t>=300 & t<=500)) %>% group_by(targetDur,cond, SubName) %>% dplyr::summarise(n= n(), m_pos = mean(P2),se_pos = sd(P2)/sqrt(n-1))
  534. ```
  535. ###mixed linear model
  536. ```{r}
  537. mdata_ica_p2_exp2$targetDur = mdata_ica_p2_exp2$targetDur /1000
  538. ```
  539. ```{r}
  540. mdata_ica_p2_exp2$cond = as.factor(mdata_ica_p2_exp2$cond)
  541. contrasts(mdata_ica_p2_exp2$cond) = contr.Sum(levels(mdata_ica_p2_exp2$cond))
  542. # fit the linear mixed model
  543. m_lm = lmer(m_pos ~
  544. cond*targetDur + # fixed effect, covariate and interaction
  545. (1+targetDur|SubName), # random effect
  546. data = mdata_ica_p2_exp2)
  547. #output as table
  548. tab_model(m_lm, p.val = 'kr') # Kenward-Roger approximation, alternative 'satterthwaite' approxmiation
  549. ```
  550. ##plot
  551. ```{r}
  552. mdata_ica_p2_exp2 <- data_ica_pos_exp2%>% filter( (t>=300 & t<=500)) %>% group_by(targetDur,cond, SubName) %>% dplyr::summarise(n= n(), m_P2 = mean(P2),se_P2 = sd(P2)/sqrt(n-1))
  553. mmdata_ica_p2_exp2 <- mdata_ica_p2_exp2 %>% group_by(targetDur,cond) %>% dplyr::summarise(n= n(), mm_P2 = mean(m_P2),se_P2 = sd(m_P2)/sqrt(n-1))
  554. m_p2_exp2 <- mmdata_ica_p2_exp2%>% mutate(cond = factor(cond, levels = c("DF", "AF")))%>%
  555. ggplot(aes(x=targetDur, y=mm_P2, color=cond))+geom_point()+
  556. geom_errorbar(aes(ymin = mm_P2-se_P2,ymax = mm_P2+se_P2),width = 40, position = position_dodge(width=0.5))+mytheme+
  557. labs(x = "Target interval (ms)", y = "Mean ampliutde (\u03BCV)") +
  558. scale_color_manual(values = colors_plot)+ theme(legend.position = c(0.9,0.9), legend.title = element_blank())+ scale_x_continuous(breaks=c(400,800,1200,1600)) + geom_line() + coord_cartesian(ylim = c(-2, 9)) + ggtitle("LPCt")
  559. m_p2_exp2
  560. ```
  561. #2.3- P2 mean amplitude
  562. ```{r}
  563. # test target Dur x condition x mean amplitude
  564. mdata_ica_p2_exp2 <- data_ica_pos_exp2%>% filter( (t>=140 & t<=300)) %>% group_by(targetDur,cond, SubName) %>% dplyr::summarise(n= n(), m_pos = mean(P2),se_pos = sd(P2)/sqrt(n-1))
  565. mdata_ica_p2_exp2_sub= mdata_ica_p2_exp2%>% group_by(cond,SubName) %>% dplyr::summarise(n= n(), mm_pos = mean(m_pos))
  566. mdata_ica_p2_exp2_sub%>% group_by(cond) %>% dplyr::summarise(n= n(), m_pos = mean(mm_pos), se_pos=sd(mm_pos)/sqrt(n-1))
  567. ```
  568. ###mixed linear model
  569. ```{r}
  570. mdata_ica_p2_exp2$targetDur = mdata_ica_p2_exp2$targetDur /1000
  571. ```
  572. ```{r}
  573. mdata_ica_p2_exp2$cond = as.factor(mdata_ica_p2_exp2$cond)
  574. contrasts(mdata_ica_p2_exp2$cond) = contr.Sum(levels(mdata_ica_p2_exp2$cond))
  575. # fit the linear mixed model
  576. m_lm = lmer(m_pos ~
  577. cond*targetDur + # fixed effect, covariate and interaction
  578. (1+targetDur|SubName), # random effect
  579. data = mdata_ica_p2_exp2)
  580. #output as table
  581. tab_model(m_lm, p.val = 'kr') # Kenward-Roger approximation, alternative 'satterthwaite' approxmiation
  582. ```
  583. ### Figure 8
  584. ```{r}
  585. fig_exp2_pos <- cowplot::plot_grid(plot_1_p2_exp2_df, p2p3_exp2_lat,plot_1_p2_exp2_af,m_p2_exp2, nrow = 2, labels = c("a", "b","c","d"), rel_widths = c(3, 1.3))
  586. ggsave("figures/figure8.png", fig_exp2_pos, width = 8, height = 4.5)
  587. fig_exp2_pos
  588. ```
  589. # CROSS_EXPERIMENT
  590. #plot
  591. ```{r}
  592. dat= m_allAverageDat_exp1 %>% filter(targetDur==992|targetDur==1008)
  593. dat2= m_allAverageDat_exp2 %>% filter(targetDur==1000)
  594. datm= rbind(dat,dat2)
  595. p1000= ggplot(datm, aes(x=t, y=m_CNV, color=as.factor(cond)))+
  596. scale_color_manual(values=c11)+
  597. geom_hline(yintercept=0, linetype="dashed")+theme(axis.title.y = element_text(angle=90))+ geom_vline(xintercept = 1000, linetype="dashed")+geom_vline(xintercept = 1300, linetype="dashed")+geom_line(size=0.5)+
  598. scale_y_reverse() + mytheme+ labs(x = "Time (ms)", y = "Amplitude (\u03BCV)", color='') +
  599. scale_x_continuous(breaks= c(0, 400,800,1200, 1600,2000), labels = c(0, 400,800,1200, 1600,2000)) +coord_cartesian(ylim = c(7.5, -6),xlim = c(-200, 2400), expand = c(0,0))+theme(legend.key.size = unit(0.3, "cm"))+ggtitle("992/1000/1008 ms target intervals")+ theme(legend.position = c(0.9,0.95))+theme(legend.text=element_text(size=8))
  600. p1000
  601. ```
  602. ```{r}
  603. dat= m_allAverageDat_exp1 %>% filter(targetDur==400|targetDur==1600)
  604. dat2= m_allAverageDat_exp2 %>% filter(targetDur==400|targetDur==1600)
  605. datm= rbind(dat,dat2)
  606. p1600= ggplot(datm %>% filter(targetDur==1600), aes(x=t, y=m_CNV, color=as.factor(cond)))+
  607. scale_color_manual(values=c11)+
  608. scale_y_reverse() + mytheme+ labs(x = "Time (ms)", y = "Amplitude (\u03BCV)", color='') +
  609. scale_x_continuous(breaks= c(0, 400,800,1200, 1600,2000), labels = c(0, 400,800,1200, 1600,2000)) +coord_cartesian(ylim = c(7.5, -6),xlim = c(-200, 2400), expand = c(0,0))+theme(legend.key.size = unit(0.3, "cm"))+ggtitle("1600 ms target interval")+ theme(legend.position = c(0.95,0.99))+
  610. geom_hline(yintercept=0, linetype="dashed")+theme(axis.title.y = element_text(angle=90))+ geom_vline(xintercept = 1600, linetype="dashed")+geom_vline(xintercept = 1900, linetype="dashed")+theme(legend.text=element_text(size=8))+geom_line(size=0.5)
  611. p1600
  612. ```
  613. ```{r}
  614. p400= ggplot(datm %>% filter(targetDur==400), aes(x=t, y=m_CNV, color=as.factor(cond)))+
  615. scale_color_manual(values=c11)+
  616. scale_y_reverse() + mytheme+ labs(x = "Time (ms)", y = "Amplitude (\u03BCV)", color='') +
  617. scale_x_continuous(breaks= c(0, 400,800,1200, 1600,2000), labels = c(0, 400,800,1200, 1600,2000)) +coord_cartesian(ylim = c(7.5, -6),xlim = c(-200, 2400), expand = c(0,0))+theme(legend.key.size = unit(0.3, "cm"))+ggtitle("400 ms target interval")+ theme(legend.position = c(0.9,0.95))+
  618. geom_hline(yintercept=0, linetype="dashed")+theme(axis.title.y = element_text(angle=90))+ geom_vline(xintercept = 400, linetype="dashed")+geom_vline(xintercept = 700, linetype="dashed")+theme(legend.text=element_text(size=8))+geom_line(size=0.5)
  619. p400
  620. ```
  621. #1. CNV slope
  622. # target 400
  623. ```{r}
  624. dat_p400= allAverageDat_exp1 %>% filter(targetDur==400)
  625. dat_p400$targetDur= dat_p400$targetDur/1000
  626. dat_p400$t = dat_p400$t /1000
  627. dat2_p400= allAverageDat_exp2 %>% filter(targetDur==400)
  628. dat2_p400$targetDur= dat2_p400$targetDur/1000
  629. dat2_p400$t = dat2_p400$t /1000
  630. dat_p400_ns <- dat_p400 %>% filter(cond == 'NS')
  631. dat_p400_ns %>% filter(t <= 0.65& t >= 0.25) %>%
  632. group_by(SubName) %>% nest() %>%
  633. mutate(model = map(data, cnv_fit)) %>%
  634. unnest(model, .drop = TRUE) -> lmdat_p400_ns
  635. lmdat_p400_ns$cond = "NS"
  636. lmdat_p400_ns$exp = "Exp.1"
  637. lmdat_p400_ns$targetDur= 400
  638. dat_p400_ps <- dat_p400 %>% filter(cond == 'PS')
  639. dat_p400_ps %>% filter(t <= 0.65& t >= 0.25) %>%
  640. group_by(SubName) %>% nest() %>%
  641. mutate(model = map(data, cnv_fit)) %>%
  642. unnest(model, .drop = TRUE) -> lmdat_p400_ps
  643. lmdat_p400_ps$cond = "PS"
  644. lmdat_p400_ps$exp = "Exp.1"
  645. lmdat_p400_ps$targetDur= 400
  646. ```
  647. ```{r}
  648. dat2_p400_df <- dat2_p400 %>% filter(cond == 'DF')
  649. dat2_p400_df %>% filter(t <= 0.65& t >= 0.25) %>%
  650. group_by(SubName) %>% nest() %>%
  651. mutate(model = map(data, cnv_fit)) %>%
  652. unnest(model, .drop = TRUE) -> lmdat2_p400_df
  653. lmdat2_p400_df$cond = "DF"
  654. lmdat2_p400_df$exp = "Exp.2"
  655. lmdat2_p400_df$targetDur= 400
  656. dat2_p400_af <- dat2_p400 %>% filter(cond == 'AF')
  657. dat2_p400_af %>% filter(t <= 0.65& t >= 0.25) %>%
  658. group_by(SubName) %>% nest() %>%
  659. mutate(model = map(data, cnv_fit)) %>%
  660. unnest(model, .drop = TRUE) -> lmdat2_p400_af
  661. lmdat2_p400_af$cond = "AF"
  662. lmdat2_p400_af$exp = "Exp.2"
  663. lmdat2_p400_af$targetDur= 400
  664. ```
  665. #target 1600
  666. ```{r}
  667. dat_p1600= allAverageDat_exp1 %>% filter(targetDur==1600)
  668. dat_p1600$targetDur= dat_p1600$targetDur/1000
  669. dat_p1600$t = dat_p1600$t /1000
  670. dat2_p1600= allAverageDat_exp2 %>% filter(targetDur==1600)
  671. dat2_p1600$targetDur= dat2_p1600$targetDur/1000
  672. dat2_p1600$t = dat2_p1600$t /1000
  673. dat_p1600_ns <- dat_p1600 %>% filter(cond == 'NS')
  674. dat_p1600_ns %>% filter(t <= 0.65& t >= 0.25) %>%
  675. group_by(SubName) %>% nest() %>%
  676. mutate(model = map(data, cnv_fit)) %>%
  677. unnest(model, .drop = TRUE) -> lmdat_p1600_ns
  678. lmdat_p1600_ns$cond = "NS"
  679. lmdat_p1600_ns$exp = "Exp.1"
  680. lmdat_p1600_ns$targetDur=1600
  681. dat_p1600_ps <- dat_p1600 %>% filter(cond == 'PS')
  682. dat_p1600_ps %>% filter(t <= 0.65& t >= 0.25) %>%
  683. group_by(SubName) %>% nest() %>%
  684. mutate(model = map(data, cnv_fit)) %>%
  685. unnest(model, .drop = TRUE) -> lmdat_p1600_ps
  686. lmdat_p1600_ps$cond = "PS"
  687. lmdat_p1600_ps$exp = "Exp.1"
  688. lmdat_p1600_ps$targetDur=1600
  689. ```
  690. ```{r}
  691. dat2_p1600_df <- dat2_p1600 %>% filter(cond == 'DF')
  692. dat2_p1600_df %>% filter(t <= 0.65& t >= 0.25) %>%
  693. group_by(SubName) %>% nest() %>%
  694. mutate(model = map(data, cnv_fit)) %>%
  695. unnest(model, .drop = TRUE) -> lmdat2_p1600_df
  696. lmdat2_p1600_df$cond = "DF"
  697. lmdat2_p1600_df$exp = "Exp.2"
  698. lmdat2_p1600_df$targetDur = 1600
  699. dat2_p1600_af <- dat2_p1600 %>% filter(cond == 'AF')
  700. dat2_p1600_af %>% filter(t <= 0.65& t >= 0.25) %>%
  701. group_by(SubName) %>% nest() %>%
  702. mutate(model = map(data, cnv_fit)) %>%
  703. unnest(model, .drop = TRUE) -> lmdat2_p1600_af
  704. lmdat2_p1600_af$cond = "AF"
  705. lmdat2_p1600_af$exp = "Exp.2"
  706. lmdat2_p1600_af$targetDur = 1600
  707. ```
  708. #target 1000
  709. ```{r}
  710. dat_p1000= allAverageDat_exp1 %>% filter(targetDur==1008 | targetDur==992)
  711. dat_p1000$targetDur= dat_p1000$targetDur/1000
  712. dat_p1000$t = dat_p1000$t /1000
  713. dat2_p1000= allAverageDat_exp2 %>% filter(targetDur==1000)
  714. dat2_p1000$targetDur= dat2_p1000$targetDur/1000
  715. dat2_p1000$t = dat2_p1000$t /1000
  716. dat_p1000_ns <- dat_p1000 %>% filter(cond == 'NS')
  717. dat_p1000_ns %>% filter(t <= 0.65& t >= 0.25) %>%
  718. group_by(SubName) %>% nest() %>%
  719. mutate(model = map(data, cnv_fit)) %>%
  720. unnest(model, .drop = TRUE) -> lmdat_p1000_ns
  721. lmdat_p1000_ns$cond = "NS"
  722. lmdat_p1000_ns$exp = "Exp.1"
  723. lmdat_p1000_ns$targetDur = 1000
  724. dat_p1000_ps <- dat_p1000 %>% filter(cond == 'PS')
  725. dat_p1000_ps %>% filter(t <= 0.65& t >= 0.25) %>%
  726. group_by(SubName) %>% nest() %>%
  727. mutate(model = map(data, cnv_fit)) %>%
  728. unnest(model, .drop = TRUE) -> lmdat_p1000_ps
  729. lmdat_p1000_ps$cond = "PS"
  730. lmdat_p1000_ps$exp = "Exp.1"
  731. lmdat_p1000_ps$targetDur = 1000
  732. ```
  733. ```{r}
  734. dat2_p1000_df <- dat2_p1000 %>% filter(cond == 'DF')
  735. dat2_p1000_df %>% filter(t <= 0.65& t >= 0.25) %>%
  736. group_by(SubName) %>% nest() %>%
  737. mutate(model = map(data, cnv_fit)) %>%
  738. unnest(model, .drop = TRUE) -> lmdat2_p1000_df
  739. lmdat2_p1000_df$cond = "DF"
  740. lmdat2_p1000_df$exp = "Exp.2"
  741. lmdat2_p1000_df$targetDur = 1000
  742. dat2_p1000_af <- dat2_p1000 %>% filter(cond == 'AF')
  743. dat2_p1000_af %>% filter(t <= 0.65& t >= 0.25) %>%
  744. group_by(SubName) %>% nest() %>%
  745. mutate(model = map(data, cnv_fit)) %>%
  746. unnest(model, .drop = TRUE) -> lmdat2_p1000_af
  747. lmdat2_p1000_af$cond = "AF"
  748. lmdat2_p1000_af$exp = "Exp.2"
  749. lmdat2_p1000_af$targetDur = 1000
  750. ```
  751. ```{r}
  752. lm= rbind(lmdat2_p1000_df, lmdat2_p1000_af,lmdat_p1000_ps,lmdat_p1000_ns,
  753. lmdat2_p400_df, lmdat2_p400_af,lmdat_p400_ps,lmdat_p400_ns,
  754. lmdat2_p1600_df, lmdat2_p1600_af,lmdat_p1600_ps,lmdat_p1600_ns)
  755. lm$Sub = lm$SubName
  756. lm%>% group_by(exp,cond) %>% dplyr::summarise(m_slope= mean(slope),n=n(), se_slope=sd(slope)/sqrt(n-1))
  757. ```
  758. ```{r}
  759. lm$cond[lm$cond =='AF']='long'
  760. lm$cond[lm$cond =='DF']='short'
  761. lm$cond[lm$cond =='PS']='short'
  762. lm$cond[lm$cond =='NS']='long'
  763. ```
  764. #lmm
  765. ```{r}
  766. lm$cond = as.factor(lm$cond)
  767. contrasts(lm$cond) = contr.Sum(levels(lm$cond))
  768. ```
  769. ```{r}
  770. # fit the linear mixed model
  771. lmm_comm = lmer(slope ~exp*cond + # fixed effect, covariate and interaction
  772. (targetDur|SubName), # random effect
  773. data = lm)
  774. #output as table
  775. tab_model(lmm_comm, p.val = 'kr') # Kenward-Roger approximation, alternative 'satterthwaite' approxmiation
  776. ```
  777. # 2. CNV end
  778. ```{r}
  779. m_turn_exp1= turn_exp1%>% group_by(targetDur, cond) %>% dplyr::summarise(n=n(),point= mean(time), se_point= sd(time)/sqrt(n-1))
  780. m_turn_exp1$cond[m_turn_exp1$cond=="NS"]= "long"
  781. m_turn_exp1$cond[m_turn_exp1$cond=="PS"]= "short"
  782. m_turn_exp1$targetDur[m_turn_exp1$targetDur==1008]= 1000
  783. m_turn_exp1$targetDur[m_turn_exp1$targetDur==992]= 1000
  784. m_turn_exp1$Exp= "Exp.1"
  785. m_turn_exp2= turn_exp2%>% group_by(targetDur, cond) %>% dplyr::summarise(n=n(),point= mean(time), se_point= sd(time)/sqrt(n-1))
  786. m_turn_exp2$cond[m_turn_exp2$cond=="AF"]= "long"
  787. m_turn_exp2$cond[m_turn_exp2$cond=="DF"]= "short"
  788. m_turn_exp2$Exp= "Exp.2"
  789. dat_turn = rbind(m_turn_exp1, m_turn_exp2)
  790. dat_turn %>% group_by(Exp, targetDur) %>% dplyr::summarise(n=n(),m_point=mean(point), se_point= sd(point)/sqrt(n-1))
  791. ```
  792. #t-test
  793. ```{r}
  794. turn_exp1$cond[turn_exp1$cond=="NS"]= "long"
  795. turn_exp1$cond[turn_exp1$cond=="PS"]= "short"
  796. turn_exp1$targetDur[turn_exp1$targetDur==1008]= 1000
  797. turn_exp1$targetDur[turn_exp1$targetDur==992]= 1000
  798. turn_exp1$Exp= "Exp.1"
  799. turn_exp2$cond[turn_exp2$cond=="AF"]= "long"
  800. turn_exp2$cond[turn_exp2$cond=="DF"]= "short"
  801. turn_exp2$Exp= "Exp.2"
  802. dat_turn_all = rbind(turn_exp1, turn_exp2)
  803. t4= turn_exp1%>%filter(targetDur==1600)
  804. t42=turn_exp2%>%filter(targetDur==1600)
  805. t.test(t4$time, t42$time)
  806. ```
  807. ```{r}
  808. t3= turn_exp1%>%filter(targetDur==1000)
  809. t32=turn_exp2%>%filter(targetDur==1000)
  810. t.test(t3$time, t32$time)
  811. ```
  812. ```{r}
  813. t2= turn_exp1%>%filter(targetDur==400)
  814. t22=turn_exp2%>%filter(targetDur==400)
  815. t.test(t2$time, t22$time)
  816. ```
  817. #plot
  818. ```{r}
  819. col1= c("#000333", "#3399CC", "#FF3333")
  820. mm_turn_exp1= dat_turn%>% group_by(targetDur,Exp) %>% dplyr::summarise(n=n(),mpoint= mean(point), se_point= sd(point)/sqrt(n-1))
  821. mm_turn_exp1$Exp= as.factor(mm_turn_exp1$Exp)
  822. mm_turn_exp1$targetDur= as.factor(mm_turn_exp1$targetDur)
  823. p_cross= ggplot(data=mm_turn_exp1, aes(x=Exp, y=mpoint, color=targetDur,fill=targetDur))+ geom_hline(yintercept = 400,linetype="dashed")+ geom_hline(yintercept = 1000,linetype="dashed")+ geom_hline(yintercept = 1600,linetype="dashed") +geom_bar(stat="identity",position=position_dodge(),width=0.8,fill="white")+
  824. mytheme+ scale_color_manual(values=col1)+ theme(legend.position = "top") + labs(x="", y="CNV end (ms)",color="",fill="") + geom_errorbar(aes(ymin=mpoint-se_point, ymax=mpoint+se_point, color=targetDur), width=.2,position=position_dodge(0.8))+theme(legend.text=element_text(size=7))+theme(legend.key.size = unit(0.2, "cm")) +theme(axis.text.x = element_text(size=8)) +theme(axis.text.y = element_text(size=8),axis.title.y = element_text(size=8))
  825. p_cross
  826. ```
  827. #barplot
  828. ```{r}
  829. lm= rbind(lmdat2_p1000_df, lmdat2_p1000_af,lmdat_p1000_ps,lmdat_p1000_ns,
  830. lmdat2_p400_df, lmdat2_p400_af,lmdat_p400_ps,lmdat_p400_ns,
  831. lmdat2_p1600_df, lmdat2_p1600_af,lmdat_p1600_ps,lmdat_p1600_ns)
  832. lm$Sub = lm$SubName
  833. lm_m = lm%>% group_by(cond, exp, SubName) %>% dplyr::summarise(mslope=mean(slope))
  834. lm_mm=lm_m%>% group_by(cond, exp) %>% dplyr::summarise(m_slope= mean(mslope), n=n(), se_slope=sd(mslope)/sqrt(n-1))
  835. p_slope= ggplot(data=lm_mm, aes(x=exp, y=m_slope, color=cond,fill=cond)) +geom_bar(stat="identity",position=position_dodge(), width=0.5, fill="white")+ scale_y_reverse() +mytheme+ scale_color_manual(values=c11)+ theme(legend.position = "top") + labs(x="", y="CNV slope (s/\u03BCV)",color="",fill="") + geom_errorbar(aes(ymin = m_slope-se_slope,ymax = m_slope+ se_slope),width = 0.1, position = position_dodge(width=0.5)) +theme(legend.text=element_text(size=7))+ coord_cartesian(ylim=c(0,-23))+theme(legend.key.size = unit(0.2, "cm"))+theme(axis.text.x = element_text(size=8)) +theme(axis.text.y = element_text(size=8),axis.title.y = element_text(size=8))
  836. p_slope
  837. ```
  838. ### Figure 9
  839. ```{r}
  840. lastrow= cowplot::plot_grid(p1600,p_slope,p_cross,labels=c("c","d","e"),rel_widths = c(1,0.5,0.5),nrow=1)
  841. firstrow <- cowplot::plot_grid(p400,p1000,nrow = 1, labels = c("a", "b"), rel_widths = c(1,1))
  842. cross_plot =cowplot::plot_grid(firstrow, lastrow, rel_heights = c(1,1),nrow=2)
  843. ggsave("figures/figure9.png", cross_plot, width = 8, height = 4.9)
  844. cross_plot
  845. ```