-
Notifications
You must be signed in to change notification settings - Fork 1
/
Anal_Wavelet_Orient_byTarget_Zscore.m
1553 lines (1273 loc) · 63.8 KB
/
Anal_Wavelet_Orient_byTarget_Zscore.m
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
% |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
% INFORMATION
% |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
% SPECTOGRAM
% A spectogram is a 3d figure that plots time on the x-axis, frequency on the
% y-axis, and shows you the power or phase-locking value for each point.
% We compute spectograms if we have power and phase information, averaged
% across trials, for at least one electrode.
% This can help us understand the changes of power and phase throughout the
% trial.
% |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
% :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
% |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
% Variables working with:
% ersp(i_sub,i_cond,i_perm,i_chan,:,:)
% itc(i_sub,i_cond,i_perm,i_chan,:,:)
% powbase,times,freqs
% The variables ersp and itc will be a 6D variable:
% (participants x conditions x events x electrodes x frequencies x timepoints)
% (participants x sets x events x electrodes x frequencies x timepoints)
% :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
% period = 1/EEG.srate;
% time (in s) = [EEG.event.latency]*period
% :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
eeglab redraw
% |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
% :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
% |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
%% Load previously processed target-aligned epoch data
% data has been converted to log scale
all_ersp_Z = struct2cell(load('all_ersp_Z.mat')); %gets loaded as a struct
all_ersp_Z = all_ersp_Z{1};
% load behavior data
load('ALLEEG_filt_byTargets_v3.mat');
% load settings
load('filt_byTargets_v3_Settings.mat');
% /////////////////////////////////////////////////////////////////////////
% -------------------------------------------------------------------------
%% $$$$$$$ BEH Data $$$$$$$
% Get BEH data for trials excluding trials that were rejected in the EEG
% preprocessing of the epochs
resp_errdeg = cell(length(exp.participants),1); %pre-allocate
for i_part = 1:length(exp.participants) % --------------------
[n,m] = size(ALLEEG(i_part).rejtrial);
% Get list of rejected trials
pip = 1;
for ni = 1:n %for when there are more than 1 column
for mi = 1:m
if ~isempty(ALLEEG(i_part).rejtrial(ni,mi).ids)
rejlist{pip} = ALLEEG(i_part).rejtrial(ni,mi).ids;
pip = 1 + pip;
end
end
clear mi
end
if pip > 1 %if trials were rejected
err_deg_tmp = ALLEEG(i_part).error_deg; %start with all the errors
% each set of rejected trials needs to be removed in order
% sequentially
for mi = 1:length(rejlist)
tmplist = [rejlist{mi}];
err_deg_tmp(tmplist) = []; %removes the trials
clear tmplist
end
clear mi
elseif pip == 1 %if no trials were rejected, rejlist variable not created
err_deg_tmp = ALLEEG(i_part).error_deg;
end
% create variable with selected BEH
resp_errdeg{i_part} = err_deg_tmp;
clear rejlist n m err_deg_tmp pip ni
end
clear i_part
% -------------------------------------------------------------------------
% /////////////////////////////////////////////////////////////////////////
% Fit errors to mixed model
model_out = cell(1,length(exp.participants)); %pre-allocate
for ii = 1:length(exp.participants)
error_deg{ii} = resp_errdeg{ii};
% error_deg{ii} = ALLEEG(ii).error_deg; %comment out if loaded data above
% model_out{ii} = MemFit(error_deg{ii});
model_out{ii} = MLE(error_deg{ii}); %fits without plotting
end
clear ii error_deg
% -------------------------------------------------------------------------
% /////////////////////////////////////////////////////////////////////////
% @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
% $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
%% >>>>>>>>>>>>>>>>>>>> POWER ANALYSES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
% $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
% @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
% #########################################################################
% /////////////////////////////////////////////////////////////////////////
%% Baseline correction of power by trial and frequency band
% /////////////////////////////////////////////////////////////////////////
% #########################################################################
% Right now it is set-up to subtract mean power across the epoch at each
% frequency from each time point at that same frequency
% note: does not include the catch trial data
tic %see how long this takes
all_erspN = cell(length(exp.participants),length(exp.singletrialselecs)); %pre-allocate
for i_part = 1:length(exp.participants) % --
for ii = 1:length(exp.singletrialselecs)
i_elect = exp.singletrialselecs(ii); %for doing only a selection of electrodes
% all_ersp is (participant x electrode).trials(freq x time x trial)
tmp_ersp = abs(all_ersp{i_part,i_elect});
for i_trial = 1:size(tmp_ersp,3)
for fq = 1:length(freqs)
% bl_freq = mean(tmp_ersp(fq,:,i_trial),2); %average each trial
bl_freq = mean(mean(tmp_ersp(fq,:,:),2),3); %average all trials
all_erspN{i_part,i_elect}.trials(fq,:,i_trial) = tmp_ersp(fq,:,i_trial)-bl_freq;
end
clear fq bl_freq
end
end
clear ii i_elect tmp_ersp
end
clear i_part
toc %see how long this takes
% #########################################################################
% #########################################################################
% /////////////////////////////////////////////////////////////////////////
%% OR use raw ERS values (log scaled)
% /////////////////////////////////////////////////////////////////////////
% --For data with targets--
all_erspN = cell(length(exp.participants),length(exp.singletrialselecs)); %pre-allocate
for i_part = 1:length(exp.participants) % --
for ii = 1:length(exp.singletrialselecs)
i_elect = exp.singletrialselecs(ii); %for doing only a selection of electrodes
% all_ersp is (participant x electrode).trials(freq x time x trial)
tmp_ersp = abs(all_ersp{i_part,i_elect});
for i_trial = 1:size(tmp_ersp,3)
all_erspN{i_part,i_elect}.trials(:,:,i_trial) = 10*log10(tmp_ersp(:,:,i_trial)); %dB converted
end
clear i_trial
end
clear ii i_elect tmp_ersp
end
clear i_part
% #########################################################################
% /////////////////////////////////////////////////////////////////////////
%% Standardize Power
% /////////////////////////////////////////////////////////////////////////
% #########################################################################
all_ersp_Z = cell(length(exp.participants),length(exp.singletrialselecs)); %pre-allocate
% Change power to z-score values per person
for i_part = 1:length(exp.participants)
% Get power across trials
for ii = 1:length(exp.singletrialselecs)
i_elect = exp.singletrialselecs(ii); %for doing only a selection of electrodes
% all_ersp is (participant x electrode).trials(freq x time x trial)
part_ersp = all_erspN{i_part,i_elect}.trials; %get single subject's baseline corrected power
% all_ersp_Z{i_part,i_elect}.trials = normalize(part_ersp,3,'zscore','robust');
all_ersp_Z{i_part,i_elect}.trials = (part_ersp - mean(part_ersp(:))) / std(part_ersp(:));
clear part_ersp i_elect
end
clear ii
end
clear i_part
% #########################################################################
% /////////////////////////////////////////////////////////////////////////
%% ERS: Power by errors
% /////////////////////////////////////////////////////////////////////////
% #########################################################################
% Create ERS by errors
x_errdeg_m = cell(1,length(exp.participants)); %pre-allocate
n_errdeg_m = cell(1,length(exp.participants)); %pre-allocate
x_pwr = cell(1,length(exp.singletrialselecs)); %pre-allocate
n_pwr = cell(1,length(exp.singletrialselecs)); %pre-allocate
errlims = cell(1,length(exp.participants)); %pre-allocate
for i_part = 1:length(exp.participants) % ----------------------
% Get upper and lower limits based on model fit
% errlims{i_part}(1) = -(model_out{1,i_part}.maxPosterior(2)); %negative value
% errlims{i_part}(2) = model_out{1,i_part}.maxPosterior(2);
errlims{i_part}(1) = -(model_out{1,i_part}(2)); %negative value
errlims{i_part}(2) = model_out{1,i_part}(2);
% Get errors values
x_errdeg_m{i_part} = resp_errdeg{i_part}(resp_errdeg{i_part}<(errlims{i_part}(2)*0.75) & resp_errdeg{i_part}>(errlims{i_part}(1)*0.75)); %small errors
n_errdeg_m{i_part} = resp_errdeg{i_part}([find(resp_errdeg{i_part}>=(errlims{i_part}(2)*1.5)) find(resp_errdeg{i_part}<=(errlims{i_part}(1)*1.5))]);
% Calculate power
for ii = 1:length(exp.singletrialselecs)
i_elect = exp.singletrialselecs(ii); %for doing only a selection of electrodes
% all_ersp is (participant x electrode).trials(freq x time x trial)
part_ersp = all_ersp_Z{i_part,i_elect}.trials; %get single subject's baseline corrected power
% Get trials with small errors
x_pwr{1,i_elect}(i_part,:,:) = squeeze(mean(part_ersp(:,:,[...
find((resp_errdeg{i_part}<(errlims{i_part}(2)*0.75) & resp_errdeg{i_part}>(errlims{i_part}(1)*0.75)))] ),3));
% Get trials with large errors
n_pwr{1,i_elect}(i_part,:,:) = squeeze(mean(part_ersp(:,:,[...
find(resp_errdeg{i_part}>=(errlims{i_part}(2)*1.5)) find(resp_errdeg{i_part}<=(errlims{i_part}(1)*1.5))] ),3));
clear part_ersp i_elect
end
end
clear ii i_part
% /////////////////////////////////////////////////////////////////////////
% #########################################################################
% &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
%% Plot spectogram across subjects &&
% &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
% Raw ERS plots
% for ii = 1:length(exp.singletrialselecs)
for ii = 1:5
i_elect = exp.singletrialselecs(ii); %for doing only a selection of electrodes
%mean across subjects
plot_ers_x = squeeze(mean(x_pwr{1,i_elect}(:,:,:),1)); %small errors
plot_ers_n = squeeze(mean(n_pwr{1,i_elect}(:,:,:),1)); %large errors
CLim = [-1.5 1.5]; %set power scale of plot
% Plot Small Errors
figure('Position', [1 1 1685 405]); colormap('jet') %open a new figure
subplot(1,2,1)
imagesc(times,freqs,plot_ers_x,CLim);
title(['Accurate: ' exp.singtrlelec_name{ii}]); set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','k','LineStyle','--','LineWidth',1.5) %vertical line
line([567 567],[min(freqs) max(freqs)],'color','m','LineStyle','--','LineWidth',1.5) %vertical line for response screen onset
ylim([3 35]); yticks(5:5:35)
xlim([-700 800]); xticks(-600:200:800)
ylabel('Freqency (Hz)'); xlabel('Time (ms)');
colorbar
% Plot Large Errors
subplot(1,2,2)
imagesc(times,freqs,plot_ers_n,CLim);
title(['Guesses: ' exp.singtrlelec_name{ii}]); set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','k','LineStyle','--','LineWidth',1.5) %vertical line
line([567 567],[min(freqs) max(freqs)],'color','m','LineStyle','--','LineWidth',1.5) %vertical line for response screen onset
ylim([3 35]); yticks(5:5:35)
xlim([-700 800]); xticks(-600:200:800)
ylabel('Freqency (Hz)'); xlabel('Time (ms)');
colorbar
clear plot_ers_x plot_ers_n CLim
end
clear ii i_elect
% :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
% Difference ERS plots
% for ii = 1:length(exp.singletrialselecs)
for ii = 1:5
i_elect = exp.singletrialselecs(ii); %for doing only a selection of electrodes
%mean across subjects
plot_ers_x = squeeze(mean(x_pwr{1,i_elect}(:,:,:),1)); %small errors
plot_ers_n = squeeze(mean(n_pwr{1,i_elect}(:,:,:),1)); %large errors
CLim = [-0.2 0.2]; %set power scale of plot
% Plot Accurate-Guesses
figure; colormap('jet') %open a new figure
imagesc(times,freqs,plot_ers_x-plot_ers_n,CLim);
title(['Accurate-Guesses: ' exp.singtrlelec_name{ii}]); set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','k','LineStyle','--','LineWidth',1.5) %vertical line
line([567 567],[min(freqs) max(freqs)],'color','m','LineStyle','--','LineWidth',1.5) %vertical line for response screen onset
ylim([3 35]); yticks(5:5:35)
xlim([-700 800]); xticks(-600:200:800)
ylabel('Freqency (Hz)'); xlabel('Time (ms)');
colorbar
clear plot_ers_x plot_ers_n CLim
end
clear ii i_elect
% -------------------------------------------------------------------------
% -------------------------------------------------------------------------
% &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
%% Plot spectogram for each subject &
% &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
for i_part = 1:length(exp.participants)
figure('Position', [1 1 624 1016]); colormap('jet') %open a new figure
for ii = 1:5 %just central electrodes
% for ii = 1:length(exp.singletrialselecs)
i_elect = exp.singletrialselecs(ii); %for doing only a selection of electrodes
plot_ers_x = squeeze(x_pwr{1,i_elect}(i_part,:,:)); %small errors data for each subject
plot_ers_n = squeeze(n_pwr{1,i_elect}(i_part,:,:)); %large errors data for each subject
CLim = [0 2000]; %set power scale of plot
figure('Position', [1 1 1685 405]); colormap('jet') %open a new figure
% Plot Small Errors
subplot(1,2,1)
imagesc(times,freqs,plot_ers_x,CLim);
title('Small Errors'); set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','k','LineStyle','--','LineWidth',1.5) %vertical line
line([567 567],[min(freqs) max(freqs)],'color','m','LineStyle','--','LineWidth',1.5) %vertical line for response screen onset
ylim([3 35]); yticks(5:5:35)
xlim([-700 800]); xticks(-600:200:800)
ylabel('Freq (Hz)'); xlabel('Time (ms)');
colorbar
% Plot Large Errors
subplot(1,2,2)
imagesc(times,freqs,plot_ers_n,CLim);
title('Large Errors'); set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','k','LineStyle','--','LineWidth',1.5) %vertical line
line([567 567],[min(freqs) max(freqs)],'color','m','LineStyle','--','LineWidth',1.5) %vertical line for response screen onset
ylim([3 35]); yticks(5:5:35)
xlim([-700 800]); xticks(-600:200:800)
ylabel('Freq (Hz)'); xlabel('Time (ms)');
colorbar
clear plot_ers_x plot_ers_n plot_ers_c
end
% Overall subplot title
supertitle(['Subj ' num2str(exp.participants{i_part}) ': ' exp.singtrlelec_name{ii}],...
'FontSize',10.5)
clear ii i_elect
end
clear i_part CLim
% -------------------------------------------------------------------------
% -------------------------------------------------------------------------
% /////////////////////////////////////////////////////////////////////////
%% Compute power in time and frequency windows for errors
% /////////////////////////////////////////////////////////////////////////
% -------------------------------------------------------------------------
clear x_pwr_win n_pwr_win c_pwr_win
%finds the frequencies you want (gamma (3590 Hz))
freqband = [15 35]; %beta
% freqband = [8 14]; %alpha
% freqband = [8 11]; %low alpha
% freqband = [10 14]; %high alpha
% freqband = [3 8]; %theta
freqlim = find(freqs>=(freqband(1)-0.5) & freqs<=(freqband(2)+0.5));
%finds the times you want from the timess variable
timewin = [-600 -400];
% timewin = [-400 -200];
% timewin = [-200 -100];
% timewin = [-200 0];
% timewin = [0 200];
% timewin = [200 400];
% timewin = [400 600];
% timewin = [300 600];
% timewin = [100 200];
% timewin = [200 300];
% timewin = [300 500];
% timewin = [400 500];
% timewin = [500 600];
timelim = find(times>=timewin(1) & times<=timewin(2));
x_pwr_win = cell(length(exp.singletrialselecs),1); %pre-allocate
n_pwr_win = cell(length(exp.singletrialselecs),1); %pre-allocate
for i_part = 1:length(exp.participants)
% for ii = 1:5 %only central electrodes
for ii = 1:length(exp.singletrialselecs)
i_elect = exp.singletrialselecs(ii); %for doing only a selection of electrodes
x_pwr_win{i_elect}(i_part) = mean(mean(x_pwr{1,i_elect}(i_part,freqlim,timelim),2),3); %small errors
n_pwr_win{i_elect}(i_part) = mean(mean(n_pwr{1,i_elect}(i_part,freqlim,timelim),2),3); %large errors
end
clear ii i_elect
end
clear i_part
% -------------------------------------------------------------------------
% +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
% -------------------------------------------------------------------------
%% Run nonparametric statistics
% for ii = 1:length(exp.singletrialselecs)
for ii = 1:5 %test central electrodes only
i_elect = exp.singletrialselecs(ii); %for doing only a selection of electrodes
sgnrank_pwr_win_elect{ii} = exp.singtrlelec_name{ii}; %save name
% Sign rank test
% accurate v guesses
[p,h,stat] = signrank(x_pwr_win{i_elect}(:),n_pwr_win{i_elect}(:));
sgnrank_pwr_win(ii,1) = p;
sgnrank_pwr_win(ii,2) = h;
sgnrank_pwr_win_stats(ii,1) = stat;
clear h p stat i_elect
end
clear ii i_elect
% Correction w/FDR
[h,crit_p,adj_ci,adj_p] = fdr_bh(sgnrank_pwr_win(:,1),0.05);
sgnrank_pwr_win(:,3) = adj_p;
sgnrank_pwr_win(:,4) = h;
clear h crit_p adj_ci adj_p
% :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%% Run parametric statistics
% for ii = 1:length(exp.singletrialselecs)
for ii = 1:5 %test central electrodes only
i_elect = exp.singletrialselecs(ii); %for doing only a selection of electrodes
ttest_pwr.elect{ii} = exp.singtrlelec_name{ii}; %save name
% t-test
[h,p,ci,stat] = ttest(x_pwr_win{i_elect}(:),n_pwr_win{i_elect}(:));
ttest_pwr_win(ii,1) = h;
ttest_pwr_win(ii,2) = p;
ttest_pwr_ci_win(ii,1) = ci(1);
ttest_pwr_ci_win(ii,2) = ci(2);
ttest_pwr_stats_win(ii,1).electrode = stat;
clear h p ci stat
end
clear ii i_elect
% Correction w/FDR
[h,crit_p,adj_ci,adj_p] = fdr_bh(ttest_pwr_win(:,2),0.05);
ttest_pwr_win(:,4) = adj_p;
ttest_pwr_win(:,3) = h;
clear h crit_p adj_ci adj_p
% $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
% $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
%% Run Permutation test
nperms = 10000; %number of permutations used to estimate null distribution
% re-set values
permtest.zval_obs = [];
permtest.p_z = [];
permtest.p_n = [];
% Permutation test at each electrode
for ii = 1:5 %test central electrodes only
i_elect = exp.singletrialselecs(ii); %for doing only a selection of electrodes
permtest.elect{ii,1} = exp.singtrlelec_name{i_elect};
obs_pwr = n_pwr_win{i_elect}(:); %large error
n_resp = length(obs_pwr);
% Make distribution of null-hypothesis test statistic
zval_perm = zeros(1,nperms); %pre-allocate
for i_perm = 1:nperms
order_resp = randperm(n_resp); %randomly set order of data
[p,h,stat] = signrank(x_pwr_win{i_elect}(:),n_pwr_win{i_elect}(order_resp));
zval_perm(i_perm) = stat.zval;
clear order_resp p h stat
end
clear i_perm pval
% Get observed zval value
[p,h,stat] = signrank(x_pwr_win{i_elect}(:),obs_pwr);
permtest.obs_zval(ii) = stat.zval;
% Plot null distribution
% figure; histogram(zval_perm)
% Get p-value based on Z distribution
% **null distribution needs to be at least approximately Gaussian
% *can use 2-tail only when null distribution is Gaussian, else 1-tail
% Z_val = (permtest.obs_zval(ii) - mean(zval_perm))/std(zval_perm);
% p_z = normcdf(Z_val); %lower-tail
% permtest.p_z(ii) = normcdf(Z_val,'upper'); %upper-tailed
[h,permtest.p_z(ii)] = ztest(permtest.obs_zval(ii), mean(zval_perm), std(zval_perm)); %two-tailed
% Get p-value based on count
% permtest.p_n(ii) = sum(permtest.obs_zval(ii) < zval_perm)/nperms; %upper-tailed
permtest.p_n(ii) = sum(abs(permtest.obs_zval(ii)) < abs(zval_perm))/nperms; %two-tailed
clear Z_val h pval zval_perm obs_pwer p stat i_elect
end
clear n_resp nperms ii
% :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%% Descriptive statistics
ii = 3;
i_elect = exp.singletrialselecs(ii); %for doing only a selection of electrodes
% nanmean(x_pwr_win{i_elect}(:))
% nanmean(n_pwr_win{i_elect}(:))
%
% nanstd(x_pwr_win{i_elect}(:))
% nanstd(n_pwr_win{i_elect}(:))
bar_vals = [nanmean(x_pwr_win{i_elect}(:)) nanmean(n_pwr_win{i_elect}(:))];
bar_errs = [(nanstd(x_pwr_win{i_elect}(:))/sqrt(length(exp.participants)))...
(nanstd(n_pwr_win{i_elect}(:))/sqrt(length(exp.participants)))];
% Bar graph
figure;
barweb(bar_vals, bar_errs);
% ylim([0.1 0.2]);
legend('Accurate','Guesses');
title([exp.singtrlelec_name{ii} ': ' num2str(timewin(1)) ' to ' num2str(timewin(2)) ' ms; ' num2str(freqband(1)) '-' num2str(freqband(2)) ' Hz'])
clear bar_vals bar_errs
% -------------------------------------------------------------------------
% /////////////////////////////////////////////////////////////////////////
%% Correlate power with errors
% /////////////////////////////////////////////////////////////////////////
% -------------------------------------------------------------------------
% :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
% Compute power in time and frequency windows
% :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
clear current_power
%finds the frequencies you want
% freqband = [8 14]; %alpha
% freqband = [8 11]; %low alpha
% freqband = [11 14]; %high alpha
freqband = [3 8]; %theta
freqlim = find(freqs>=(freqband(1)-0.5) & freqs<=(freqband(2)+0.5));
%finds the times you want from the timess variable
timewin = [-600 -400];
% timewin = [-400 -200];
% timewin = [-200 0];
% timewin = [0 200];
% timewin = [200 400];
% timewin = [400 600];
timelim = find(times>=timewin(1) & times<=timewin(2));
current_power = cell(length(exp.participants),length(exp.singletrialselecs)); %pre-allocate
for i_part = 1:length(exp.participants)
% for ii = 1:5 %only central electrodes
for ii = 1:length(exp.singletrialselecs)
i_elect = exp.singletrialselecs(ii); %for doing only a selection of electrodes
% all_ersp is (participant x electrode).trials(freq x time x trial)
% part_ersp = all_ersp{i_part,i_elect}; %get single subject's ersp
part_ersp = all_ersp_Z{i_part,i_elect}.trials; %get single subject's ersp
for i_trial = 1:(size(part_ersp,3))
% current_power{i_part,i_elect}(i_trial) = squeeze(mean(mean(abs(part_ersp(freqlim,timelim,i_trial)),1),2));
current_power{i_part,i_elect}(i_trial) = squeeze(mean(mean(part_ersp(freqlim,timelim,i_trial),1),2));
end
clear part_ersp i_trial
% plot trial power on a histogram
% figure; hist(current_power{i_part,i_elect},30)
% ylabel('Count');
% title(['Subj ' num2str(exp.participants{i_part}) '-' exp.singtrlelec_name{ii}])
end
end
clear i_elect i_part timelim
% /////////////////////////////////////////////////////////////////////////
%% Correlate power with degrees error each subject
% /////////////////////////////////////////////////////////////////////////
% Loop through each Participant & plot correlation
for i_part = 1:length(exp.participants) % --------------
for ii = 1 %only central electrodes
% for ii = 1:length(exp.singletrialselecs)
i_elect = exp.singletrialselecs(ii); %for doing only a selection of electrodes
[rho,pval] = circ_corrcl(circ_ang2rad(resp_errdeg{i_part}),current_power{i_part,i_elect});
% correlation betwen power and errors and plot
figure; polarscatter(circ_ang2rad(resp_errdeg{i_part}),current_power{i_part,i_elect})
hold on
pog = convhull(polyshape(circ_ang2rad(resp_errdeg{i_part}),current_power{i_part,i_elect}));
polarplot(pog.Vertices(:,1),pog.Vertices(:,2))
title(['Subj ' num2str(exp.participants{i_part}) '-' exp.singtrlelec_name{ii}...
': rho=' num2str(round(rho,2)) ' pval=' num2str(round(pval,2))])
clear x y rho pval pog pog1
end
clear i_elect ii
end
clear i_part
% /////////////////////////////////////////////////////////////////////////
%% Correlate power with degrees error
% /////////////////////////////////////////////////////////////////////////
% Create one big array of power
all_currentpwr = cell(1,length(exp.singletrialselecs)); %pre-allocate
for ii = 1:length(exp.singletrialselecs)
i_elect = exp.singletrialselecs(ii); %for doing only a selection of electrodes
all_currentpwr{1,i_elect} = cat(2,current_power{1:end,i_elect});
end
clear ii i_elect
% Put all the response errors across subjects into vector
resp_errdeg_cat = cat(2,resp_errdeg{1:end});
% :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
% Plot correlation overall all subjects and trials
for ii = 1 %only central electrodes
% for ii = 1:length(exp.singletrialselecs)
i_elect = exp.singletrialselecs(ii); %for doing only a selection of electrodes
[rho,pval] = circ_corrcl(circ_ang2rad(resp_errdeg_cat),all_currentpwr{1,i_elect});
% correlation betwen power and errors and plot
figure; polarscatter(circ_ang2rad(resp_errdeg_cat),all_currentpwr{1,i_elect},'filled','MarkerFaceAlpha',.5)
title([exp.singtrlelec_name{ii} ': ' num2str(timewin(1)) ' to ' num2str(timewin(2)) ' ms; ' num2str(freqband(1)) '-' num2str(freqband(2)) ' Hz'...
': rho=' num2str(round(rho,2)) ' pval=' num2str(round(pval,2))])
clear x y rho pval
end
clear i_elect ii
% :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
% /////////////////////////////////////////////////////////////////////////
%% Correlate power with degrees error each subject in one plot
% /////////////////////////////////////////////////////////////////////////
% Loop through each Participant & plot correlation
figure('Position', [1 1 1893 402])
for i_part = 1:length(exp.participants) % --------------
for ii = 2 %only central electrodes
% for ii = 1:length(exp.singletrialselecs)
i_elect = exp.singletrialselecs(ii); %for doing only a selection of electrodes
[rho,pval] = circ_corrcl(circ_ang2rad(resp_errdeg{i_part}),current_power{i_part,i_elect});
% correlation betwen power and errors and plot
subtightplot(2,13,i_part,[0.01 0.03],[0.001 0.001],[0.05 0.05])
polarscatter(circ_ang2rad(resp_errdeg{i_part}),current_power{i_part,i_elect},16)
hold on
pog = convhull(polyshape(circ_ang2rad(resp_errdeg{i_part}),current_power{i_part,i_elect}));
polarplot(pog.Vertices(:,1),pog.Vertices(:,2))
% title(['Subj ' num2str(exp.participants{i_part}) ': rho=' num2str(round(rho,2)) ' pval=' num2str(round(pval,2))])
title(['rho=' num2str(round(rho,2)) ' pval=' num2str(round(pval,2))],'FontSize',8)
clear x y rho pval pog
end
% Overall subplot title
supertitle([exp.singtrlelec_name{ii} ': ' num2str(timewin(1)) ' to ' num2str(timewin(2)) ' ms; '...
num2str(freqband(1)) '-' num2str(freqband(2)) ' Hz'])
clear i_elect ii
end
clear i_part
% :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
% $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
% -------------------------------------------------------------------------
% --- Permutation test ---
% -------------------------------------------------------------------------
% $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
nperms = 10000; %number of permutations used to estimate null distribution
resp_errrad_cat = circ_ang2rad(resp_errdeg_cat); %convert to radians for circular corr
n_resp = length(resp_errrad_cat); %number of observations to permute
% re-set values
permtest.rho_obs = [];
permtest.p_z = [];
permtest.p_n = [];
% Permutation test at each electrode
for ii = 1:5 %test central electrodes only
i_elect = exp.singletrialselecs(ii); %for doing only a selection of electrodes
permtest.elect{ii,1} = exp.singtrlelec_name{i_elect};
obs_power = all_currentpwr{1,i_elect}; %get power data from electrode
% Make distribution of null-hypothesis test statistic
rho_perm = zeros(1,nperms); %pre-allocate
for i_perm = 1:nperms
order_resp = randperm(n_resp); %randomly set order of data
[rho_perm(i_perm), pval] = circ_corrcl(resp_errrad_cat(order_resp),obs_power);
clear order_resp
end
clear i_perm pval i_elect
% Get observed rho value
[permtest.rho_obs(ii), pval] = circ_corrcl(resp_errrad_cat,obs_power);
% Plot null distribution
% figure; histogram(rho_perm)
% Get p-value based on Z distribution
% **null distribution needs to be at least approximately Gaussian
% *can use 2-tail only when null distribution is Gaussian, else 1-tail
Z_val = (permtest.rho_obs(ii) - mean(rho_perm))/std(rho_perm);
% p_z = normcdf(Z_val); %lower-tail
permtest.p_z(ii) = normcdf(Z_val,'upper'); %upper-tailed
% [h,p_z] = ztest(rho_obs, mean(rho_perm), std(rho_perm)); %two-tailed
% Get p-value based on count
permtest.p_n(ii) = sum(permtest.rho_obs(ii) < rho_perm)/nperms; %upper-tailed
% p_n = sum(abs(rho_obs) < abs(rho_perm))/nperms; %two-tailed
clear Z_val h pval rho_perm obs_power
end
clear n_resp nperms ii
% Correction w/FDR
[h,crit_p,adj_ci,permtest.adj_pz] = fdr_bh(permtest.p_z,0.05);
[h,crit_p,adj_ci,permtest.adj_pn] = fdr_bh(permtest.p_n,0.05);
clear h crit_p adj_ci adj_p
% '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
%% ''''''''''''''''''''''' Topographys ''''''''''''''''''''''''''''''
% '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
% List electrodes to get ERP topograph plots (need all of them)
elect_erp = [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];
el_erp_names = {'M2';'Oz';'Pz';'Cz';'FCz';'Fz';'O1';'O2';'PO3';'PO4';'P7';'P8';'P5';'P6';'P3';'P4';'CP5';...
'CP6';'CP1';'CP2';'C3';'C4';'FC5';'FC6';'FC1';'FC2';'F7';'F8';'F3';'F4';'Fp1';'Fp2'};
% Set the range of time to consider
tWin{1} = [-600 -400];
tWin{2} = [-400 -200];
tWin{3} = [-200 0];
tWin{4} = [0 200];
tWin{5} = [200 400];
tWin{6} = [400 600];
% tWin{1} = [-600 -300];
% tWin{2} = [-300 0];
% tWin{3} = [0 300];
% tWin{4} = [300 600];
%finds the frequencies you want
freqband = [8 14]; %alpha
% freqband = [8 11]; %low alpha
% freqband = [10 14]; %high alpha
% freqband = [3 8]; %theta
freqlim = find(freqs>=(freqband(1)-0.5) & freqs<=(freqband(2)+0.5));
% ERSP averaged across subjects
% ersp(participants x conditions x events x electrodes x frequencies x timepoints)
out_ersp_elect = squeeze(mean(ersp(:,1,1,:,:,:),1));
CLim = [40 70]; %set power scale of plot
colormap('jet')
for tw_i = 1:length(tWin) %loop through several time windows
itWin = tWin{tw_i}; %select each time range if looping
%finds the times you want from the times variable
time_window = find(times>= itWin(1),1):find(times>= itWin(2),1)-1;
% figure('Color',[1 1 1],'Position',[1 1 941 349]);
figure('Color',[1 1 1]);
set(gca,'Color',[1 1 1]);
temp = mean(mean(out_ersp_elect(:,freqlim,time_window),2),3)';
temp(1) = NaN; %not M2 electrode
topoplot(temp,ALLEEG(1).chanlocs,'whitebk','on','plotrad',0.6,'maplimits',CLim,...
'plotchans',elect_erp,'emarker',{'.','k',11,1})
title([num2str(freqband(1)) '-' num2str(freqband(2)) ' Hz: ' num2str(itWin(1)) ' to ' num2str(itWin(2)) ' ms']);
t = colorbar('peer',gca);
set(get(t,'ylabel'),'String', 'Power (uV^2)');
clear itWin time_window temp
end
clear tw_i
clear freqlim CLim
% :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%% Correlate power in time and frequency windows with response error
% :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
clear current_power
%finds the frequencies you want
freqband = [8 14]; %alpha
% freqband = [8 11]; %low alpha
% freqband = [10 14]; %high alpha
% freqband = [3 8]; %theta
freqlim = find(freqs>=(freqband(1)-0.5) & freqs<=(freqband(2)+0.5));
%finds the times you want from the timess variable
% timewin = [-600 -400];
% timewin = [-400 -200];
timewin = [-200 0];
% timewin = [0 200];
% timewin = [200 400];
% timewin = [400 600];
timelim = find(times>=timewin(1) & times<=timewin(2));
current_power = cell(length(exp.participants),length(exp.singletrialselecs)); %pre-allocate
for i_part = 1:length(exp.participants)
% for ii = 1:5 %only central electrodes
for ii = 1:length(exp.singletrialselecs)
i_elect = exp.singletrialselecs(ii); %for doing only a selection of electrodes
% all_ersp is (participant x electrode).trials(freq x time x trial)
% part_ersp = all_ersp{i_part,i_elect}; %get single subject's ersp
part_ersp = all_ersp_Z{i_part,i_elect}.trials; %get single subject's ersp
for i_trial = 1:(size(part_ersp,3))
% current_power{i_part,i_elect}(i_trial) = squeeze(mean(mean(abs(part_ersp(freqlim,timelim,i_trial)),1),2));
current_power{i_part,i_elect}(i_trial) = squeeze(mean(mean(part_ersp(freqlim,timelim,i_trial),1),2));
end
clear part_ersp i_trial
% plot trial power on a histogram
% figure; hist(current_power{i_part,i_elect},30)
% ylabel('Count');
% title(['Subj ' num2str(exp.participants{i_part}) '-' exp.singtrlelec_name{ii}])
end
end
clear i_elect i_part timelim
% /////////////////////////////////////////////////////////////////////////
%% Correlate power with degrees error each subject
% /////////////////////////////////////////////////////////////////////////
% Loop through each participant & plot correlation
for i_part = 1:length(exp.participants) % --------------
for ii = 1 %only central electrodes
% for ii = 1:length(exp.singletrialselecs)
i_elect = exp.singletrialselecs(ii); %for doing only a selection of electrodes
[rho,pval] = circ_corrcl(circ_ang2rad(resp_errdeg{i_part}),current_power{i_part,i_elect});
% correlation betwen power and errors and plot
figure; polarscatter(circ_ang2rad(resp_errdeg{i_part}),current_power{i_part,i_elect})
title(['Subj ' num2str(exp.participants{i_part}) '-' exp.singtrlelec_name{ii}...
': rho=' num2str(round(rho,2)) ' pval=' num2str(round(pval,2))])
clear x y rho pval
end
clear i_elect ii
end
clear i_part
% /////////////////////////////////////////////////////////////////////////
%% Correlate power with degrees error
% /////////////////////////////////////////////////////////////////////////
% Create one big array of power
all_currentpwr = cell(1,length(exp.singletrialselecs)); %pre-allocate
for ii = 1:length(exp.singletrialselecs)
i_elect = exp.singletrialselecs(ii); %for doing only a selection of electrodes
all_currentpwr{1,i_elect} = cat(2,current_power{1:end,i_elect});
end
clear ii i_elect
% Put all the response errors across subjects into vector
resp_errdeg_cat = cat(2,resp_errdeg{1:end});
% :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
% Plot correlation overall all subjects and trials
for ii = 1:5 %only central electrodes
% for ii = 1:length(exp.singletrialselecs)
i_elect = exp.singletrialselecs(ii); %for doing only a selection of electrodes
[rho,pval] = circ_corrcl(circ_ang2rad(resp_errdeg_cat),all_currentpwr{1,i_elect});
% correlation betwen power and errors and plot
figure; polarscatter(circ_ang2rad(resp_errdeg_cat),all_currentpwr{1,i_elect},'filled','MarkerFaceAlpha',.5)
title([exp.singtrlelec_name{ii} ': ' num2str(timewin(1)) ' to ' num2str(timewin(2)) ' ms; ' num2str(freqband(1)) '-' num2str(freqband(2)) ' Hz'...
': rho=' num2str(round(rho,2)) ' pval=' num2str(round(pval,2))])
clear x y rho pval
end
clear i_elect ii
% :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
% $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
% -------------------------------------------------------------------------
% --- Permutation test ---
% -------------------------------------------------------------------------
% $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
nperms = 10000; %number of permutations used to estimate null distribution
resp_errrad_cat = circ_ang2rad(resp_errdeg_cat); %convert to radians for circular corr
n_resp = length(resp_errrad_cat); %number of observations to permute
% re-set values
permtest.rho_obs = [];
permtest.p_z = [];
permtest.p_n = [];
% Permutation test at each electrode
for ii = 1:5 %test central electrodes only
i_elect = exp.singletrialselecs(ii); %for doing only a selection of electrodes
permtest.elect{ii,1} = exp.singtrlelec_name{i_elect};
obs_power = all_currentpwr{1,i_elect}; %get power data from electrode
% Make distribution of null-hypothesis test statistic
rho_perm = zeros(1,nperms); %pre-allocate
for i_perm = 1:nperms
order_resp = randperm(n_resp); %randomly set order of data
[rho_perm(i_perm), pval] = circ_corrcl(resp_errrad_cat(order_resp),obs_power);
clear order_resp
end
clear i_perm pval i_elect
% Get observed rho value
[permtest.rho_obs(ii), pval] = circ_corrcl(resp_errrad_cat,obs_power);
% Plot null distribution
% figure; histogram(rho_perm)
% Get p-value based on Z distribution
% **null distribution needs to be at least approximately Gaussian
% *can use 2-tail only when null distribution is Gaussian, else 1-tail
Z_val = (permtest.rho_obs(ii) - mean(rho_perm))/std(rho_perm);
% p_z = normcdf(Z_val); %lower-tail
permtest.p_z(ii) = normcdf(Z_val,'upper'); %upper-tailed
% [h,p_z] = ztest(rho_obs, mean(rho_perm), std(rho_perm)); %two-tailed
% Get p-value based on count
permtest.p_n(ii) = sum(permtest.rho_obs(ii) < rho_perm)/nperms; %upper-tailed
% p_n = sum(abs(rho_obs) < abs(rho_perm))/nperms; %two-tailed
clear Z_val h pval rho_perm obs_power
end
clear n_resp nperms ii
% #########################################################################
% -------------------------------------------------------------------------
% /////////////////////////////////////////////////////////////////////////
%% Separate response errors by power
% /////////////////////////////////////////////////////////////////////////
% -------------------------------------------------------------------------
% #########################################################################
% :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
% Compute power in time and frequency windows
% :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
clear current_power
%finds the frequencies you want
% freqband = [15 35]; %beta
% freqband = [8 14]; %alpha
% freqband = [8 11]; %low alpha
% freqband = [10 14]; %high alpha
freqband = [3 8]; %theta
freqlim = find(freqs>=(freqband(1)-0.5) & freqs<=(freqband(2)+0.5));
%finds the times you want from the times variable
% timewin = [-600 -500];
% timewin = [-500 -400];
% timewin = [-400 -300];
% timewin = [-300 -200];
% timewin = [-200 -100];
% timewin = [-100 0];
% timewin = [0 100];
% timewin = [100 200];
% timewin = [200 300];