highspeed-analysis-sequence.Rmd 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675
  1. ## Detecting sequentiality
  2. ### Initialization
  3. We load the data and relevant functions:
  4. ```{r, warning=FALSE, message=FALSE, echo=TRUE}
  5. # find the path to the root of this project:
  6. if (!requireNamespace("here")) install.packages("here")
  7. if ( basename(here::here()) == "highspeed" ) {
  8. path_root = here::here("highspeed-analysis")
  9. } else {
  10. path_root = here::here()
  11. }
  12. # source all relevant functions from the setup R script:
  13. source(file.path(path_root, "code", "highspeed-analysis-setup.R"))
  14. load(file.path(path_root, "data", "tmp", "dt_periods.Rdata"))
  15. load(file.path(path_root, "data", "tmp", "dt_odd_seq_sim_diff.Rdata"))
  16. load(file.path(path_root, "data", "tmp", "dt_odd_seq_sim.Rdata"))
  17. sub_exclude <- c("sub-24", "sub-31", "sub-37", "sub-40")
  18. ```
  19. ### Data preparation
  20. We create a function to determine early and late zones of forward and backward periods:
  21. ```{r}
  22. get_zones = function(trs_in){
  23. if (length(trs_in) == 3) {
  24. early = trs_in[c(2)]
  25. late = trs_in[c(3)]
  26. } else if (length(trs_in) == 4) {
  27. early = trs_in[c(2)]
  28. late = trs_in[c(4)]
  29. } else if (length(trs_in) == 5) {
  30. early = trs_in[c(2)]
  31. late = trs_in[c(4)]
  32. } else if (length(trs_in) == 6) {
  33. early = trs_in[c(2, 3)]
  34. late = trs_in[c(5, 6)]
  35. } else if (length(trs_in) == 7) {
  36. early = trs_in[c(2, 3)]
  37. late = trs_in[c(5, 6)]
  38. }
  39. return(list(early = early, late = late))
  40. }
  41. ```
  42. We prepare event and decoding data of sequence trials:
  43. ```{r}
  44. # create a subset of the events data table only including sequence task events:
  45. dt_events_seq = dt_events %>%
  46. filter(condition == "sequence" & trial_type == "stimulus")
  47. # create a subset of the decoding data only including the sequence task data:
  48. dt_pred_seq = dt_pred %>%
  49. filter(condition == "sequence" & class != "other" & mask == "cv" & stim != "cue") %>%
  50. setDT(.) %>%
  51. # add serial position, change trial and target cue to the sequence data table:
  52. .[, c("position", "change", "trial_cue", "accuracy", "trial_cue_position") := get_pos(
  53. .SD, dt_events_seq), by = .(id, trial, class), .SDcols = c("id", "trial", "class")] %>%
  54. # add variable to later pool trial_cue_position 1 to 3:
  55. mutate(cue_pos_label = ifelse(trial_cue_position <= 3, "1-3", trial_cue_position)) %>%
  56. setDT(.)
  57. ```
  58. We define the forward and backward periods depending on the response functions:
  59. ```{r}
  60. # define forward and backward period depending on response functions:
  61. for (cspeed in unique(dt_pred_seq$tITI)) {
  62. for (period in c("forward", "backward")) {
  63. # get trs in the relevant forward or backward period based on response functions:
  64. trs_period = dt_periods[[which(dt_periods$speed == cspeed), period]]
  65. # set the period variable in the sequence data table accordingly:
  66. dt_pred_seq$period[
  67. dt_pred_seq$tITI %in% cspeed & dt_pred_seq$seq_tr %in% trs_period] = period
  68. for (zone in c("early", "late")) {
  69. trs_zone = get_zones(trs_period)[[zone]]
  70. dt_pred_seq$zone[
  71. dt_pred_seq$tITI %in% cspeed & dt_pred_seq$seq_tr %in% trs_zone] = zone
  72. }
  73. }
  74. }
  75. # assign the excluded label to all trs that are not in the forward or backward period:
  76. dt_pred_seq$period[is.na(dt_pred_seq$period)] = "excluded"
  77. ```
  78. We exclude all participants with below-chance performance from the analyses:
  79. ```{r}
  80. # exclude participants with below-chance performance:
  81. dt_pred_seq = dt_pred_seq %>%
  82. filter(!(id %in% sub_exclude)) %>%
  83. verify(length(unique(id)) == 36) %>%
  84. setDT(.)
  85. ```
  86. We calculate the number of correct and incorrect sequence trials:
  87. ```{r}
  88. dt_num_correct <- dt_pred_seq %>%
  89. # calculate the number of trials for each accuracy level for each participant:
  90. .[, by = .(classification, id, accuracy), .(
  91. num_trials = length(unique(trial))
  92. )] %>%
  93. # select only the one-versus-rest decoding approach:
  94. filter(classification == "ovr") %>%
  95. setDT(.) %>%
  96. # verify that the sum of all trials equals 75 for all participants:
  97. verify(.[, by = .(classification, id), .(
  98. sum_trials = sum(num_trials))]$sum_trials == 75) %>%
  99. # complete missing values for number of trials for each accuracy level:
  100. complete(classification, id, accuracy, fill = list(num_trials = 0)) %>%
  101. setDT(.) %>%
  102. # verify that there are two accuracy levels per participant:
  103. verify(.[, by = .(classification, id), .(
  104. num_acc_levels = .N)]$num_acc_levels == 2) %>%
  105. # calculate the mean number of (in)accurate trials per participant:
  106. .[, by = .(classification, accuracy), .(
  107. num_subs = .N,
  108. mean_num_trials = mean(num_trials),
  109. percent_trials = mean(num_trials)/75
  110. )]
  111. # print formatted table:
  112. rmarkdown::paged_table(dt_num_correct)
  113. ```
  114. ### Probability time courses
  115. We calculate the decoding probability time-courses:
  116. ```{r}
  117. # select the variable of interest:
  118. variable = "probability_norm"
  119. dt_pred_seq_prob = dt_pred_seq %>%
  120. # average across trials separately for each position, TR, and participant
  121. .[, by = .(id, classification, tITI, period, seq_tr, position), .(
  122. num_trials = .N,
  123. mean_prob = mean(get(variable)) * 100
  124. )] %>%
  125. # check if the averaged data consists of 15 sequence trial per participant:
  126. verify(all(num_trials == 15)) %>%
  127. # average across participants and calculate standard error of the mean:
  128. .[, by = .(classification, tITI, period, seq_tr, position), .(
  129. num_subs = .N,
  130. mean_prob = mean(mean_prob),
  131. sem_upper = mean(mean_prob) + (sd(mean_prob)/sqrt(.N)),
  132. sem_lower = mean(mean_prob) - (sd(mean_prob)/sqrt(.N))
  133. )] %>%
  134. # check if averaged data is consistent with expected number of participants:
  135. verify(all(num_subs == 36)) %>%
  136. # create a new variable that expresses TRs as time from stimulus onset:
  137. mutate(time = (seq_tr - 1) * 1.25) %>%
  138. setDT(.)
  139. ```
  140. We plot the decoding probability time-courses:
  141. ```{r}
  142. plot_raw_probas = function(dt1) {
  143. dt_reduced = dt1 %>% setDT(.) %>%
  144. .[, by = .(classification, tITI, period), .(
  145. xmin = min(seq_tr) - 0.5,
  146. xmax = max(seq_tr) + 0.5
  147. )] %>%
  148. filter(period != "excluded") %>%
  149. mutate(fill = ifelse(period == "forward", "dodgerblue", "red"))
  150. plot = ggplot(data = dt1, mapping = aes(
  151. x = as.factor(seq_tr), y = as.numeric(mean_prob),
  152. group = as.factor(position))) +
  153. geom_rect(data = dt_reduced, aes(
  154. xmin = xmin, xmax = xmax, ymin = 0, ymax = 40),
  155. alpha = 0.05, inherit.aes = FALSE, show.legend = FALSE, fill = dt_reduced$fill) +
  156. facet_wrap(facets = ~ as.factor(tITI), labeller = get_labeller(array = dt1$tITI), nrow = 1) +
  157. geom_ribbon(aes(ymin = sem_lower, ymax = sem_upper,
  158. fill = as.factor(position)), alpha = 0.5) +
  159. geom_line(mapping = aes(color = as.factor(position))) +
  160. #annotate("text", x = 13, y = 1, label = "1 TR = 1.25 s", hjust = 1, size = rel(2)) +
  161. theme(legend.position = "top", legend.direction = "horizontal",
  162. legend.justification = "center", legend.margin = margin(0, 0, 0, 0),
  163. legend.box.margin = margin(t = 0, r = 0, b = -5, l = 0)) +
  164. xlab("Time from sequence onset (TRs; 1 TR = 1.25 s)") + ylab("Probability (%)") +
  165. scale_color_manual(values = color_events, name = "Serial event") +
  166. scale_fill_manual(values = color_events, name = "Serial event") +
  167. scale_x_discrete(labels = label_fill(seq(1, 13, 1), mod = 4), breaks = seq(1, 13, 1)) +
  168. coord_capped_cart(left = "both", bottom = "both", expand = TRUE, ylim = c(0, 25)) +
  169. theme(panel.border = element_blank(), axis.line = element_line()) +
  170. theme(axis.line = element_line(colour = "black"),
  171. panel.grid.major = element_blank(),
  172. panel.grid.minor = element_blank(),
  173. panel.border = element_blank(),
  174. panel.background = element_blank()) +
  175. theme(strip.text.x = element_text(margin = margin(b = 2, t = 2))) +
  176. guides(color = guide_legend(nrow = 1))
  177. return(plot)
  178. }
  179. fig_seq_probas = plot_raw_probas(dt1 = subset(dt_pred_seq_prob, classification == "ovr"))
  180. fig_seq_probas
  181. ```
  182. We plot the decoding probabilities as a heat-map:
  183. ```{r, echo=FALSE}
  184. plot_data = subset(dt_pred_seq_prob, classification == "ovr" & !(tITI %in% c(2.048)))
  185. ggplot(plot_data, aes(x = as.factor(position), y = as.factor(seq_tr), fill = as.numeric(mean_prob))) +
  186. facet_wrap(facets = ~ as.factor(tITI),
  187. labeller = get_labeller(array = plot_data$tITI), nrow = 1) +
  188. geom_tile() +
  189. xlab("Serial event position") + ylab("Time from sequence onset (TRs)") +
  190. scale_fill_viridis(option = "inferno", name = "Probability (%)") +
  191. scale_y_discrete(labels = label_fill(seq(1, 13, 1), mod = 4), breaks = seq(1, 13, 1)) +
  192. lemon::coord_capped_cart(left = "both", bottom = "both", expand = TRUE) +
  193. theme(panel.border = element_blank(), axis.line = element_line()) +
  194. theme(strip.text.x = element_text(margin = margin(b = 2, t = 2))) +
  195. theme(legend.position = "top", legend.direction = "horizontal",
  196. legend.justification = "center", legend.margin = margin(0, 0, 0, 0),
  197. legend.box.margin = margin(t = 0, r = 0, b = -5, l = 0)) +
  198. guides(color = guide_legend(nrow = 1)) +
  199. theme(panel.border = element_blank(), axis.line = element_line())
  200. ```
  201. ### Influence of target cue position
  202. We analyze the probabilities by target cue position:
  203. ```{r}
  204. # select the variable of interest:
  205. variable = "probability_norm"
  206. dt_pred_seq_prob_cue = dt_pred_seq %>%
  207. # average across trials separately for each position, TR, and participant
  208. .[, by = .(id, classification, tITI, period, seq_tr, position, cue_pos_label), .(
  209. num_trials = .N,
  210. mean_prob = mean(get(variable)) * 100
  211. )] %>%
  212. # average across participants and calculate standard error of the mean:
  213. .[, by = .(classification, tITI, period, seq_tr, position,cue_pos_label), .(
  214. num_subs = .N,
  215. mean_num_trials = mean(num_trials),
  216. sd_num_trials = sd(num_trials),
  217. mean_prob = mean(mean_prob),
  218. sem_upper = mean(mean_prob) + (sd(mean_prob)/sqrt(.N)),
  219. sem_lower = mean(mean_prob) - (sd(mean_prob)/sqrt(.N))
  220. )] %>%
  221. # check if averaged data is consistent with expected number of participants:
  222. verify(all(num_subs == 36)) %>%
  223. # check that the SD of the number of trials per cue position is 0
  224. # which means that each participant has the same number of trials per cue position:
  225. verify(all(sd_num_trials == 0)) %>%
  226. verify(all(mean_num_trials == 5)) %>%
  227. # create a new variable that expresses TRs as time from stimulus onset:
  228. mutate(time = (seq_tr - 1) * 1.25) %>%
  229. transform(tITI = paste0(as.numeric(tITI) * 1000, " ms")) %>%
  230. transform(tITI = factor(tITI, levels = c(
  231. "32 ms", "64 ms", "128 ms", "512 ms", "2048 ms"))) %>%
  232. setDT(.)
  233. ```
  234. We plot the probabilities by target cue position:
  235. ```{r}
  236. plot_raw_probas_cue = function(dt1) {
  237. plot = ggplot(data = dt1, mapping = aes(
  238. x = as.factor(seq_tr), y = as.numeric(mean_prob),
  239. group = as.factor(position))) +
  240. facet_grid(rows = vars(cue_pos_label), cols = vars(tITI)) +
  241. geom_ribbon(aes(ymin = sem_lower, ymax = sem_upper,
  242. fill = as.factor(position)), alpha = 0.5) +
  243. geom_line(mapping = aes(color = as.factor(position))) +
  244. theme(legend.position = "top", legend.direction = "horizontal",
  245. legend.justification = "center", legend.margin = margin(0, 0, 0, 0),
  246. legend.box.margin = margin(t = 0, r = 0, b = -5, l = 0)) +
  247. xlab("Time from sequence onset (TRs)") + ylab("Probability (%)") +
  248. scale_color_manual(values = color_events, name = "Serial event") +
  249. scale_fill_manual(values = color_events, name = "Serial event") +
  250. scale_x_discrete(labels = label_fill(seq(1, 13, 1), mod = 4), breaks = seq(1, 13, 1)) +
  251. coord_capped_cart(left = "both", bottom = "both", expand = TRUE, ylim = c(0, 25)) +
  252. theme(panel.border = element_blank(), axis.line = element_line()) +
  253. theme(axis.line = element_line(colour = "black"),
  254. panel.grid.major = element_blank(),
  255. panel.grid.minor = element_blank(),
  256. panel.border = element_blank(),
  257. panel.background = element_blank())
  258. theme(strip.text.x = element_text(margin = margin(b = 2, t = 2))) +
  259. guides(color = guide_legend(nrow = 1))
  260. return(plot)
  261. }
  262. fig_seq_probas_cue = plot_raw_probas_cue(dt1 = subset(dt_pred_seq_prob_cue, classification == "ovr"))
  263. fig_seq_probas_cue
  264. ```
  265. ### Regression slopes
  266. ```{r, echo=FALSE, eval=FALSE}
  267. # select positions within every TR that should be selected:
  268. pos_sel = seq(1, 5)
  269. set.seed(4)
  270. probability = runif(5)
  271. # earlier events have higher probability:
  272. probability = c(0.6, 0.9, 0.1, 0.5, 0.2)
  273. position = seq(1, 5, 1)
  274. position = c(1, 3, 2, 4, 5)
  275. ordered_positions = probability[order(probability, decreasing = TRUE)]
  276. diff(ordered_positions)
  277. # order the probabilities in decreasing order (first = highest):
  278. prob_order_idx = order(probability, decreasing = TRUE)
  279. # order the positions by probability:
  280. pos_order = position[prob_order_idx]
  281. # order the probabilities:
  282. prob_order = probability[prob_order_idx]
  283. # select positions
  284. pos_order_sel = pos_order[pos_sel]
  285. prob_order_sel = prob_order[pos_sel]
  286. ```
  287. We compare the mean indices of association (regression slope, correlation,
  288. mean serial position) for every TR:
  289. ```{r}
  290. # select positions within every TR that should be selected:
  291. pos_sel = seq(1, 5)
  292. # define relevant variables:
  293. variable = "probability_norm"
  294. cor_method = "kendall"
  295. # calculate indices of association at every TR:
  296. dt_pred_seq_cor = dt_pred_seq %>%
  297. # here, we can filter for specific sequence events:
  298. filter(position %in% seq(1, 5, by = 1)) %>% setDT(.) %>%
  299. # order positions by decreasing probability and calculate step size
  300. # calculate correlation and slope between position and probability
  301. # verify that there are five probabilities (one for each class) per volume
  302. # verify that all correlations range between -1 and 1
  303. .[, by = .(id, classification, tITI, period, trial_tITI, seq_tr), {
  304. # order the probabilities in decreasing order (first = highest):
  305. prob_order_idx = order(get(variable), decreasing = TRUE)
  306. # order the positions by probability:
  307. pos_order = position[prob_order_idx]
  308. # order the probabilities:
  309. prob_order = get(variable)[prob_order_idx]
  310. # select positions
  311. pos_order_sel = pos_order[pos_sel]
  312. prob_order_sel = prob_order[pos_sel]
  313. list(
  314. # calculate the number of events:
  315. num_events = length(pos_order_sel[!is.na(pos_order_sel)]),
  316. # calculate the mean step size between probability-ordered events:
  317. mean_step = mean(diff(pos_order_sel)),
  318. # calculate the mean correlation between positions and their probabilities:
  319. cor = cor.test(pos_order_sel, prob_order_sel, method = cor_method)$estimate,
  320. # calculate the slope of a linear regression between position and probabilities:
  321. slope = coef(lm(prob_order_sel ~ pos_order_sel))[2]
  322. # verify that the number of events matches selection and correlations -1 < r < 1
  323. )}] %>% verify(all(num_events == length(pos_sel))) %>% #verify(between(cor, -1, 1)) %>%
  324. # average across trials for each participant (flip values by multiplying with -1):
  325. # verify that the number of trials per participant is correct:
  326. .[, by = .(id, classification, tITI, period, seq_tr), .(
  327. num_trials = .N,
  328. mean_cor = mean(cor) * (-1),
  329. mean_step = mean(mean_step),
  330. mean_slope = mean(slope) * (-1)
  331. )] %>%
  332. verify(all(num_trials == 15)) %>%
  333. # shorten the period name:
  334. mutate(period_short = ifelse(period == "forward", "fwd", period)) %>%
  335. transform(period_short = ifelse(period == "backward", "bwd", period_short)) %>%
  336. mutate(color = ifelse(period_short == "fwd", "dodgerblue", "red")) %>% setDT(.)
  337. ```
  338. ```{r, echo=FALSE, eval=FALSE, include=FALSE}
  339. cfg = list(variable = "mean_cor", threshold = 2.021, baseline = 0,
  340. grouping = c("classification", "tITI"), n_perms = 10000, n_trs = 13)
  341. dt_pred_seq_cor_cluster = cluster_permutation(dt_pred_seq_cor, cfg)
  342. ```
  343. We compare the mean indices of association (regression slope, correlation,
  344. mean serial position) against zero (the expectation of no association)
  345. for every TR:
  346. ```{r}
  347. seq_test_time <- function(data, variable){
  348. data_out = data %>%
  349. # average across participants for every speed at every TR:
  350. # check if the number of participants matches:
  351. .[, by = .(classification, tITI, period, seq_tr), {
  352. # perform a two-sided one-sample t-test against zero (baseline):
  353. ttest_results = t.test(get(variable), alternative = "two.sided", mu = 0);
  354. list(
  355. num_subs = .N,
  356. mean_variable = mean(get(variable)),
  357. pvalue = ttest_results$p.value,
  358. tvalue = ttest_results$statistic,
  359. df = ttest_results$parameter,
  360. cohens_d = round(abs(mean(mean(get(variable)) - 0)/sd(get(variable))), 2),
  361. sem_upper = mean(get(variable)) + (sd(get(variable))/sqrt(.N)),
  362. sem_lower = mean(get(variable)) - (sd(get(variable))/sqrt(.N))
  363. )}] %>% verify(all(num_subs == 36)) %>% verify(all((num_subs - df) == 1)) %>%
  364. # adjust p-values for multiple comparisons (filter for forward and backward period):
  365. # check if the number of comparisons matches expectations:
  366. .[period %in% c("forward", "backward"), by = .(classification), ":=" (
  367. num_comp = .N,
  368. pvalue_adjust = p.adjust(pvalue, method = "fdr", n = .N)
  369. )] %>% verify(all(num_comp == 39, na.rm = TRUE)) %>%
  370. # round the original p-values according to the apa standard:
  371. mutate(pvalue_round = round_pvalues(pvalue)) %>%
  372. # round the adjusted p-value:
  373. mutate(pvalue_adjust_round = round_pvalues(pvalue_adjust)) %>%
  374. # sort data table:
  375. setorder(., classification, period, tITI, seq_tr) %>%
  376. # create new variable indicating significance below 0.05
  377. mutate(significance = ifelse(pvalue_adjust < 0.05, "*", ""))
  378. return(data_out)
  379. }
  380. ```
  381. ```{r}
  382. # filter for significant p-values to make reporting easier:
  383. rmarkdown::paged_table(seq_test_time(data = dt_pred_seq_cor, variable = "mean_cor") %>%
  384. filter(pvalue_adjust < 0.05, classification == "ovr"))
  385. rmarkdown::paged_table(seq_test_time(data = dt_pred_seq_cor, variable = "mean_step") %>%
  386. filter(pvalue_adjust < 0.05, classification == "ovr"))
  387. rmarkdown::paged_table(seq_test_time(data = dt_pred_seq_cor, variable = "mean_slope") %>%
  388. filter(pvalue_adjust < 0.05, classification == "ovr"))
  389. ```
  390. ```{r}
  391. plot_seq_cor_time = function(dt, variable) {
  392. # select the variable of interest, determine y-axis label and adjust axis:
  393. if (variable == "mean_slope") {
  394. ylabel = "Regression slope"
  395. adjust_axis = 0.1
  396. } else if (variable == "mean_cor") {
  397. ylabel = expression("Correlation ("*tau*")")
  398. adjust_axis = 1
  399. } else if (variable == "mean_step") {
  400. ylabel = "Mean step size"
  401. adjust_axis = 1
  402. }
  403. plot = ggplot(data = dt, mapping = aes(
  404. x = seq_tr, y = mean_variable, group = as.factor(as.numeric(tITI) * 1000),
  405. fill = as.factor(as.numeric(tITI) * 1000))) +
  406. geom_hline(aes(yintercept = 0), linetype = "solid", color = "gray") +
  407. geom_ribbon(aes(ymin = sem_lower, ymax = sem_upper), alpha = 0.5, color = NA) +
  408. geom_line(mapping = aes(color = as.factor(as.numeric(tITI) * 1000))) +
  409. xlab("Time from sequence onset (TRs)") + ylab(ylabel) +
  410. scale_colour_viridis(name = "Speed (ms)", discrete = TRUE, option = "cividis") +
  411. scale_fill_viridis(name = "Speed (ms)", discrete = TRUE, option = "cividis") +
  412. scale_x_continuous(labels = label_fill(seq(1, 13, 1), mod = 4), breaks = seq(1, 13, 1)) +
  413. guides(color = guide_legend(nrow = 1)) +
  414. annotate("text", x = 1, y = -0.4 * adjust_axis, label = "1 TR = 1.25 s",
  415. hjust = 0, size = rel(2)) +
  416. coord_capped_cart(left = "both", bottom = "both", expand = TRUE,
  417. ylim = c(-0.4 * adjust_axis, 0.4 * adjust_axis)) +
  418. theme(panel.border = element_blank(), axis.line = element_line()) +
  419. theme(axis.line = element_line(colour = "black"),
  420. panel.grid.major = element_blank(),
  421. panel.grid.minor = element_blank(),
  422. panel.border = element_blank(),
  423. panel.background = element_blank()) +
  424. theme(legend.position = "top", legend.direction = "horizontal",
  425. legend.justification = "center", legend.margin = margin(t = 0, r = 0, b = 0, l = 0),
  426. legend.box.margin = margin(t = 0, r = 0, b = 0, l = 0)) +
  427. geom_segment(aes(x = 0.05, xend = 0.05, y = 0.01 * adjust_axis, yend = 0.4 * adjust_axis),
  428. arrow = arrow(length = unit(5, "pt")), color = "darkgray") +
  429. geom_segment(aes(x = 0.05, xend = 0.05, y = -0.01 * adjust_axis, yend = -0.4 * adjust_axis),
  430. arrow = arrow(length = unit(5, "pt")), color = "darkgray") +
  431. annotate(geom = "text", x = 0.4, y = 0.2 * adjust_axis, label = "Forward order",
  432. color = "darkgray", angle = 90, size = 3) +
  433. annotate(geom = "text", x = 0.4, y = -0.2 * adjust_axis, label = "Backward order",
  434. color = "darkgray", angle = 90, size = 3)
  435. return(plot)
  436. }
  437. ```
  438. ```{r}
  439. fig_seq_cor_time = plot_seq_cor_time(dt = subset(
  440. seq_test_time(data = dt_pred_seq_cor, variable = "mean_cor"),
  441. classification == "ovr"), variable = "mean_cor")
  442. fig_seq_slope_time = plot_seq_cor_time(dt = subset(
  443. seq_test_time(data = dt_pred_seq_cor, variable = "mean_slope"),
  444. classification == "ovr"), variable = "mean_slope")
  445. fig_seq_step_time = plot_seq_cor_time(dt = subset(
  446. seq_test_time(data = dt_pred_seq_cor, variable = "mean_step"),
  447. classification == "ovr"), variable = "mean_step")
  448. fig_seq_cor_time; fig_seq_slope_time; fig_seq_step_time
  449. ```
  450. ```{r, eval=FALSE, echo=FALSE}
  451. ggsave(filename = "highsspeed_plot_decoding_sequence_timecourses_slopes.pdf",
  452. plot = fig_seq_slope_time, device = cairo_pdf, path = path_figures, scale = 1,
  453. dpi = "retina", width = 5.5, height = 3)
  454. ```
  455. We test depending on the target cue position:
  456. ```{r}
  457. # select positions within every TR that should be selected:
  458. pos_sel = seq(1, 5)
  459. # define relevant variables:
  460. variable = "probability_norm"
  461. cor_method = "kendall"
  462. # calculate indices of association at every TR:
  463. dt_pred_seq_cor_cue = dt_pred_seq %>%
  464. # here, we can filter for specific sequence events:
  465. filter(position %in% seq(1, 5, by = 1)) %>% setDT(.) %>%
  466. # order positions by decreasing probability and calculate step size
  467. # calculate correlation and slope between position and probability
  468. # verify that there are five probabilities (one for each class) per volume
  469. # verify that all correlations range between -1 and 1
  470. .[, by = .(id, classification, tITI, period, trial_tITI, seq_tr, cue_pos_label), {
  471. # order the probabilities in decreasing order (first = highest):
  472. prob_order_idx = order(get(variable), decreasing = TRUE)
  473. # order the positions by probability:
  474. pos_order = position[prob_order_idx]
  475. # order the probabilities:
  476. prob_order = get(variable)[prob_order_idx]
  477. # select positions
  478. pos_order_sel = pos_order[pos_sel]
  479. prob_order_sel = prob_order[pos_sel]
  480. list(
  481. # calculate the number of events:
  482. num_events = length(pos_order_sel[!is.na(pos_order_sel)]),
  483. # calculate the mean step size between probability-ordered events:
  484. mean_step = mean(diff(pos_order_sel)),
  485. # calculate the mean correlation between positions and their probabilities:
  486. cor = cor.test(pos_order_sel, prob_order_sel, method = cor_method)$estimate,
  487. # calculate the slope of a linear regression between position and probabilities:
  488. slope = coef(lm(prob_order_sel ~ pos_order_sel))[2]
  489. # verify that the number of events matches selection and correlations -1 < r < 1
  490. )}] %>% verify(all(num_events == length(pos_sel))) %>% #verify(between(cor, -1, 1)) %>%
  491. # average across trials for each participant (flip values by multiplying with -1):
  492. # verify that the number of trials per participant is correct:
  493. .[, by = .(id, classification, tITI, period, seq_tr, cue_pos_label), .(
  494. num_trials = .N,
  495. mean_cor = mean(cor) * (-1),
  496. mean_step = mean(mean_step),
  497. mean_slope = mean(slope) * (-1)
  498. )] %>%
  499. verify(all(num_trials == 5)) %>%
  500. # shorten the period name:
  501. mutate(period_short = ifelse(period == "forward", "fwd", period)) %>%
  502. transform(period_short = ifelse(period == "backward", "bwd", period_short)) %>%
  503. mutate(color = ifelse(period_short == "fwd", "dodgerblue", "red")) %>% setDT(.)
  504. ```
  505. ```{r}
  506. seq_test_time_cue <- function(data, variable){
  507. data_out = data %>%
  508. # average across participants for every speed at every TR:
  509. # check if the number of participants matches:
  510. .[, by = .(classification, tITI, period, seq_tr, cue_pos_label), {
  511. # perform a two-sided one-sample t-test against zero (baseline):
  512. ttest_results = t.test(get(variable), alternative = "two.sided", mu = 0);
  513. list(
  514. num_subs = .N,
  515. mean_variable = mean(get(variable)),
  516. pvalue = ttest_results$p.value,
  517. tvalue = ttest_results$statistic,
  518. df = ttest_results$parameter,
  519. cohens_d = round(abs(mean(mean(get(variable)) - 0)/sd(get(variable))), 2),
  520. sem_upper = mean(get(variable)) + (sd(get(variable))/sqrt(.N)),
  521. sem_lower = mean(get(variable)) - (sd(get(variable))/sqrt(.N))
  522. )}] %>% verify(all(num_subs == 36)) %>% verify(all((num_subs - df) == 1)) %>%
  523. # adjust p-values for multiple comparisons (filter for forward and backward period):
  524. # check if the number of comparisons matches expectations:
  525. .[period %in% c("forward", "backward"), by = .(classification), ":=" (
  526. num_comp = .N,
  527. pvalue_adjust = p.adjust(pvalue, method = "fdr", n = .N)
  528. )] %>% #verify(all(num_comp == 39, na.rm = TRUE)) %>%
  529. # round the original p-values according to the apa standard:
  530. mutate(pvalue_round = round_pvalues(pvalue)) %>%
  531. # round the adjusted p-value:
  532. mutate(pvalue_adjust_round = round_pvalues(pvalue_adjust)) %>%
  533. # sort data table:
  534. setorder(., classification, period, tITI, seq_tr) %>%
  535. # create new variable indicating significance below 0.05
  536. mutate(significance = ifelse(pvalue_adjust < 0.05, "*", ""))
  537. return(data_out)
  538. }
  539. ```
  540. ```{r, echo=FALSE}
  541. plot_seq_cor_time_cue = function(dt, variable) {
  542. # select the variable of interest, determine y-axis label and adjust axis:
  543. if (variable == "mean_slope") {
  544. ylabel = "Regression slope"
  545. adjust_axis = 0.1
  546. } else if (variable == "mean_cor") {
  547. ylabel = expression("Correlation ("*tau*")")
  548. adjust_axis = 1
  549. } else if (variable == "mean_step") {
  550. ylabel = "Mean step size"
  551. adjust_axis = 1
  552. }
  553. plot = ggplot(data = dt, mapping = aes(
  554. x = seq_tr, y = mean_variable, group = as.factor(as.numeric(tITI) * 1000),
  555. fill = as.factor(as.numeric(tITI) * 1000))) +
  556. geom_hline(aes(yintercept = 0), linetype = "solid", color = "gray") +
  557. geom_ribbon(aes(ymin = sem_lower, ymax = sem_upper), alpha = 0.5, color = NA) +
  558. geom_line(mapping = aes(color = as.factor(as.numeric(tITI) * 1000))) +
  559. facet_wrap(~ cue_pos_label) +
  560. xlab("Time from sequence onset (TRs)") + ylab(ylabel) +
  561. scale_colour_viridis(name = "Speed (ms)", discrete = TRUE, option = "cividis") +
  562. scale_fill_viridis(name = "Speed (ms)", discrete = TRUE, option = "cividis") +
  563. scale_x_continuous(labels = label_fill(seq(1, 13, 1), mod = 4), breaks = seq(1, 13, 1)) +
  564. guides(color = guide_legend(nrow = 1)) +
  565. annotate("text", x = 1, y = -0.4 * adjust_axis, label = "1 TR = 1.25 s",
  566. hjust = 0, size = rel(2)) +
  567. coord_capped_cart(left = "both", bottom = "both", expand = TRUE,
  568. ylim = c(-0.4 * adjust_axis, 0.4 * adjust_axis)) +
  569. theme(panel.border = element_blank(), axis.line = element_line()) +
  570. theme(axis.line = element_line(colour = "black"),
  571. panel.grid.major = element_blank(),
  572. panel.grid.minor = element_blank(),
  573. panel.border = element_blank(),
  574. panel.background = element_blank()) +
  575. theme(legend.position = "top", legend.direction = "horizontal",
  576. legend.justification = "center", legend.margin = margin(t = 0, r = 0, b = 0, l = 0),
  577. legend.box.margin = margin(t = 0, r = 0, b = 0, l = 0)) +
  578. geom_segment(aes(x = 0.05, xend = 0.05, y = 0.01 * adjust_axis, yend = 0.4 * adjust_axis),
  579. arrow = arrow(length = unit(5, "pt")), color = "darkgray") +
  580. geom_segment(aes(x = 0.05, xend = 0.05, y = -0.01 * adjust_axis, yend = -0.4 * adjust_axis),
  581. arrow = arrow(length = unit(5, "pt")), color = "darkgray") +
  582. annotate(geom = "text", x = 0.6, y = 0.2 * adjust_axis, label = "Forward",
  583. color = "darkgray", angle = 90, size = 2) +
  584. annotate(geom = "text", x = 0.6, y = -0.2 * adjust_axis, label = "Backward",
  585. color = "darkgray", angle = 90, size = 2)
  586. return(plot)
  587. }
  588. ```
  589. ```{r}
  590. fig_seq_slope_time_cue = plot_seq_cor_time_cue(dt = subset(
  591. seq_test_time_cue(data = dt_pred_seq_cor_cue, variable = "mean_slope"),
  592. classification == "ovr"), variable = "mean_slope")
  593. fig_seq_slope_time_cue
  594. ```
  595. ### Correlation of regression time courses
  596. #### Correlation between participants
  597. We calculate the correlations between the predicted and the observed time courses *between* participants for each of the five speed conditions (inter-stimulus intervals):
  598. ```{r, echo=TRUE}
  599. # observed time courses:
  600. dt_data_between = seq_test_time(
  601. data = dt_pred_seq_cor, variable = "mean_slope") %>%
  602. filter(classification == "ovr") %>%
  603. transform(tITI = as.factor(as.numeric(tITI) * 1000)) %>%
  604. setorder(classification, tITI, seq_tr)
  605. # predicted time courses:
  606. dt_model_between = dt_odd_seq_sim_diff %>%
  607. transform(time = time + 1) %>%
  608. filter(time %in% seq(1, 13, 1)) %>%
  609. setorder(classification, speed, time)
  610. # combine in one data table:
  611. dt_between = data.table(
  612. speed = dt_data_between$tITI,
  613. tr = dt_data_between$seq_tr,
  614. empirical = dt_data_between$mean_variable,
  615. prediction = dt_model_between$mean_difference)
  616. # calculate the correlation between
  617. dt_between_results = dt_between %>%
  618. .[, by = .(speed), {
  619. cor = cor.test(empirical, prediction, method = "pearson")
  620. list(
  621. num_trs = .N,
  622. pvalue = cor$p.value,
  623. pvalue_round = round_pvalues(cor$p.value),
  624. correlation = round(cor$estimate, 2)
  625. )
  626. }] %>%
  627. verify(num_trs == 13) %>%
  628. select(-num_trs)
  629. # show the table with the correlations:
  630. rmarkdown::paged_table(dt_between_results)
  631. ```
  632. We plot the correlations between the regression slope time courses predicted by the model vs. the observed data *between* participants:
  633. ```{r, echo=TRUE, warning=FALSE, message=FALSE}
  634. fig_seq_cor_between = ggplot(
  635. data = dt_between,
  636. mapping = aes(
  637. x = prediction, y = empirical, color = speed, fill = speed)) +
  638. geom_point(alpha = 1) +
  639. geom_smooth(method = lm, se = FALSE, alpha = 0.5, fullrange = TRUE) +
  640. scale_colour_viridis(
  641. name = "Speed (ms)", discrete = TRUE,
  642. option = "cividis", guide = FALSE) +
  643. scale_fill_viridis(
  644. name = "Speed (ms)", discrete = TRUE,
  645. option = "cividis", guide = FALSE) +
  646. xlab("Predicted slope") +
  647. ylab("Observed slope") +
  648. # guides(color = guide_legend(nrow = 1)) +
  649. coord_capped_cart(
  650. left = "both", bottom = "both", expand = TRUE,
  651. xlim = c(-0.4, 0.4), ylim = c(-0.05, 0.05)) +
  652. theme(legend.position = "top",
  653. legend.direction = "horizontal",
  654. legend.justification = "center",
  655. legend.margin = margin(t = 0, r = 0, b = 0, l = 0),
  656. legend.box.margin = margin(t = 0, r = 0, b = 0, l = 0)) +
  657. theme(axis.line = element_line(colour = "black"),
  658. panel.grid.major = element_blank(),
  659. panel.grid.minor = element_blank(),
  660. panel.border = element_blank(),
  661. panel.background = element_blank())
  662. # show the plot:
  663. fig_seq_cor_between
  664. ```
  665. #### Correlation within participants
  666. We calculate the correlations between the predicted and the observed time courses *within* participants for each of the five speed conditions (inter-stimulus intervals):
  667. ```{r}
  668. # observed regression slope time courses
  669. dt_data_within = dt_pred_seq_cor %>%
  670. filter(classification == "ovr") %>%
  671. transform(tITI = as.factor(as.numeric(tITI) * 1000)) %>%
  672. setorder(classification, id, tITI, seq_tr)
  673. # predicted regression slope time courses
  674. dt_model_within = dt_odd_seq_sim %>%
  675. transform(time = time + 1) %>%
  676. filter(time %in% seq(1, 13, 1)) %>%
  677. setorder(classification, id, speed, time)
  678. # combine in one data table:
  679. dt_within = data.table(
  680. id = dt_data_within$id,
  681. speed = dt_data_within$tITI,
  682. time = dt_data_within$seq_tr,
  683. empirical = dt_data_within$mean_slope,
  684. prediction = dt_model_within$probability)
  685. # run correlations:
  686. dt_within_cor = dt_within %>%
  687. .[, by = .(id, speed), {
  688. cor = cor.test(empirical, prediction, method = "pearson")
  689. list(
  690. num_trs = .N,
  691. pvalue = cor$p.value,
  692. estimate = as.numeric(cor$estimate)
  693. )}] %>%
  694. verify(num_trs == 13)
  695. # run t-tests over correlation coefficients for each speed level:
  696. dt_within_cor_results = setDT(dt_within_cor) %>%
  697. .[, by = .(speed), {
  698. ttest_results = t.test(
  699. estimate, mu = 0, alternative = "two.sided", paired = FALSE)
  700. list(
  701. num_subs = .N,
  702. mean_estimate = round(mean(estimate), 2),
  703. pvalue = ttest_results$p.value,
  704. tvalue = round(ttest_results$statistic, 2),
  705. df = ttest_results$parameter,
  706. cohens_d = round((mean(estimate) - 0)/sd(estimate), 2)
  707. )}] %>%
  708. verify(num_subs == 36) %>%
  709. verify((num_subs - df) == 1) %>%
  710. # adjust p-values for multiple comparisons:
  711. # check if the number of comparisons matches expectations:
  712. .[, ":=" (
  713. num_comp = .N,
  714. pvalue_adjust = p.adjust(pvalue, method = "fdr", n = .N)
  715. )] %>%
  716. # round the original p-values according to the apa standard:
  717. mutate(pvalue_round = round_pvalues(pvalue)) %>%
  718. # round the adjusted p-value:
  719. mutate(pvalue_adjust_round = round_pvalues(pvalue_adjust)) %>%
  720. # create new variable indicating significance below 0.05
  721. mutate(significance = ifelse(pvalue_adjust < 0.05, "*", ""))
  722. # show the table with the t-test results:
  723. rmarkdown::paged_table(dt_within_cor_results)
  724. ```
  725. We plot the correlations between the predicted and the observed time courses *within* participants for each of the five speed conditions (inter-stimulus intervals):
  726. ```{r, echo=TRUE}
  727. fig_seq_cor_within = ggplot(
  728. data = dt_within_cor,
  729. mapping = aes(
  730. x = speed, y = estimate, color = speed, fill = speed, group = speed)) +
  731. stat_summary(geom = "bar", fun = "mean") +
  732. geom_dotplot(binaxis = "y", stackdir = "center", stackratio = 0.5,
  733. color = "white", alpha = 0.5,
  734. inherit.aes = TRUE, binwidth = 0.05) +
  735. stat_summary(geom = "errorbar", fun.data = "mean_se", width = 0, color = "black") +
  736. scale_colour_viridis(
  737. name = "Speed (ms)", discrete = TRUE, option = "cividis", guide = FALSE) +
  738. scale_fill_viridis(
  739. name = "Speed (ms)", discrete = TRUE, option = "cividis", guide = FALSE) +
  740. xlab("Speed (in ms)") +
  741. ylab("Correlation (r)") +
  742. #guides(color = guide_legend(nrow = 1)) +
  743. coord_capped_cart(left = "both", bottom = "both", expand = TRUE) +
  744. theme(legend.position = "top", legend.direction = "horizontal",
  745. legend.justification = "center", legend.margin = margin(t = 0, r = 0, b = 0, l = 0),
  746. legend.box.margin = margin(t = 0, r = 0, b = 0, l = 0)) +
  747. theme(axis.ticks.x = element_line(colour = "white"),
  748. axis.line.x = element_line(colour = "white")) +
  749. theme(axis.title.x = element_blank(), axis.text.x = element_blank()) +
  750. theme(axis.line = element_line(colour = "black"),
  751. panel.grid.major = element_blank(),
  752. panel.grid.minor = element_blank(),
  753. panel.border = element_blank(),
  754. panel.background = element_blank())
  755. # show figure:
  756. fig_seq_cor_within
  757. ```
  758. Calculate the average correlation or average regression slope
  759. for each time period (forward versus backward) for all five speed conditions:
  760. ```{r, echo=TRUE}
  761. seq_test_period <- function(data, variable){
  762. data_out = data %>%
  763. # filter out the excluded time period (select only forward and backward period):
  764. filter(period != "excluded") %>%
  765. setDT(.) %>%
  766. # average for each time period and speed condition for every participant:
  767. .[, by = .(classification, id, tITI, period), .(
  768. mean_variable = mean(get(variable)))] %>%
  769. # average across participants for every speed at every TR:
  770. # check if the number of participants matches:
  771. .[, by = .(classification, tITI, period), {
  772. # perform a two-sided one-sample t-test against zero (baseline):
  773. ttest_results = t.test(mean_variable, alternative = "two.sided", mu = 0);
  774. list(
  775. num_subs = .N,
  776. mean_variable = mean(mean_variable),
  777. pvalue = ttest_results$p.value,
  778. tvalue = round(abs(ttest_results$statistic), 2),
  779. df = ttest_results$parameter,
  780. cohens_d = abs(round((mean(mean_variable) - 0) / sd(mean_variable), 2)),
  781. sem_upper = mean(mean_variable) + (sd(mean_variable)/sqrt(.N)),
  782. sem_lower = mean(mean_variable) - (sd(mean_variable)/sqrt(.N))
  783. )
  784. }] %>%
  785. verify(all(num_subs == 36)) %>%
  786. verify(all((num_subs - df) == 1)) %>%
  787. # adjust p-values for multiple comparisons:
  788. # check if the number of comparisons matches expectations:
  789. .[period %in% c("forward", "backward"), by = .(classification), ":=" (
  790. num_comp = .N,
  791. pvalue_adjust = p.adjust(pvalue, method = "fdr", n = .N)
  792. )] %>%
  793. verify(num_comp == 10) %>%
  794. # add variable that indicates significance with stupid significance stars:
  795. mutate(significance = ifelse(pvalue < 0.05, "*", "")) %>%
  796. # round the original p-values according to APA manual:
  797. mutate(pvalue_round = round_pvalues(pvalue)) %>%
  798. # round the adjusted p-value:
  799. mutate(pvalue_adjust_round = round_pvalues(pvalue_adjust)) %>%
  800. # sort data table:
  801. setorder(classification, period, tITI) %>%
  802. # shorten the period name:
  803. mutate(period_short = ifelse(period == "forward", "fwd", period)) %>%
  804. transform(period_short = ifelse(period == "backward", "bwd", period_short)) %>%
  805. mutate(color = ifelse(period_short == "fwd", "dodgerblue", "red")) %>% setDT(.)
  806. return(data_out)
  807. }
  808. ```
  809. ```{r}
  810. rmarkdown::paged_table(
  811. seq_test_period(data = dt_pred_seq_cor, variable = "mean_cor") %>%
  812. filter(pvalue_adjust < 0.05, classification == "ovr"))
  813. rmarkdown::paged_table(
  814. seq_test_period(data = dt_pred_seq_cor, variable = "mean_step") %>%
  815. filter(pvalue_adjust < 0.05, classification == "ovr"))
  816. rmarkdown::paged_table(
  817. seq_test_period(data = dt_pred_seq_cor, variable = "mean_slope") %>%
  818. filter(pvalue_adjust < 0.05, classification == "ovr"))
  819. ```
  820. ```{r, echo=TRUE}
  821. plot_seq_cor_period = function(data, variable) {
  822. # select the variable of interest, determine y-axis label and adjust axis:
  823. if (variable == "mean_slope") {
  824. ylabel = "Regression slope"
  825. adjust_axis = 0.1
  826. } else if (variable == "mean_cor") {
  827. ylabel = expression("Correlation ("*tau*")")
  828. adjust_axis = 1
  829. } else if (variable == "mean_step") {
  830. ylabel = "Mean step size"
  831. adjust_axis = 1
  832. }
  833. dt_forward = data.table(xmin = 0, xmax = 5.5, ymin = 0, ymax = 0.4 * adjust_axis)
  834. dt_backward = data.table(xmin = 0, xmax = 5.5, ymin = 0, ymax = -0.4 * adjust_axis)
  835. # average across participants for every speed at every TR:
  836. plot_data = data %>% setDT(.) %>%
  837. .[, by = .(classification, id, tITI, period_short), .(
  838. mean_variable = mean(get(variable))
  839. )] %>% filter(classification == "ovr" & period_short != "excluded")
  840. plot_stat = seq_test_period(data = data, variable = variable)
  841. # plot average correlation or betas for each speed condition and time period:
  842. plot = ggplot(data = plot_data, aes(
  843. x = fct_rev(as.factor(period_short)), y = as.numeric(mean_variable),
  844. fill = as.factor(as.numeric(tITI) * 1000))) +
  845. geom_bar(stat = "summary", fun = "mean", width = 0.9, show.legend = TRUE) +
  846. geom_dotplot(binaxis = "y", stackdir = "center", stackratio = 0.5, alpha = 0.2,
  847. binwidth = 0.01 * adjust_axis, show.legend = FALSE) +
  848. #geom_point(position = position_jitterdodge(jitter.height = 0, seed = 4, jitter.width = 0.2),
  849. # pch = 21, alpha = 0.2, show.legend = FALSE) +
  850. geom_errorbar(stat = "summary", fun.data = "mean_se", width = 0.0, color = "black") +
  851. geom_text(data = subset(plot_stat, classification == "ovr"), aes(
  852. x = fct_rev(as.factor(period_short)), y = round_updown(as.numeric(mean_variable), 0.6 * adjust_axis),
  853. label = paste0("d=", sprintf("%.2f", cohens_d), significance)), size = 3.3, show.legend = FALSE,
  854. color = subset(plot_stat, classification == "ovr")$color) +
  855. facet_wrap(~ as.factor(as.numeric(tITI) * 1000), strip.position = "bottom", nrow = 1) +
  856. xlab("Period") + ylab(ylabel) +
  857. scale_fill_viridis(name = "Speed (ms)", discrete = TRUE, option = "cividis") +
  858. coord_capped_cart(left = "both", bottom = "both", expand = TRUE, ylim = c(-0.6, 0.6) * adjust_axis) +
  859. theme(panel.border = element_blank(), axis.line = element_line()) +
  860. theme(axis.line = element_line(colour = "black"),
  861. panel.grid.major = element_blank(),
  862. panel.grid.minor = element_blank(),
  863. panel.border = element_blank(),
  864. panel.background = element_blank()) +
  865. theme(axis.ticks.x = element_line(color = "white"),
  866. axis.line.x = element_line(color = "white")) +
  867. theme(legend.position = "top", legend.direction = "horizontal", legend.box = "vertical",
  868. legend.justification = "center", legend.margin = margin(t = 0, r = 0, b = 0, l = 0),
  869. legend.box.margin = margin(t = 0, r = 0, b = -5, l = 0)) +
  870. theme(panel.spacing = unit(0, "lines"), strip.background = element_blank(),
  871. strip.placement = "outside", strip.text = element_blank())
  872. return(plot)
  873. }
  874. fig_seq_cor_period = plot_seq_cor_period(data = dt_pred_seq_cor, variable = "mean_cor")
  875. fig_seq_slope_period = plot_seq_cor_period(data = dt_pred_seq_cor, variable = "mean_slope")
  876. fig_seq_step_period = plot_seq_cor_period(data = dt_pred_seq_cor, variable = "mean_step")
  877. fig_seq_cor_period; fig_seq_step_period; fig_seq_slope_period;
  878. ```
  879. ```{r, echo = FALSE}
  880. plot_seq_cor_facet = function(dt, variable) {
  881. # create separate datatable to plot rectangles indicating forward / backward period:
  882. dt_reduced = dt %>% setDT(.) %>%
  883. .[, by = .(classification, tITI, period), .(
  884. xmin = min(seq_tr) - 0.5,
  885. xmax = max(seq_tr) + 0.5
  886. )] %>%
  887. filter(period != "excluded") %>%
  888. mutate(fill = ifelse(period == "forward", "dodgerblue", "red"))
  889. # select the variable of interest, determine y-axis label and adjust axis:
  890. if (variable == "mean_slope") {
  891. ylabel = "Regression slope"
  892. adjust_axis = 0.1
  893. } else if (variable == "mean_cor") {
  894. ylabel = expression("Correlation ("*tau*")")
  895. adjust_axis = 1
  896. } else if (variable == "mean_step") {
  897. ylabel = "Mean step size"
  898. adjust_axis = 1
  899. }
  900. plot = ggplot(data = dt, mapping = aes(
  901. x = as.factor(seq_tr), y = as.numeric(mean_variable),
  902. group = as.factor(tITI), fill = as.factor(tITI), color = as.factor(tITI))) +
  903. # add background rectangles to indicate the forward and backward period:
  904. geom_rect(data = dt_reduced, aes(
  905. xmin = xmin, xmax = xmax, ymin = -0.4 * adjust_axis, ymax = 0.4 * adjust_axis),
  906. alpha = 0.05, inherit.aes = FALSE, show.legend = FALSE, fill = dt_reduced$fill) +
  907. geom_hline(aes(yintercept = 0), linetype = "solid", color = "gray") +
  908. facet_wrap(facets = ~ as.factor(tITI), labeller = get_labeller(dt$tITI), nrow = 1) +
  909. geom_ribbon(aes(ymin = sem_lower, ymax = sem_upper), alpha = 0.5, color = NA) +
  910. geom_line() +
  911. geom_point(data = subset(dt, pvalue_adjust < 0.05), pch = 21, fill = "red",
  912. color = "black", show.legend = FALSE) +
  913. xlab("Time from sequence onset (TRs)") + ylab(ylabel) +
  914. theme(legend.position = "top", legend.direction = "horizontal",
  915. legend.justification = "center", legend.margin = margin(0, 0, 0, 0),
  916. legend.box.margin = margin(t = 0, r = 0, b = -5, l = 0)) +
  917. scale_colour_viridis(name = "Speed (ms)", discrete = TRUE, option = "cividis", guide = FALSE) +
  918. scale_fill_viridis(name = "Speed (ms)", discrete = TRUE, option = "cividis", guide = FALSE) +
  919. scale_x_discrete(labels = label_fill(seq(1, 13, 1), mod = 4), breaks = seq(1, 13, 1)) +
  920. theme(strip.text.x = element_text(margin = margin(b = 2, t = 2))) +
  921. coord_capped_cart(left = "both", bottom = "both", expand = TRUE, ylim = c(-0.4, 0.4) * adjust_axis) +
  922. theme(axis.line = element_line(colour = "black"),
  923. panel.grid.major = element_blank(),
  924. panel.grid.minor = element_blank(),
  925. panel.border = element_blank(),
  926. panel.background = element_blank())
  927. return(plot)
  928. }
  929. ```
  930. We repeat the same calculations, just splitting up the data by the serial position of the cued image:
  931. ```{r}
  932. seq_test_period_cue <- function(data, variable){
  933. data_out = data %>%
  934. # filter out the excluded time period (select only forward and backward period):
  935. filter(period != "excluded") %>% setDT(.) %>%
  936. # average for each time period and speed condition for every participant:
  937. .[, by = .(classification, id, tITI, period, cue_pos_label), .(
  938. mean_variable = mean(get(variable)))] %>%
  939. # average across participants for every speed at every TR:
  940. # check if the number of participants matches:
  941. .[, by = .(classification, tITI, period, cue_pos_label), {
  942. # perform a two-sided one-sample t-test against zero (baseline):
  943. ttest_results = t.test(mean_variable, alternative = "two.sided", mu = 0);
  944. list(
  945. num_subs = .N,
  946. mean_variable = mean(mean_variable),
  947. pvalue = ttest_results$p.value,
  948. tvalue = round(abs(ttest_results$statistic), 2),
  949. df = ttest_results$parameter,
  950. cohens_d = abs(round((mean(mean_variable) - 0) / sd(mean_variable), 2)),
  951. sem_upper = mean(mean_variable) + (sd(mean_variable)/sqrt(.N)),
  952. sem_lower = mean(mean_variable) - (sd(mean_variable)/sqrt(.N))
  953. )
  954. }] %>% verify(all(num_subs == 36)) %>% verify(all((num_subs - df) == 1)) %>%
  955. # adjust p-values for multiple comparisons:
  956. # check if the number of comparisons matches expectations:
  957. .[period %in% c("forward", "backward"), by = .(classification), ":=" (
  958. num_comp = .N,
  959. pvalue_adjust = p.adjust(pvalue, method = "fdr", n = .N)
  960. )] %>% #verify(num_comp == 10) %>%
  961. # add variable that indicates significance with stupid significance stars:
  962. mutate(significance = ifelse(pvalue < 0.05, "*", "")) %>%
  963. # round the original p-values according to APA manual:
  964. mutate(pvalue_round = round_pvalues(pvalue)) %>%
  965. # round the adjusted p-value:
  966. mutate(pvalue_adjust_round = round_pvalues(pvalue_adjust)) %>%
  967. # sort data table:
  968. setorder(classification, period, tITI) %>%
  969. # shorten the period name:
  970. mutate(period_short = ifelse(period == "forward", "fwd", period)) %>%
  971. transform(period_short = ifelse(period == "backward", "bwd", period_short)) %>%
  972. mutate(color = ifelse(period_short == "fwd", "dodgerblue", "red")) %>% setDT(.)
  973. return(data_out)
  974. }
  975. ```
  976. ```{r}
  977. rmarkdown::paged_table(
  978. seq_test_period_cue(data = dt_pred_seq_cor_cue, variable = "mean_cor") %>%
  979. filter(pvalue_adjust < 0.05, classification == "ovr"))
  980. rmarkdown::paged_table(
  981. seq_test_period_cue(data = dt_pred_seq_cor_cue, variable = "mean_step") %>%
  982. filter(pvalue_adjust < 0.05, classification == "ovr"))
  983. rmarkdown::paged_table(
  984. seq_test_period_cue(data = dt_pred_seq_cor_cue, variable = "mean_slope") %>%
  985. filter(pvalue_adjust < 0.05, classification == "ovr"))
  986. ```
  987. We plot the same data, just splitting up the data by the serial position of the cued image:
  988. ```{r}
  989. plot_seq_cor_period_cue = function(data, variable) {
  990. # select the variable of interest, determine y-axis label and adjust axis:
  991. if (variable == "mean_slope") {
  992. ylabel = "Regression slope"
  993. adjust_axis = 0.1
  994. } else if (variable == "mean_cor") {
  995. ylabel = expression("Correlation ("*tau*")")
  996. adjust_axis = 1
  997. } else if (variable == "mean_step") {
  998. ylabel = "Mean step size"
  999. adjust_axis = 1
  1000. }
  1001. dt_forward = data.table(xmin = 0, xmax = 5.5, ymin = 0, ymax = 0.4 * adjust_axis)
  1002. dt_backward = data.table(xmin = 0, xmax = 5.5, ymin = 0, ymax = -0.4 * adjust_axis)
  1003. # average across participants for every speed at every TR:
  1004. plot_data = data %>% setDT(.) %>%
  1005. .[, by = .(classification, id, tITI, period_short, cue_pos_label), .(
  1006. mean_variable = mean(get(variable))
  1007. )] %>% filter(classification == "ovr" & period_short != "excluded")
  1008. plot_stat = seq_test_period_cue(data = data, variable = variable)
  1009. # plot average correlation or betas for each speed condition and time period:
  1010. plot = ggplot(data = plot_data, aes(
  1011. x = fct_rev(as.factor(period_short)), y = as.numeric(mean_variable),
  1012. fill = as.factor(as.numeric(tITI) * 1000))) +
  1013. geom_bar(stat = "summary", fun = "mean", width = 0.9, show.legend = TRUE) +
  1014. geom_dotplot(binaxis = "y", stackdir = "center", stackratio = 0.5, alpha = 0.2,
  1015. binwidth = 0.01 * adjust_axis, show.legend = FALSE) +
  1016. geom_errorbar(stat = "summary", fun.data = "mean_se", width = 0.0, color = "black") +
  1017. geom_text(data = subset(plot_stat, classification == "ovr"), aes(
  1018. x = fct_rev(as.factor(period_short)), y = round_updown(as.numeric(mean_variable), 0.5 * adjust_axis),
  1019. label = paste0("d=", cohens_d, significance)), size = 3.0, show.legend = FALSE,
  1020. color = subset(plot_stat, classification == "ovr")$color) +
  1021. facet_grid(rows = vars(cue_pos_label),
  1022. cols = vars(as.factor(as.numeric(tITI) * 1000))) +
  1023. xlab("Period") + ylab(ylabel) +
  1024. scale_fill_viridis(name = "Speed (ms)", discrete = TRUE, option = "cividis") +
  1025. coord_capped_cart(left = "both", bottom = "both", expand = TRUE, ylim = c(-0.6, 0.6) * adjust_axis) +
  1026. theme(panel.border = element_blank(), axis.line = element_line()) +
  1027. theme(axis.line = element_line(colour = "black"),
  1028. panel.grid.major = element_blank(),
  1029. panel.grid.minor = element_blank(),
  1030. panel.border = element_blank(),
  1031. panel.background = element_blank()) +
  1032. #theme(axis.ticks.x = element_blank(), axis.line.x = element_blank()) +
  1033. theme(legend.position = "top", legend.direction = "horizontal", legend.box = "vertical",
  1034. legend.justification = "center", legend.margin = margin(t = 0, r = 0, b = 0, l = 0),
  1035. legend.box.margin = margin(t = 0, r = 0, b = -5, l = 0))
  1036. #theme(panel.spacing = unit(0, "lines"), strip.background = element_blank(),
  1037. # strip.placement = "outside", strip.text = element_blank())
  1038. return(plot)
  1039. }
  1040. fig_seq_cor_period_cue = plot_seq_cor_period_cue(data = dt_pred_seq_cor_cue, variable = "mean_cor")
  1041. fig_seq_slope_period_cue = plot_seq_cor_period_cue(data = dt_pred_seq_cor_cue, variable = "mean_slope")
  1042. fig_seq_step_period_cue = plot_seq_cor_period_cue(data = dt_pred_seq_cor_cue, variable = "mean_step")
  1043. fig_seq_cor_period_cue; fig_seq_step_period_cue; fig_seq_slope_period_cue;
  1044. ```
  1045. Combine plots for cue period:
  1046. ```{r, echo=FALSE}
  1047. plot_grid(fig_seq_probas_cue, fig_seq_slope_time_cue, fig_seq_slope_period_cue,
  1048. labels = c("a", "b", "c"), nrow = 3, rel_heights = c(5, 4, 6))
  1049. ```
  1050. ```{r, include=FALSE, eval=FALSE, echo=FALSE}
  1051. ggsave(filename = "highspeed_plot_decoding_sequence_cue_effects.pdf",
  1052. plot = last_plot(), device = cairo_pdf, path = path_figures, scale = 1,
  1053. dpi = "retina", width = 6, height = 10)
  1054. ```
  1055. ```{r}
  1056. # create a data frame with the relevant data to run the LME:
  1057. lme_seq_cor_data = dt_pred_seq_cor %>%
  1058. filter(classification == "ovr" & period != "excluded") %>%
  1059. transform(tITI = as.factor(tITI))
  1060. # define linear mixed effects model with by-participant random intercepts:
  1061. lme_seq_cor = lmer(mean_slope ~ tITI * period + (1|id),
  1062. data = lme_seq_cor_data, na.action = na.omit, control = lcctrl)
  1063. summary(lme_seq_cor)
  1064. anova(lme_seq_cor)
  1065. emmeans_results = emmeans(lme_seq_cor, list(pairwise ~ period | tITI))
  1066. emmeans_pvalues = round_pvalues(summary(emmeans_results[[2]])$p.value)
  1067. ```
  1068. ### Serial target position
  1069. We calculate the average serial position at each TR:
  1070. ```{r}
  1071. dt_pred_seq_pos = dt_pred_seq %>%
  1072. # get the position with the highest probability at every TR:
  1073. .[, by = .(classification, id, period, tITI, trial_tITI, seq_tr), .(
  1074. num_positions = .N,
  1075. max_position = position[which.max(probability_norm)]
  1076. )] %>%
  1077. # verify that the number of position per TR matches:
  1078. verify(all(num_positions == 5)) %>%
  1079. # average the maximum position across trials for each speed condition:
  1080. .[, by = .(classification, id, period, tITI, seq_tr), .(
  1081. num_trials = .N,
  1082. mean_position = mean(max_position)
  1083. )] %>%
  1084. # verify that the number of trials per participant is correct:
  1085. verify(all(num_trials == 15)) %>%
  1086. # calculate the difference of the mean position from baseline (which is 3)
  1087. mutate(position_diff = mean_position - 3) %>%
  1088. setDT(.) %>%
  1089. # set the speed condition and period variable to a factorial variable:
  1090. transform(tTII = as.factor(tITI)) %>%
  1091. transform(period = as.factor(period))
  1092. ```
  1093. We calculate whether the average serial position is significantly different
  1094. from baseline separately for every speed and period (forward vs. backward):
  1095. ```{r}
  1096. dt_pred_seq_pos_period = dt_pred_seq_pos %>%
  1097. # focus on the forward and backward period only:
  1098. filter(period != "excluded") %>% setDT(.) %>%
  1099. # average the mean position across trs for each period and speed condition:
  1100. .[, by = .(classification, id, period, tITI), .(
  1101. position_diff = mean(position_diff)
  1102. )] %>%
  1103. # average across participants for each speed condition and volume:
  1104. .[, by = .(classification, period, tITI), {
  1105. ttest_results = t.test(position_diff, alternative = "two.sided", mu = 0)
  1106. list(
  1107. num_subs = .N,
  1108. tvalue = round(ttest_results$statistic, 2),
  1109. pvalue = ttest_results$p.value,
  1110. df = ttest_results$parameter,
  1111. cohens_d = abs(round((mean(position_diff) - 0) / sd(position_diff), 2)),
  1112. position_diff = mean(position_diff),
  1113. conf_lb = round(ttest_results$conf.int[1], 2),
  1114. conf_ub = round(ttest_results$conf.int[2], 2),
  1115. sd_position = sd(position_diff),
  1116. sem_upper = mean(position_diff) + (sd(position_diff)/sqrt(.N)),
  1117. sem_lower = mean(position_diff) - (sd(position_diff)/sqrt(.N))
  1118. )
  1119. }] %>% verify(all(num_subs == 36)) %>%
  1120. # adjust p-values for multiple comparisons:
  1121. .[, by = .(classification), ":=" (
  1122. num_comp = .N,
  1123. pvalue_adjust = p.adjust(pvalue, method = "fdr", n = .N)
  1124. )] %>%
  1125. verify(all(num_comp == 10)) %>%
  1126. # add variable that indicates significance with stupid significance stars:
  1127. mutate(significance = ifelse(pvalue_adjust < 0.05, "*", "")) %>%
  1128. # round the original p-values:
  1129. mutate(pvalue_round = round_pvalues(pvalue)) %>%
  1130. # round the p-values adjusted for multiple comparisons:
  1131. mutate(pvalue_adjust_round = round_pvalues(pvalue_adjust)) %>%
  1132. # sort the datatable for each speed and TR:
  1133. setorder(., classification, period, tITI) %>%
  1134. # shorten the period name:
  1135. mutate(period_short = ifelse(period == "forward", "fwd", period)) %>%
  1136. transform(period_short = ifelse(period == "backward", "bwd", period_short)) %>%
  1137. mutate(color = ifelse(period_short == "fwd", "dodgerblue", "red")) %>% setDT(.)
  1138. dt_pred_seq_pos_period %>%
  1139. filter(classification == "ovr", pvalue_adjust < 0.05) %>%
  1140. rmarkdown::paged_table(.)
  1141. ```
  1142. We calculate the mean serial position at every TR and compare it against baseline:
  1143. ```{r}
  1144. dt_pred_seq_pos_tr = dt_pred_seq_pos %>%
  1145. # average across participants for each speed condition and volume:
  1146. .[, by = .(classification, period, tITI, seq_tr), {
  1147. ttest_results = t.test(mean_position, alternative = "two.sided", mu = 3)
  1148. list(
  1149. num_subs = .N,
  1150. tvalue = ttest_results$statistic,
  1151. pvalue = ttest_results$p.value,
  1152. df = ttest_results$parameter,
  1153. mean_position = mean(mean_position),
  1154. sd_position = sd(mean_position),
  1155. sem_upper = mean(mean_position) + (sd(mean_position)/sqrt(.N)),
  1156. sem_lower = mean(mean_position) - (sd(mean_position)/sqrt(.N))
  1157. )
  1158. }] %>% verify(all(num_subs == 36)) %>%
  1159. # adjust p-values for multiple comparisons:
  1160. .[period %in% c("forward", "backward"), by = .(classification), ":=" (
  1161. num_comp = .N,
  1162. pvalue_adjust = p.adjust(pvalue, method = "fdr", n = .N)
  1163. )] %>% verify(all(num_comp == 39, na.rm = TRUE)) %>%
  1164. # round the original p-values:
  1165. mutate(pvalue_rounded = round_pvalues(pvalue)) %>%
  1166. # round the p-values adjusted for multiple comparisons:
  1167. mutate(pvalue_adjust_rounded = round_pvalues(pvalue_adjust)) %>%
  1168. #
  1169. mutate(significance = ifelse(pvalue_adjust < 0.05, "***", "")) %>%
  1170. # sort the datatable for each speed and TR:
  1171. setorder(., classification, period, tITI, seq_tr)
  1172. dt_pred_seq_pos_tr %>%
  1173. filter(classification == "ovr", pvalue_adjust < 0.05)
  1174. ```
  1175. ```{r, eval = FALSE}
  1176. cfg = list(variable = "mean_position", threshold = 2.021, baseline = 3,
  1177. grouping = c("classification", "tITI"), n_perms = 10000, n_trs = 13)
  1178. dt_pred_seq_pos_cluster = cluster_permutation(dt_pred_seq_pos_sub, cfg)
  1179. ```
  1180. ```{r}
  1181. # define linear mixed effects model with by-participant random intercepts:
  1182. lme_seq_pos = lmer(position_diff ~ tITI * period + (1 + tITI + period |id),
  1183. data = subset(dt_pred_seq_pos, classification == "ovr" & period != "excluded"),
  1184. na.action = na.omit, control = lcctrl)
  1185. summary(lme_seq_pos)
  1186. anova(lme_seq_pos)
  1187. emmeans_results = emmeans(lme_seq_pos, list(pairwise ~ period | tITI))
  1188. emmeans_pvalues = round_pvalues(summary(emmeans_results[[2]])$p.value)
  1189. ```
  1190. ```{r, echo=TRUE}
  1191. variable = "position_diff"
  1192. plot_data = dt_pred_seq_pos %>%
  1193. # average across participants for every speed at every TR:
  1194. .[, by = .(classification, id, tITI, period), .(
  1195. mean_variable = mean(get(variable))
  1196. )] %>%
  1197. filter(classification == "ovr" & period != "excluded") %>%
  1198. # shorten the period name:
  1199. mutate(period_short = ifelse(period == "forward", "fwd", period)) %>%
  1200. transform(period_short = ifelse(period == "backward", "bwd", period_short)) %>%
  1201. mutate(color = ifelse(period_short == "fwd", "dodgerblue", "red")) %>%
  1202. setDT(.)
  1203. # plot average correlation or betas for each speed condition and time period:
  1204. fig_seq_pos_period = ggplot(data = plot_data, aes(
  1205. x = fct_rev(as.factor(period_short)), y = as.numeric(mean_variable),
  1206. fill = as.factor(as.numeric(tITI) * 1000))) +
  1207. geom_bar(stat = "summary", fun = "mean", width = 0.9, show.legend = TRUE) +
  1208. geom_dotplot(binaxis = "y", stackdir = "center", stackratio = 0.5, alpha = 0.2,
  1209. binwidth = 0.05, show.legend = FALSE) +
  1210. geom_errorbar(stat = "summary", fun.data = "mean_se", width = 0.0, color = "black") +
  1211. geom_text(data = subset(dt_pred_seq_pos_period, classification == "ovr"), aes(
  1212. x = fct_rev(as.factor(period_short)), y = round_updown(as.numeric(get(variable)), 1.2),
  1213. label = paste0("d=", sprintf("%.2f", cohens_d), significance)), show.legend = FALSE, size = 3.2,
  1214. color = subset(dt_pred_seq_pos_period, classification == "ovr")$color) +
  1215. facet_wrap(~ as.factor(as.numeric(tITI) * 1000), strip.position = "bottom", nrow = 1) +
  1216. xlab("Period") + ylab("Event position\ncompared to baseline") +
  1217. scale_colour_viridis(name = "Speed (ms)", discrete = TRUE, option = "cividis") +
  1218. scale_fill_viridis(name = "Speed (ms)", discrete = TRUE, option = "cividis") +
  1219. coord_capped_cart(left = "both", bottom = "both", expand = TRUE, ylim = c(-1.5, 1.5)) +
  1220. theme(legend.position = "top", legend.direction = "horizontal",
  1221. legend.justification = "center", legend.margin = margin(t = 0, r = 0, b = 0, l = 0),
  1222. legend.box.margin = margin(t = 0, r = 0, b = 0, l = 0)) +
  1223. theme(panel.spacing = unit(0, "lines"), strip.background = element_blank(),
  1224. strip.placement = "outside", strip.text = element_blank()) +
  1225. theme(axis.ticks.x = element_line(colour = "white"),
  1226. axis.line.x = element_line(colour = "white")) +
  1227. theme(axis.line.y = element_line(colour = "black"),
  1228. panel.grid.major = element_blank(),
  1229. panel.grid.minor = element_blank(),
  1230. panel.border = element_blank(),
  1231. panel.background = element_blank())
  1232. fig_seq_pos_period
  1233. ```
  1234. ```{r, include = FALSE, eval = FALSE}
  1235. ggsave(filename = "highspeed_plot_decoding_sequence_position_period.pdf",
  1236. plot = last_plot(), device = cairo_pdf, path = path_figures, scale = 1,
  1237. dpi = "retina", width = 5, height = 3)
  1238. ```
  1239. ```{r, echo = FALSE}
  1240. plot_seq_pos <- function(dt){
  1241. # create separate datatable to plot rectangles indicating forward / backward period:
  1242. dt_reduced = dt %>% setDT(.) %>%
  1243. .[, by = .(classification, tITI, period), .(
  1244. xmin = min(seq_tr) - 0.5,
  1245. xmax = max(seq_tr) + 0.5
  1246. )] %>%
  1247. filter(period != "excluded") %>%
  1248. mutate(fill = ifelse(period == "forward", "dodgerblue", "red"))
  1249. ggplot(data = dt, mapping = aes(
  1250. x = as.factor(seq_tr), y = as.numeric(mean_position),
  1251. group = as.factor(as.numeric(tITI)*1000), fill = as.factor(as.numeric(tITI)*1000))) +
  1252. #geom_rect(data = dt_reduced, aes(xmin = xmin, xmax = xmax, ymin = 2, ymax = 4),
  1253. # alpha = 0.05, inherit.aes = FALSE, show.legend = FALSE, fill = dt_reduced$fill) +
  1254. geom_hline(aes(yintercept = 3), linetype = "solid", color = "gray") +
  1255. #facet_wrap(facets = ~ as.factor(tITI), labeller = get_labeller(dt$tITI), nrow = 1) +
  1256. geom_ribbon(aes(ymin = sem_lower, ymax = sem_upper), alpha = 0.5, color = NA) +
  1257. geom_line(mapping = aes(color = as.factor(as.numeric(tITI)*1000))) +
  1258. xlab("Time from sequence onset (TRs)") +
  1259. ylab("Event position") +
  1260. scale_colour_viridis(discrete = TRUE, option = "cividis", name = "Speed (ms)", guide = FALSE) +
  1261. scale_fill_viridis(discrete = TRUE, option = "cividis", name = "Speed (ms)", guide = FALSE) +
  1262. scale_x_discrete(labels = label_fill(seq(1, 13, 1), mod = 4), breaks = seq(1, 13, 1)) +
  1263. coord_capped_cart(left = "both", bottom = "both", expand = TRUE, ylim = c(2,4)) +
  1264. theme(strip.text.x = element_text(margin = margin(b = 2, t = 2))) +
  1265. theme(legend.position = "top", legend.direction = "horizontal",
  1266. legend.justification = "center", legend.margin = margin(t = 0, r = 0, b = 0, l = 0),
  1267. legend.box.margin = margin(t = 0, r = 0, b = 0, l = 0)) +
  1268. geom_segment(aes(x = 0.9, xend = 0.9, y = 3.01, yend = 4),
  1269. arrow = arrow(length = unit(5, "pt")), color = "darkgray") +
  1270. geom_segment(aes(x = 0.9, xend = 0.9, y = 3.01, yend = 2),
  1271. arrow = arrow(length = unit(5, "pt")), color = "darkgray") +
  1272. annotate(geom = "text", x = 1.4, y = 3.5, label = "Later",
  1273. color = "darkgray", angle = 90, size = 3) +
  1274. annotate(geom = "text", x = 1.4, y = 2.5, label = "Earlier",
  1275. color = "darkgray", angle = 90, size = 3) +
  1276. annotate("text", x = 13, y = 2, label = "1 TR = 1.25 s",
  1277. hjust = 1, size = rel(2)) +
  1278. theme(axis.line = element_line(colour = "black"),
  1279. panel.grid.major = element_blank(),
  1280. panel.grid.minor = element_blank(),
  1281. panel.border = element_blank(),
  1282. panel.background = element_blank())
  1283. }
  1284. fig_seq_pos_time = plot_seq_pos(dt = subset(dt_pred_seq_pos_tr, classification == "ovr"))
  1285. fig_seq_pos_time
  1286. ```
  1287. ```{r, include = FALSE, eval = FALSE}
  1288. ggsave(filename = "highspeed_plot_decoding_sequence_timecourse_position.pdf",
  1289. plot = last_plot(), device = cairo_pdf, path = path_figures, scale = 1,
  1290. dpi = "retina", width = 6, height = 3)
  1291. ```
  1292. ### Transititons
  1293. We calculate the step size between consecutively decoded (highest probability) events:
  1294. ```{r}
  1295. dt_pred_seq_step = dt_pred_seq %>%
  1296. # get the position with the highest probability for every TR:
  1297. .[, by = .(classification, id, tITI, trial_tITI, seq_tr), ":=" (
  1298. num_classes = .N,
  1299. rank_order = rank(-probability),
  1300. max_prob = as.numeric(probability == max(probability))
  1301. )] %>%
  1302. # verify that there are five classes per TR:
  1303. verify(all(num_classes == 5)) %>%
  1304. # sort the data table:
  1305. setorder(., classification, id, tITI, trial_tITI, seq_tr) %>%
  1306. # select only classes with the highest probability for every TR:
  1307. filter(max_prob == 1) %>%
  1308. setDT(.) %>%
  1309. # check if the rank order of the event with highest probability match:
  1310. verify(all(rank_order == max_prob)) %>%
  1311. # group by classification, id, speed and trial and calculate step sizes:
  1312. .[, by = .(classification, id, tITI, trial_tITI),
  1313. step := position - shift(position)]
  1314. ```
  1315. We calculate the mean step size for early and late period in the forward and backward phase:
  1316. ```{r}
  1317. dt_pred_seq_step_mean = dt_pred_seq_step %>%
  1318. filter(period != "excluded") %>%
  1319. filter(!(is.na(zone))) %>% setDT(.) %>%
  1320. # shorten the period name:
  1321. mutate(period_short = ifelse(period == "forward", "fwd", period)) %>%
  1322. transform(period_short = ifelse(period == "backward", "bwd", period_short)) %>%
  1323. setDT(.) %>%
  1324. .[, by = .(classification, id, tITI, period_short, zone), .(
  1325. mean_step = mean(step, na.rm = TRUE))]
  1326. ```
  1327. We compare the forward and the backward period using t-tests:
  1328. ```{r}
  1329. dt_pred_seq_step_stat = dt_pred_seq_step_mean %>%
  1330. spread(key = period_short, value = mean_step, drop = TRUE) %>%
  1331. mutate(difference = fwd - bwd) %>% setDT(.) %>%
  1332. # average across participants for each speed condition and volume:
  1333. .[, by = .(classification, tITI, zone), {
  1334. ttest_results = t.test(fwd, bwd, alternative = "two.sided", paired = TRUE)
  1335. list(
  1336. num_subs = .N,
  1337. tvalue = round(ttest_results$statistic, 2),
  1338. pvalue = ttest_results$p.value,
  1339. df = ttest_results$parameter,
  1340. cohens_d = abs(round((mean(fwd) - mean(bwd)) / sd(fwd - bwd), 2)),
  1341. mean_step = mean(difference),
  1342. sd_step = sd(difference),
  1343. sem_upper = mean(difference) + (sd(difference)/sqrt(.N)),
  1344. sem_lower = mean(difference) - (sd(difference)/sqrt(.N))
  1345. )
  1346. }] %>% verify(all(num_subs == 36)) %>%
  1347. # adjust p-values for multiple comparisons:
  1348. .[, by = .(classification), ":=" (
  1349. num_comp = .N,
  1350. pvalue_adjust = p.adjust(pvalue, method = "fdr", n = .N)
  1351. )] %>%
  1352. verify(all(num_comp == 10)) %>%
  1353. # add variable that indicates significance with stupid significance stars:
  1354. mutate(significance = ifelse(pvalue < 0.05, "*", "")) %>%
  1355. # round the original p-values:
  1356. mutate(pvalue_round = round_pvalues(pvalue)) %>%
  1357. # round the p-values adjusted for multiple comparisons:
  1358. mutate(pvalue_adjust_round = round_pvalues(pvalue_adjust)) %>%
  1359. # sort the datatable for each speed and TR:
  1360. setorder(., classification, zone, tITI)
  1361. dt_pred_seq_step_stat %>%
  1362. filter(classification == "ovr", pvalue_adjust < 0.05)
  1363. ```
  1364. We compare each period to the baseline:
  1365. ```{r}
  1366. dt_pred_seq_step_stat_baseline = dt_pred_seq_step_mean %>%
  1367. # average across participants for each speed condition and volume:
  1368. .[, by = .(classification, tITI, period_short, zone), {
  1369. ttest_results = t.test(mean_step, mu = 0, alternative = "two.sided")
  1370. list(
  1371. num_subs = .N,
  1372. tvalue = round(ttest_results$statistic, 2),
  1373. pvalue = ttest_results$p.value,
  1374. df = ttest_results$parameter,
  1375. cohens_d = abs(round((mean(mean_step) - 0) / sd(mean_step), 2)),
  1376. mean_step = mean(mean_step),
  1377. sd_step = sd(mean_step),
  1378. sem_upper = mean(mean_step) + (sd(mean_step)/sqrt(.N)),
  1379. sem_lower = mean(mean_step) - (sd(mean_step)/sqrt(.N))
  1380. )
  1381. }] %>% verify(all(num_subs == 36)) %>%
  1382. # adjust p-values for multiple comparisons:
  1383. .[, by = .(classification), ":=" (
  1384. num_comp = .N,
  1385. pvalue_adjust = p.adjust(pvalue, method = "fdr", n = .N)
  1386. )] %>%
  1387. # verf
  1388. verify(all(num_comp == 20)) %>%
  1389. # add variable that indicates significance with stupid significance stars:
  1390. mutate(significance = ifelse(pvalue_adjust < 0.05, "*", "")) %>%
  1391. # round the original p-values:
  1392. mutate(pvalue_round = round_pvalues(pvalue)) %>%
  1393. # round the p-values adjusted for multiple comparisons:
  1394. mutate(pvalue_adjust_round = round_pvalues(pvalue_adjust)) %>%
  1395. # sort the datatable for each speed and TR:
  1396. setorder(., classification, period_short, zone, tITI)
  1397. dt_pred_seq_step_stat_baseline %>%
  1398. filter(classification == "ovr", pvalue < 0.05)
  1399. ```
  1400. ```{r}
  1401. # plot average correlation or betas for each speed condition and time period:
  1402. fig_seq_step = ggplot(data = subset(dt_pred_seq_step_mean, classification == "ovr"), aes(
  1403. x = fct_rev(as.factor(period_short)), y = as.numeric(mean_step),
  1404. fill = as.factor(as.numeric(tITI) * 1000)), color = as.factor(as.numeric(tITI) * 1000)) +
  1405. facet_grid(vars(as.factor(zone)), vars(as.factor(as.numeric(tITI) * 1000)), switch = "x") +
  1406. geom_bar(stat = "summary", fun = "mean", width = 0.9) +
  1407. geom_point(position = position_jitterdodge(jitter.height = 0, seed = 4, jitter.width = 0.2),
  1408. pch = 21, alpha = 0.05, color = "black", show.legend = FALSE) +
  1409. geom_errorbar(stat = "summary", fun.data = "mean_se", width = 0.0, color = "black") +
  1410. geom_text(data = subset(dt_pred_seq_step_stat, classification == "ovr"), aes(
  1411. y = 2, label = paste0("d=", sprintf("%.2f", cohens_d), significance), x = 1.5),
  1412. inherit.aes = FALSE, color = "black", size = 3.3) +
  1413. xlab("Period") + ylab("Step size") +
  1414. scale_colour_viridis(discrete = TRUE, option = "cividis", name = "Speed (ms)") +
  1415. scale_fill_viridis(discrete = TRUE, option = "cividis", name = "Speed (ms)") +
  1416. coord_capped_cart(left = "both", bottom = "both", expand = TRUE, ylim = c(-2, 2)) +
  1417. theme(legend.position = "top", legend.direction = "horizontal",
  1418. legend.justification = "center", legend.margin = margin(t = 0, r = 0, b = -5, l = 0),
  1419. legend.box.margin = margin(t = 0, r = 0, b = 0, l = 0)) +
  1420. theme(panel.spacing.x = unit(0, "lines"), strip.background.x = element_blank(),
  1421. strip.placement.x = "outside", strip.text.x = element_blank()) +
  1422. theme(axis.ticks.x = element_line(colour = "white"),
  1423. axis.line.x = element_line(colour = "white")) +
  1424. #theme(axis.title.x = element_blank()) +
  1425. theme(strip.text = element_text(margin = margin(b = 2, t = 2, r = 2, l = 2))) +
  1426. theme(axis.line.y = element_line(colour = "black"),
  1427. panel.grid.major = element_blank(),
  1428. panel.grid.minor = element_blank(),
  1429. panel.border = element_blank(),
  1430. panel.background = element_blank())
  1431. fig_seq_step
  1432. ```
  1433. ```{r, echo=FALSE, eval=FALSE, include=FALSE}
  1434. ggsave(filename = "highspeed_plot_decoding_sequence_step_size.pdf",
  1435. plot = last_plot(), device = cairo_pdf, path = path_figures, scale = 1,
  1436. dpi = "retina", width = 5, height = 3)
  1437. ```
  1438. ```{r, fig.width = 10, fig.height = 4}
  1439. plot_grid(fig_seq_pos_period, fig_seq_step, labels = "auto",
  1440. ncol = 2, label_fontface = "bold", rel_widths = c(5, 6))
  1441. ```
  1442. ```{r, echo=FALSE, eval=FALSE, include=FALSE}
  1443. ggsave(filename = "highspeed_plot_decoding_sequence_between_tr.pdf",
  1444. plot = last_plot(), device = cairo_pdf, path = path_figures, scale = 1,
  1445. dpi = "retina", width = 10, height = 4)
  1446. ```
  1447. Plot Figure 3 in the main text:
  1448. ```{r}
  1449. plot_grid(
  1450. plot_grid(fig_seq_probas, labels = c("a"), nrow = 1),
  1451. plot_grid(fig_seq_slope_time, fig_seq_slope_period, labels = c("b", "c"),
  1452. ncol = 2, nrow = 1, label_fontface = "bold", rel_widths = c(4.9, 5)),
  1453. plot_grid(fig_seq_cor_between, fig_seq_cor_within, fig_seq_pos_time,
  1454. labels = c("d", "e", "f"), ncol = 3, rel_widths = c(0.325, 0.325, 0.35)),
  1455. plot_grid(fig_seq_pos_period, fig_seq_step, labels = c("g", "h"),
  1456. ncol = 2, label_fontface = "bold", nrow = 1),
  1457. nrow = 4, label_fontface = "bold", rel_heights = c(2, 3)
  1458. )
  1459. ```
  1460. ```{r, echo=FALSE, eval=FALSE, include=FALSE}
  1461. ggsave(filename = "highspeed_plot_decoding_sequence_data.pdf",
  1462. plot = last_plot(), device = cairo_pdf, path = path_figures, scale = 1,
  1463. dpi = "retina", width = 10, height = 12)
  1464. ```
  1465. ```{r, include=FALSE, echo=TRUE, eval=FALSE, warning=FALSE, message=FALSE}
  1466. title_nomax = ggdraw() + draw_label("Sequence item with highest probability removed", fontface = "plain")
  1467. title_nofirst = ggdraw() + draw_label("First sequence item removed", fontface = "plain")
  1468. title_nolast = ggdraw() + draw_label("Last sequence item removed", fontface = "plain")
  1469. # create a common legend used for the entire figure panel:
  1470. common_legend <- get_legend(fig_seq_slope_time + theme(legend.position = "top"))
  1471. # create the plot of sequence data with the maximum probability removed:
  1472. plot_nomax = plot_grid(fig_seq_slope_time + theme(legend.position = "none"),
  1473. fig_seq_slope_period + theme(legend.position = "none"),
  1474. rel_widths = c(4, 5), labels = "auto", ncol = 2, nrow = 1,
  1475. label_fontface = "bold")
  1476. plot_nofirst = plot_grid(fig_seq_slope_time + theme(legend.position = "none"),
  1477. fig_seq_slope_period + theme(legend.position = "none"),
  1478. rel_widths = c(4, 5), labels = c("c", "d"), ncol = 2, nrow = 1,
  1479. label_fontface = "bold")
  1480. plot_nolast = plot_grid(fig_seq_slope_time + theme(legend.position = "none"),
  1481. fig_seq_slope_period + theme(legend.position = "none"),
  1482. rel_widths = c(4, 5), labels = c("e", "f"), ncol = 2, nrow = 1,
  1483. label_fontface = "bold")
  1484. plot_all = plot_grid(
  1485. legend_b, title_nomax, plot_nomax,
  1486. title_nofirst, plot_nofirst,
  1487. title_nolast, plot_nolast,
  1488. ncol = 1, rel_heights=c(0.1, 0.1, 1, 0.1, 1, 0.1, 1))
  1489. plot_all
  1490. ```
  1491. ```{r, echo=FALSE, eval=FALSE, include=FALSE}
  1492. ggsave(filename = "highspeed_plot_decoding_sequence_slope_remove_items.pdf",
  1493. plot = last_plot(), device = cairo_pdf, path = path_figures, scale = 1,
  1494. dpi = "retina", width = 10, height = 10)
  1495. ```
  1496. ```{r, echo=FALSE, eval=FALSE, include=FALSE}
  1497. plot_grid(fig_seq_cor_time, fig_seq_cor_period, fig_seq_step_time, fig_seq_step_period,
  1498. labels = "auto", ncol = 2, nrow = 2, label_fontface = "bold")
  1499. ggsave(filename = "highspeed_plot_decoding_sequence_correlation_step.pdf",
  1500. plot = last_plot(), device = cairo_pdf, path = path_figures, scale = 1,
  1501. dpi = "retina", width = 10, height = )
  1502. ```
  1503. ```{r}
  1504. fig_seq_slope_time_facet = plot_seq_cor_facet(dt = subset(
  1505. seq_test_time(data = dt_pred_seq_cor, variable = "mean_slope"),
  1506. classification == "ovr"), variable = "mean_slope")
  1507. fig_seq_cor_time_facet = plot_seq_cor_facet(dt = subset(
  1508. seq_test_time(data = dt_pred_seq_cor, variable = "mean_cor"),
  1509. classification == "ovr"), variable = "mean_cor")
  1510. fig_seq_step_time_facet = plot_seq_cor_facet(dt = subset(
  1511. seq_test_time(data = dt_pred_seq_cor, variable = "mean_step"),
  1512. classification == "ovr"), variable = "mean_step")
  1513. remove_xaxis = theme(axis.title.x = element_blank())
  1514. remove_facets = theme(strip.background = element_blank(), strip.text.x = element_blank())
  1515. plot_grid(fig_seq_slope_time_facet + remove_xaxis,
  1516. fig_seq_pos_time + remove_xaxis + theme(legend.position = "none") + remove_facets,
  1517. fig_seq_cor_time_facet + remove_xaxis + theme(legend.position = "none") + remove_facets,
  1518. fig_seq_step_time_facet + theme(legend.position = "none") + remove_facets,
  1519. labels = "auto", ncol = 1, label_fontface = "bold")
  1520. ```
  1521. ```{r, echo=FALSE, eval=FALSE, include=FALSE}
  1522. ggsave(filename = "highspeed_plot_decoding_sequence_timecourse_slope_correlation_step.pdf",
  1523. plot = last_plot(), device = cairo_pdf, path = path_figures, scale = 1,
  1524. dpi = "retina", width = 6, height = 7.5)
  1525. ```