OSCR

Strong mnemonic prediction errors increase cognitive control, attention, and arousal.

Code ↔ Paper

3 matches between paragraphs of the paper and lines of its authors' code, computed by the harvester (lexical-v1). Click a colored paragraph or line to see its counterpart.

The 3 matches
  1. [1] § Results › Effects of mnemonic prediction errors › Distinct pupil features reflect MPEs and attentional orienting ↔ scripts/pupil/pupil_temporal_pca.Rmd, lines 1339 to 1389 · score 0.64 · cognitive effort, temporal PCA, orienting responses, PC6, PC2, PC5 · Report a mismatch
  2. [2] § Methods › EEG and pupillometry analyses › Pupillary temporal PCA ↔ scripts/pupil/pupil_temporal_pca.Rmd, lines 161 to 281 · score 0.51 · Temporal PCA, matrix, rotation, varimax, RT, components · Report a mismatch
  3. [3] § Methods › EEG and pupillometry analyses › Pupillary temporal PCA ↔ scripts/pupil/pupil_temporal_pca_include_incorrect.Rmd, lines 186 to 309 · score 0.51 · Temporal PCA, matrix, rotation, varimax, RT, components · Report a mismatch

Paper

Loaded from Europe PMC by your browser; OSCR does not store it: doi.org · Europe PMC

The paper is loaded when this pane is shown.

The authors' code

R Markdown · 2,507 lines · 88 KB · GPL-3.0 · 2 matches

  1. ---
  2. title: "Temporal PCA on pupil data during associative retrieval"
  3. author: "Alice M. Xue ([email hidden])"
  4. date: "`r format(Sys.time(), '%B %d, %Y')`"
  5. params:
  6. exp_id: "MPE"
  7. run_models: yes
  8. output:
  9. html_document:
  10. toc: yes
  11. toc_depth: 3
  12. toc_float:
  13. collapsed: no
  14. self_contained: true
  15. pdf_document:
  16. toc: yes
  17. toc_depth: '3'
  18. ---
  19. ```{r setup, include=FALSE}
  20. library(plyr)
  21. library(Hmisc)
  22. library(ggplot2)
  23. library(knitr)
  24. library(kableExtra)
  25. library(data.table)
  26. library(psycho)
  27. library(lme4)
  28. library(sjPlot)
  29. library(afex)
  30. library(viridis)
  31. library(showtext)
  32. library(tidyverse)
  33. library(mgcv)
  34. library(itsadug)
  35. library(RColorBrewer)
  36. library(ggcorrplot)
  37. library(devtools)
  38. library(factoextra)
  39. library(tidyjson)
  40. library(purrr)
  41. library(broom)
  42. library(zoo)
  43. library(psych)
  44. library(patchwork)
  45. library(emmeans)
  46. library(ggpubr)
  47. library(stats)
  48. library(broom.mixed)
  49. library(pracma)
  50. library(afex)
  51. library(stringr)
  52. library(ggeffects)
  53. font_add(family = "Avenir Next",
  54. regular = "../utils/AvenirNext-Regular.ttf",
  55. bold = "../utils/AvenirNext-DemiBold.ttf",
  56. italic = "../utils/AvenirNext-Italic.ttf")
  57. showtext.auto()
  58. op <- showtext_opts()
  59. fontfamily <- "Avenir Next"
  60. set_theme(
  61. base=theme_classic(base_size = 12,
  62. base_family="Avenir Next")
  63. )
  64. options(dplyr.summarise.inform = FALSE)
  65. #knitr::opts_chunk$set(dev = 'pdf') # set output device to pdf
  66. knitr::opts_chunk$set(echo = TRUE, warning=FALSE, message = FALSE)
  67. ```
  68. ```{r, echo=F}
  69. source("../utils/summary_se_utils.R")
  70. ```
  71. ```{r get_behavior, echo=F}
  72. set.seed(478)
  73. exp_id <- params$exp_id
  74. run_models <- params$run_models
  75. data_ret <- fread("../../data/behavior/MPE_asso_retrieval_behavior.csv") %>%
  76. mutate(key = match_response.keys,
  77. rt = match_response.rt * 1000)
  78. data_recog <- fread("../../data/behavior/MPE_mismatch_recog_behavior.csv") %>%
  79. mutate(rt = recognition_response.rt * 1000)
  80. recognition_behavior <- data_recog %>%
  81. mutate(trialN = trials.thisTrialN) %>%
  82. select(trialN,
  83. recognition_response.keys,
  84. recognition_response.rt,
  85. is_old,
  86. img,
  87. subjectId,
  88. expId) %>%
  89. mutate(hit = is_old & (recognition_response.keys==1)) %>%
  90. mutate(false_alarm = !is_old & (recognition_response.keys==1)) %>%
  91. mutate(miss = is_old & (recognition_response.keys==2)) %>%
  92. mutate(correct_rejection = !is_old & (recognition_response.keys==2)) %>%
  93. mutate(trial_outcome = case_when(hit ~ "Hit",
  94. false_alarm ~ "FA",
  95. miss ~ "Miss",
  96. correct_rejection ~ "CR")) %>%
  97. group_by(subjectId,
  98. trialN,
  99. trial_outcome,
  100. is_old,
  101. img,
  102. recognition_response.keys,
  103. expId) %>%
  104. summarize(recognition_response.rt = mean(recognition_response.rt)) %>%
  105. ungroup()
  106. ```
  107. ```{r load_models, echo=F, eval=!run_models}
  108. load("tpca_models_correct_only.RData")
  109. ```
  110. ```{r save_or_load_data, echo=F}
  111. retrieval_pupil <- fread("../../data/pupil/MPE_imm_retrieval_pupil.csv")
  112. retrieval_pupil.del <- fread("../../data/pupil/MPE_del_retrieval_pupil.csv")
  113. retrieval_pupil <- bind_rows(retrieval_pupil, retrieval_pupil.del) %>%
  114. mutate(expId = case_when(grepl("MPE1", subjectId) ~ "imm",
  115. grepl("MPE3", subjectId) ~ "del",
  116. grepl("MPE5", subjectId) ~ "del")) %>%
  117. mutate(across(expId,
  118. factor,
  119. levels=c("imm", "del"))) %>%
  120. mutate(room_setup = case_when(grepl("MPE1", subjectId) ~ "1", # eeg + pupil, shared-eeg lab
  121. grepl("MPE3", subjectId) ~ "2", # pupil only, shared-eeg lab
  122. grepl("MPE5", subjectId) ~ "3")) # pupil only, 4th floor lab
  123. ```
  124. ```{r labels, echo=F}
  125. palette <- c("#008080", "#9fdfbf")
  126. hit_miss_palette <- c("#6E9075", "#FC7753")
  127. ret_labels_strong <- tibble(event = c("cue", "delay", "probe", "ITI"),
  128. time = c(0, 1, 2, 2.75))
  129. ret_labels_weak <- tibble(event = c("cue", "delay", "probe", "ITI"),
  130. time = c(0, 1, 2, 2.75))
  131. ret_labels <- rbind(ret_labels_strong, ret_labels_weak)
  132. ret_labels$position <- 0.12
  133. ```
  134. ```{r tempPCA_trial, echo=F}
  135. subj_timeseries_trial <- retrieval_pupil %>%
  136. dplyr::filter(trial_outcome %in% c("Hit", "CR")) %>%
  137. mutate(pupil_diameter = pupil_diameter_z)
  138. subj_timeseries_trial %>%
  139. pivot_wider(id_cols = c("subjectId",
  140. "expId",
  141. "room_setup",
  142. "trial_outcome",
  143. "match_response.rt",
  144. "association_strength",
  145. "trialN",
  146. "img"),
  147. names_from = c("time"),
  148. values_from = c("pupil_diameter")) %>%
  149. mutate(across(trial_outcome,
  150. factor,
  151. levels=c("Hit", "CR", "Miss", "FA"))) %>%
  152. group_by(trial_outcome, association_strength) %>%
  153. summarize(count = n()) %>%
  154. ungroup() %>%
  155. mutate(total_ntrials = sum(count)) %>%
  156. mutate(percent_data = round(count / total_ntrials * 100, 2)) %>%
  157. kable() %>%
  158. kable_styling(bootstrap_options = "striped",
  159. full_width = F,
  160. position = "left")
  161. subj.data <- subj_timeseries_trial %>%
  162. pivot_wider(id_cols = c("trial_outcome",
  163. "association_strength",
  164. "subjectId",
  165. "expId",
  166. "room_setup",
  167. "trialN",
  168. "img",
  169. "trialN_block",
  170. "match_response.rt",
  171. "block"),
  172. names_from = c("time"),
  173. values_from = c("pupil_diameter"))
  174. pca_input <- subj.data %>%
  175. select(-trial_outcome,
  176. -association_strength,
  177. -subjectId,
  178. -expId,
  179. -room_setup,
  180. -match_response.rt,
  181. -trialN,
  182. -img,
  183. -trialN_block,
  184. -block)
  185. ncomp <- 6
  186. pc_names <- paste("PC", 1:ncomp, sep = "")
  187. data.pca <- prcomp(pca_input, center=T, scale=T)
  188. # scree plot
  189. plot(data.pca)
  190. # change the sign of PC4 columns to flip the sign of the loadings
  191. rot <- data.pca$rotation
  192. rot[, c(4)] <- rot[, c(4)] * -1
  193. data.pca$rotation <- rot
  194. raw.loadings <- data.pca$rotation[,1:ncomp] %*% diag(data.pca$sdev, ncomp, ncomp)
  195. rotated.loadings <- varimax(raw.loadings)$loadings
  196. inv.loadings <- t(pracma::pinv(rotated.loadings))
  197. loadings <- rotated.loadings
  198. scores <- scale(pca_input) %*% inv.loadings
  199. subj.pred <- as_tibble(scores) %>%
  200. rename_with(~ gsub("V", "PC", .))
  201. fviz_eig(data.pca, addlabels = TRUE)
  202. pca.loadings <- as_tibble(matrix(as.numeric(loadings),
  203. attributes(loadings)$dim,
  204. dimnames=attributes(loadings)$dimnames),
  205. rownames="time") %>%
  206. mutate(time = as.numeric(time))
  207. pca.loadings <- pca.loadings %>%
  208. rename_with(~ gsub("V", "PC", .))
  209. ss_loadings <- colSums(loadings^2)
  210. var_explained <- as_tibble(ss_loadings/nrow(loadings)) %>%
  211. mutate(percent_var_explained = value * 100) %>%
  212. add_column(component = pc_names)
  213. var_explained <- var_explained %>%
  214. arrange(desc(percent_var_explained)) %>%
  215. mutate(component_renamed = paste("PC", row_number(), sep=""))
  216. pca.loadings <- pca.loadings %>%
  217. pivot_longer(cols = pc_names,
  218. names_to = "component",
  219. values_to = "loading") %>%
  220. left_join(var_explained, by=c("component")) %>%
  221. rename(component_orig = component,
  222. component = component_renamed)
  223. # ordered by "peak time"
  224. ordered_pcs <- c("PC2",
  225. "PC5",
  226. "PC1",
  227. "PC6",
  228. "PC3",
  229. "PC4")
  230. component_order_with_var_explained <- c("PC2: 25.89%",
  231. "PC5: 5.66%",
  232. "PC1: 28.81%",
  233. "PC6: 1.83%",
  234. "PC3: 19.29%",
  235. "PC4: 14.68%")
  236. ```
  237. ```{r plot_pc_loadings, echo=F}
  238. rainbow <- c("#E82C42",
  239. "#F46D43",
  240. "#FFCC00",
  241. "#29A343",
  242. "#1E8DD2",
  243. "#59169C")
  244. pca.loadings %>%
  245. mutate(component = paste(component, ": ", round(percent_var_explained, 2),
  246. "%",
  247. sep = "")) %>%
  248. mutate(across(component,
  249. factor,
  250. levels=component_order_with_var_explained)) %>%
  251. ggplot(aes(x = time,
  252. y = loading,
  253. color = component,
  254. fill = component)) +
  255. geom_vline(xintercept = c(0, 1, 2, 2.75),
  256. linetype = "dashed",
  257. color = "gray37") +
  258. geom_line(linewidth=1.2) +
  259. geom_text(aes(label = event,
  260. x = time,
  261. y = 0.9),
  262. data = ret_labels,
  263. inherit.aes = FALSE,
  264. angle = 90,
  265. hjust = 1,
  266. vjust = 1.2,
  267. size = 6,
  268. colour = "gray37",
  269. family = fontfamily) +
  270. labs(x = "Time (s)",
  271. y = "PC loading",
  272. title = "Components extracted by temporal PCA") +
  273. scale_color_manual(values=rainbow, name="") +
  274. theme(text = element_text(size=15))
  275. pca.loadings %>%
  276. mutate(component = paste(component, ": ", round(percent_var_explained, 2),
  277. "%",
  278. sep = "")) %>%
  279. ggplot(aes(x = time,
  280. y = loading,
  281. color = component,
  282. fill = component)) +
  283. geom_vline(xintercept = c(0, 1, 2, 2.75),
  284. linetype = "dashed",
  285. color = "gray37") +
  286. geom_line(color="black", linewidth=1) +
  287. geom_text(aes(label = event,
  288. x = time,
  289. y = 0.75),
  290. data = ret_labels,
  291. inherit.aes = FALSE,
  292. angle = 90,
  293. hjust = 1,
  294. vjust = 1.2,
  295. size = 3,
  296. colour = "gray37",
  297. family = fontfamily) +
  298. facet_wrap(~component) +
  299. labs(x = "Time (s)",
  300. y = "PC loading",
  301. title = "Temporal PCA") +
  302. scale_color_brewer(palette="Set2") +
  303. theme(legend.position = "none")
  304. subj.data.scores <- subj.data %>%
  305. add_column(subj.pred) %>%
  306. pivot_longer(cols = pc_names,
  307. names_to = "component",
  308. values_to = "component_score") %>%
  309. left_join(var_explained, by=c("component")) %>%
  310. rename(component_orig = component,
  311. component = component_renamed)
  312. scores <- subj.data.scores %>%
  313. select(subjectId,
  314. trialN,
  315. img,
  316. trialN_block,
  317. block,
  318. trial_outcome,
  319. association_strength,
  320. component,
  321. component_score)
  322. save(loadings,
  323. inv.loadings,
  324. subj.data.scores,
  325. file="pupil_tPCA_correct_only.RData")
  326. write.csv(scores, "pupil_tPCA_scores_correct_trials_only.csv")
  327. write.csv(pca.loadings, "pupil_tPCA_loadings_correct_trials_only.csv")
  328. ```
  329. ```{r loadings_plts, echo=F}
  330. loadings.plt <- pca.loadings %>%
  331. mutate(component = paste(component, ": ", round(percent_var_explained, 2),
  332. "%",
  333. sep = "")) %>%
  334. ggplot(aes(x = time,
  335. y = loading,
  336. color = component,
  337. fill = component)) +
  338. geom_vline(xintercept = c(0, 1, 2, 2.75),
  339. linetype = "dashed",
  340. color = "gray37") +
  341. geom_line(color="black", linewidth=1) +
  342. geom_text(aes(label = event,
  343. x = time,
  344. y = 0.75),
  345. data = ret_labels,
  346. inherit.aes = FALSE,
  347. angle = 90,
  348. hjust = 1,
  349. vjust = 1,
  350. size = 3,
  351. colour = "gray37",
  352. family = fontfamily) +
  353. facet_wrap(~component, ncol=1) +
  354. labs(x = "Time (s)",
  355. y = "PC loading") +
  356. theme(legend.position = "none")
  357. loadings.vrt.plt <- pca.loadings %>%
  358. mutate(component = paste(component, ": ", round(percent_var_explained, 2),
  359. "%",
  360. sep = "")) %>%
  361. mutate(across(component,
  362. factor,
  363. levels=component_order_with_var_explained)) %>%
  364. ggplot(aes(x = time,
  365. y = loading,
  366. color = component,
  367. fill = component)) +
  368. geom_vline(xintercept = c(0, 1, 2, 2.75),
  369. linetype = "dashed",
  370. color = "gray37") +
  371. geom_line(color="black", linewidth=1) +
  372. geom_text(aes(label = event,
  373. x = time,
  374. y = 0.75),
  375. data = ret_labels,
  376. inherit.aes = FALSE,
  377. angle = 90,
  378. hjust = 1,
  379. vjust = 1.2,
  380. size = 3,
  381. colour = "gray37",
  382. family = fontfamily) +
  383. facet_wrap(~component, ncol=1) +
  384. labs(x = "Time (s)",
  385. y = "PC loading") +
  386. theme(legend.position = "none")
  387. ```
  388. ## Memory outcome models
  389. Match v. mismatch, correct v. incorrect
  390. ```{r match_mismatch_acc_models, echo=F, eval=run_models, fig.width=6.5, fig.height=3, results="asis"}
  391. models <- c()
  392. models.compact.no.mismatch <- c()
  393. for (i in 1:ncomp) {
  394. mod.data <- subj.data.scores %>%
  395. dplyr::filter(component == paste("PC", i, sep="")) %>%
  396. mutate(is_mismatch = if_else(trial_outcome %in% c("Hit", "Miss"),
  397. 0,
  398. 1)) %>%
  399. mutate(is_incorrect = if_else(trial_outcome %in% c("Hit", "CR"),
  400. 0,
  401. 1))
  402. mod <- mod.data %>%
  403. lmer(component_score ~ is_mismatch * association_strength +
  404. (is_mismatch * association_strength | subjectId) +
  405. (1 | expId),
  406. data = .)
  407. if (isSingular(mod) | any(grepl("failed to converge", mod@optinfo$conv$lme4$messages))) {
  408. mod <- mod.data %>%
  409. lmer(component_score ~ is_mismatch * association_strength +
  410. (is_mismatch * association_strength | subjectId),
  411. data = .)
  412. }
  413. if (isSingular(mod) | any(grepl("failed to converge", mod@optinfo$conv$lme4$messages))) {
  414. mod <- mod.data %>%
  415. lmer(component_score ~ is_mismatch * association_strength +
  416. (is_mismatch + association_strength | subjectId) +
  417. (1 | expId),
  418. data = .)
  419. }
  420. if (isSingular(mod) | any(grepl("failed to converge", mod@optinfo$conv$lme4$messages))) {
  421. mod <- mod.data %>%
  422. lmer(component_score ~ is_mismatch * association_strength +
  423. (is_mismatch + association_strength | subjectId),
  424. data = .)
  425. }
  426. if (isSingular(mod) | any(grepl("failed to converge", mod@optinfo$conv$lme4$messages))) {
  427. mod <- mod.data %>%
  428. lmer(component_score ~ is_mismatch * association_strength +
  429. (1 | subjectId) +
  430. (1 | expId),
  431. data = .)
  432. }
  433. if (isSingular(mod) | any(grepl("failed to converge", mod@optinfo$conv$lme4$messages))) {
  434. mod <- mod.data %>%
  435. lmer(component_score ~ is_mismatch * association_strength +
  436. (1 | subjectId),
  437. data = .)
  438. }
  439. final.mod.formula <- formula(mod) [3]
  440. final.mod.formula <- paste("component_score ~", final.mod.formula)
  441. no.mismatch.formula <- gsub("is_mismatch \\* ", "", final.mod.formula)
  442. no.mismatch.formula <- gsub("is_mismatch \\+ ", "", no.mismatch.formula)
  443. mod.no.mismatch <- mod.data %>%
  444. lmer(as.formula(no.mismatch.formula),
  445. data = .)
  446. if (isSingular(mod.no.mismatch) | any(grepl("failed to converge", mod.no.mismatch@optinfo$conv$lme4$messages))) {
  447. mod.no.mismatch <- mod.data %>%
  448. lmer(component_score ~ association_strength +
  449. (association_strength | subjectId),
  450. data = .)
  451. }
  452. if (isSingular(mod.no.mismatch) | any(grepl("failed to converge", mod.no.mismatch@optinfo$conv$lme4$messages))) {
  453. mod.no.mismatch <- mod.data %>%
  454. lmer(component_score ~ association_strength +
  455. (1 | subjectId),
  456. data = .)
  457. }
  458. models <- c(models, mod)
  459. models.compact.no.mismatch <- c(models.compact.no.mismatch, mod.no.mismatch)
  460. }
  461. ```
  462. ```{r show_match_mismatch_acc_models, echo=F, eval=T, fig.width=6.5, fig.height=3, results="asis"}
  463. for (i in 1:ncomp) {
  464. fig1 <- pca.loadings %>%
  465. dplyr::filter(component == paste("PC", i, sep="")) %>%
  466. mutate(component = paste(component, ": ", round(percent_var_explained, 2),
  467. "%",
  468. sep = "")) %>%
  469. ggplot(aes(x = time,
  470. y = loading,
  471. color = component,
  472. fill = component)) +
  473. geom_vline(xintercept = c(0, 1, 2, 2.75),
  474. linetype = "dashed",
  475. color = "gray37") +
  476. geom_line(color="black", linewidth=1) +
  477. geom_text(aes(label = event,
  478. x = time,
  479. y = 0.75),
  480. data = ret_labels,
  481. inherit.aes = FALSE,
  482. angle = 90,
  483. hjust = 1,
  484. vjust = 1.2,
  485. size = 3,
  486. colour = "gray37",
  487. family = fontfamily) +
  488. facet_wrap(~component) +
  489. labs(x = "Time (s)",
  490. y = "PC loading") +
  491. scale_color_brewer(palette="Set2") +
  492. scale_fill_brewer(palette="Set2") +
  493. theme(legend.position = "none")
  494. fig2 <- subj.data.scores %>%
  495. dplyr::filter(component == paste("PC", i, sep="")) %>%
  496. group_by(subjectId, association_strength, trial_outcome, component) %>%
  497. summarize(component_score = mean(component_score)) %>%
  498. summarySEwithin(.,
  499. measurevar="component_score",
  500. withinvars=c("association_strength",
  501. "trial_outcome",
  502. "component"),
  503. idvar="subjectId") %>%
  504. mutate(across(trial_outcome,
  505. factor,
  506. levels=c("Hit", "CR", "Miss", "FA"))) %>%
  507. ggplot(aes(x = association_strength,
  508. y = component_score,
  509. fill = trial_outcome)) +
  510. stat_summary(fun = "mean",
  511. geom = "bar",
  512. color = "black",
  513. position=position_dodge()) +
  514. geom_errorbar(aes(ymin=component_score-se, ymax=component_score+se),
  515. position=position_dodge(.9),
  516. width=.2) +
  517. facet_wrap(~component, scales = "free") +
  518. scale_color_manual(values=c("#1a75ff", "#ff751a", "#80b3ff", "#ffb380"), name="") +
  519. scale_fill_manual(values=c("#1a75ff", "#ff751a", "#80b3ff", "#ffb380"), name="") +
  520. labs(x = "", y = "PC score")
  521. print(fig1 + fig2)
  522. mod <- models[[i]]
  523. mod.no.mismatch <- models.compact.no.mismatch[[i]]
  524. title <- formula(mod) [3]
  525. title <- gsub("is_mismatch", "Mismatch", title)
  526. title <- gsub("is_incorrect", "Accuracy", title)
  527. title <- gsub("association_strength", "Strength", title)
  528. title <- gsub(":", " x ", title)
  529. tidy(mod, effects = "fixed", conf.int = T, conf.level = 0.95) %>%
  530. mutate(CI = paste0("[",
  531. sprintf("%.03f", conf.low),
  532. ", ",
  533. sprintf("%.03f", conf.high),
  534. "]",
  535. sep="")) %>%
  536. select(term, estimate, CI, p.value, std.error, df) %>%
  537. rename("Estimate" = estimate,
  538. "p-value" = p.value,
  539. "SE" = std.error,
  540. "DF" = df
  541. ) %>%
  542. mutate(term = gsub("is_mismatch", "Mismatch", term),
  543. term = gsub("is_incorrect", "Accuracy", term),
  544. term = gsub("association_strengthWeak \\(1x\\)", "Strength", term),
  545. term = gsub(":", " x ", term)) %>%
  546. kable(caption=paste("PC", i, " score ~ ", title, sep=""),
  547. digits=3) %>%
  548. kable_styling() %>%
  549. cat()
  550. tidy(mod, effects = "ran_pars", scales = "vcov") %>%
  551. select(term, estimate) %>%
  552. mutate(term = gsub("var__", "Variance ", term),
  553. term = gsub("cov__", "Covariance ", term),
  554. term = gsub("\\(Intercept\\)", "intercept", term),
  555. term = gsub("is_mismatch", "Mismatch", term),
  556. term = gsub("is_incorrect", "Accuracy", term),
  557. term = gsub("association_strengthWeak \\(1x\\)", "Strength", term),
  558. term = gsub("\\.", ", ", term)) %>%
  559. kable(digits=3, caption="Random effects") %>%
  560. kable_styling() %>%
  561. cat()
  562. anova(mod.no.mismatch, mod, test="LRT") %>%
  563. tidy() %>%
  564. kable(caption=paste("mod.no.mismatch:",
  565. formula(mod.no.mismatch) [3])) %>%
  566. kable_styling() %>%
  567. cat()
  568. print(paste("No mismatch compact model failed to converge: ",
  569. isSingular(mod.no.mismatch) | any(grepl("failed to converge", mod.no.mismatch@optinfo$conv$lme4$messages))))
  570. emm <- emmeans(mod, ~ is_mismatch | association_strength, lmerTest.limit = 10346)
  571. print(emm)
  572. tidy(emm, effects = "fixed", conf.int = T, conf.level = 0.95) %>%
  573. kable(digits=3, caption=paste("PC", i, "; Emmeans", sep="")) %>%
  574. kable_styling() %>%
  575. cat()
  576. emm.pairs <- pairs(emm)
  577. print(emm.pairs)
  578. tidy(emm.pairs, effects = "fixed", conf.int = T, conf.level = 0.95) %>%
  579. kable(digits=3, caption=paste("PC", i, "; Pairwise comparisons", sep="")) %>%
  580. kable_styling() %>%
  581. cat()
  582. }
  583. ```
  584. ```{r full_model_all_pc, echo=F, eval=F}
  585. mod <- subj.data.scores %>%
  586. mutate(is_mismatch = if_else(trial_outcome %in% c("Hit", "Miss"),
  587. 0,
  588. 1)) %>%
  589. mutate(is_incorrect = if_else(trial_outcome %in% c("Hit", "CR"),
  590. 0,
  591. 1)) %>%
  592. lmer(component_score ~ component * is_mismatch * association_strength +
  593. (component + is_mismatch + association_strength | subjectId),
  594. data = .)
  595. mod %>%
  596. summary()
  597. tidy(mod, effects = "fixed", conf.int = T, conf.level = 0.95) %>%
  598. select(term, estimate, conf.low, conf.high, p.value, std.error, df) %>%
  599. kable(digits=3) %>%
  600. kable_styling()
  601. tidy(mod, effects = "ran_pars", scales = "vcov") %>%
  602. select(term, estimate) %>%
  603. kable(digits=3, caption="Random effects") %>%
  604. kable_styling()
  605. ```
  606. ## RT models
  607. ```{r get_rt_data_obj, echo=F}
  608. ### joint plot
  609. rt_data <- subj.data.scores %>%
  610. mutate_at(c('trial_outcome'), as.character) %>%
  611. mutate(trial_outcome=replace(trial_outcome, trial_outcome=="Hit", "Match")) %>%
  612. mutate(trial_outcome=replace(trial_outcome, trial_outcome=="CR", "Mismatch")) %>%
  613. dplyr::filter(trial_outcome %in% c("Match", "Mismatch"))
  614. ```
  615. ```{r match_mismatch_rt, echo=F, eval=run_models, fig.width=7, fig.height=10}
  616. models.rt <- c()
  617. for (pc in 1:ncomp) {
  618. mod <- rt_data %>%
  619. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  620. mutate(match_response.rt = log(match_response.rt)) %>%
  621. lmer(match_response.rt ~ component_score * association_strength * trial_outcome +
  622. (component_score * association_strength * trial_outcome | subjectId) +
  623. (1 | expId),
  624. data = .)
  625. if (isSingular(mod) | any(grepl("failed to converge", mod@optinfo$conv$lme4$messages))) {
  626. mod <- rt_data %>%
  627. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  628. mutate(match_response.rt = log(match_response.rt)) %>%
  629. lmer(match_response.rt ~ component_score * association_strength * trial_outcome +
  630. (component_score * trial_outcome * association_strength | subjectId),
  631. data = .)
  632. }
  633. if (isSingular(mod) | any(grepl("failed to converge", mod@optinfo$conv$lme4$messages))) {
  634. mod <- rt_data %>%
  635. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  636. mutate(match_response.rt = log(match_response.rt)) %>%
  637. lmer(match_response.rt ~ component_score * association_strength * trial_outcome +
  638. (component_score + trial_outcome + association_strength | subjectId) +
  639. (1 | expId),
  640. data = .)
  641. }
  642. if (isSingular(mod) | any(grepl("failed to converge", mod@optinfo$conv$lme4$messages))) {
  643. mod <- rt_data %>%
  644. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  645. mutate(match_response.rt = log(match_response.rt)) %>%
  646. lmer(match_response.rt ~ component_score * association_strength * trial_outcome +
  647. (component_score + trial_outcome + association_strength | subjectId),
  648. data = .)
  649. }
  650. if (isSingular(mod) | any(grepl("failed to converge", mod@optinfo$conv$lme4$messages))) {
  651. mod <- rt_data %>%
  652. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  653. mutate(match_response.rt = log(match_response.rt)) %>%
  654. lmer(match_response.rt ~ component_score * association_strength * trial_outcome +
  655. (component_score | subjectId) +
  656. (1 | expId),
  657. data = .)
  658. }
  659. if (isSingular(mod) | any(grepl("failed to converge", mod@optinfo$conv$lme4$messages))) {
  660. mod <- rt_data %>%
  661. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  662. mutate(match_response.rt = log(match_response.rt)) %>%
  663. lmer(match_response.rt ~ component_score * association_strength * trial_outcome +
  664. (1 | subjectId),
  665. data = .)
  666. }
  667. if (isSingular(mod) | any(grepl("failed to converge", mod@optinfo$conv$lme4$messages))) {
  668. print(paste("MODEL DID NOT CONVERGE FOR PC", pc))
  669. }
  670. models.rt <- c(models.rt, mod)
  671. }
  672. ```
  673. ```{r binned_rt, echo=F, fig.width=7, fig.height=10}
  674. nbins <- 4
  675. rt.binned.plt <- rt_data %>%
  676. group_by(subjectId, component, association_strength, trial_outcome) %>%
  677. mutate(trial_count = n()) %>%
  678. ungroup() %>%
  679. dplyr::filter(trial_count > nbins-1) %>%
  680. group_by(subjectId, component, association_strength, trial_outcome) %>%
  681. mutate(component_score_bin = cut_interval(component_score,
  682. n = nbins,
  683. labels = F)) %>%
  684. ungroup() %>%
  685. group_by(subjectId, trial_outcome, association_strength,
  686. component, component_score_bin) %>%
  687. summarize(rt = median(match_response.rt)) %>%
  688. summarySEwithin(.,
  689. measurevar="rt",
  690. withinvars=c("association_strength",
  691. "trial_outcome",
  692. "component",
  693. "component_score_bin"),
  694. idvar="subjectId") %>%
  695. ggplot(aes(x = as.numeric(as.character(component_score_bin)),
  696. y = rt,
  697. color = trial_outcome,
  698. group = trial_outcome)) +
  699. geom_line(linewidth=0.8) +
  700. geom_point(size=1.5) +
  701. geom_errorbar(aes(ymin=rt-se, ymax=rt+se),
  702. width=0.2) +
  703. facet_grid(rows=vars(component),
  704. cols=vars(association_strength),
  705. axes="all_x",
  706. scales = "free") +
  707. scale_color_manual(values=c("#1a75ff", "#ff751a"), name="") +
  708. scale_fill_manual(values=c("#1a75ff", "#ff751a"), name="") +
  709. labs(x = "PC score quartile", y = "RT (s)") +
  710. theme(strip.text.y = element_blank())
  711. loadings.plt + rt.binned.plt +
  712. plot_layout(widths=c(1,2))
  713. ```
  714. ```{r show_mismatch_rt_models, echo=F, fig.width=6.5, fig.height=3, results="asis"}
  715. for (pc in 1:ncomp) {
  716. fig1 <- pca.loadings %>%
  717. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  718. mutate(component = paste(component, ": ", round(percent_var_explained, 2),
  719. "%",
  720. sep = "")) %>%
  721. ggplot(aes(x = time,
  722. y = loading,
  723. color = component,
  724. fill = component)) +
  725. geom_vline(xintercept = c(0, 1, 2, 2.75),
  726. linetype = "dashed",
  727. color = "gray37") +
  728. geom_line(color="black", linewidth=1) +
  729. geom_text(aes(label = event,
  730. x = time,
  731. y = 0.75),
  732. data = ret_labels,
  733. inherit.aes = FALSE,
  734. angle = 90,
  735. hjust = 1,
  736. vjust = 1.2,
  737. size = 3,
  738. colour = "gray37",
  739. family = fontfamily) +
  740. facet_wrap(~component) +
  741. labs(x = "Time (s)",
  742. y = "PC loading") +
  743. scale_color_brewer(palette="Set2") +
  744. scale_fill_brewer(palette="Set2") +
  745. theme(legend.position = "none")
  746. mod <- models.rt[[pc]]
  747. fig2 <- mod %>%
  748. plot_model(type="pred",
  749. terms=c("component_score[all]",
  750. "trial_outcome[all]",
  751. "association_strength[Strong (4x), Weak (1x)]")) +
  752. scale_color_manual(values=c("#1a75ff", "#ff751a"), name="") +
  753. scale_fill_manual(values=c("#1a75ff", "#ff751a"), name="") +
  754. labs(y = "RT (s)") +
  755. theme(plot.title=element_blank(),
  756. axis.title.x=element_blank())
  757. print(fig1 + fig2)
  758. title <- formula(mod)[3]
  759. title <- gsub("trial_outcome", "Mismatch", title)
  760. title <- gsub("component_score",
  761. paste("PC", pc, " score", sep=""),
  762. title)
  763. title <- gsub("association_strength", "Strength", title)
  764. title <- gsub(":", " x ", title)
  765. tidy(mod, effects = "fixed", conf.int = T, conf.level = 0.95) %>%
  766. mutate(CI = paste0("[",
  767. sprintf("%.03f", conf.low),
  768. ", ",
  769. sprintf("%.03f", conf.high),
  770. "]",
  771. sep="")) %>%
  772. select(term, estimate, CI, p.value, std.error, df) %>%
  773. rename("Estimate" = estimate,
  774. "p-value" = p.value,
  775. "SE" = std.error,
  776. "DF" = df
  777. ) %>%
  778. mutate(term = gsub("trial_outcomeMismatch", "Mismatch", term),
  779. term = gsub("component_score",
  780. paste("PC", pc, " score", sep=""),
  781. term),
  782. term = gsub("association_strengthWeak \\(1x\\)", "Strength", term),
  783. term = gsub(":", " x ", term)) %>%
  784. kable(caption=paste("RT ~ ", title, sep=""),
  785. digits=3) %>%
  786. kable_styling() %>%
  787. cat()
  788. tidy(mod, effects = "ran_pars", scales = "vcov") %>%
  789. select(term, estimate) %>%
  790. mutate(term = gsub("var__", "Variance ", term),
  791. term = gsub("cov__", "Covariance ", term),
  792. term = gsub("\\(Intercept\\)", "intercept", term),
  793. term = gsub("is_mismatch", "Mismatch", term),
  794. term = gsub("is_incorrect", "Accuracy", term),
  795. term = gsub("association_strengthWeak \\(1x\\)", "Strength", term),
  796. term = gsub("\\.", ", ", term)) %>%
  797. kable(digits=3, caption="Random effects") %>%
  798. kable_styling() %>%
  799. cat()
  800. slopes <- emtrends(mod, pairwise ~ association_strength * trial_outcome, var = "component_score", lmerTest.limit = 10346)
  801. print(slopes)
  802. tidy(slopes$emtrends, conf.int = T, conf.level = 0.95) %>%
  803. kable(caption=paste("slopes: RT ~ ", title, sep=""),
  804. digits=3) %>%
  805. kable_styling() %>%
  806. cat()
  807. tidy(slopes$contrasts, conf.int = T, conf.level = 0.95) %>%
  808. select(contrast, estimate, std.error, df, conf.low, conf.high, statistic, adj.p.value) %>%
  809. kable(caption=paste("slope contrasts: RT ~ ", title, sep=""),
  810. digits=3) %>%
  811. kable_styling() %>%
  812. cat()
  813. }
  814. ```
  815. ## Individual PC plots
  816. ```{r indivi_pc_plots, fig.width=6, fig.height=2, echo=F}
  817. showtext_opts(dpi = 300)
  818. for (pc in 1:ncomp) {
  819. fig1 <- pca.loadings %>%
  820. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  821. mutate(component = paste(component, ": ", round(percent_var_explained, 2),
  822. "%",
  823. sep = "")) %>%
  824. ggplot(aes(x = time,
  825. y = loading,
  826. color = component,
  827. fill = component)) +
  828. geom_vline(xintercept = c(0, 1, 2, 2.75),
  829. linetype = "dashed",
  830. color = "gray37") +
  831. geom_line(color="black", linewidth=1) +
  832. geom_text(aes(label = event,
  833. x = time,
  834. y = 0.75),
  835. data = ret_labels,
  836. inherit.aes = FALSE,
  837. angle = 90,
  838. hjust = 1,
  839. vjust = 1.2,
  840. size = 3,
  841. colour = "gray37",
  842. family = fontfamily) +
  843. facet_wrap(~component) +
  844. ylim(-0.25, 1) +
  845. labs(x = "Time (s)",
  846. y = "PC loading") +
  847. scale_color_brewer(palette="Set2") +
  848. scale_fill_brewer(palette="Set2") +
  849. theme(legend.position = "none")
  850. fig2 <- subj.data.scores %>%
  851. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  852. mutate(trial_outcome = ifelse(as.character(trial_outcome) %in% c("Hit"),
  853. "Match", "Mismatch")) %>%
  854. group_by(subjectId, association_strength, trial_outcome, component) %>%
  855. summarize(component_score = mean(component_score)) %>%
  856. summarySEwithin(.,
  857. measurevar="component_score",
  858. withinvars=c("association_strength",
  859. "trial_outcome",
  860. "component"),
  861. idvar="subjectId") %>%
  862. ggplot(aes(x = exp_id,
  863. y = component_score,
  864. fill = trial_outcome)) +
  865. stat_summary(fun = "mean",
  866. geom = "bar",
  867. color = "black",
  868. position=position_dodge()) +
  869. geom_errorbar(aes(ymin=component_score-se, ymax=component_score+se),
  870. position=position_dodge(.9),
  871. width=.2) +
  872. facet_grid(cols=vars(association_strength),
  873. rows=vars(component)) +
  874. scale_color_manual(values=c("#1a75ff", "#ff751a"), name="") +
  875. scale_fill_manual(values=c("#1a75ff", "#ff751a"), name="") +
  876. labs(x = "", y = "PC score") +
  877. theme(axis.title.x=element_blank(),
  878. axis.text.x=element_blank(),
  879. axis.ticks.x=element_blank())
  880. fig3 <- rt_data %>%
  881. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  882. group_by(subjectId, component, association_strength, trial_outcome) %>%
  883. # exclude any subjects with # trials < # bins
  884. mutate(trial_count = n()) %>%
  885. ungroup() %>%
  886. dplyr::filter(trial_count > nbins-1) %>%
  887. group_by(subjectId, component, association_strength, trial_outcome) %>%
  888. mutate(component_score_bin = cut_interval(component_score,
  889. n = nbins,
  890. labels = F)) %>%
  891. ungroup() %>%
  892. group_by(subjectId, trial_outcome, association_strength,
  893. component, component_score_bin) %>%
  894. summarize(rt = median(match_response.rt)) %>%
  895. summarySEwithin(.,
  896. measurevar="rt",
  897. withinvars=c("association_strength",
  898. "trial_outcome",
  899. "component",
  900. "component_score_bin"),
  901. idvar="subjectId") %>%
  902. mutate(across(component,
  903. factor,
  904. levels=ordered_pcs)) %>%
  905. ggplot(aes(x = as.numeric(as.character(component_score_bin)),
  906. y = rt,
  907. color = trial_outcome,
  908. group = trial_outcome)) +
  909. geom_line(linewidth=0.8) +
  910. geom_point(size=1.5) +
  911. geom_errorbar(aes(ymin=rt-se, ymax=rt+se),
  912. width=0.2) +
  913. facet_grid(cols=vars(association_strength),
  914. rows=vars(component)) +
  915. scale_color_manual(values=c("#1a75ff", "#ff751a"), name="") +
  916. scale_fill_manual(values=c("#1a75ff", "#ff751a"), name="") +
  917. labs(x = "PC score quartile", y = "RT (s)") +
  918. guides(color = "none")
  919. joint.fig <- (fig1 + fig2 + fig3) +
  920. plot_layout(widths=c(1,1,1),
  921. guides="collect", axis_titles = "collect") &
  922. theme(strip.text.x = element_text(size=12),
  923. strip.text.y = element_blank(),
  924. strip.background = element_blank(),
  925. text = element_text(size = 10),
  926. )
  927. ggsave(paste("tPCA_figures_correct_only/PC", pc, "_scores_and_RT.pdf", sep=""),
  928. plot=joint.fig,
  929. width=22,
  930. height=7,
  931. dpi=300,
  932. create.dir=T,
  933. units="cm")
  934. }
  935. showtext_opts(op)
  936. ```
  937. ```{r other_version_of_pc_plots, fig.width=6, fig.height=2, echo=F}
  938. showtext_opts(dpi = 300)
  939. for (pc in 1:ncomp) {
  940. fig1 <- pca.loadings %>%
  941. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  942. ggplot(aes(x = time,
  943. y = loading,
  944. color = component,
  945. fill = component)) +
  946. geom_vline(xintercept = c(0, 1, 2, 2.75),
  947. linetype = "dashed",
  948. color = "gray37") +
  949. geom_line(color="black", linewidth=1) +
  950. geom_text(aes(label = event,
  951. x = time,
  952. y = 0.75),
  953. data = ret_labels,
  954. inherit.aes = FALSE,
  955. angle = 90,
  956. hjust = 1,
  957. vjust = 1.2,
  958. size = 3,
  959. colour = "gray37",
  960. family = fontfamily) +
  961. facet_wrap(~component) +
  962. ylim(-0.25, 1) +
  963. labs(x = "Time (s)",
  964. y = "PC loading") +
  965. scale_color_brewer(palette="Set2") +
  966. scale_fill_brewer(palette="Set2") +
  967. theme(legend.position = "none",
  968. strip.text.x = element_text(size=12),
  969. strip.background = element_blank())
  970. fig2 <- subj.data.scores %>%
  971. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  972. mutate(trial_outcome = ifelse(as.character(trial_outcome) %in% c("Hit"),
  973. "Match", "Mismatch")) %>%
  974. group_by(subjectId, association_strength, trial_outcome, component) %>%
  975. summarize(component_score = mean(component_score)) %>%
  976. summarySEwithin(.,
  977. measurevar="component_score",
  978. withinvars=c("association_strength",
  979. "trial_outcome",
  980. "component"),
  981. idvar="subjectId") %>%
  982. ggplot(aes(x = exp_id,
  983. y = component_score,
  984. fill = trial_outcome)) +
  985. stat_summary(fun = "mean",
  986. geom = "bar",
  987. color = "black",
  988. position=position_dodge()) +
  989. geom_errorbar(aes(ymin=component_score-se, ymax=component_score+se),
  990. position=position_dodge(.9),
  991. width=.2) +
  992. facet_grid(cols=vars(association_strength),
  993. rows=vars(component)) +
  994. scale_color_manual(values=c("#1a75ff", "#ff751a"), name="") +
  995. scale_fill_manual(values=c("#1a75ff", "#ff751a"), name="") +
  996. labs(x = "", y = "PC score") +
  997. theme(axis.title.x=element_blank(),
  998. axis.text.x=element_blank(),
  999. axis.ticks.x=element_blank())
  1000. fig3 <- rt_data %>%
  1001. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  1002. group_by(subjectId, component, association_strength, trial_outcome) %>%
  1003. # exclude any subjects with # trials < # bins
  1004. mutate(trial_count = n()) %>%
  1005. ungroup() %>%
  1006. dplyr::filter(trial_count > nbins-1) %>%
  1007. group_by(subjectId, component, association_strength, trial_outcome) %>%
  1008. mutate(component_score_bin = cut_interval(component_score,
  1009. n = nbins,
  1010. labels = F)) %>%
  1011. ungroup() %>%
  1012. group_by(subjectId, trial_outcome, association_strength,
  1013. component, component_score_bin) %>%
  1014. summarize(rt = median(match_response.rt)) %>%
  1015. summarySEwithin(.,
  1016. measurevar="rt",
  1017. withinvars=c("association_strength",
  1018. "trial_outcome",
  1019. "component",
  1020. "component_score_bin"),
  1021. idvar="subjectId") %>%
  1022. mutate(across(component,
  1023. factor,
  1024. levels=ordered_pcs)) %>%
  1025. ggplot(aes(x = as.numeric(as.character(component_score_bin)),
  1026. y = rt,
  1027. color = trial_outcome,
  1028. group = trial_outcome)) +
  1029. geom_line(linewidth=0.8) +
  1030. geom_point(size=1.5) +
  1031. geom_errorbar(aes(ymin=rt-se, ymax=rt+se),
  1032. width=0.2) +
  1033. facet_grid(cols=vars(association_strength),
  1034. rows=vars(component)) +
  1035. scale_color_manual(values=c("#1a75ff", "#ff751a"), name="") +
  1036. scale_fill_manual(values=c("#1a75ff", "#ff751a"), name="") +
  1037. labs(x = "PC score quartile", y = "RT (s)") +
  1038. guides(color = "none")
  1039. joint.fig <- (fig1 + fig2 + fig3) +
  1040. plot_layout(widths=c(1,1,1),
  1041. guides="collect", axis_titles = "collect") &
  1042. theme(strip.text.y = element_blank()
  1043. )
  1044. ggsave(paste("tPCA_figures_correct_only/PC", pc, "_scores_and_RT_framed.pdf", sep=""),
  1045. plot=joint.fig,
  1046. width=23,
  1047. height=7,
  1048. dpi=300,
  1049. create.dir=T,
  1050. units="cm")
  1051. }
  1052. showtext_opts(op)
  1053. ```
  1054. ```{r rainbow_indivi_pc_plots, fig.width=2, fig.height=6, echo=F}
  1055. showtext_opts(dpi = 300)
  1056. full.rainbow.fig <- list()
  1057. for (pc in 1:ncomp) {
  1058. ordered.rainbow <- c("#FFCC00", "#E82C42", "#59169C", "#1E8DD2", "#F46D43", "#29A343")
  1059. pc.color <- ordered.rainbow[[pc]]
  1060. fig1 <- pca.loadings %>%
  1061. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  1062. mutate(component = paste(component, ": ", round(percent_var_explained, 2),
  1063. "%",
  1064. sep = "")) %>%
  1065. ggplot(aes(x = time,
  1066. y = loading,
  1067. color = component,
  1068. fill = component)) +
  1069. geom_vline(xintercept = c(0, 1, 2, 2.75),
  1070. linetype = "dashed",
  1071. color = "gray37") +
  1072. geom_line(color=pc.color, linewidth=1) +
  1073. geom_text(aes(label = event,
  1074. x = time,
  1075. y = 0.75),
  1076. data = ret_labels,
  1077. inherit.aes = FALSE,
  1078. angle = 90,
  1079. hjust = 1,
  1080. vjust = 1.2,
  1081. size = 3,
  1082. colour = "gray37",
  1083. family = fontfamily) +
  1084. facet_wrap(~component) +
  1085. ylim(-0.25, 1) +
  1086. labs(x = "Time (s)",
  1087. y = "PC loading") +
  1088. scale_color_brewer(palette="Set2") +
  1089. scale_fill_brewer(palette="Set2") +
  1090. theme(legend.position = "none")
  1091. fig2 <- subj.data.scores %>%
  1092. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  1093. mutate(trial_outcome = ifelse(trial_outcome %in% c("Hit"),
  1094. "Match", "Mismatch")) %>%
  1095. group_by(subjectId, association_strength, trial_outcome, component) %>%
  1096. summarize(component_score = mean(component_score)) %>%
  1097. summarySEwithin(.,
  1098. measurevar="component_score",
  1099. withinvars=c("association_strength",
  1100. "trial_outcome",
  1101. "component"),
  1102. idvar="subjectId") %>%
  1103. ggplot(aes(x = exp_id,
  1104. y = component_score,
  1105. fill = trial_outcome)) +
  1106. stat_summary(fun = "mean",
  1107. geom = "bar",
  1108. color = "black",
  1109. position=position_dodge()) +
  1110. geom_errorbar(aes(ymin=component_score-se, ymax=component_score+se),
  1111. position=position_dodge(.9),
  1112. width=.2) +
  1113. facet_grid(cols=vars(association_strength),
  1114. rows=vars(component)) +
  1115. scale_color_manual(values=c("#1a75ff", "#ff751a"), name="") +
  1116. scale_fill_manual(values=c("#1a75ff", "#ff751a"), name="") +
  1117. labs(x = "Memory outcome", y = "PC score") +
  1118. theme(axis.text.x=element_blank())
  1119. fig3 <- rt_data %>%
  1120. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  1121. mutate(trial_outcome = ifelse(trial_outcome %in% c("Hit"),
  1122. "Match", "Mismatch")) %>%
  1123. group_by(subjectId, component, association_strength, trial_outcome) %>%
  1124. # exclude any subjects with # trials < # bins
  1125. mutate(trial_count = n()) %>%
  1126. ungroup() %>%
  1127. dplyr::filter(trial_count > nbins-1) %>%
  1128. group_by(subjectId, component, association_strength, trial_outcome) %>%
  1129. mutate(component_score_bin = cut_interval(component_score,
  1130. n = nbins,
  1131. labels = F)) %>%
  1132. ungroup() %>%
  1133. group_by(subjectId, trial_outcome, association_strength,
  1134. component, component_score_bin) %>%
  1135. summarize(rt = median(match_response.rt)) %>%
  1136. summarySEwithin(.,
  1137. measurevar="rt",
  1138. withinvars=c("association_strength",
  1139. "trial_outcome",
  1140. "component",
  1141. "component_score_bin"),
  1142. idvar="subjectId") %>%
  1143. mutate(across(component,
  1144. factor,
  1145. levels=ordered_pcs)) %>%
  1146. ggplot(aes(x = as.numeric(as.character(component_score_bin)),
  1147. y = rt,
  1148. color = trial_outcome,
  1149. group = trial_outcome)) +
  1150. geom_line(linewidth=0.8) +
  1151. geom_point(size=1.5) +
  1152. geom_errorbar(aes(ymin=rt-se, ymax=rt+se),
  1153. width=0.2) +
  1154. facet_grid(cols=vars(association_strength),
  1155. rows=vars(component)) +
  1156. scale_color_manual(values=c("#1a75ff", "#ff751a"), name="") +
  1157. scale_fill_manual(values=c("#1a75ff", "#ff751a"), name="") +
  1158. labs(x = "PC score quartile", y = "RT (s)") +
  1159. guides(color = "none")
  1160. full.rainbow.fig[[pc]] <- fig1 / fig2 / fig3
  1161. }
  1162. rainbow.fig <- (full.rainbow.fig[[2]] |
  1163. full.rainbow.fig[[5]] |
  1164. full.rainbow.fig[[1]] |
  1165. full.rainbow.fig[[6]] |
  1166. full.rainbow.fig[[4]] |
  1167. full.rainbow.fig[[3]]) +
  1168. plot_layout(
  1169. guides = "collect", axes = "collect", axis_titles = "collect") &
  1170. theme(strip.text.x = element_text(size=12),
  1171. strip.text.y = element_blank(),
  1172. strip.background = element_blank(),
  1173. text = element_text(size = 10),
  1174. legend.position = "left"
  1175. )
  1176. ggsave(paste("tPCA_figures_correct_only/rainbow_all_PC_scores_and_RT.pdf", sep=""),
  1177. plot=rainbow.fig,
  1178. width=40,
  1179. height=18,
  1180. dpi=300,
  1181. create.dir=T,
  1182. units="cm")
  1183. showtext_opts(op)
  1184. ```
  1185. ```{r rainbow_with_titles_indivi_pc_plots, fig.width=2, fig.height=6, echo=F}
  1186. showtext_opts(dpi = 300)
  1187. titles <- list("PC1"="'Memory reinstatement strength'",
  1188. "PC2"="'Preparatory attention'",
  1189. "PC3"="'Prediction error x strength'",
  1190. "PC4"="'Orienting response to errors'",
  1191. "PC5"="'Cognitive effort'",
  1192. "PC6"="'Mismatch detection'")
  1193. full.rainbow.fig <- list()
  1194. for (pc in 1:ncomp) {
  1195. ordered.rainbow <- c("#FFCC00", "#E82C42", "#59169C", "#1E8DD2", "#F46D43", "#29A343")
  1196. pc.color <- ordered.rainbow[[pc]]
  1197. fig1 <- pca.loadings %>%
  1198. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  1199. mutate(component = paste(component, ": ", round(percent_var_explained, 2),
  1200. "%",
  1201. sep = "")) %>%
  1202. ggplot(aes(x = time,
  1203. y = loading,
  1204. color = component,
  1205. fill = component)) +
  1206. geom_vline(xintercept = c(0, 1, 2, 2.75),
  1207. linetype = "dashed",
  1208. color = "gray37") +
  1209. geom_line(color=pc.color, linewidth=1) +
  1210. geom_text(aes(label = event,
  1211. x = time,
  1212. y = 0.75),
  1213. data = ret_labels,
  1214. inherit.aes = FALSE,
  1215. angle = 90,
  1216. hjust = 1,
  1217. vjust = 1.2,
  1218. size = 3,
  1219. colour = "gray37",
  1220. family = fontfamily) +
  1221. facet_wrap(~component) +
  1222. ylim(-0.25, 1) +
  1223. labs(x = "Time (s)",
  1224. y = "PC loading",
  1225. title = titles[[pc]]) +
  1226. theme(legend.position = "none",
  1227. plot.title=element_text(hjust=0),
  1228. plot.subtitle=element_text(size=12))
  1229. fig2 <- subj.data.scores %>%
  1230. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  1231. mutate(across(trial_outcome,
  1232. factor,
  1233. levels=c("Hit", "Miss", "CR", "FA"))) %>%
  1234. group_by(subjectId, association_strength, trial_outcome, component) %>%
  1235. summarize(component_score = mean(component_score)) %>%
  1236. summarySEwithin(.,
  1237. measurevar="component_score",
  1238. withinvars=c("association_strength",
  1239. "trial_outcome",
  1240. "component"),
  1241. idvar="subjectId") %>%
  1242. ggplot(aes(x = exp_id,
  1243. y = component_score,
  1244. fill = trial_outcome)) +
  1245. stat_summary(fun = "mean",
  1246. geom = "bar",
  1247. color = "black",
  1248. position=position_dodge()) +
  1249. geom_errorbar(aes(ymin=component_score-se, ymax=component_score+se),
  1250. position=position_dodge(.9),
  1251. width=.2) +
  1252. facet_grid(cols=vars(association_strength),
  1253. rows=vars(component)) +
  1254. scale_color_manual(values=c("#1a75ff", "#ff751a"), name="") +
  1255. scale_fill_manual(values=c("#1a75ff", "#ff751a"), name="") +
  1256. labs(x = "Memory outcome", y = "PC score") +
  1257. theme(axis.text.x=element_blank())
  1258. fig3 <- rt_data %>%
  1259. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  1260. group_by(subjectId, component, association_strength, trial_outcome) %>%
  1261. # exclude any subjects with # trials < # bins
  1262. mutate(trial_count = n()) %>%
  1263. ungroup() %>%
  1264. dplyr::filter(trial_count > nbins-1) %>%
  1265. group_by(subjectId, component, association_strength, trial_outcome) %>%
  1266. mutate(component_score_bin = cut_interval(component_score,
  1267. n = nbins,
  1268. labels = F)) %>%
  1269. ungroup() %>%
  1270. group_by(subjectId, trial_outcome, association_strength,
  1271. component, component_score_bin) %>%
  1272. summarize(rt = median(match_response.rt)) %>%
  1273. summarySEwithin(.,
  1274. measurevar="rt",
  1275. withinvars=c("association_strength",
  1276. "trial_outcome",
  1277. "component",
  1278. "component_score_bin"),
  1279. idvar="subjectId") %>%
  1280. mutate(across(component,
  1281. factor,
  1282. levels=ordered_pcs)) %>%
  1283. ggplot(aes(x = as.numeric(as.character(component_score_bin)),
  1284. y = rt,
  1285. color = trial_outcome,
  1286. group = trial_outcome)) +
  1287. geom_line(linewidth=0.8) +
  1288. geom_point(size=1.5) +
  1289. geom_errorbar(aes(ymin=rt-se, ymax=rt+se),
  1290. width=0.2) +
  1291. facet_grid(cols=vars(association_strength),
  1292. rows=vars(component)) +
  1293. scale_color_manual(values=c("#1a75ff", "#ff751a"), name="") +
  1294. scale_fill_manual(values=c("#1a75ff", "#ff751a"), name="") +
  1295. labs(x = "PC score quartile", y = "RT (s)") +
  1296. guides(color = "none")
  1297. full.rainbow.fig[[pc]] <- fig1 / fig2 / fig3
  1298. }
  1299. rainbow.fig <- (full.rainbow.fig[[2]] |
  1300. full.rainbow.fig[[5]] |
  1301. full.rainbow.fig[[1]] |
  1302. full.rainbow.fig[[6]] |
  1303. full.rainbow.fig[[3]] |
  1304. full.rainbow.fig[[4]]) +
  1305. plot_layout(guides="collect",
  1306. tag_level = 'new') +
  1307. plot_annotation(tag_levels = list(c("A", "", "",
  1308. "B", "", "",
  1309. "C", "", "",
  1310. "D", "", "",
  1311. "E", "", "",
  1312. "F", "", ""))) &
  1313. theme(strip.text.x = element_text(size=12),
  1314. strip.text.y = element_blank(),
  1315. strip.background = element_blank(),
  1316. text = element_text(size = 10),
  1317. plot.margin = margin(0, 10, 0, 10),
  1318. plot.title.position = "plot",
  1319. plot.subtitle=element_text(hjust=0, margin=margin(t=5)),
  1320. plot.tag = element_text(face="bold", margin=margin(t=5, b=5)),
  1321. axis.title.y = element_text(margin = margin(r=0)),
  1322. axis.text.y = element_text(margin = margin(l=3, r=3)))
  1323. ggsave(paste("tPCA_figures_correct_only/rainbow_all_PC_scores_and_RT_titled.pdf", sep=""),
  1324. plot=rainbow.fig,
  1325. width=46,
  1326. height=16,
  1327. dpi=300,
  1328. create.dir=T,
  1329. units="cm")
  1330. showtext_opts(op)
  1331. ```
  1332. ```{r custom_indivi_pc_plots, fig.width=6, fig.height=2, echo=F}
  1333. indiv_pc_plots <- list()
  1334. showtext_opts(dpi = 300)
  1335. for (pc in 1:ncomp) {
  1336. fig1 <- pca.loadings %>%
  1337. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  1338. mutate(component = paste(component, ": ", round(percent_var_explained, 2),
  1339. "%",
  1340. sep = "")) %>%
  1341. ggplot(aes(x = time,
  1342. y = loading,
  1343. color = component,
  1344. fill = component)) +
  1345. geom_vline(xintercept = c(0, 1, 2, 2.75),
  1346. linetype = "dashed",
  1347. color = "gray37") +
  1348. geom_line(color="black", linewidth=1) +
  1349. geom_text(aes(label = event,
  1350. x = time,
  1351. y = 0.75),
  1352. data = ret_labels,
  1353. inherit.aes = FALSE,
  1354. angle = 90,
  1355. hjust = 1,
  1356. vjust = 1.2,
  1357. size = 4,
  1358. colour = "gray37",
  1359. family = fontfamily) +
  1360. facet_wrap(~component) +
  1361. ylim(-0.25, 1) +
  1362. labs(x = "Time (s)",
  1363. y = "PC loading",
  1364. subtitle = titles[[paste("PC", pc, sep="")]]) +
  1365. scale_color_brewer(palette="Set2") +
  1366. scale_fill_brewer(palette="Set2") +
  1367. theme(legend.position = "none",
  1368. plot.title=element_text(hjust=-0.1),
  1369. plot.subtitle=element_text(size=12))
  1370. fig2 <- subj.data.scores %>%
  1371. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  1372. mutate(across(trial_outcome,
  1373. factor,
  1374. levels=c("Hit", "Miss", "CR", "FA"))) %>%
  1375. group_by(subjectId, association_strength, trial_outcome, component) %>%
  1376. summarize(component_score = mean(component_score)) %>%
  1377. summarySEwithin(.,
  1378. measurevar="component_score",
  1379. withinvars=c("association_strength",
  1380. "trial_outcome",
  1381. "component"),
  1382. idvar="subjectId") %>%
  1383. ggplot(aes(x = exp_id,
  1384. y = component_score,
  1385. fill = trial_outcome)) +
  1386. stat_summary(fun = "mean",
  1387. geom = "bar",
  1388. color = "black",
  1389. position=position_dodge()) +
  1390. geom_errorbar(aes(ymin=component_score-se, ymax=component_score+se),
  1391. position=position_dodge(.9),
  1392. width=.2) +
  1393. facet_grid(cols=vars(association_strength),
  1394. rows=vars(component)) +
  1395. scale_color_manual(values=c("#1a75ff", "#ff751a"), name="") +
  1396. scale_fill_manual(values=c("#1a75ff", "#ff751a"), name="") +
  1397. labs(x = "", y = "PC score") +
  1398. theme(axis.title.x=element_blank(),
  1399. axis.text.x=element_blank(),
  1400. axis.ticks.x=element_blank())
  1401. fig3 <- rt_data %>%
  1402. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  1403. group_by(subjectId, component, association_strength, trial_outcome) %>%
  1404. # exclude any subjects with # trials < # bins
  1405. mutate(trial_count = n()) %>%
  1406. ungroup() %>%
  1407. dplyr::filter(trial_count > nbins-1) %>%
  1408. group_by(subjectId, component, association_strength, trial_outcome) %>%
  1409. mutate(component_score_bin = cut_interval(component_score,
  1410. n = nbins,
  1411. labels = F)) %>%
  1412. ungroup() %>%
  1413. group_by(subjectId, trial_outcome, association_strength,
  1414. component, component_score_bin) %>%
  1415. summarize(rt = median(match_response.rt)) %>%
  1416. summarySEwithin(.,
  1417. measurevar="rt",
  1418. withinvars=c("association_strength",
  1419. "trial_outcome",
  1420. "component",
  1421. "component_score_bin"),
  1422. idvar="subjectId") %>%
  1423. mutate(across(component,
  1424. factor,
  1425. levels=ordered_pcs)) %>%
  1426. ggplot(aes(x = as.numeric(as.character(component_score_bin)),
  1427. y = rt,
  1428. color = trial_outcome,
  1429. group = trial_outcome)) +
  1430. geom_line(linewidth=0.8) +
  1431. geom_point(size=1.5) +
  1432. geom_errorbar(aes(ymin=rt-se, ymax=rt+se),
  1433. width=0.2) +
  1434. facet_grid(cols=vars(association_strength),
  1435. rows=vars(component)) +
  1436. scale_color_manual(values=c("#1a75ff", "#ff751a"), name="") +
  1437. scale_fill_manual(values=c("#1a75ff", "#ff751a"), name="") +
  1438. labs(x = "PC score quartile", y = "RT (s)") +
  1439. guides(color = "none")
  1440. joint.fig <- (fig1 + fig2 + fig3) +
  1441. plot_layout(widths=c(1,1,1),
  1442. guides="collect", axes = "collect", axis_titles = "collect") &
  1443. theme(strip.text.x = element_text(size=12),
  1444. strip.text.y = element_blank(),
  1445. strip.background = element_blank(),
  1446. text = element_text(size = 10),
  1447. plot.title=element_text(hjust=0.5)
  1448. )
  1449. indiv_pc_plots[[paste("PC", pc, sep="")]] <- fig1 + fig2 + fig3
  1450. # ggsave(paste("tPCA_figures_correct_only/PC", pc, "_scores_and_RT.pdf", sep=""),
  1451. # plot=joint.fig,
  1452. # width=22,
  1453. # height=7,
  1454. # dpi=300,
  1455. # units="cm")
  1456. }
  1457. plot.full <- (indiv_pc_plots[["PC2"]] /
  1458. indiv_pc_plots[["PC5"]] /
  1459. indiv_pc_plots[["PC1"]] /
  1460. indiv_pc_plots[["PC6"]] /
  1461. indiv_pc_plots[["PC3"]] /
  1462. indiv_pc_plots[["PC4"]]) +
  1463. plot_layout(guides="collect",
  1464. tag_level = 'new') +
  1465. plot_annotation(tag_levels = list(c("A", "", "",
  1466. "B", "", "",
  1467. "C", "", "",
  1468. "D", "", "",
  1469. "E", "", "",
  1470. "F", "", ""))) &
  1471. theme(strip.text.x = element_text(size=12),
  1472. strip.text.y = element_blank(),
  1473. strip.background = element_blank(),
  1474. text = element_text(size = 10),
  1475. plot.margin = margin(0, 0, 0, 5),
  1476. plot.title.position = "plot",
  1477. plot.subtitle=element_text(hjust=0, margin=margin(t=5)),
  1478. plot.tag = element_text(face="bold", margin=margin(t=0)),
  1479. axis.title.y = element_text(margin = margin(r=0)),
  1480. axis.text.y = element_text(margin = margin(l=3, r=3)))
  1481. ggsave(paste("tPCA_figures_correct_only/full_PC_plot.pdf", sep=""),
  1482. plot=plot.full,
  1483. width=9,
  1484. height=16,
  1485. dpi=300,
  1486. create.dir=T,
  1487. units="in")
  1488. showtext_opts(op)
  1489. ```
  1490. ## Individual PC plots: score by RT
  1491. ```{r rainbow_indivi_pc_plots_yx, fig.width=2, fig.height=6, echo=F}
  1492. showtext_opts(dpi = 300)
  1493. full.rainbow.fig <- list()
  1494. for (pc in 1:ncomp) {
  1495. ordered.rainbow <- c("#FFCC00", "#E82C42", "#59169C", "#1E8DD2", "#F46D43", "#29A343")
  1496. pc.color <- ordered.rainbow[[pc]]
  1497. fig1 <- pca.loadings %>%
  1498. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  1499. mutate(component = paste(component, ": ", round(percent_var_explained, 2),
  1500. "%",
  1501. sep = "")) %>%
  1502. ggplot(aes(x = time,
  1503. y = loading,
  1504. color = component,
  1505. fill = component)) +
  1506. geom_vline(xintercept = c(0, 1, 2, 2.75),
  1507. linetype = "dashed",
  1508. color = "gray37") +
  1509. geom_line(color=pc.color, linewidth=1) +
  1510. geom_text(aes(label = event,
  1511. x = time,
  1512. y = 0.75),
  1513. data = ret_labels,
  1514. inherit.aes = FALSE,
  1515. angle = 90,
  1516. hjust = 1,
  1517. vjust = 1.2,
  1518. size = 3,
  1519. colour = "gray37",
  1520. family = fontfamily) +
  1521. facet_wrap(~component) +
  1522. ylim(-0.25, 1) +
  1523. labs(x = "Time (s)",
  1524. y = "PC loading") +
  1525. scale_color_brewer(palette="Set2") +
  1526. scale_fill_brewer(palette="Set2") +
  1527. theme(legend.position = "none")
  1528. fig2 <- subj.data.scores %>%
  1529. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  1530. mutate(across(trial_outcome,
  1531. factor,
  1532. levels=c("Hit", "Miss", "CR", "FA"))) %>%
  1533. group_by(subjectId, association_strength, trial_outcome, component) %>%
  1534. summarize(component_score = mean(component_score)) %>%
  1535. summarySEwithin(.,
  1536. measurevar="component_score",
  1537. withinvars=c("association_strength",
  1538. "trial_outcome",
  1539. "component"),
  1540. idvar="subjectId") %>%
  1541. ggplot(aes(x = exp_id,
  1542. y = component_score,
  1543. fill = trial_outcome)) +
  1544. stat_summary(fun = "mean",
  1545. geom = "bar",
  1546. color = "black",
  1547. position=position_dodge()) +
  1548. geom_errorbar(aes(ymin=component_score-se, ymax=component_score+se),
  1549. position=position_dodge(.9),
  1550. width=.2) +
  1551. facet_grid(cols=vars(association_strength),
  1552. rows=vars(component)) +
  1553. scale_color_manual(values=c("#1a75ff", "#ff751a"), name="") +
  1554. scale_fill_manual(values=c("#1a75ff", "#ff751a"), name="") +
  1555. labs(x = "Memory outcome", y = "PC score") +
  1556. theme(axis.text.x=element_blank())
  1557. fig3 <- rt_data %>%
  1558. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  1559. mutate(rt = round(match_response.rt, 1)) %>%
  1560. group_by(association_strength) %>%
  1561. mutate(rt_bin = cut_interval(rt,
  1562. n = nbins)) %>%
  1563. ungroup() %>%
  1564. group_by(subjectId, trial_outcome, association_strength,
  1565. component, rt_bin) %>%
  1566. summarize(component_score = mean(component_score)) %>%
  1567. summarySEwithin(.,
  1568. measurevar="component_score",
  1569. withinvars=c("association_strength",
  1570. "trial_outcome",
  1571. "component",
  1572. "rt_bin"),
  1573. idvar="subjectId") %>%
  1574. mutate(across(component,
  1575. factor,
  1576. levels=ordered_pcs)) %>%
  1577. ggplot(aes(x = rt_bin,
  1578. y = component_score,
  1579. color = trial_outcome,
  1580. group = trial_outcome)) +
  1581. geom_line(linewidth=0.8) +
  1582. geom_point(size=1.5) +
  1583. geom_errorbar(aes(ymin=component_score-se, ymax=component_score+se),
  1584. width=0.2) +
  1585. facet_grid(cols=vars(association_strength),
  1586. rows=vars(component)) +
  1587. scale_color_manual(values=c("#1a75ff", "#ff751a"), name="") +
  1588. scale_fill_manual(values=c("#1a75ff", "#ff751a"), name="") +
  1589. labs(y = "PC score", x = "RT bin") +
  1590. guides(color = "none") +
  1591. theme(axis.text.x = element_text(angle = 45, hjust = 1))
  1592. full.rainbow.fig[[pc]] <- fig1 / fig2 / fig3
  1593. }
  1594. rainbow.fig <- (full.rainbow.fig[[2]] |
  1595. full.rainbow.fig[[5]] |
  1596. full.rainbow.fig[[1]] |
  1597. full.rainbow.fig[[6]] |
  1598. full.rainbow.fig[[3]] |
  1599. full.rainbow.fig[[4]]) +
  1600. plot_layout(
  1601. guides = "collect", axes = "collect", axis_titles = "collect") &
  1602. theme(strip.text.x = element_text(size=12),
  1603. strip.text.y = element_blank(),
  1604. strip.background = element_blank(),
  1605. text = element_text(size = 10),
  1606. legend.position = "left"
  1607. )
  1608. ggsave(paste("tPCA_figures_correct_only/rainbow_all_PC_scores_and_RT_yx.pdf", sep=""),
  1609. plot=rainbow.fig,
  1610. width=40,
  1611. height=18,
  1612. dpi=300,
  1613. create.dir=T,
  1614. units="cm")
  1615. showtext_opts(op)
  1616. ```
  1617. ```{r rainbow_with_titles_indivi_pc_plots_yx, fig.width=2, fig.height=6, echo=F}
  1618. showtext_opts(dpi = 300)
  1619. titles <- list("PC1"="'Memory reinstatement strength'",
  1620. "PC2"="'Preparatory attention'",
  1621. "PC3"="'Prediction error x strength'",
  1622. "PC4"="'Orienting response to errors'",
  1623. "PC5"="'Cognitive effort'",
  1624. "PC6"="'Mismatch detection'")
  1625. full.rainbow.fig <- list()
  1626. for (pc in 1:ncomp) {
  1627. ordered.rainbow <- c("#FFCC00", "#E82C42", "#59169C", "#1E8DD2", "#F46D43", "#29A343")
  1628. pc.color <- ordered.rainbow[[pc]]
  1629. fig1 <- pca.loadings %>%
  1630. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  1631. mutate(component = paste(component, ": ", round(percent_var_explained, 2),
  1632. "%",
  1633. sep = "")) %>%
  1634. ggplot(aes(x = time,
  1635. y = loading,
  1636. color = component,
  1637. fill = component)) +
  1638. geom_vline(xintercept = c(0, 1, 2, 2.75),
  1639. linetype = "dashed",
  1640. color = "gray37") +
  1641. geom_line(color=pc.color, linewidth=1) +
  1642. geom_text(aes(label = event,
  1643. x = time,
  1644. y = 0.75),
  1645. data = ret_labels,
  1646. inherit.aes = FALSE,
  1647. angle = 90,
  1648. hjust = 1,
  1649. vjust = 1.2,
  1650. size = 3,
  1651. colour = "gray37",
  1652. family = fontfamily) +
  1653. facet_wrap(~component) +
  1654. ylim(-0.25, 1) +
  1655. labs(x = "Time (s)",
  1656. y = "PC loading",
  1657. title = titles[[pc]]) +
  1658. theme(legend.position = "none",
  1659. plot.title=element_text(hjust=0),
  1660. plot.subtitle=element_text(size=12))
  1661. fig2 <- subj.data.scores %>%
  1662. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  1663. mutate(across(trial_outcome,
  1664. factor,
  1665. levels=c("Hit", "Miss", "CR", "FA"))) %>%
  1666. group_by(subjectId, association_strength, trial_outcome, component) %>%
  1667. summarize(component_score = mean(component_score)) %>%
  1668. summarySEwithin(.,
  1669. measurevar="component_score",
  1670. withinvars=c("association_strength",
  1671. "trial_outcome",
  1672. "component"),
  1673. idvar="subjectId") %>%
  1674. ggplot(aes(x = exp_id,
  1675. y = component_score,
  1676. fill = trial_outcome)) +
  1677. stat_summary(fun = "mean",
  1678. geom = "bar",
  1679. color = "black",
  1680. position=position_dodge()) +
  1681. geom_errorbar(aes(ymin=component_score-se, ymax=component_score+se),
  1682. position=position_dodge(.9),
  1683. width=.2) +
  1684. facet_grid(cols=vars(association_strength),
  1685. rows=vars(component)) +
  1686. scale_color_manual(values=c("#1a75ff", "#ff751a"), name="") +
  1687. scale_fill_manual(values=c("#1a75ff", "#ff751a"), name="") +
  1688. labs(x = "Memory outcome", y = "PC score") +
  1689. theme(axis.text.x=element_blank())
  1690. fig3 <- rt_data %>%
  1691. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  1692. mutate(rt = round(match_response.rt, 1)) %>%
  1693. group_by(association_strength) %>%
  1694. mutate(rt_bin = cut_interval(rt,
  1695. n = nbins)) %>%
  1696. ungroup() %>%
  1697. group_by(subjectId, trial_outcome, association_strength,
  1698. component, rt_bin) %>%
  1699. summarize(component_score = mean(component_score)) %>%
  1700. summarySEwithin(.,
  1701. measurevar="component_score",
  1702. withinvars=c("association_strength",
  1703. "trial_outcome",
  1704. "component",
  1705. "rt_bin"),
  1706. idvar="subjectId") %>%
  1707. mutate(across(component,
  1708. factor,
  1709. levels=ordered_pcs)) %>%
  1710. ggplot(aes(x = rt_bin,
  1711. y = component_score,
  1712. color = trial_outcome,
  1713. group = trial_outcome)) +
  1714. geom_line(linewidth=0.8) +
  1715. geom_point(size=1.5) +
  1716. geom_errorbar(aes(ymin=component_score-se, ymax=component_score+se),
  1717. width=0.2) +
  1718. facet_grid(cols=vars(association_strength),
  1719. rows=vars(component)) +
  1720. scale_color_manual(values=c("#1a75ff", "#ff751a"), name="") +
  1721. scale_fill_manual(values=c("#1a75ff", "#ff751a"), name="") +
  1722. labs(y = "PC score", x = "RT bin") +
  1723. guides(color = "none") +
  1724. theme(axis.text.x = element_text(angle = 45, hjust = 1))
  1725. full.rainbow.fig[[pc]] <- fig1 / fig2 / fig3
  1726. }
  1727. rainbow.fig <- (full.rainbow.fig[[2]] |
  1728. full.rainbow.fig[[5]] |
  1729. full.rainbow.fig[[1]] |
  1730. full.rainbow.fig[[6]] |
  1731. full.rainbow.fig[[3]] |
  1732. full.rainbow.fig[[4]]) +
  1733. plot_layout(guides="collect",
  1734. tag_level = 'new') +
  1735. plot_annotation(tag_levels = list(c("A", "", "",
  1736. "B", "", "",
  1737. "C", "", "",
  1738. "D", "", "",
  1739. "E", "", "",
  1740. "F", "", ""))) &
  1741. theme(strip.text.x = element_text(size=12),
  1742. strip.text.y = element_blank(),
  1743. strip.background = element_blank(),
  1744. text = element_text(size = 10),
  1745. plot.margin = margin(0, 10, 0, 10),
  1746. plot.title.position = "plot",
  1747. plot.subtitle=element_text(hjust=0, margin=margin(t=5)),
  1748. plot.tag = element_text(face="bold", margin=margin(t=5, b=5)),
  1749. axis.title.y = element_text(margin = margin(r=0)),
  1750. axis.text.y = element_text(margin = margin(l=3, r=3)))
  1751. ggsave(paste("tPCA_figures_correct_only/rainbow_all_PC_scores_and_RT_titled_yx.pdf", sep=""),
  1752. plot=rainbow.fig,
  1753. width=46,
  1754. height=16,
  1755. dpi=300,
  1756. create.dir=T,
  1757. units="cm")
  1758. showtext_opts(op)
  1759. ```
  1760. ```{r custom_indivi_pc_plots_yx, fig.width=6, fig.height=2, echo=F}
  1761. indiv_pc_plots <- list()
  1762. showtext_opts(dpi = 300)
  1763. for (pc in 1:ncomp) {
  1764. fig1 <- pca.loadings %>%
  1765. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  1766. mutate(component = paste(component, ": ", round(percent_var_explained, 2),
  1767. "%",
  1768. sep = "")) %>%
  1769. ggplot(aes(x = time,
  1770. y = loading,
  1771. color = component,
  1772. fill = component)) +
  1773. geom_vline(xintercept = c(0, 1, 2, 2.75),
  1774. linetype = "dashed",
  1775. color = "gray37") +
  1776. geom_line(color="black", linewidth=1) +
  1777. geom_text(aes(label = event,
  1778. x = time,
  1779. y = 0.75),
  1780. data = ret_labels,
  1781. inherit.aes = FALSE,
  1782. angle = 90,
  1783. hjust = 1,
  1784. vjust = 1.2,
  1785. size = 4,
  1786. colour = "gray37",
  1787. family = fontfamily) +
  1788. facet_wrap(~component) +
  1789. ylim(-0.25, 1) +
  1790. labs(x = "Time (s)",
  1791. y = "PC loading",
  1792. subtitle = titles[[paste("PC", pc, sep="")]]) +
  1793. scale_color_brewer(palette="Set2") +
  1794. scale_fill_brewer(palette="Set2") +
  1795. theme(legend.position = "none",
  1796. plot.title=element_text(hjust=-0.1),
  1797. plot.subtitle=element_text(size=12))
  1798. fig2 <- subj.data.scores %>%
  1799. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  1800. mutate(across(trial_outcome,
  1801. factor,
  1802. levels=c("Hit", "Miss", "CR", "FA"))) %>%
  1803. group_by(subjectId, association_strength, trial_outcome, component) %>%
  1804. summarize(component_score = mean(component_score)) %>%
  1805. summarySEwithin(.,
  1806. measurevar="component_score",
  1807. withinvars=c("association_strength",
  1808. "trial_outcome",
  1809. "component"),
  1810. idvar="subjectId") %>%
  1811. ggplot(aes(x = exp_id,
  1812. y = component_score,
  1813. fill = trial_outcome)) +
  1814. stat_summary(fun = "mean",
  1815. geom = "bar",
  1816. color = "black",
  1817. position=position_dodge()) +
  1818. geom_errorbar(aes(ymin=component_score-se, ymax=component_score+se),
  1819. position=position_dodge(.9),
  1820. width=.2) +
  1821. facet_grid(cols=vars(association_strength),
  1822. rows=vars(component)) +
  1823. scale_color_manual(values=c("#1a75ff", "#ff751a"), name="") +
  1824. scale_fill_manual(values=c("#1a75ff", "#ff751a"), name="") +
  1825. labs(x = "", y = "PC score") +
  1826. theme(axis.title.x=element_blank(),
  1827. axis.text.x=element_blank(),
  1828. axis.ticks.x=element_blank())
  1829. fig3 <- rt_data %>%
  1830. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  1831. mutate(rt = round(match_response.rt, 1)) %>%
  1832. group_by(association_strength) %>%
  1833. mutate(rt_bin = cut_interval(rt,
  1834. n = nbins)) %>%
  1835. ungroup() %>%
  1836. group_by(subjectId, trial_outcome, association_strength,
  1837. component, rt_bin) %>%
  1838. summarize(component_score = mean(component_score)) %>%
  1839. summarySEwithin(.,
  1840. measurevar="component_score",
  1841. withinvars=c("association_strength",
  1842. "trial_outcome",
  1843. "component",
  1844. "rt_bin"),
  1845. idvar="subjectId") %>%
  1846. mutate(across(component,
  1847. factor,
  1848. levels=ordered_pcs)) %>%
  1849. ggplot(aes(x = rt_bin,
  1850. y = component_score,
  1851. color = trial_outcome,
  1852. group = trial_outcome)) +
  1853. geom_line(linewidth=0.8) +
  1854. geom_point(size=1.5) +
  1855. geom_errorbar(aes(ymin=component_score-se, ymax=component_score+se),
  1856. width=0.2) +
  1857. facet_grid(cols=vars(association_strength),
  1858. rows=vars(component)) +
  1859. scale_color_manual(values=c("#1a75ff", "#ff751a"), name="") +
  1860. scale_fill_manual(values=c("#1a75ff", "#ff751a"), name="") +
  1861. labs(y = "PC score", x = "RT bin") +
  1862. guides(color = "none") +
  1863. theme(axis.text.x = element_text(angle = 45, hjust = 1))
  1864. joint.fig <- (fig1 + fig2 + fig3) +
  1865. plot_layout(widths=c(1,1,1),
  1866. guides="collect", axes = "collect", axis_titles = "collect") &
  1867. theme(strip.text.x = element_text(size=12),
  1868. strip.text.y = element_blank(),
  1869. strip.background = element_blank(),
  1870. text = element_text(size = 10),
  1871. plot.title=element_text(hjust=0.5)
  1872. )
  1873. indiv_pc_plots[[paste("PC", pc, sep="")]] <- fig1 + fig2 + fig3
  1874. # ggsave(paste("tPCA_figures_correct_only/PC", pc, "_scores_and_RT.pdf", sep=""),
  1875. # plot=joint.fig,
  1876. # width=22,
  1877. # height=7,
  1878. # dpi=300,
  1879. # units="cm")
  1880. }
  1881. plot.full <- (indiv_pc_plots[["PC2"]] /
  1882. indiv_pc_plots[["PC5"]] /
  1883. indiv_pc_plots[["PC1"]] /
  1884. indiv_pc_plots[["PC6"]] /
  1885. indiv_pc_plots[["PC3"]] /
  1886. indiv_pc_plots[["PC4"]]) +
  1887. plot_layout(guides="collect",
  1888. tag_level = 'new') +
  1889. plot_annotation(tag_levels = list(c("A", "", "",
  1890. "B", "", "",
  1891. "C", "", "",
  1892. "D", "", "",
  1893. "E", "", "",
  1894. "F", "", ""))) &
  1895. theme(strip.text.x = element_text(size=12),
  1896. strip.text.y = element_blank(),
  1897. strip.background = element_blank(),
  1898. text = element_text(size = 10),
  1899. plot.margin = margin(0, 0, 0, 5),
  1900. plot.title.position = "plot",
  1901. plot.subtitle=element_text(hjust=0, margin=margin(t=5)),
  1902. plot.tag = element_text(face="bold", margin=margin(t=0)),
  1903. axis.title.y = element_text(margin = margin(r=0)),
  1904. axis.text.y = element_text(margin = margin(l=3, r=3)))
  1905. ggsave(paste("tPCA_figures_correct_only/full_PC_plot_yx.pdf", sep=""),
  1906. plot=plot.full,
  1907. width=9,
  1908. height=16,
  1909. dpi=300,
  1910. create.dir=T,
  1911. units="in")
  1912. showtext_opts(op)
  1913. ```
  1914. ## Subsequent memory - CR trials
  1915. ```{r submem_hist, echo=F, eval=T}
  1916. palette <- c("#59735e", "#b7c8ba")
  1917. palette <- c("#008080", "#9fdfbf")
  1918. subs.exclude <- c("MPE514", "MPE527") # insufficient number of trials
  1919. subj.data.submem <- subj.data.scores %>%
  1920. dplyr::filter(!subjectId %in% subs.exclude) %>%
  1921. left_join(recognition_behavior,
  1922. by = c("subjectId", "expId", "img"),
  1923. suffix = c(".retrieval", ".recognition")) %>%
  1924. dplyr::filter(!is.na(trial_outcome.recognition)) %>%
  1925. dplyr::filter(trial_outcome.retrieval == "CR") %>%
  1926. mutate(across(trial_outcome.recognition,
  1927. factor,
  1928. levels=c("Hit", "Miss"))) %>%
  1929. mutate(trial_outcome.recognition = recode(trial_outcome.recognition,
  1930. "Hit" = 1,
  1931. "Miss" = 0)) %>%
  1932. group_by(subjectId, component) %>%
  1933. mutate(component_score = scale(component_score)) %>%
  1934. ungroup() %>%
  1935. mutate(expId = recode(expId,
  1936. "imm" = "Experiment 1 (same day)",
  1937. "del" = "Experiment 2 (48 hrs later)"))
  1938. ```
  1939. ### Models
  1940. ```{r recog_models, echo=F, results="asis", eval=run_models}
  1941. recog.glmer.models <- list()
  1942. for (i in 1:ncomp) {
  1943. mod.data <- subj.data.submem %>%
  1944. dplyr::filter(component == paste("PC", i, sep=""))
  1945. mod <- mod.data %>%
  1946. glmer(trial_outcome.recognition ~ component_score * association_strength * expId +
  1947. (component_score * association_strength | subjectId),
  1948. data = .)
  1949. if (isSingular(mod) | any(grepl("failed to converge", mod@optinfo$conv$lme4$messages))) {
  1950. mod <- mod.data %>%
  1951. glmer(trial_outcome.recognition ~ component_score * association_strength * expId +
  1952. (component_score + association_strength | subjectId),
  1953. family = "binomial",
  1954. data = .)
  1955. }
  1956. if (isSingular(mod) | any(grepl("failed to converge", mod@optinfo$conv$lme4$messages))) {
  1957. mod <- mod.data %>%
  1958. glmer(trial_outcome.recognition ~ component_score * association_strength * expId +
  1959. (component_score | subjectId),
  1960. family = "binomial",
  1961. data = .)
  1962. }
  1963. if (isSingular(mod) | any(grepl("failed to converge", mod@optinfo$conv$lme4$messages))) {
  1964. mod <- mod.data %>%
  1965. glmer(trial_outcome.recognition ~ component_score * association_strength * expId +
  1966. (1 | subjectId),
  1967. family = "binomial",
  1968. data = .)
  1969. }
  1970. if (isSingular(mod) | any(grepl("failed to converge", mod@optinfo$conv$lme4$messages))) {
  1971. print("MODEL DID NOT CONVERGE")
  1972. }
  1973. recog.glmer.models[[i]] <- mod
  1974. }
  1975. ```
  1976. ```{r show_recog_mods, echo=F, results="asis", fig.height=3, fig.width=6}
  1977. for (i in 1:ncomp) {
  1978. mod <- recog.glmer.models[[i]]
  1979. tidy(mod, effects = "fixed", conf.int = T, conf.level = 0.95, exponentiate=T) %>%
  1980. # select(term, estimate, conf.low, conf.high, p.value, std.error, df) %>%
  1981. kable(caption=paste("PC", i, ": ", formula(mod)[3], sep=""),
  1982. digits=3) %>%
  1983. kable_styling() %>%
  1984. cat()
  1985. tidy(mod, effects = "ran_pars", scales = "vcov") %>%
  1986. select(term, estimate) %>%
  1987. kable(digits=3, caption="Random effects") %>%
  1988. kable_styling() %>%
  1989. cat()
  1990. if (i == 1) {
  1991. slopes <- emtrends(mod, pairwise ~ association_strength * expId, var = "component_score")
  1992. print(slopes)
  1993. title <- formula(mod)[3]
  1994. tidy(slopes$emtrends, conf.int = T, conf.level = 0.95) %>%
  1995. kable(caption=paste("slopes: remember ~ ", title, sep=""),
  1996. digits=3) %>%
  1997. kable_styling() %>%
  1998. cat()
  1999. tidy(slopes$contrasts, conf.int = T, conf.level = 0.95) %>%
  2000. select(contrast, estimate, std.error, df, conf.low, conf.high, statistic, adj.p.value) %>%
  2001. kable(caption=paste("slope contrasts: remember ~ ", title, sep=""),
  2002. digits=3) %>%
  2003. kable_styling() %>%
  2004. cat()
  2005. }
  2006. plt <- mod %>%
  2007. plot_model(type="pred",
  2008. terms=c("component_score[all]",
  2009. "association_strength[Strong (4x), Weak (1x)]",
  2010. "expId"),
  2011. show.data=F) +
  2012. scale_color_manual(values=palette, name="") +
  2013. scale_fill_manual(values=palette, name="") +
  2014. scale_y_continuous(limits = c(0, 1)) +
  2015. labs(y = "P(remember probe)",
  2016. x = paste("PC", i, " score (z)", sep=""),
  2017. title = paste("PC", i, sep="")) +
  2018. theme(plot.subtitle = element_text(size = 12))
  2019. print(plt)
  2020. nbins <- 4
  2021. submem.summary <- subj.data.submem %>%
  2022. dplyr::filter(component == paste("PC", i, sep="")) %>%
  2023. group_by(subjectId, association_strength, expId) %>%
  2024. mutate(component_score_bin = cut(component_score,
  2025. breaks = nbins,
  2026. labels = F)) %>%
  2027. ungroup() %>%
  2028. group_by(subjectId, component_score_bin, association_strength, expId) %>%
  2029. summarize(component_score = mean(component_score),
  2030. prob.remember = mean(trial_outcome.recognition)) %>%
  2031. ungroup() %>%
  2032. group_by(component_score_bin, association_strength, expId) %>%
  2033. mutate(component_score_mean = mean(component_score)) %>%
  2034. ungroup() %>%
  2035. mutate(component_score_mean = as.numeric(component_score_mean)) %>%
  2036. summarySEwithin(.,
  2037. measurevar="prob.remember",
  2038. withinvars=c("association_strength",
  2039. "component_score_mean"),
  2040. betweenvars=c("expId"),
  2041. idvar="subjectId") %>%
  2042. mutate(component_score_mean = as.numeric(as.character(component_score_mean)))
  2043. pred <- data.frame(ggpredict(mod, terms = c("component_score[all]",
  2044. "association_strength[Strong (4x), Weak (1x)]",
  2045. "expId"))) %>%
  2046. rename(component_score_mean = x,
  2047. association_strength = group,
  2048. expId = facet)
  2049. plt <- ggplot() +
  2050. geom_ribbon(data = as.data.frame(pred),
  2051. aes(x = component_score_mean,
  2052. ymin = conf.low,
  2053. ymax = conf.high,
  2054. fill = association_strength),
  2055. alpha = 0.2, color = NA) +
  2056. geom_line(data = as.data.frame(pred),
  2057. aes(x = component_score_mean,
  2058. y = predicted,
  2059. color = association_strength),
  2060. linewidth = 1) +
  2061. geom_point(data = submem.summary,
  2062. aes(x = component_score_mean,
  2063. y = prob.remember,
  2064. color = association_strength),
  2065. size = 1.5) +
  2066. geom_errorbar(data = submem.summary,
  2067. aes(x = component_score_mean,
  2068. ymin = prob.remember - se,
  2069. ymax = prob.remember + se,
  2070. color = association_strength),
  2071. width = 0.2) +
  2072. facet_wrap(~ expId) +
  2073. scale_color_manual(values = palette, name = "") +
  2074. scale_fill_manual(values = palette, name = "") +
  2075. scale_y_continuous(limits = c(0, 1)) +
  2076. labs(y = "P(remember probe)",
  2077. x = paste("PC", i, " score (z)", sep=""),
  2078. title = paste("PC", i, sep = "")) +
  2079. theme(strip.text.x = element_text(size=12))
  2080. print(plt)
  2081. showtext_opts(dpi = 300)
  2082. ggsave(paste("submem_figures/submem_PC", i, ".pdf", sep=""),
  2083. plot=plt,
  2084. width=17,
  2085. height=8,
  2086. dpi=300,
  2087. create.dir=T,
  2088. units="cm")
  2089. showtext_opts(op)
  2090. # print(mod %>%
  2091. # plot_model(type="pred",
  2092. # pred.type = "re",
  2093. # terms=c("component_score",
  2094. # "subjectId",
  2095. # "expId",
  2096. # "association_strength"
  2097. # ),
  2098. # ci.lvl = NA,
  2099. # show.data=F,
  2100. # color = "gray",
  2101. # line.size = 0.2) +
  2102. # scale_y_continuous(limits = c(0, 1)) +
  2103. # theme(legend.position = "none"))
  2104. }
  2105. ```
  2106. ## Subsequent memory RT
  2107. ```{r rt_data_submem, echo=F}
  2108. ### joint plot
  2109. rt_data.submem <- subj.data.submem %>%
  2110. dplyr::filter(trial_outcome.recognition == 1) %>%
  2111. mutate(across(association_strength,
  2112. factor,
  2113. levels=c("Strong (4x)", "Weak (1x)")))
  2114. ```
  2115. ```{r submem_rt_joint_plot, echo=F, fig.width=10, fig.height=8, results="asis", eval=run_models}
  2116. models.recog.rt <- list()
  2117. for (pc in 1:ncomp) {
  2118. mod <- rt_data.submem %>%
  2119. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  2120. mutate(recognition_response.rt = log(recognition_response.rt)) %>%
  2121. lmer(recognition_response.rt ~ component_score * association_strength * expId +
  2122. (component_score * association_strength * expId | subjectId),
  2123. data = .)
  2124. if (isSingular(mod) | any(grepl("failed to converge", mod@optinfo$conv$lme4$messages))) {
  2125. mod <- rt_data.submem %>%
  2126. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  2127. mutate(recognition_response.rt = log(recognition_response.rt)) %>%
  2128. lmer(recognition_response.rt ~ component_score * association_strength * expId +
  2129. (component_score + association_strength + expId | subjectId),
  2130. data = .)
  2131. }
  2132. if (isSingular(mod) | any(grepl("failed to converge", mod@optinfo$conv$lme4$messages))) {
  2133. mod <- rt_data.submem %>%
  2134. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  2135. mutate(recognition_response.rt = log(recognition_response.rt)) %>%
  2136. lmer(recognition_response.rt ~ component_score * association_strength * expId +
  2137. (component_score | subjectId),
  2138. data = .)
  2139. }
  2140. if (isSingular(mod) | any(grepl("failed to converge", mod@optinfo$conv$lme4$messages))) {
  2141. mod <- rt_data.submem %>%
  2142. dplyr::filter(component == paste("PC", pc, sep="")) %>%
  2143. mutate(recognition_response.rt = log(recognition_response.rt)) %>%
  2144. lmer(recognition_response.rt ~ component_score * association_strength * expId +
  2145. (1 | subjectId),
  2146. data = .)
  2147. }
  2148. models.recog.rt[[pc]] <- mod
  2149. }
  2150. ```
  2151. ```{r submem.binned.rt, echo=F, fig.width=7, fig.height=10, eval=T}
  2152. nbins <- 4
  2153. rt.binned.plt <- rt_data.submem %>%
  2154. mutate(trial_outcome = trial_outcome.recognition) %>%
  2155. mutate(across(association_strength,
  2156. factor,
  2157. levels=c("Strong (4x)", "Weak (1x)"))) %>%
  2158. group_by(subjectId, component, association_strength, trial_outcome, expId) %>%
  2159. mutate(trial_count = n()) %>%
  2160. ungroup() %>%
  2161. dplyr::filter(trial_count > nbins-1) %>%
  2162. group_by(subjectId, component, association_strength, trial_outcome, expId) %>%
  2163. mutate(component_score_bin = cut_interval(component_score,
  2164. n = nbins,
  2165. labels = F)) %>%
  2166. ungroup() %>%
  2167. group_by(subjectId, trial_outcome, association_strength,
  2168. component, component_score_bin, expId) %>%
  2169. summarize(rt = median(recognition_response.rt)) %>%
  2170. summarySEwithin(.,
  2171. measurevar="rt",
  2172. withinvars=c("association_strength",
  2173. "trial_outcome",
  2174. "component",
  2175. "component_score_bin"),
  2176. betweenvars=c("expId"),
  2177. idvar="subjectId") %>%
  2178. mutate(across(component,
  2179. factor,
  2180. levels=ordered_pcs)) %>%
  2181. ggplot(aes(x = as.numeric(as.character(component_score_bin)),
  2182. y = rt,
  2183. color = association_strength,
  2184. group = association_strength)) +
  2185. geom_line(linewidth=0.8) +
  2186. geom_point(size=1.5) +
  2187. geom_errorbar(aes(ymin=rt-se, ymax=rt+se),
  2188. width=0.2) +
  2189. facet_grid(rows=vars(component),
  2190. axes="all",
  2191. scales = "free") +
  2192. scale_color_manual(values=palette, name="") +
  2193. scale_fill_manual(values=palette, name="") +
  2194. labs(x = "PC score", y = "RT (s)") +
  2195. theme(strip.text.y = element_blank()) +
  2196. facet_grid(rows = vars(component),
  2197. cols = vars(expId))
  2198. loadings.vrt.plt + rt.binned.plt +
  2199. plot_layout(widths=c(1,1.5))
  2200. ```
  2201. ### Models
  2202. ```{r print_submem_rt_models, echo=F, results="asis", eval=T}
  2203. for (pc in 1:ncomp) {
  2204. tidy(models.recog.rt[[pc]], effects = "fixed", conf.int = T, conf.level = 0.95) %>%
  2205. select(term, estimate, conf.low, conf.high, p.value, std.error, df) %>%
  2206. kable(caption=paste("PC", pc, ": ", formula(mod)[3], sep=""),
  2207. digits=3) %>%
  2208. kable_styling() %>%
  2209. cat()
  2210. tidy(models.recog.rt[[pc]], effects = "ran_pars", scales = "vcov") %>%
  2211. select(term, estimate) %>%
  2212. kable(digits=3, caption="Random effects") %>%
  2213. kable_styling() %>%
  2214. cat()
  2215. }
  2216. ```
  2217. ## Save
  2218. ```{r save_models, echo=F, eval=run_models}
  2219. save(models,
  2220. models.compact.no.mismatch,
  2221. models.rt,
  2222. recog.glmer.models,
  2223. models.recog.rt,
  2224. file="tpca_models_correct_only.RData")
  2225. ```

pupil_temporal_pca.Rmd at commit d144397, under GPL-3.0 · at the source

Overview

Authors: Alice M Xue1,2, Jenna Jokhani1,2, Anthony M Norcia1,2, Anthony D Wagner1,2
  1. Department of Psychology, Stanford University Stanford United States
  2. Wu Tsai Neurosciences Institute, Stanford University Stanford United States
Institutions: Stanford University (United States)
Journal: eLife, volume 15, article RP109678
Dates: published online 21 October 2026
Type: Research article · Language: English
License: CC BY
Identifiers: DOI 10.7554/elife.109678 · PMID 42905440 · PMCID PMC13686618 · OpenAlex W7130661913
Open access: gold, a free copy (OpenAlex)
Status: code verified (how the code is found and verified)
Categories: EEG (modality), human (organism), cognitive (subfield)
Methods: Spectral & time-frequency, Connectivity, Statistics, Smoothing, state filtering, decompositions, Machine learning, Preprocessing, Single-unit activity, calcium imaging, Physiology & signal measures
Keywords: arousal, attention, cognitive control, episodic memory, learning, prediction error, Human
MeSH: Arousal*, Attention*, Cognition*, Memory*, Adult, Brain, Electroencephalography, Female, Humans, Male, Pupil, Young Adult (* major topic)
Journal subjects: Neuroscience
Topic: Memory Processes and Influences (Cognitive Neuroscience, Neuroscience), according to OpenAlex
Funding: National Institute on Aging (R01AG065255); National Science Foundation (Graduate Research Fellowship Program DGE-2146755); School of Medicine, Stanford University (Stanford Interdisciplinary Graduate Fellowship)
Citations: not cited yet (Europe PMC); 96 references in the paper

Abstract

Ongoing experience is continuously processed in the context of past events. Divergence between current experience and memory-based predictions (i.e., a mnemonic prediction error; MPE) is theorized to be a signal that encoding, as opposed to retrieval, should be prioritized. We asked how MPEs place demands on control and attention-related processes, and whether these demands differ as a function of prediction strength. We investigated these questions across two experiments, wherein we recorded scalp electroencephalography and/or pupillometry as 101 young human adults performed an associative memory task. Strong MPEs, more so than weak MPEs, increased physiological indices of cognitive control (frontal theta), attention (posterior alpha and pupil size), and arousal (pupil size). Greater cognitive control (frontal theta) during strong MPEs promoted better learning of prediction-violating (i.e., unexpected) stimuli. Collectively, these findings reveal multiple pathways through which the mind and brain respond adaptively to violations of strong mnemonic predictions.

Reproduced under the paper's license (CC BY), from the paper cited above.

Repository

Its files are read in the Code ↔ Paper reader above, with 3 matches between paragraphs and lines of code.

alicexue/mpe-attention-cog-ctrl

License: GPL-3.0
State: the link answers, verified on 25 October 2026
Evidence: files inventoried
Commit: d1443971f1b402b0abcfa740f67bedc99daf88db, 20 October 2026
Languages: R (20)
Size: 34 files, 20 scripts
Software Heritage: archived
Found in: “Data availability”
Holds: README, license file, 16 notebooks
Not found: CITATION.cff, environment file, tests, continuous integration, documentation
Tools: broom (17 files), ggplot2 (17 files), patchwork (17 files), tidyverse (17 files), afex (16 files), data.table (16 files), emmeans (16 files), lme4 (16 files), ggpubr (15 files), mgcv (15 files), psych (15 files), brms (1 file), easystats (1 file)
Availability: 1 check, the latest on 25 October 2026: the link answers
  • 25 October 2026: the link answers
22 files

The paper's code and data availability statement is in the Data section.

Tracing map

Proposed by the machine: these links were found in the paper and verified at the source, without human review. The map will receive a Zenodo DOI once one of the paper's authors has validated it with their ORCID.

What the map holds:

  • 1 repository of the authors' code, each at its verified commit, with its license and how the link was found in the paper;
  • 20 scripts, each with its path and the digest of its content;
  • 3 matches between paragraphs of the paper and lines of the code (method lexical-v1);
  • neither the text of the paper nor the code itself.

Its JSON (tracing-map.json) is deposited on Zenodo with its DOI once the map is validated.

Data

No dataset and no data link were found in the paper.

Data availability

Data and code for this manuscript are accessible at https://github.com/alicexue/mpe-attention-cog-ctrl (copy archived at Xue, 2026).

Reproduced under the paper's license (CC BY), from the paper cited above.

Versions

The history of this record: each version stored by the harvester or made by a correction of its authors or of the maintainers of its code, and what changed in its facts. The texts of the paper (its abstract, its availability statements) are not part of it; versions that changed only those are not listed.

Version 1, 25 October 2026: the first record

Recorded: type, language, journal, volume, pages, dates, 4 authors, 7 keywords, 12 MeSH terms, 3 funders, 94 references.

Cite

This paper

Xue, A. M., Jokhani, J., Norcia, A. M., & Wagner, A. D. (2026). Strong mnemonic prediction errors increase cognitive control, attention, and arousal. eLife, 15, RP109678. https://doi.org/10.7554/elife.109678

BibTeX

@article{xue2026strong,
author = {Xue, Alice M and Jokhani, Jenna and Norcia, Anthony M and Wagner, Anthony D},
title = {{Strong mnemonic prediction errors increase cognitive control, attention, and arousal}},
journal = {eLife},
year = {2026},
month = oct,
volume = {15},
pages = {RP109678},
publisher = {eLife Sciences Publications, Ltd},
issn = {2050-084X},
doi = {10.7554/elife.109678},
url = {https://doi.org/10.7554/elife.109678},
pmid = {42905440},
pmcid = {PMC13686618}
}

RIS

TY - JOUR
AU - Xue, Alice M
AU - Jokhani, Jenna
AU - Norcia, Anthony M
AU - Wagner, Anthony D
TI - Strong mnemonic prediction errors increase cognitive control, attention, and arousal
T2 - eLife
J2 - eLife
PY - 2026
DA - 2026/10/21
VL - 15
SP - RP109678
SN - 2050-084X
PB - eLife Sciences Publications, Ltd
DO - 10.7554/elife.109678
UR - https://doi.org/10.7554/elife.109678
LA - en
ER -

CSL-JSON

{
"id": "10.7554/elife.109678",
"type": "article-journal",
"title": "Strong mnemonic prediction errors increase cognitive control, attention, and arousal",
"container-title": "eLife",
"author": [
{
"family": "Xue",
"given": "Alice M"
},
{
"family": "Jokhani",
"given": "Jenna"
},
{
"family": "Norcia",
"given": "Anthony M"
},
{
"family": "Wagner",
"given": "Anthony D"
}
],
"container-title-short": "eLife",
"volume": "15",
"page": "RP109678",
"DOI": "10.7554/elife.109678",
"PMID": "42905440",
"PMCID": "PMC13686618",
"ISSN": "2050-084X",
"publisher": "eLife Sciences Publications, Ltd",
"URL": "https://doi.org/10.7554/elife.109678",
"language": "en",
"issued": {
"date-parts": [
[
2026,
10,
21
]
]
}
}

The tracing map gets a citation of its own once an author has validated it and it has a DOI.

Similar papers

The papers with a page that share the most with this one: the tools found in their code, their categories, datasets, cited references and authors, the rarest counting most.

[1] doi:10.1186/s13229-025-00678-w [code]
Locus coeruleus tonic upregulation increases selectivity to inconspicuous auditory information in autistic compared to non-autistic individuals: a combined pupillometry and electroencephalography study.
Journal: Molecular autism
In common: brms, easystats, psych, 5 other tools, EEG, cognitive, 6 references
[2] doi:10.1371/journal.pone.0355165 [code]
Pupillary dynamics during hands-off L2 driving and transitions of control under high cognitive load.
Journal: PloS one
In common: brms, mgcv, easystats, 6 other tools, cognitive, 4 references
[3] doi:10.1073/pnas.2503535122 [code]
Hippocampal mismatch signals are based on episodic memories and not schematic knowledge.
Journal: Proceedings of the National Academy of Sciences of the United States of America
In common: cognitive, 11 references
[4] doi:10.3758/s13423-022-02124-x [code]
Explicitly predicting outcomes enhances learning of expectancy-violating information.
Journal: Psychonomic bulletin & review
In common: afex, easystats, psych, 6 other tools, cognitive, 3 references
[5] doi:10.1186/s11689-025-09651-3 [code]
The association between infant EEG aperiodic exponent and the trajectory of restricted and repetitive behaviors for toddlers with and without autism.
Journal: Journal of neurodevelopmental disorders
In common: brms, afex, mgcv, 8 other tools, EEG
[6] doi:10.1162/imag_a_00312 [code]
Alpha-band fluctuations represent behaviorally relevant excitability changes as a consequence of top-down guided spatial attention in a probabilistic spatial cueing design.
Journal: Imaging neuroscience (Cambridge, Mass.)
In common: brms, afex, broom, 6 other tools, EEG, cognitive, 2 references
[7] doi:10.1038/s41593-024-01672-w [code]
Cognitive control training with domain-general response inhibition does not change children's brains or behavior.
Journal: Nature neuroscience
In common: brms, afex, broom, 8 other tools, cognitive
[8] doi:10.1038/s41398-026-03948-0 [code]
Locus coeruleus modulation of neurophysiological sensory selectivity differs in autism and other mental health conditions.
Journal: Translational psychiatry
In common: easystats, psych, emmeans, 5 other tools, EEG, 4 references
[9] doi:10.1016/j.neuroimage.2026.122115 [code]
Midfrontal theta power relates to response speeding following frustrative nonreward.
Journal: NeuroImage
In common: easystats, psych, emmeans, 5 other tools, EEG, cognitive, 4 references
[10] doi:10.1038/s41467-022-31040-w [code]
Hippocampal representations switch from errors to predictions during acquisition of predictive associations.
Journal: Nature communications
In common: cognitive, 10 references

Contribute

The authors of this paper can claim it, correct its record and validate its tracing map, and the maintainers of its code (its owner, or a public member of its organization) correct what it says of their repository; anyone signed in can ask for its removal. Every request goes to OSCR's own machine, which answers it; your account page follows them.

Sign in with ORCID to claim this paper as one of its authors, correct its record or validate its tracing map: when the paper's metadata lists your ORCID iD, you are recognized at once. Maintainers of its code: sign in with GitHub, then claim the repository on your account page.

Add the badge to the code's README

OSCR: the badge of this paper

If you are an author or a maintainer of this paper's code, you can add this badge. It says what OSCR knows of the paper (its code, its validated tracing map) and links the code to this page. Paste one of these snippets under the README's title. Keep the two marks around it, so that the badge can be replaced in place and is never added twice. OSCR changes nothing in your repository.

Markdown
<!-- oscr-badge:start -->
[![OSCR](https://openscicode.org/badge/doi/10.7554/elife.109678.svg)](https://openscicode.org/badge/doi/10.7554/elife.109678)
<!-- oscr-badge:end -->
reStructuredText
.. oscr-badge:start

.. image:: https://openscicode.org/badge/doi/10.7554/elife.109678.svg
   :target: https://openscicode.org/badge/doi/10.7554/elife.109678
   :alt: OSCR

.. oscr-badge:end
HTML
<!-- oscr-badge:start -->
<a href="https://openscicode.org/badge/doi/10.7554/elife.109678"><img src="https://openscicode.org/badge/doi/10.7554/elife.109678.svg" alt="OSCR"></a>
<!-- oscr-badge:end -->

On GitHub, open the README, choose Edit (the pencil), paste the snippet under the title, then "Commit changes".

If you choose to, you can have the badge kept up to date for you, with oscr badge --write from the command line or with the GitHub Action in your repository's workflows. Every other form and option (a card, AsciiDoc, LaTeX, shields.io) is on the badge page.

Request its removal

To ask OSCR to remove this record, the copies of its authors' scripts or its tracing map, use the removal request page: signed in, you say who you are, what to remove and why, then review and confirm the request. Published rules decide every request (how).

Discussion, reproductions, activity

Discussion: code errors and mismatches between the code and the paper about this paper and its code, from readers and its authors, beside the paper and its tracing map.

Report a mismatch between the code and the paper · Report a code error (signed in)

Reproductions: reports from readers who ran the authors' code and did not get the paper's result, what they ran, with which environment, commit and data.

Report a reproduction failure (signed in)

Versions of its code: the releases of its code tied to a version of this paper (the preprint, the accepted manuscript, the version of record…), each with the tracing map versioned with it.

Activity: what happens around this paper: new versions of its record, its map's validation, discussions and reproductions. It opens with sign-in.