-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-BEH_analysis_markdown.Rmd
1769 lines (1482 loc) · 88.7 KB
/
01-BEH_analysis_markdown.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "ReconTime analysis"
output: html_document
date: "2023-08-22"
---
# housekeeping
```{r housekeeping}
# clear workspace
rm(list = ls())
# define workspace
wd = 'C:/Users/alexander.lepauvre/Documents/GitHub/Reconstructed_time_analysis'
setwd(wd)
bids_root = 'C:/Users/alexander.lepauvre/Documents/GitHub/Reconstructed_time_analysis/bids'
```
# required packages
```{r packages}
# get package manager package
if (!('pacman' %in% installed.packages()))
{install.packages("pacman")}
library(pacman)
# install all needed packages
pacman::p_load('dplyr', 'ggdist', 'ggeffects', 'ggpubr', 'lme4', 'emmeans', 'rstatix', 'rsq', 'car', 'sjPlot', 'MuMIn', 'dgof')
```
# Experiment 1
## Experiment 2 preprocessing
### Subjects list:
```{r subjects1}
# vector of included subjects (experiment of subjects 104 was aborted because of technique issues, number 122 is missing because of error in subject number assignment [see below at experiment 2])
subNums = c(101, 102, 103, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 123)
n = length(subNums)
ses = 1
Lab_ID = 'SX'
# experiment 1 is refered to as 'prp' task
task = 'prp'
```
### loading files for experiment 1
```{r loading1}
# initialize variables for loop
count = 0
sub_ids = NaN
table_size = 1:n
# loop through subject numbers and load data
for (subNum in subNums ){
count = count + 1
sub_ids[count] = paste0(Lab_ID, subNum)
# make file name
sub_folder = paste0('sub-', Lab_ID, subNum)
ses_folder = paste0('ses-', ses)
file_name = paste0(sub_folder, '_', ses_folder, '_run-all_task-', task,'_events.csv')
# make file directory
file = file.path(bids_root, sub_folder, ses_folder, 'beh', file_name)
# load file name
event_table = read.csv(file)
# store table size
table_size[count] = dim(event_table)[1]
# make format consistent
if ('X' %in% colnames(event_table))
{event_table$X <- NULL}
colnames(event_table)[which(names(event_table) == "has_repsonse_vis")] <- "has_response_vis"
colnames(event_table)[which(names(event_table) == "trial_repsonse_vis")] <- "trial_response_vis"
# add subject number column
event_table = event_table %>% mutate(sub_num = subNum)
# z-score of RT_vis and RTaud (for within subject z-scoring)
event_table = event_table %>% mutate(z_RT_vis = (RT_vis-mean(RT_vis, na.rm = TRUE))/sd(RT_vis, na.rm = TRUE))
event_table = event_table %>% mutate(z_RT_aud = (RT_aud-mean(RT_aud, na.rm = TRUE))/sd(RT_aud, na.rm = TRUE))
# concatenate the tables
if ( exists('all_event_table')){
all_event_table = rbind(all_event_table, event_table)
} else {
all_event_table = event_table
}
}
# Create the directory to store the results:
# Specify the desired directory path
save_dir <- file.path(bids_root, "derivatives", "beh", "prp")
# Create the directory if it doesn't exist
if (!file.exists(save_dir)) {
dir.create(save_dir, recursive = TRUE)
}
```
### apply trial exclusion
[Pre-registered]:
"For the analyses, subjects with low mean performances in the visual (<80% hits or >20% false alarms) or auditory task (<80% accuracy) will be excluded. Moreover, all participants will be between 18 and 35 years old, have reportedly normal or corrected-to-normal visual, and no hearing impairments.
In addition, only task-relevant non-target, and task-irrelevant trials will be included. Moreover, trials with no or incorrect inaccurate, implausible short auditory RT (<100 ms), or false alarms on the visual task (non-target trials with a visual response) will be excluded from the analysis"
```{r trial_exclusion1}
# store table without trial exclusion
raw_event_table = all_event_table
# add response window column (time between tone and end of trial)
all_event_table = all_event_table %>% mutate(resp_window = (2-onset_SOA)+stim_jit )
# remove RTaud < 100 ms
all_event_table = all_event_table[all_event_table$RT_aud > 0.1, ]
# remove false alarm
all_event_table = all_event_table[all_event_table$trial_response_vis != 'fa', ]
# remove incorrect auditory responses
all_event_table = all_event_table[all_event_table$trial_accuracy_aud == 1, ]
# remove NaNs from auditory responses
all_event_table = all_event_table[!is.na(all_event_table$RT_aud), ]
# store table with targets
target_event_table = all_event_table
# remove target trials from all_event_table
all_event_table = all_event_table[all_event_table$task_relevance != 'target', ]
```
### transfrom data
[Pre-registered]:
"
Variables for the glmm:
RT – raw (gamma identity link function)
SOA – ordinal
onset/offset – binary, numeric, centered
task-relevance – binary, numeric, centered
duration – ordinal
pitch – binary, numeric, centered
category – nominal
"
``` {r transform_data1}
# make task relevance and SOA lock numeric
all_event_table = all_event_table %>% mutate(is_task_relevant = ifelse(task_relevance == 'non-target',1,0))
all_event_table = all_event_table %>% mutate(is_onset = ifelse(SOA_lock == 'onset',1,0))
# make SOA and duration oridnal factors
all_event_table = all_event_table %>% mutate(f_SOA = factor(SOA, ordered = TRUE, levels = c("0", "0.116", "0.232", "0.466")))
all_event_table = all_event_table %>% mutate(f_duration = factor(duration, ordered = TRUE, levels = c("0.5", "1", "1.5")))
# center variables
all_event_table = all_event_table %>% mutate(c_task_relevant = is_task_relevant - mean(is_task_relevant))
all_event_table = all_event_table %>% mutate(c_is_onset = is_onset - mean(is_onset))
all_event_table = all_event_table %>% mutate(c_SOA = SOA - mean(SOA))
all_event_table = all_event_table %>% mutate(c_duration = duration - mean(duration))
all_event_table = all_event_table %>% mutate(c_pitch = (pitch-1050)/100) # makes pitches 0.5 for high and -0.5 for low
```
### test exclusion criteria
```{R test_exclusion1}
# after trials removal
print('number of onset trials after trial removal (incl: targets)')
sum(target_event_table$SOA_lock == 'onset')
print('number of offset trials after trial removal (incl: targets)')
sum(target_event_table$SOA_lock == 'offset')
# before
print('number of onset trials before trial removal (incl: targets)')
sum(raw_event_table$SOA_lock == 'onset')
print('number of offset trials after trial removal (incl: targets)')
sum(raw_event_table$SOA_lock == 'offset')
# difference
print('number of onset trials removed (excl: targets)')
sum(raw_event_table$SOA_lock == 'onset' & raw_event_table$task_relevance != 'target') - sum(all_event_table$SOA_lock == 'onset')
print('number of offset trials removed (excl: targets)')
sum(raw_event_table$SOA_lock == 'offset' & raw_event_table$task_relevance != 'target') - sum(all_event_table$SOA_lock == 'offset')
# relative trial removal w/o targets
print('perceptage of onset trials removed (excl: targets)')
(sum(raw_event_table$SOA_lock == 'onset' & raw_event_table$task_relevance != 'target') - sum(all_event_table$SOA_lock == 'onset'))/sum(raw_event_table$SOA_lock == 'onset' & raw_event_table$task_relevance != 'target')
print('perceptage of offset trials removed (excl: targets)')
(sum(raw_event_table$SOA_lock == 'offset' & raw_event_table$task_relevance != 'target') - sum(all_event_table$SOA_lock == 'offset'))/sum(raw_event_table$SOA_lock == 'offset' & raw_event_table$task_relevance != 'target')
# relative trial removal targets
print('perceptage of onset target trials removed')
(sum(raw_event_table$SOA_lock == 'onset' & raw_event_table$task_relevance == 'target') - sum(target_event_table$SOA_lock == 'onset' & target_event_table$task_relevance == 'target'))/sum(raw_event_table$SOA_lock == 'onset' & raw_event_table$task_relevance == 'target')
print('perceptage of offset target trials removed')
(sum(raw_event_table$SOA_lock == 'offset' & raw_event_table$task_relevance == 'target') - sum(target_event_table$SOA_lock == 'offset' & target_event_table$task_relevance == 'target'))/sum(raw_event_table$SOA_lock == 'offset' & raw_event_table$task_relevance == 'target')
```
### performance experiment 1
``` {r perfromance1}
# initialize data frame with all columns needed
perf = data.frame(matrix(ncol = 0, nrow = 1))
perf$sub_num = 'NaN'
perf$hits = NaN
perf$misses = NaN
perf$crs = NaN # crs = correct rejects
perf$fas = NaN # fas = false alarms
perf$hit_rate = NaN
perf$fa_rate = NaN
perf$aud_acc = NaN
perf$RT_aud = NaN # RT after trial exclusion
perf$RT_vis = NaN
perf$RT_aud_raw = NaN # RT before trial exclusion
perf$RT_vis_raw = NaN
perf = perf[-c(1), ]
# loop through all subjects and get performance
for (sub_num in subNums) {
perf = perf %>% add_row(sub_num = as.character(sub_num),
hits = sum(raw_event_table$trial_response_vis =='hit' & raw_event_table$sub_num == sub_num),
misses = sum(raw_event_table$trial_response_vis =='miss' & raw_event_table$sub_num == sub_num),
crs = sum(raw_event_table$trial_response_vis =='cr' & raw_event_table$sub_num == sub_num),
fas = sum(raw_event_table$trial_response_vis =='fa' & raw_event_table$sub_num == sub_num),
hit_rate = hits/(hits+misses)*100,
fa_rate = fas/(fas+crs)*100,
aud_acc = mean(raw_event_table$trial_accuracy_aud[raw_event_table$sub_num == sub_num], na.rm = TRUE)*100,
RT_aud = mean(target_event_table$RT_aud[target_event_table$sub_num == sub_num], na.rm = TRUE)*1000,
RT_vis = mean(target_event_table$RT_vis[target_event_table$sub_num == sub_num], na.rm = TRUE)*1000,
RT_aud_raw = mean(raw_event_table$RT_aud[raw_event_table$sub_num == sub_num], na.rm = TRUE)*1000,
RT_vis_raw = mean(raw_event_table$RT_vis[raw_event_table$sub_num == sub_num], na.rm = TRUE)*1000)
}
# transfer hit rate and false alarm rate into visual accuracy
perf = perf %>% mutate(vis_acc = (hits + crs)/(hits + misses + crs + fas)*100)
# add mean and SD
perf = perf %>% add_row(sub_num = 'mean',
hits = mean(perf$hits),
misses = mean(perf$misses),
crs = mean(perf$crs),
fas = mean(perf$fas),
hit_rate = mean(perf$hit_rate),
fa_rate = mean(perf$fa_rate),
aud_acc = mean(perf$aud_acc),
vis_acc = mean(perf$vis_acc),
RT_aud = mean(perf$RT_aud),
RT_vis = mean(perf$RT_vis),
RT_aud_raw = mean(perf$RT_aud_raw),
RT_vis_raw = mean(perf$RT_vis_raw))
perf = perf %>% add_row(sub_num = 'sd',
hits = sd(perf$hits[1:n]),
misses = sd(perf$misses[1:n]),
crs = sd(perf$crs[1:n]),
fas = sd(perf$fas[1:n]),
hit_rate = sd(perf$hit_rate[1:n]),
fa_rate = sd(perf$fa_rate[1:n]),
aud_acc = sd(perf$aud_acc[1:n]),
vis_acc = sd(perf$vis_acc[1:n]),
RT_aud = sd(perf$RT_aud[1:n]),
RT_vis = sd(perf$RT_vis[1:n]),
RT_aud_raw = sd(perf$RT_aud_raw[1:n]),
RT_vis_raw = sd(perf$RT_vis_raw[1:n]))
# Save performances to file:
write.csv(perf, file = file.path(save_dir, "Experiment1-performances.csv"), row.names = TRUE)
```
## Experiment 1 analysis:
### Full model:
[Pre-registered]:
"general linear mixed effects analysis of SOA, onset/offset, and task-relevance (fixed effects, including interaction terms) on RTaud (dependent variable). As random effects, we will have intercepts for subjects and duration, including random slopes for the effects of SOA, onset/offset, and task relevance. "
Generalized linear mixed effect model (GLMM)
fixed effects: SOA (ordinal), SOA-Lock (as c_is_onset, numeric, centered), Task-relevance (as is relevant, numeric, centered)
random effects: Subject ID, Duration (ordinal)
```{r glmm1_epx1}
# single trial level (as preregistered)
# fit model
model1 = glmer(RT_aud ~ f_SOA*c_is_onset*c_task_relevant +
(1*f_SOA*c_is_onset*c_task_relevant | sub_id)+
(1*f_SOA*c_is_onset*c_task_relevant | f_duration),
family=Gamma(link="identity"), data=all_event_table)
summary(model1)
# get chisquare and p values
anova_results <- Anova(model1)
write.csv(anova_results, file = file.path(save_dir, "Experiment1-full_mdl_anova.csv"), row.names = TRUE)
anova_results
# residual plots for visual inspection
plot(fitted(model1),residuals(model1))
qqnorm(residuals(model1))
# get R square
rsq.glmm(model1)
```
### Onset model
[Pre-registered]:
Fitting the model on trials in which the auditory tone was presented at various SOA relative to the visual stimulus onset.
RT2 is modeled as a function of SOA and task relevance. In addition, random intercept and slope of both parameters are taken separately for each
subject and duration.
We predicted that an interaction between SOA and task relevance such that in both T1 relevance conditions, an negative slope of RT2 is observed as a function
of SOA but that the slope is larger (i.e. more negative) for task relevant compared to task irrelevant T1 trials.
```{r onset_PRP1}
# sub analysis restricted to onset
onset_event_table = all_event_table %>% filter(SOA_lock == "onset")
# fit onset model
onset_model_prereg = glmer(RT_aud ~ f_SOA*c_task_relevant +
(1*f_SOA*c_task_relevant | sub_id) +
(1*f_SOA*c_task_relevant | f_duration),
family = Gamma(link="identity"), data = onset_event_table)
summary(onset_model_prereg)
# Fit onset model without the effect of duration due to fitting issues:
onset_model_no_dur = glmer(RT_aud ~ f_SOA*c_task_relevant +
(1*f_SOA*c_task_relevant | sub_id),
family = Gamma(link="identity"), data = onset_event_table)
# Compare the two models to see which is best:
anova(onset_model_no_dur, onset_model_prereg)
# get chisquare and p values
anova_results <- Anova(onset_model_no_dur)
write.csv(anova_results, file = file.path(save_dir, "Experiment1-onset_mdl_anova.csv"), row.names = TRUE)
anova_results
# Post-hoc test: compare RT2 between task relevance condition separately for each SOA to quantify the difference in central stage occupation
# between task relevance conditions
em1 <- emmeans(onset_model_no_dur, "c_task_relevant", by = "f_SOA")
onset_soa_contrasts <- contrast(em1, "pairwise", adjust = "bonferroni")
onset_soa_contrasts
# get R square
rsq.glmm(onset_model_no_dur)
```
### Offset model
[Pre-registered]:
Fitting the model on trials in which the auditory tone was presented at various SOA relative to the visual stimulus offset
RT2 is modeled as a function of SOA, task relevance and T1 duration. The effect of duration was added to account for potential lingering effect of T1 processing on RT2. In addition, random intercept and slope of both parameters are taken separately for each subject.
We predicted that no effect of SOA should be observed when the tone was presented relative to the offset of the visual stimulus, as this event is not task relevance and therefore should not be processed by the central stage. We however predicted that if T1 processing last longer than the T1 stimuli duration, an effect of SOA should be observed not because of the offset itself but rather due to the increased temporal interval between T1 onset and T2 onset with increasing SOAs. We expected such an effect could occur for short T1 trials (lasting only 500ms), but not for larger T1 duration. In other words, we predict that if such lingering T1 processing effect occurs, it should decrease with increased T1 duration.
```{r offset_PRP1}
# sub analysis restricted to offset
offset_event_table = all_event_table %>% filter(SOA_lock == "offset")
# fit offset model
offset_model = glmer(RT_aud ~ f_SOA*f_duration*c_task_relevant +
(1*f_SOA*f_duration*c_task_relevant | sub_id),
family = Gamma(link="identity"), data = offset_event_table)
summary(offset_model)
# get chisquare and p values
anova_results <- Anova(offset_model)
write.csv(anova_results, file = file.path(save_dir, "Experiment1-offset_mdl_anova.csv"), row.names = TRUE)
anova_results
# get R square
rsq.glmm(offset_model)
# post-hoc bonferroni corrected pairwise comparison
em1 <- emmeans(offset_model, "f_SOA", by = "f_duration")
offset_soa_contrasts <- contrast(em1, "pairwise", adjust = "bonferroni")
# Save to file:
write.csv(offset_soa_contrasts, file = file.path(save_dir, "Experiment1-offset_soa_contrasts.csv"), row.names = TRUE)
```
### Control: Post-hoc offset PRP analysis separately for each duration
[Not pre-registered]:
We observed a significant interaction between SOA and T1 duration on RT2. We had predicted that such an effect could be induced by a lingering of T1 processing, in which case the effect of SOA should decrease with increased duration. To further investigate the effect of T1 duration on RT2, we modeled RT2 separately for short, intermediate and long T1 trials (for offset trials).
Note: This constitutes a deviation from our pre-registered post-hoc testing, as we declared that the effect of SOA in the different duration will be investigating using post-hoc pairwise comparison. The current approach constitutes a better alternative, as it investigates the slope across SOA conditions rather than comparing pairs of SOA conditions. The results are consistent between both methods (see Post-hoc pairwise comparison between SOA conditions for offset locked trials)
```{r offset_PRP1 post-hoc GLMs}
# =======================================================================
# Short
offset_short_event_table = offset_event_table %>% filter(duration == 0.5)
# Modelling:
offset_short_model = glmer(RT_aud ~ f_SOA*c_task_relevant +
(1*f_SOA*c_task_relevant | sub_id),
family = Gamma(link="identity"), data = offset_short_event_table)
summary(offset_short_model)
# Anova
anova_results <- Anova(offset_short_model)
write.csv(anova_results, file = file.path(save_dir, "Experiment1-offset_short_mdl_anova.csv"), row.names = TRUE)
anova_results
# =======================================================================
# Intermediate
offset_int_event_table = offset_event_table %>% filter(duration == 1.0)
# Modelling:
offset_int_model = glmer(RT_aud ~ f_SOA*c_task_relevant +
(1*f_SOA*c_task_relevant | sub_id),
family = Gamma(link="identity"), data = offset_int_event_table)
summary(offset_int_model)
# Anova
anova_results <- Anova(offset_int_model)
write.csv(anova_results, file = file.path(save_dir, "Experiment1-offset_int_mdl_anova.csv"), row.names = TRUE)
anova_results
# =======================================================================
# Long
offset_long_event_table = offset_event_table %>% filter(duration == 1.5)
# Modelling:
offset_long_model = glmer(RT_aud ~ f_SOA*c_task_relevant +
(1*f_SOA*c_task_relevant | sub_id),
family = Gamma(link="identity"), data = offset_long_event_table)
summary(offset_long_model)
# Anova
anova_results <- Anova(offset_long_model)
write.csv(anova_results, file = file.path(save_dir, "Experiment1-offset_long_mdl_anova.csv"), row.names = TRUE)
anova_results
```
### Control: Kolmogorov smirnoff test
[Not pre-registered]:
We observed a significant effect of SOA on RT2 in the onset trials and in the offset trials. To test whether the effect is induced by a shift in the RT distribution, using a Kolmogorov smirnoff test to compare the empirical cummulative distribution functions between SOA condtions within task relevance conditions and onset offset separately. In the case of the offset, run separately for each of the stimuli durations
```{r Experiment 1 Kolmogorov smirnoff test to compare ECDF}
# Define conditions
soa_lock_conditions <- c("onset", "offset")
task_relevance_conditions <- c("non-target", "irrelevant")
durations <- c(0.5, 1.0, 1.5)
soa_values <- c(0, 0.466)
results <- data.frame(
SOA_lock = character(),
Task_relevance = character(),
Duration = numeric(),
D_statistic = numeric(),
P_value = numeric(),
stringsAsFactors = FALSE
)
# Loop through conditions and perform KS test
for (lck in soa_lock_conditions) {
for (tsk in task_relevance_conditions) {
if (lck == "onset") {
# Filter data for onset conditions
data_filtered <- all_event_table %>%
filter(SOA_lock == lck & task_relevance == tsk)
soa_0 <- data_filtered %>% filter(SOA == soa_values[1]) %>% pull(RT_aud)
soa_466 <- data_filtered %>% filter(SOA == soa_values[2]) %>% pull(RT_aud)
# Perform KS test
ks_result <- ks.test(soa_0, soa_466, alternative = "less", exact=FALSE)
# Append the results to the table:
results <- rbind(results, data.frame(
SOA_lock = lck,
Task_relevance = tsk,
Duration = NA,
D_statistic = ks_result$statistic,
P_value = ks_result$p.value
))
} else {
for (dur in durations) {
# Filter data for offset conditions
data_filtered <- all_event_table %>%
filter(SOA_lock == lck & task_relevance == tsk & duration == dur)
soa_0 <- data_filtered %>% filter(SOA == soa_values[1]) %>% pull(RT_aud)
soa_466 <- data_filtered %>% filter(SOA == soa_values[2]) %>% pull(RT_aud)
# Perform KS test
ks_result <- ks.test(soa_0, soa_466, alternative = "less", exact=FALSE)
# Append the results to the table:
results <- rbind(results, data.frame(
SOA_lock = lck,
Task_relevance = tsk,
Duration = dur,
D_statistic = ks_result$statistic,
P_value = ks_result$p.value
))
}
}
}
}
# Save these results to a table:
write.csv(results, file = file.path(save_dir, "Experiment1-kolmogorov-smirnoff.csv"), row.names = TRUE)
```
### Control: Modelling target trials
[Not pre-registered]:
For completeness's sake, modelling RT2 as a function of SOA on target trials, despite the smaller number of targets. Modelling onset and offset trials separately from the get go, no need to run the full model
```{r Experiment 1 Target trials}
# Filter target trials:
target_trials = target_event_table %>% filter(task_relevance == 'target')
# Convert SOA to ordinal factor:
target_trials = target_trials %>% mutate(f_SOA = factor(SOA, ordered = TRUE, levels = c("0", "0.116", "0.232", "0.466")))
# ======================================================================
# Onset trials:
print('=================================')
print('ONSET:')
target_onset_trials = target_trials %>% filter(SOA_lock == 'onset')
# Fit onset model without the effect of duration due to fitting issues:
target_onset_mdl = glmer(RT_aud ~ f_SOA + (1*f_SOA | sub_id),
family = Gamma(link="identity"), data = target_onset_trials)
summary(target_onset_mdl)
# get chisquare and p values
anova_results <- Anova(target_onset_mdl)
write.csv(anova_results, file = file.path(save_dir, "Experiment1-target-onset_mdl_anova.csv"), row.names = TRUE)
anova_results
# ======================================================================
# Offset trials:
target_offset_trials = target_trials %>% filter(SOA_lock == 'offset')
# =======================================
# Short trials:
print('=================================')
print('OFFSET SHORT:')
target_offset_short_trials = target_offset_trials %>% filter(duration == 0.5)
# Modelling:
target_offset_short_model = glmer(RT_aud ~ f_SOA+
(1*f_SOA | sub_id),
family = Gamma(link="identity"), data = target_offset_short_trials)
summary(target_offset_short_model)
# Anova
anova_results <- Anova(target_offset_short_model)
write.csv(anova_results, file = file.path(save_dir, "Experiment1-target-offset_short_mdl_anova.csv"), row.names = TRUE)
anova_results
# =======================================
# Intermediate trials:
print('=================================')
print('OFFSET INT:')
target_offset_int_trials = target_offset_trials %>% filter(duration == 1.0)
# Modelling:
target_offset_int_model = glmer(RT_aud ~ f_SOA+
(1*f_SOA | sub_id),
family = Gamma(link="identity"), data = target_offset_int_trials)
summary(target_offset_int_model)
# Anova
anova_results <- Anova(target_offset_int_model)
write.csv(anova_results, file = file.path(save_dir, "Experiment1-target-offset_int_mdl_anova.csv"), row.names = TRUE)
anova_results
# =======================================
# Long trials:
print('=================================')
print('OFFSET LONG:')
target_offset_long_trials = target_offset_trials %>% filter(duration == 1.5)
# Modelling:
target_offset_long_model = glmer(RT_aud ~ f_SOA+
(1*f_SOA | sub_id),
family = Gamma(link="identity"), data = target_offset_long_trials)
summary(target_offset_long_model)
# Anova
anova_results <- Anova(target_offset_long_model)
write.csv(anova_results, file = file.path(save_dir, "Experiment1-target-offset_long_mdl_anova.csv"), row.names = TRUE)
anova_results
```
### Control: Post-hoc offset PRP analysis separately for each duration
[Pre-registered]:
To investigate the effect of SOA in offset locked trials in the different T1 duration conditions, we computed pairwise comparisons between all pairs of SOA conditions.
```{r offset_PRP1 post-hoc pairwise}
# Comparing RT2 between all pairs of SOAs separately for each T1 duration conditions:
em1 <- emmeans(offset_model, "f_SOA", by = "f_duration")
offset_soa_contrasts <- contrast(em1, "pairwise", adjust = "bonferroni")
onset_soa_contrasts
```
### Control: Comparing onset locked 466ms SOA vs long trials 0ms SOA
[Not pre-registered]:
We observed a PRP effect at stimulus offset especially for long trials. This may indicate that the disappearance of the visual stimulus in the long trials was indeed processed by the central stage and accordingly induced a PRP effect when T2 was presented in close succession to that event. Alternatively, the decrease in RT2 with increased SOA might reflect a facilitation of T2 processing at increased SOA, due to the increased probability of the event occuring at large SOAs (hazard rate). If that is the case RT2 should be at baseline at SOA=0 and fastest at SOA=0.466s. We do not have a clean baseline in our study. However, in the onset trials (where a clear PRP effect is observed), RT2 at SOA = 0.466 should be the closest to it. It is likely that the interference period was not completely over by 0.466ms, in which case, RT2 would be higher compared to baseline. If we observe that RT2 in SOA=0s in offset locked long trials is larger than at SOA=0.466ms onset locked trials, then this implies that RT2 in the offset locked long trials is larger than a data point that is itself larger than baseline. In that case, the decrease of RT2 with increased SOA in offset locked long trials does not reflect a facilitation at larger SOAs but rather a delaying at short SOAs, consistent with a PRP effect.
```{r ctr_model2}
# Extract the two conditions of interest:
comp_tbl = all_event_table %>% filter((duration == 1.5 & SOA == 0 & SOA_lock == 'offset') | (SOA == 0.466 & SOA_lock == 'onset'))
# with duration, category and pitch as random effects
comparison_mdl = glmer(RT_aud ~ SOA_lock +
(1*SOA_lock | sub_id),
family=Gamma(link="identity"), data=comp_tbl)
summary(comparison_mdl)
# get chisquare and p values
Anova(comparison_mdl)
em1 <- emmeans(comparison_mdl, "SOA_lock")
onset_vs_long_soa_contrast <- contrast(em1, "pairwise", adjust = "bonferroni")
# Save to file:
write.csv(onset_vs_long_soa_contrast, file = file.path(save_dir, "Experiment1-offset_vs_onset_soa_contrasts.csv"), row.names = TRUE)
```
### Control: control model
[Pre-registered]:
Adding the random effect of T2 pitch, T1 duration and T1 category as random effect to our model.
```{r ctr_model1}
# with duration, category and pitch as random effects
ctr_model = glmer(RT_aud ~ f_SOA*c_is_onset*c_task_relevant +
(1*f_SOA*c_is_onset*c_task_relevant | sub_id) +
(1*f_SOA*c_is_onset*c_task_relevant | f_duration) +
(1*f_SOA*c_is_onset*c_task_relevant | c_pitch) +
(1*f_SOA*c_is_onset*c_task_relevant | category),
family=Gamma(link="identity"), data=all_event_table)
summary(ctr_model)
# get chisquare and p values
Anova(ctr_model)
# get R square
rsq.glmm(ctr_model)
# compare control_model with main model
anova(model1, ctr_model)
```
### effect size of PRP
PRP effect magnitude: Difference in ms between short SOA and longest SOA for certain condition
PRP effect size: Cohen's D effect size for the same contrast
```{r effect_size1}
# initialize varibales
effect_size_table = matrix(ncol = 6, nrow = 5)
rownames(effect_size_table) = c("Mean_SOA0", "Mean_SOA466", "PRP_size", "joined_SD", "Cohensd")
colnames(effect_size_table) = c("target", "non_target_onset", "irrelevant_onset", "offset_short", "offset_intermediate", "offset_long")
# mean of SOA = 0
effect_size_table[1, ] = c(mean(target_event_table %>% filter(SOA == 0 & SOA_lock == 'onset' & task_relevance == 'target') %>% pull(RT_aud), na.rm = TRUE),
mean(target_event_table %>% filter(SOA == 0 & SOA_lock == 'onset' & task_relevance == 'non-target') %>% pull(RT_aud), na.rm = TRUE),
mean(target_event_table %>% filter(SOA == 0 & SOA_lock == 'onset' & task_relevance == 'irrelevant') %>% pull(RT_aud), na.rm = TRUE),
mean(all_event_table %>% filter(SOA == 0 & SOA_lock == 'offset' & duration == 0.5) %>% pull(RT_aud), na.rm = TRUE),
mean(all_event_table %>% filter(SOA == 0 & SOA_lock == 'offset' & duration == 1) %>% pull(RT_aud), na.rm = TRUE),
mean(all_event_table %>% filter(SOA == 0 & SOA_lock == 'offset' & duration == 1.5) %>% pull(RT_aud), na.rm = TRUE))
# mean of SOA = 466
effect_size_table[2, ] = c(mean(target_event_table %>% filter(SOA == 0.466 & SOA_lock == 'offset' & task_relevance == 'target' & duration == 1.5) %>% pull(RT_aud), na.rm = TRUE),
mean(target_event_table %>% filter(SOA == 0.466 & SOA_lock == 'onset' & task_relevance == 'non-target') %>% pull(RT_aud), na.rm = TRUE),
mean(target_event_table %>% filter(SOA == 0.466 & SOA_lock == 'onset' & task_relevance == 'irrelevant') %>% pull(RT_aud), na.rm = TRUE),
mean(all_event_table %>% filter(SOA == 0.466 & SOA_lock == 'offset' & duration == 0.5) %>% pull(RT_aud), na.rm = TRUE),
mean(all_event_table %>% filter(SOA == 0.466 & SOA_lock == 'offset' & duration == 1) %>% pull(RT_aud), na.rm = TRUE),
mean(all_event_table %>% filter(SOA == 0.466 & SOA_lock == 'offset' & duration == 1.5) %>% pull(RT_aud), na.rm = TRUE))
# PRP size
effect_size_table[3, ] = effect_size_table[1, ] - effect_size_table[2, ]
# joined SD
effect_size_table[4, ] = c(sd(c(target_event_table %>% filter(SOA == 0 & SOA_lock == 'onset' & task_relevance == 'target') %>% pull(RT_aud),target_event_table %>% filter(SOA == 0.466 & SOA_lock == 'offset' & task_relevance == 'target' & duration == 1.5) %>% pull(RT_aud))),
sd(c(target_event_table %>% filter(SOA == 0 & SOA_lock == 'onset' & task_relevance == 'non-target') %>% pull(RT_aud), target_event_table %>% filter(SOA == 0.466 & SOA_lock == 'onset' & task_relevance == 'non-target') %>% pull(RT_aud))),
sd(c(target_event_table %>% filter(SOA == 0 & SOA_lock == 'onset' & task_relevance == 'irrelevant') %>% pull(RT_aud), target_event_table %>% filter(SOA == 0.466 & SOA_lock == 'onset' & task_relevance == 'irrelevant') %>% pull(RT_aud))),
sd(c(all_event_table %>% filter(SOA == 0 & SOA_lock == 'offset' & duration == 0.5) %>% pull(RT_aud), all_event_table %>% filter(SOA == 0.466 & SOA_lock == 'offset' & duration == 0.5) %>% pull(RT_aud))),
sd(c(all_event_table %>% filter(SOA == 0 & SOA_lock == 'offset' & duration == 1) %>% pull(RT_aud), all_event_table %>% filter(SOA == 0.466 & SOA_lock == 'offset' & duration == 1) %>% pull(RT_aud))),
sd(c(all_event_table %>% filter(SOA == 0 & SOA_lock == 'offset' & duration == 1.5) %>% pull(RT_aud), all_event_table %>% filter(SOA == 0.466 & SOA_lock == 'offset' & duration == 1.5) %>% pull(RT_aud))))
# cohens d
effect_size_table[5, ] = (effect_size_table[1, ] - effect_size_table[2, ])/effect_size_table[4, ]
# Save the effect size table to file:
write.csv(effect_size_table, file = file.path(save_dir, "Experiment1-effect_sizes.csv"), row.names = TRUE)
```
# Experiment 2
As experiment 1 but with introspective ratings of decision time for both tasks (called iRT_aud and iRT_vis)
## Experiment 2 preprocessing:
### loading files for experiment 2
```{r loading2}
# details of subjects
subNums2 = c(105, 106, 108, 109, 110, 113, 114, 115, 116, 118) # 101 excluded, 122 is the same as participant 116 from experiment one
n2 = length(subNums2)
sessions2 = c(2,3)
Lab_ID = 'SX'
# experiment 2 will be refered to as task 'introspection'
task = 'introspection'
# initialize loop variables
count = 0
sub_ids = NaN
table_size = 1:n2*2
# loading the files
# loop through subject and session numbers
for (subNum in subNums2){
for (ses in sessions2){
count = count + 1
sub_ids[count] = paste0(Lab_ID, subNum)
# make folder and file name
sub_folder = paste0('sub-', Lab_ID, subNum)
ses_folder = paste0('ses-', ses)
file_name = paste0(sub_folder, '_', ses_folder, '_run-all_task-', task,'_events.csv')
# make file directory
file = file.path(bids_root, sub_folder, ses_folder, 'beh', file_name)
# load file
event_table2 = read.csv(file)
# store table dimensions
table_size[count] = dim(event_table2 %>% filter(task_relevance != 'target'))[1]
# make format consistent
if ('X' %in% colnames(event_table2))
{event_table2$X <- NULL}
colnames(event_table2)[which(names(event_table2) == "has_repsonse_vis")] <- "has_response_vis"
colnames(event_table2)[which(names(event_table2) == "trial_repsonse_vis")] <- "trial_response_vis"
# add subject number and session column
event_table2 = event_table2 %>% mutate(sub_num = subNum)
event_table2 = event_table2 %>% mutate(ses = ses)
# z-score of iRT_vis and iRT_aud
event_table2 = event_table2 %>% mutate(z_iRT_vis = (iRT_vis-mean(iRT_vis))/sd(iRT_vis))
event_table2 = event_table2 %>% mutate(z_iRT_aud = (iRT_aud-mean(iRT_aud))/sd(iRT_aud))
# z-score of RT_vis and RTaud
event_table2 = event_table2 %>% mutate(z_RT_vis = (RT_vis-mean(RT_vis, na.rm = TRUE))/sd(RT_vis, na.rm = TRUE))
event_table2 = event_table2 %>% mutate(z_RT_aud = (RT_aud-mean(RT_aud, na.rm = TRUE))/sd(RT_aud, na.rm = TRUE))
# concatenate the tables
if ( exists('all_event_table2')){
all_event_table2 = rbind(all_event_table2, event_table2)
} else {
all_event_table2 = event_table2
}
}}
# 116 and 122 are the same person - should have the same id
all_event_table2 = all_event_table2 %>% mutate(sub_num = ifelse(sub_num == 122, 116, sub_num))
all_event_table2 = all_event_table2 %>% mutate(sub_id = ifelse(sub_id == 'SX122','SX116', sub_id))
# Create the directory to store the results:
save_dir <- file.path(bids_root, "derivatives", "beh", "introspection")
# Create the directory if it doesn't exist
if (!file.exists(save_dir)) {
dir.create(save_dir, recursive = TRUE)
}
```
### apply trial exclusion
[Pre-registered]:
"For the analyses, subjects with low mean performances in the visual (<80% hits or >20% false alarms) or auditory task (<80% accuracy) will be excluded. Moreover, all participants will be between 18 and 35 years old, have reportedly normal or corrected-to-normal visual, and no hearing impairments.
In addition, only task-relevant non-target, and task-irrelevant trials will be included. Moreover, trials with no or incorrect inaccurate, implausible short auditory RT (<100 ms), or false alarms on the visual task (non-target trials with a visual response) will be excluded from the analysis"
```{r trial_exclusion2}
# store table without exclusion
raw_event_table2 = all_event_table2
# # remove false alarms
all_event_table2 = all_event_table2[all_event_table2$trial_response_vis != 'fa', ]
# make iRT of 0 a 1 (zeros cause errors in GLMM with gamma family)
all_event_table2$iRT_vis[all_event_table2$iRT_vis == 0] = 1
all_event_table2$iRT_aud[all_event_table2$iRT_aud == 0] = 1
# # remove RT < 100 ms
all_event_table2 = all_event_table2[all_event_table2$RT_aud > 0.1, ]
# remove incorrect auditory responses
all_event_table2 = all_event_table2[all_event_table2$trial_accuracy_aud == 1, ]
# remove NaNs from auditory responses
all_event_table2 = all_event_table2[!is.na(all_event_table2$RT_aud), ]
# store table with targets
target_event_table2 = all_event_table2
# remove target trials from all_event_table
all_event_table2 = all_event_table2[all_event_table2$task_relevance != 'target', ]
```
### transfrom data
[Pre-registered]:
"
Variables for the glmm:
RT – raw (gamma identity link function)
SOA – ordinal
onset/offset – binary, numeric, centered
task-relevance – binary, numeric, centered
duration – ordinal
pitch – binary, numeric, centered
category – nominal
"
``` {r transform_data2}
# add log transformed column
all_event_table2 = all_event_table2 %>% mutate(log_RT_aud = log(RT_aud))
# make task relevance and SOA lock numeric
all_event_table2 = all_event_table2 %>% mutate(is_task_relevant = ifelse(task_relevance == 'non-target',1,0))
all_event_table2 = all_event_table2 %>% mutate(is_onset = ifelse(SOA_lock == 'onset',1,0))
# make SOA and duration factors
all_event_table2 = all_event_table2 %>% mutate(f_SOA = factor(SOA, ordered = TRUE, levels = c("0", "0.232", "0.466")))
all_event_table2 = all_event_table2 %>% mutate(f_duration = factor(duration, ordered = TRUE, levels = c("0.5", "1", "1.5")))
# center variables
all_event_table2 = all_event_table2 %>% mutate(c_task_relevant = is_task_relevant - mean(is_task_relevant))
all_event_table2 = all_event_table2 %>% mutate(c_is_onset = is_onset - mean(is_onset))
all_event_table2 = all_event_table2 %>% mutate(c_SOA = SOA - mean(SOA))
all_event_table2 = all_event_table2 %>% mutate(c_duration = duration - mean(duration))
all_event_table2 = all_event_table2 %>% mutate(c_iRT_vis = (iRT_vis - mean(iRT_vis))/1000)
all_event_table2 = all_event_table2 %>% mutate(c_pitch = (pitch-1050)/100) # makes pitches 0.5 for high and -0.5 for low
```
### test exclusion criteria
```{R test exclusion2}
# after trials removal
print('number of onset trials after trial removal (incl: targets)')
sum(target_event_table2$SOA_lock == 'onset')
print('number of offset trials after trial removal (incl: targets)')
sum(target_event_table2$SOA_lock == 'offset')
# before
print('number of onset trials before trial removal (incl: targets)')
sum(raw_event_table2$SOA_lock == 'onset')
print('number of offset trials after trial removal (incl: targets)')
sum(raw_event_table2$SOA_lock == 'offset')
# difference
print('number of onset trials removed (excl: targets)')
sum(raw_event_table2$SOA_lock == 'onset' & raw_event_table2$task_relevance != 'target') - sum(all_event_table2$SOA_lock == 'onset')
print('number of offset trials removed (excl: targets)')
sum(raw_event_table2$SOA_lock == 'offset' & raw_event_table2$task_relevance != 'target') - sum(all_event_table2$SOA_lock == 'offset')
# relative trial removal w/o targets
print('perceptage of onset trials removed (excl: targets)')
(sum(raw_event_table2$SOA_lock == 'onset' & raw_event_table2$task_relevance != 'target') - sum(all_event_table2$SOA_lock == 'onset'))/sum(raw_event_table2$SOA_lock == 'onset' & raw_event_table2$task_relevance != 'target')
print('perceptage of offset trials removed (excl: targets)')
(sum(raw_event_table2$SOA_lock == 'offset' & raw_event_table2$task_relevance != 'target') - sum(all_event_table2$SOA_lock == 'offset'))/sum(raw_event_table2$SOA_lock == 'offset' & raw_event_table2$task_relevance != 'target')
# relative trial removal targets
print('perceptage of onset target trials removed')
(sum(raw_event_table2$SOA_lock == 'onset' & raw_event_table2$task_relevance == 'target') - sum(target_event_table2$SOA_lock == 'onset' & target_event_table2$task_relevance == 'target'))/sum(raw_event_table2$SOA_lock == 'onset' & raw_event_table2$task_relevance == 'target')
print('perceptage of offset target trials removed')
(sum(raw_event_table2$SOA_lock == 'offset' & raw_event_table2$task_relevance == 'target') - sum(target_event_table2$SOA_lock == 'offset' & target_event_table2$task_relevance == 'target'))/sum(raw_event_table2$SOA_lock == 'offset' & raw_event_table2$task_relevance == 'target')
```
### performance experiment 2
Compute the performance for T1 and T2 in the second experiment.
``` {r perfromance2}
perf2 = data.frame(matrix(ncol = 0, nrow = 1))
perf2$sub_num = 'NaN'
perf2$hits = NaN
perf2$misses = NaN
perf2$crs = NaN
perf2$fas = NaN
perf2$hit_rate = NaN
perf2$fa_rate = NaN
perf2$aud_acc = NaN
perf2$RT_aud = NaN
perf2$RT_vis = NaN
perf2$RT_aud_raw = NaN
perf2$RT_vis_raw = NaN
perf2 = perf2[-c(1), ]
for (sub_num in subNums2) {
perf2 = perf2 %>% add_row(sub_num = as.character(sub_num),
hits = sum(raw_event_table2$trial_response_vis =='hit' & raw_event_table2$sub_num == sub_num),
misses = sum(raw_event_table2$trial_response_vis =='miss' & raw_event_table2$sub_num == sub_num),
crs = sum(raw_event_table2$trial_response_vis =='cr' & raw_event_table2$sub_num == sub_num),
fas = sum(raw_event_table2$trial_response_vis =='fa' & raw_event_table2$sub_num == sub_num),
hit_rate = hits/(hits+misses)*100,
fa_rate = fas/(fas+crs)*100,
aud_acc = mean(raw_event_table2$trial_accuracy_aud[raw_event_table2$sub_num == sub_num], na.rm = TRUE)*100,
RT_aud = mean(target_event_table2$RT_aud[target_event_table2$sub_num == sub_num], na.rm = TRUE)*1000,
RT_vis = mean(target_event_table2$RT_vis[target_event_table2$sub_num == sub_num], na.rm = TRUE)*1000,
RT_aud_raw = mean(raw_event_table2$RT_aud[raw_event_table2$sub_num == sub_num], na.rm = TRUE)*1000,
RT_vis_raw = mean(raw_event_table2$RT_vis[raw_event_table2$sub_num == sub_num], na.rm = TRUE)*1000)
}
# transfer hit rate and false alarm rate into accuracy
perf2 = perf2 %>% mutate(vis_acc = (hits + crs)/(hits + misses + crs + fas)*100)
# add mean and SD
perf2 = perf2 %>% add_row(sub_num = 'mean',
hits = mean(perf2$hits),
misses = mean(perf2$misses),
crs = mean(perf2$crs),
fas = mean(perf2$fas),
hit_rate = mean(perf2$hit_rate),
fa_rate = mean(perf2$fa_rate),
aud_acc = mean(perf2$aud_acc),
vis_acc = mean(perf2$vis_acc),
RT_aud = mean(perf2$RT_aud),
RT_vis = mean(perf2$RT_vis),
RT_aud_raw = mean(perf2$RT_aud_raw),
RT_vis_raw = mean(perf2$RT_vis_raw))
perf2 = perf2 %>% add_row(sub_num = 'sd',
hits = sd(perf2$hits[1:n2]),
misses = sd(perf2$misses[1:n2]),
crs = sd(perf2$crs[1:n2]),
fas = sd(perf2$fas[1:n2]),
hit_rate = sd(perf2$hit_rate[1:n2]),
fa_rate = sd(perf2$fa_rate[1:n2]),
aud_acc = sd(perf2$aud_acc[1:n2]),
vis_acc = sd(perf2$vis_acc[1:n2]),
RT_aud = sd(perf2$RT_aud[1:n2]),
RT_vis = sd(perf2$RT_vis[1:n2]),
RT_aud_raw = sd(perf2$RT_aud_raw[1:n2]),
RT_vis_raw = sd(perf2$RT_vis_raw[1:n2]))
# Save performances to file:
write.csv(perf2, file = file.path(save_dir, "Experiment2-performances.csv"), row.names = TRUE)
# percentage of underestimation in RTaud
underest_RTaud = mean(all_event_table2$iRT_aud)/mean(all_event_table2$RT_aud*1000)
```
## Experiment 2 analysis:
### RT2 analysis:
#### Full, Onset and offset models:
[Pre-registered]:
"general linear mixed effects analysis of SOA, onset/offset, and task-relevance (fixed effects, including interaction terms) on RTaud (dependent variable). As random effects, we will have intercepts for subjects and duration, including random slopes for the effects of SOA, onset/offset, and task relevance. "
Generalized linear mixed effect model (GLMM)
fixed effects: SOA (ordinal), SOA-Lock (as c_is_onset, numeric, centered), Task-relevance (as is relevant, numeric, centered)
random effects: Subject ID, Duration (ordinal)
GLMMs as in section "glmm1_epx1", "onset_PRP1", and "offset_PRP1" but with data from second experiment
```{r model_from_exp1}
# ======================================================================
# Full model:
model_exp2 = glmer(RT_aud ~ f_SOA*c_is_onset*c_task_relevant +
(1*f_SOA*c_is_onset*c_task_relevant | sub_id)+
(1*f_SOA*c_is_onset*c_task_relevant | f_duration),
family=Gamma(link="identity"), data=all_event_table2)
summary(model_exp2)
# Anova
anova_results <- Anova(model_exp2)
write.csv(anova_results, file = file.path(save_dir, "Experiment2-RT2_full_mdl_anova.csv"), row.names = TRUE)
anova_results
# get R square
rsq.glmm(model_exp2)
# ======================================================================
# Onset model:
onset_event_table2 = all_event_table2 %>% filter(SOA_lock == "onset")
# Model
onset_model2 = glmer(RT_aud ~ f_SOA*c_task_relevant +
(1*f_SOA*c_task_relevant | sub_id) +
(1*f_SOA*c_task_relevant | f_duration),
family = Gamma(link="identity"), data = onset_event_table2)
summary(onset_model2)
# Anova:
anova_results <- Anova(onset_model2)
write.csv(anova_results, file = file.path(save_dir, "Experiment2-RT2_onset_mdl_anova.csv"), row.names = TRUE)
anova_results
# get R square
rsq.glmm(onset_model2)
# Compute pairwise comparison between SOA conditions for the onset locked trials:
em2 <- emmeans(onset_model2, "f_SOA")
onset_soa_contrasts <- contrast(em2, "pairwise", adjust = "bonferroni")
# Save to file:
write.csv(onset_soa_contrasts, file = file.path(save_dir, "Experiment2-RT2_offset_soa_contrasts.csv"), row.names = TRUE)
# ======================================================================
# Offset model:
offset_event_table2 = all_event_table2 %>% filter(SOA_lock == "offset")
# Model:
offset_model2 = glmer(RT_aud ~ f_SOA*f_duration*c_task_relevant +
(1*f_SOA*f_duration*c_task_relevant | sub_id),
family = Gamma(link="identity"), data = offset_event_table2)
summary(offset_model2)
# Anova:
anova_results <- Anova(offset_model2)
write.csv(anova_results, file = file.path(save_dir, "Experiment2-RT2_offset_mdl_anova.csv"), row.names = TRUE)
anova_results
# get R square
rsq.glmm(offset_model2)