/
limo_eeg.m
1127 lines (975 loc) · 54.2 KB
/
limo_eeg.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
function limo_eeg(varargin)
% LIMO_EEG - start up and master function of the LIMO_EEG toolbox
% Calling this function brings up different GUIs.
% Each time an option is used it calls subroutines.
% In this function is also implemented the call to the GLM, creating files
% etc .. see input
%
% LIMO_EEG is designed to perform a hierarchical LInear MOdeling of EEG data
% All analyses can be performed with this toolbox but the visualization
% relies heavily on EEGlab functions http://sccn.ucsd.edu/eeglab/
% In addition, the data format is the one used by EEGlab.
%
% INPUT limo_eeg(value,option)
% 1 - load the GUI
% 2,X - call limo_import (time X=1 or freuqency X=2), creating LIMO.mat file and call limo_egg(3)
% 3 - call limo_design_matrix and populate LIMO.design
% 4 - call limo_glm1 (mass univariate) or limo_glm2 (multivariate)
% 5 - shortcut to limo_results, look at possible results and print a report
% 6,C - shortcut to limo_contrast for the current directory,
% ask for a list of contrasts if not given as 2nd argument) and run them all
% e.g. C = [1 1 -1 -1; 1 -1 1 -1]; limo_eeg(6,C) would do those
% two contrasts for the data located in the current dir
%
% LIMO_EEG was primarily designed by Cyril Pernet and Guillaume Rousselet,
% with the contributon of Andrew Stewart, Nicolas Chauveau, Carl Gaspar,
% Luisa Frei, Ignacio Suay Mas and Marianne Latinus. These authors are thereafter
% referred as the LIMO Team
%
% THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
% APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS
% AND/OR OTHER PARTIES PROVIDE THE PROGRAM “AS IS�? WITHOUT WARRANTY OF ANY KIND,
% EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
% WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
% THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.
% SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
% REPAIR OR CORRECTION.
%
% IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY
% COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS
% PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL
% OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING
% BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR
% THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH
% HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
%
% Cyril Pernet & Andrew Stewart v6 21/01/2014
% ------------------------------------------
% Copyright (C) LIMO Team 2014
% make sure paths are ok
local_path = which('limo_eeg');
root = local_path(1:max(find(local_path == filesep))-1);
addpath([root filesep 'limo_cluster_functions'])
addpath([root filesep 'help'])
% in case data are already there
if isempty(varargin);
global EEG
varargin={1};
end
% start
switch varargin{1}
%------
case {1}
% ------------------------------------------------------------------------
% GUI
% ------------------------------------------------------------------------
% if not called via the eeglab menu but via the matlab command window
% show the GUI
disp('LIMO_EEG was primarily designed by Cyril Pernet and Guillaume Rousselet,');
disp(' with the contributon of Andrew Stewart, Nicolas Chauveau, Carl Gaspar,');
disp('Luisa Frei, Ignacio Suay Mas and Marianne Latinus. These authors are thereafter');
disp(' referred as the LIMO Team');
disp(' ')
disp('LIMO_EEG Copyright (C) 2010 LIMO TEAM');
disp('This program comes with ABSOLUTELY NO WARRANTY.');
disp('This is free software, and you are welcome to redistribute');
disp('it under certain conditions - type help limo_eeg for details');
disp(' ');
limo_gui
%------
case {2}
% ------------------------------------------------------------------------
% IMPORT
% ------------------------------------------------------------------------
% the EEG data are not imported but path / name is saved in LIMO.mat
% Cat and Cont are imported manually from a txt or mat file
% Other informations are i) the starting time point (sec), ii) the method to
% use (if multivariate stats have to be computed) and iii) the working
% directory where all informations will be saved
clc;
if varargin{2} == 1
limo_import_t; % Data from electrodes over time in each trial
elseif varargin{2} == 2
limo_import_f; % Data from electrodes spectral power in each trial
elseif varargin{2} == 3
limo_import_tf; % Data from electrodes spectral power over time in each trial
end
% if bootstrap with tfce - get the neighbourghing matrix now so
% the estimation and results can be all computed without any other
% input from user (see limo_eeg(5))
% if bootstrap do TFCE
try
load LIMO
if LIMO.design.bootstrap == 1
if ~isfield(LIMO.data,'neighbouring_matrix')
answer = questdlg('load or compute neighbouring matrix?','channel neighbouring definition','Load','Compute','Compute');
if strcmp(answer,'Load')
[file,path,whatsup] = uigetfile('*.mat','select neighbourghing matrix (or expected chanloc file)');
if whatsup == 0
disp('selection aborded');
return
else
cd(path); load(file); cd(LIMO.dir);
end
else
channeighbstructmat = limo_expected_chanlocs(LIMO.data.data, LIMO.data.data_dir);
end
LIMO.data.neighbouring_matrix = channeighbstructmat;
save LIMO LIMO
end
end
disp('import done');
catch
disp('import aborded');
return
end
% now estimate the design matrix
limo_eeg(3)
%------
case {3}
% ------------------------------------------------------------------------
% DESIGN MATRIX
% ------------------------------------------------------------------------
% returns the design matrix and some info about the matrix
% some files are also created to be filled during the model computation
% get the LIMO.mat
try
load LIMO
catch
[file,dir_path] = uigetfile('LIMO.mat','select a LIMO.mat file');
if file ==0
return
else
cd (dir_path); load LIMO.mat;
end
end
cd (LIMO.dir);
% Check data where specified and load
try
cd (LIMO.data.data_dir);
disp('reloading data ..');
EEG=pop_loadset(LIMO.data.data);
catch
error('error loading data (most likely a memory issue) or cannot find the data ; error line 103/104 cd/pop_loadset')
end
% Load either elec voltage over time, elec power over frequency, or
% electrode time-frequency - depending on declared analysis
if strcmp(LIMO.Analysis,'Time')
Y = EEG.data(:,LIMO.data.trim1:LIMO.data.trim2,:);
clear EEG
elseif strcmp(LIMO.Analysis,'Frequency')
Y = EEG.etc.limo_psd(:,LIMO.data.trim1:LIMO.data.trim2,:);
clear EEG
elseif strcmp(LIMO.Analysis,'Time-Frequency')
clear EEG; disp('Time-Frequency implementation - loading tf data...');
Y=load(LIMO.data.tf_data_filepath); % Load tf data from path in *.set from import stage
Y=Y.limo_tf(:,LIMO.data.trim_low_f:LIMO.data.trim_high_f,LIMO.data.trim1:LIMO.data.trim2,:);
LIMO.data.size4D=size(Y);
LIMO.data.freq_list=repmat(LIMO.data.tf_freqs,[1 numel(LIMO.data.tf_times)]);
LIMO.data.size3D= [LIMO.data.size4D(1) LIMO.data.size4D(2)*LIMO.data.size4D(3) LIMO.data.size4D(4)];
save LIMO LIMO
end
clear ALLCOM ALLEEG CURRENTSET CURRENTSTUDY LASTCOM STUDY
cd (LIMO.dir)
% make the design matrix
disp('computing design matrix');
if strcmp(LIMO.Analysis,'Time-Frequency') % use limo_design_matrix_tf
[LIMO.design.X, LIMO.design.nb_conditions, LIMO.design.nb_interactions,...
LIMO.design.nb_continuous] = limo_design_matrix_tf(Y, LIMO,1);
else % for time or power use limo_design_matrix
[LIMO.design.X, LIMO.design.nb_conditions, LIMO.design.nb_interactions,...
LIMO.design.nb_continuous] = limo_design_matrix(Y, LIMO,1);
end
% update LIMO.mat
if prod(LIMO.design.nb_conditions) > 0 && LIMO.design.nb_continuous == 0
if length(LIMO.design.nb_conditions) == 1
if LIMO.design.nb_conditions == 2
LIMO.design.name = sprintf('Categorical: T-test i.e. %g conditions',LIMO.design.nb_conditions);
else
LIMO.design.name = sprintf('Categorical: 1 way ANOVA with %g conditions',LIMO.design.nb_conditions);
end
else
LIMO.design.name = sprintf('Categorical: N way ANOVA with %g factors',length(LIMO.design.nb_conditions));
end
elseif prod(LIMO.design.nb_conditions) == 0 && LIMO.design.nb_continuous > 0
if LIMO.design.nb_continuous == 1
LIMO.design.name = sprintf('Continuous: Simple Regression');
else
LIMO.design.name = sprintf('Continuous: Multiple Regression with %g continuous variables',LIMO.design.nb_continuous);
end
elseif prod(LIMO.design.nb_conditions) > 0 && LIMO.design.nb_continuous > 0
if length(LIMO.design.nb_conditions) == 1
LIMO.design.name = sprintf('AnCOVA with %g conditions and %g continuous variable(s)',LIMO.design.nb_conditions,LIMO.design.nb_continuous);
else
LIMO.design.name = sprintf('AnCOVA with %g factors and %g continuous variable(s)',length(LIMO.design.nb_conditions),LIMO.design.nb_continuous);
end
end
disp('design matrix done ...')
% fix a bug which occurs if you run several subjects in a row with
% the GUI and use contrasts - a new subject will have a contrast field
% must be a way to solve this properly ??
tofix = isfield(LIMO,'contrast');
if tofix == 1
LIMO.contrast = [];
end
% ---------------
LIMO.design.status = 'to do';
save LIMO LIMO
clear Y Cat Cont
a = questdlg('run the analysis?','Start GLM analysis','Yes','No','Yes');
if strcmp(a,'Yes')
if strcmp(LIMO.Analysis,'Time-Frequency')
limo_eeg_tf(4);
limo_eeg_tf(5);
else
limo_eeg(4);
limo_eeg(5);
end
clear LIMO
limo_gui
else
return
end
%% ------------------------------------------------------------------------
% ANALYZE
% ------------------------------------------------------------------------
% estimates the model specified in (2)
% save all info onto disk
case{4}
% NBOOT (updated if specified in LIMO.design)
% ------------------------------------------
nboot = 599;
% ----------
% get the LIMO.mat
try
load('LIMO.mat');
catch
[file,dir_path,ind] = uigetfile('LIMO.mat','select a LIMO.mat file');
if ind ==0
return
else
cd (dir_path); load LIMO.mat;
end
end
cd (LIMO.dir);
% ---------------- univariate analysis ------------------
% --------------------------------------------------------
if strcmp(LIMO.design.type_of_analysis,'Mass-univariate')
% --------- load files created by limo_design_matrix ------------------
load Yr; load Yhat; load Res; load R2; load Betas;
% ------------- prepare weight matrix -------------------------------------
if strcmp(LIMO.design.method,'WLS') || strcmp(LIMO.design.method,'OLS')
W = ones(size(Yr,1),size(Yr,3));
elseif strcmp(LIMO.design.method,'IRLS')
W = zeros(size(Yr));
end
% ------------ prepare condition/covariates -------------------
if LIMO.design.nb_conditions ~=0
tmp_Condition_effect = NaN(size(Yr,1),size(Yr,2),length(LIMO.design.nb_conditions),2);
end
if LIMO.design.nb_interactions ~=0
tmp_Interaction_effect = NaN(size(Yr,1),size(Yr,2),length(LIMO.design.nb_interactions),2);
end
if LIMO.design.nb_continuous ~=0
tmp_Covariate_effect = NaN(size(Yr,1),size(Yr,2),LIMO.design.nb_continuous,2);
end
% -------------- loop the analysis electrode per electrode
if size(Yr,1) == 1
array = 1;
else
array = find(~isnan(Yr(:,1,1))); % skip empty electrodes
end
if strcmp(LIMO.design.status,'to do')
update = 1;
X = LIMO.design.X;
for e = 1:size(array,1)
electrode = array(e); warning off;
fprintf('analyzing electrode %g/%g \n',electrode,size(Yr,1));
if LIMO.Level == 2
Y = squeeze(Yr(electrode,:,:));
index = find(~isnan(Y(1,:)));
Y = Y(:,index);
LIMO.design.X = X(index,:);
model = limo_glm1(Y',LIMO); warning on;
if isempty(index)
index = [1:size(Y,2)];
end
else % level 1 we should not have any NaNs
index = [1:size(Yr,3)];
model = limo_glm1(squeeze(Yr(electrode,:,:))',LIMO);
end
% update the LIMO.mat (do it only once)
if update == 1
LIMO.model.model_df = model.df;
if LIMO.design.nb_conditions ~=0
LIMO.model.conditions_df = model.conditions.df;
end
if LIMO.design.nb_interactions ~=0
LIMO.model.interactions_df = model.interactions.df;
end
if LIMO.design.nb_continuous ~=0
LIMO.model.continuous_df = model.continuous.df;
end
update = 0;
end
% update the files to be stored on the disk
if strcmp(LIMO.design.method,'IRLS')
W(electrode,:,index) = model.W;
else
W(electrode,index) = model.W;
end
fitted_data = LIMO.design.X*model.betas;
Yhat(electrode,:,index) = fitted_data';
Res(electrode,:,index) = squeeze(Yr(electrode,:,index)) - fitted_data'; clear fitted_data
R2(electrode,:,1) = model.R2_univariate;
R2(electrode,:,2) = model.F;
R2(electrode,:,3) = model.p;
Betas(electrode,:,:,1) = model.betas';
if prod(LIMO.design.nb_conditions) ~=0
if length(LIMO.design.nb_conditions) == 1
tmp_Condition_effect(electrode,:,1,1) = model.conditions.F;
tmp_Condition_effect(electrode,:,1,2) = model.conditions.p;
else
for i=1:length(LIMO.design.nb_conditions)
tmp_Condition_effect(electrode,:,i,1) = model.conditions.F(i,:);
tmp_Condition_effect(electrode,:,i,2) = model.conditions.p(i,:);
end
end
end
if LIMO.design.fullfactorial == 1
if length(LIMO.design.nb_interactions) == 1
tmp_Interaction_effect(electrode,:,1,1) = model.interactions.F;
tmp_Interaction_effect(electrode,:,1,2) = model.interactions.p;
else
for i=1:length(LIMO.design.nb_interactions)
tmp_Interaction_effect(electrode,:,i,1) = model.interactions.F(i,:);
tmp_Interaction_effect(electrode,:,i,2) = model.interactions.p(i,:);
end
end
end
if LIMO.design.nb_continuous ~=0
if LIMO.design.nb_continuous == 1
tmp_Covariate_effect(electrode,:,1,1) = model.continuous.F;
tmp_Covariate_effect(electrode,:,1,2) = model.continuous.p;
else
for i=1:LIMO.design.nb_continuous
tmp_Covariate_effect(electrode,:,i,1) = model.continuous.F(i,:);
tmp_Covariate_effect(electrode,:,i,2) = model.continuous.p(i,:);
end
end
end
end
% save data on the disk and clean out
disp('saving data to disk')
LIMO.design.X = X;
LIMO.design.weights = W;
LIMO.design.status = 'done';
save LIMO LIMO; save Yhat Yhat -v7.3;
save Res Res; save Betas Betas -v7.3;
save R2 R2 -v7.3; clear Yhat Res Betas R2
if prod(LIMO.design.nb_conditions) ~=0
for i=1:length(LIMO.design.nb_conditions)
name = sprintf('Condition_effect_%g',i);
if size(tmp_Condition_effect,1) == 1
tmp = squeeze(tmp_Condition_effect(1,:,i,:));
Condition_effect = NaN(1,size(tmp_Condition_effect,2),2);
Condition_effect(1,:,:) = tmp;
else
Condition_effect = squeeze(tmp_Condition_effect(:,:,i,:));
end
save(name,'Condition_effect','-v7.3')
end
clear Condition_effect tmp_Condition_effect
end
if LIMO.design.fullfactorial == 1
for i=1:length(LIMO.design.nb_interactions)
name = sprintf('Interaction_effect_%g',i);
if size(tmp_Interaction_effect,1) == 1
tmp = squeeze(tmp_Interaction_effect(1,:,i,:));
Interaction_effect = NaN(1,size(tmp_Interaction_effect,2),2);
Interaction_effect(1,:,:) = tmp;
else
Interaction_effect = squeeze(tmp_Interaction_effect(:,:,i,:));
end
save(name,'Interaction_effect','-v7.3')
end
clear Interaction_effect tmp_Interaction_effect
end
if LIMO.design.nb_continuous ~=0
for i=1:LIMO.design.nb_continuous
name = sprintf('Covariate_effect_%g',i);
if size(tmp_Covariate_effect,1) == 1
tmp = squeeze(tmp_Covariate_effect(1,:,i,:));
Covariate_effect = NaN(1,size(tmp_Covariate_effect,2),2);
Covariate_effect(1,:,:) = tmp;
else
Covariate_effect = squeeze(tmp_Covariate_effect(:,:,i,:));
end
save(name,'Covariate_effect','-v7.3')
end
clear Covariate_effect tmp_Covariate_effect
end
clear file electrode filename model reg dir i W
end
% as above for bootstrap under H0
% -------------------------------
boot_go = 0;
if LIMO.design.bootstrap ~=0
if exist('H0','dir')
if strcmp(questdlg('H0 directory detected, overwrite?','data check','Yes','No','No'),'No');
if LIMO.design.tfce == 1
errordlg2('bootstrap skipped - attempting to continue with tfce');
else
return
end
else
boot_go = 1;
end
else
boot_go = 1;
end
end
if boot_go == 1
try
fprintf('\n %%%%%%%%%%%%%%%%%%%%%%%% \n Bootstrapping data with the GLM can take a while, be patient .. \n %%%%%%%%%%%%%%%%%%%%%%%% \n')
mkdir H0; load Yr;
if LIMO.design.bootstrap > 599
nboot = LIMO.design.bootstrap;
end
if LIMO.Level == 2
boot_table = limo_create_boot_table(Yr,nboot);
else
boot_table = randi(size(Yr,3),size(Yr,3),nboot);
end
H0_Betas = NaN(size(Yr,1), size(Yr,2), size(LIMO.design.X,2), nboot);
H0_R2 = NaN(size(Yr,1), size(Yr,2), 3, nboot); % stores R, F and p values for each boot
if LIMO.design.nb_conditions ~= 0
tmp_H0_Conditions = NaN(size(Yr,1), size(Yr,2), length(LIMO.design.nb_continuous), 2, nboot);
end
if LIMO.design.nb_interactions ~=0
tmp_H0_Interaction_effect = NaN(size(Yr,1),size(Yr,2),length(LIMO.design.nb_interactions), 2, nboot);
end
if LIMO.design.nb_continuous ~= 0
tmp_H0_Covariates = NaN(size(Yr,1), size(Yr,2), LIMO.design.nb_continuous, 2, nboot);
end
warning off;
X = LIMO.design.X;
h = waitbar(0,'bootstraping data','name','% done');
for e = 1:size(array,1)
electrode = array(e);
waitbar(e/size(array,1))
fprintf('bootstrapping electrode %g \n',electrode);
if LIMO.Level == 2
Y = squeeze(Yr(electrode,:,:));
index = find(~isnan(Y(1,:)));
model = limo_glm1_boot(Y(:,index)',X(index,:),LIMO.design.nb_conditions,LIMO.design.nb_interactions,LIMO.design.nb_continuous,LIMO.design.zscore,LIMO.design.method,boot_table{electrode});
else
% index = [1:size(Yr,3)];
model = limo_glm1_boot(squeeze(Yr(electrode,:,:))',LIMO,boot_table);
end
% update the files to be stored on the disk
H0_Betas(electrode,:,:,:) = model.Betas;
for B = 1:nboot % now loop because we use cells
H0_R2(electrode,:,1,B) = model.R2{B};
H0_R2(electrode,:,2,B) = model.F{B};
H0_R2(electrode,:,3,B) = model.p{B};
if prod(LIMO.design.nb_conditions) ~=0
if length(LIMO.design.nb_conditions) == 1
tmp_H0_Conditions(electrode,:,1,1,B) = model.conditions.F{B};
tmp_H0_Conditions(electrode,:,1,2,B) = model.conditions.p{B};
else
for i=1:length(LIMO.design.nb_conditions)
tmp_H0_Conditions(electrode,:,i,1,B) = model.conditions.F{B}(i,:);
tmp_H0_Conditions(electrode,:,i,2,B) = model.conditions.p{B}(i,:);
end
end
end
if LIMO.design.fullfactorial == 1
if length(LIMO.design.nb_interactions) == 1
tmp_H0_Interaction_effect(electrode,:,1,1,B) = model.interactions.F{B};
tmp_H0_Interaction_effect(electrode,:,1,2,B) = model.interactions.p{B};
else
for i=1:length(LIMO.design.nb_interactions)
tmp_H0_Interaction_effect(electrode,:,i,1,B) = model.interactions.F{B}(i,:);
tmp_H0_Interaction_effect(electrode,:,i,2,B) = model.interactions.p{B}(i,:);
end
end
end
if LIMO.design.nb_continuous ~=0
if LIMO.design.nb_continuous == 1
tmp_H0_Covariates(electrode,:,1,1,B) = model.continuous.F{B};
tmp_H0_Covariates(electrode,:,1,2,B) = model.continuous.p{B};
else
for i=1:LIMO.design.nb_continuous
tmp_H0_Covariates(electrode,:,i,1,B) = model.continuous.F{B}(i,:);
tmp_H0_Covariates(electrode,:,i,2,B) = model.continuous.p{B}(i,:);
end
end
end
end
end
close(h)
warning on;
% save data on the disk and clear out
cd H0
save boot_table boot_table
save H0_Betas H0_Betas -v7.3
save H0_R2 H0_R2 -v7.3
if prod(LIMO.design.nb_conditions) ~=0
for i=1:length(LIMO.design.nb_conditions)
name = sprintf('H0_Condition_effect_%g',i);
H0_Condition_effect = squeeze(tmp_H0_Conditions(:,:,i,:,:));
save(name,'H0_Condition_effect','-v7.3');
clear H0_Condition_effect
end
clear tmp_H0_Conditions
end
if LIMO.design.fullfactorial == 1
for i=1:length(LIMO.design.nb_interactions)
name = sprintf('H0_Interaction_effect_%g',i);
H0_Interaction_effect = squeeze(tmp_H0_Interaction_effect(:,:,i,:,:));
save(name,'H0_Interaction_effect','-v7.3');
clear H0_Interaction_effect
end
clear tmp_H0_Interaction_effect
end
if LIMO.design.nb_continuous ~=0
for i=1:length(LIMO.design.nb_continuous)
name = sprintf('H0_Covariate_effect_%g',i);
H0_Covariate_effect = squeeze(tmp_H0_Covariates(:,:,i,:,:));
save(name,'H0_Covariate_effect','-v7.3');
clear H0_Covariate_effect
end
clear tmp_H0_Covariates
end
clear electrode model H0_R2; cd ..
disp(' ');
catch boot_error
disp('an error occured while attempting to bootstrap the data')
fprintf('%s \n',boot_error.message); return
end
end
% TFCE if requested
% --------------
if LIMO.design.tfce == 1
load Yr;
if isfield(LIMO.data,'neighbouring_matrix') 1 && LIMO.design.bootstrap ~=0
clear Yr;
if exist('TFCE','dir')
if strcmp(questdlg('TFCE directory detected, overwrite?','data check','Yes','No','No'),'No');
return
end
end
fprintf('\n %%%%%%%%%%%%%%%%%%%%%%%% \n Computing TFCE for GLM takes a while, be patient .. \n %%%%%%%%%%%%%%%%%%%%%%%% \n')
mkdir TFCE;
% R2
load R2.mat; fprintf('Creating R2 TFCE scores \n'); cd('TFCE');
if size(R2,1) == 1
tfce_score(1,:) = limo_tfce(1, squeeze(R2(:,:,2)),LIMO.data.neighbouring_matrix);
else
tfce_score = limo_tfce(2, squeeze(R2(:,:,2)),LIMO.data.neighbouring_matrix);
end
save('tfce_R2','tfce_score'); clear R2; cd ..;
cd('H0'); fprintf('Thresholding H0_R2 using TFCE \n'); load H0_R2;
if size(H0_R2,1) == 1
tfce_H0_score(1,:,:) = limo_tfce(1, squeeze(H0_R2(:,:,2,:)),LIMO.data.neighbouring_matrix);
else
tfce_H0_score = limo_tfce(2, squeeze(H0_R2(:,:,2,:)),LIMO.data.neighbouring_matrix);
end
save('tfce_H0_R2','tfce_H0_score'); clear H0_R2; cd ..;
% conditions
if prod(LIMO.design.nb_conditions) ~=0
for i=1:length(LIMO.design.nb_conditions)
name = sprintf('Condition_effect_%g.mat',i); load(name);
cd('TFCE'); fprintf('Creating Condition %g TFCE scores \n',i)
if size(Condition_effect,1) == 1
tfce_score(1,:) = limo_tfce(1, squeeze(Condition_effect(:,:,1)),LIMO.data.neighbouring_matrix);
else
tfce_score = limo_tfce(2, squeeze(Condition_effect(:,:,1)),LIMO.data.neighbouring_matrix);
end
full_name = sprintf('tfce_%s',name); save(full_name,'tfce_score');
clear Condition_effect tfce_score; cd ..
end
cd('H0'); fprintf('Creating H0 Condition(s) TFCE scores \n');
for i=1:length(LIMO.design.nb_conditions)
name = sprintf('H0_Condition_effect_%g.mat',i); load(name);
if size(H0_Condition_effect,1)
tfce_H0_score(1,:,:) = limo_tfce(1,squeeze(H0_Condition_effect(:,:,1,:)),LIMO.data.neighbouring_matrix);
else
tfce_H0_score = limo_tfce(2,squeeze(H0_Condition_effect(:,:,1,:)),LIMO.data.neighbouring_matrix);
end
full_name = sprintf('tfce_%s',name); save(full_name,'tfce_H0_score');
clear H0_Condition_effect tfce_H0_score;
end
cd ..
end
% interactions
if LIMO.design.fullfactorial == 1
for i=1:length(LIMO.design.fullfactorial)
name = sprintf('Interaction_effect_%g.mat',i); load(name);
cd('TFCE'); fprintf('Creating Interaction %g TFCE scores \n',i)
if size(Interaction_effect,1) == 1
tfce_score(1,:) = limo_tfce(1,squeeze(Interaction_effect(:,:,1)),LIMO.data.neighbouring_matrix);
else
tfce_score = limo_tfce(2,squeeze(Interaction_effect(:,:,1)),LIMO.data.neighbouring_matrix);
end
full_name = sprintf('tfce_%s',name); save(full_name,'tfce_score');
clear Interaction_effect tfce_score; cd ..
end
cd('H0'); fprintf('Creating H0 Interaction(s) TFCE scores \n');
for i=1:length(LIMO.design.fullfactorial)
name = sprintf('H0_Interaction_effect_%g.mat',i); load(name);
if size(H0_Interaction_effect,1) == 1
tfce_H0_score(1,:,:) = limo_tfce(1,squeeze(H0_Interaction_effect(:,:,1,:)),LIMO.data.neighbouring_matrix);
else
tfce_H0_score = limo_tfce(2,squeeze(H0_Interaction_effect(:,:,1,:)),LIMO.data.neighbouring_matrix);
end
full_name = sprintf('tfce_%s',name); save(full_name,'tfce_H0_score');
clear H0_Interaction_effect tfce_H0_score;
end
cd ..
end
% covariates / continuous regressors
if LIMO.design.nb_continuous ~=0
for i=1:LIMO.design.nb_continuous
name = sprintf('Covariate_effect_%g.mat',i); load(name);
cd('TFCE'); fprintf('Creating Covariate %g TFCE scores \n',i);
if size(Covariate_effect,1) == 1
tfce_score(1,:) = limo_tfce(1,squeeze(Covariate_effect(:,:,1)),LIMO.data.neighbouring_matrix);
else
tfce_score = limo_tfce(2,squeeze(Covariate_effect(:,:,1)),LIMO.data.neighbouring_matrix);
end
full_name = sprintf('tfce_%s',name); save(full_name,'tfce_score');
clear Covariate_effect tfce_score; cd ..
end
cd('H0'); fprintf('Creating H0 Covariate(s) TFCE scores \n');
for i=1:LIMO.design.nb_continuous
name = sprintf('H0_Covariate_effect_%g.mat',i); load(name);
if size(H0_Covariate_effect,1)
tfce_H0_score(1,:,:) = limo_tfce(1,squeeze(H0_Covariate_effect(:,:,1,:)),LIMO.data.neighbouring_matrix);
else
tfce_H0_score = limo_tfce(2,squeeze(H0_Covariate_effect(:,:,1,:)),LIMO.data.neighbouring_matrix);
end
full_name = sprintf('tfce_%s',name); save(full_name,'tfce_H0_score');
clear H0_Covariate_effect tfce_H0_score
end
cd ..
end
elseif ~isfield(LIMO.data,'neighbouring_matrix')
disp('No TFCE performed, neighbourhood matrix missing')
elseif LIMO.design.bootstrap ==0
disp('No TFCE performed, since there was no bootstraps computed')
end
end
% ----------------------------------------------------------
%% ---------------- multivariate analysis ------------------
% --------------------------------------------------------
elseif strcmp(LIMO.design.type_of_analysis,'Multivariate')
update = 1;
% --------- load files created by limo_design_matrix ------------------
load Yr; load Yhat; load Res; load Betas;
% ------------- prepare weight matrice -------------------------------------
if strcmp(LIMO.design.method,'WLS') || strcmp(LIMO.design.method,'OLS')
W = ones(size(Yr,1),size(Yr,3));
elseif strcmp(LIMO.design.method,'IRLS')
W = ones(size(Yr));
end
% -------------- loop the analysis time frames per time frames
if strcmp(LIMO.design.status,'to do')
% 1st get weights based on time
if strcmp(LIMO.design.method,'WLS')
fprintf('getting trial weights \n')
array = find(~isnan(Yr(:,1,1))); % skip empty electrodes
for e = 1:size(Yr,1)
electrode = array(e); [Betas,W(e,:)] = limo_WLS(LIMO.design.X,squeeze(Yr(electrode,:,:))');
end
LIMO.design.weights = W;
end
% 2nd run the multivariate analysis over electrodes
for t = 1:size(Yr,2)
fprintf('analysing time frame %g/%g \n',t,size(Yr,2));
model = limo_mglm(squeeze(Yr(:,t,:))',LIMO); warning off;
% update the LIMO.mat
if update == 1
if LIMO.design.nb_conditions ~=0
LIMO.model.conditions_df = [model.conditions.Roy.df' model.conditions.Roy.dfe' model.conditions.Pillai.df' model.conditions.Pillai.dfe'];
end
if LIMO.design.nb_interactions ~=0
LIMO.model.interactions_df = [model.interactions.Roy.df' model.interactions.Roy.dfe' model.interactions.Pillai.df' model.interactions.Pillai.dfe' ];
end
if LIMO.design.nb_continuous ~=0
LIMO.model.continuous_df = [model.continuous.Roy.df model.continuous.Roy.dfe];
end
update = 0;
end
% update the files to be stored on the disk
fitted_data = LIMO.design.X*model.betas;
Yhat(:,t,:) = fitted_data';
Res(:,t,:) = squeeze(Yr(:,t,:)) - fitted_data'; clear fitted_data
R2{t} = model.R2;
Betas(:,t,:) = model.betas';
if prod(LIMO.design.nb_conditions) ~=0
if length(LIMO.design.nb_conditions) == 1
tmp_Condition_effect{t} = model.conditions;
else
for i=1:length(LIMO.design.nb_conditions)
tmp_Condition_effect{t}(i).EV = model.conditions.EV(i,:);
tmp_Condition_effect{t}(i).Roy.F = model.conditions.Roy.F(i);
tmp_Condition_effect{t}(i).Roy.p = model.conditions.Roy.p(i);
tmp_Condition_effect{t}(i).Pillai.F = model.conditions.Pillai.F(i);
tmp_Condition_effect{t}(i).Pillai.p = model.conditions.Pillai.p(i);
end
end
end
if LIMO.design.fullfactorial == 1
if length(LIMO.design.nb_interactions) == 1
tmp_Interaction_effect{t} = model.interactions;
else
for i=1:length(LIMO.design.nb_interactions)
tmp_Interaction_effect{t}(i).EV = model.conditions.EV(i,:);
tmp_Interaction_effect{t}(i).Roy.F = model.conditions.Roy.F(i);
tmp_Interaction_effect{t}(i).Roy.p = model.conditions.Roy.p(i);
tmp_Interaction_effect{t}(i).Pillai.F = model.conditions.Pillai.F(i);
tmp_Interaction_effect{t}(i).Pillai.p = model.conditions.Pillai.p(i);
end
end
end
if LIMO.design.nb_continuous ~=0
if LIMO.design.nb_continuous == 1
tmp_Covariate_effect{t} = model.continuous;
else
for i=1:LIMO.design.nb_continuous
tmp_Covariate_effect{t}(i).EV = model.conditions.EV(i,:);
tmp_Covariate_effect{t}(i).Roy.F = model.conditions.Roy.F(i);
tmp_Covariate_effect{t}(i).Roy.p = model.conditions.Roy.p(i);
tmp_Covariate_effect{t}(i).Pillai.F = model.conditions.Pillai.F(i);
tmp_Covariate_effect{t}(i).Pillai.p = model.conditions.Pillai.p(i);
end
end
end
end
% save data on the disk and clean out
LIMO.design.weights = W;
LIMO.design.status = 'done';
save LIMO LIMO; save Yhat Yhat;
save Res Res; save Betas Betas;
clear Yhat Res Betas
% R2 data
name = sprintf('R2_EV',i); R2_EV = NaN(size(Yr,1),size(Yr,2));
for t=1:size(Yr,2); R2_EV(:,t) = real(R2{t}.EV); end
save(name,'R2_EV','-v7.3')
name = sprintf('R2'); tmp = NaN(size(Yr,2),5);
for t=1:size(Yr,2); tmp(t,:) = [R2{t}.V R2{t}.Roy.F R2{t}.Roy.p R2{t}.Pillai.F R2{t}.Pillai.p]; end
R2 = tmp; save(name,'R2','-v7.3')
% condition effects
if prod(LIMO.design.nb_conditions) ~=0
for i=1:length(LIMO.design.nb_conditions)
name = sprintf('Condition_effect_%g_EV',i);
if length(LIMO.design.nb_conditions) == 1
for t=1:size(Yr,2); Condition_effect_EV(:,t) = real(tmp_Condition_effect{t}.EV); end
save(name,'Condition_effect_EV','-v7.3')
name = sprintf('Condition_effect_%g',i);
for t=1:size(Yr,2); Condition_effect(t,:) = [tmp_Condition_effect{t}.Roy.F tmp_Condition_effect{t}.Roy.p tmp_Condition_effect{t}.Pillai.F tmp_Condition_effect{t}.Pillai.p]; end
save(name,'Condition_effect','-v7.3')
else
for t=1:size(Yr,2); Condition_effect_EV(:,t) = real(tmp_Condition_effect{t}(i).EV); end
save(name,'Condition_effect_EV','-v7.3')
name = sprintf('Condition_effect_%g',i);
for t=1:size(Yr,2); Condition_effect(t,:) = [tmp_Condition_effect{t}(i).Roy.F tmp_Condition_effect{t}(i).Roy.p tmp_Condition_effect{t}(i).Pillai.F tmp_Condition_effect{t}(i).Pillai.p]; end
save(name,'Condition_effect','-v7.3')
end
end
clear Condition_effect Condition_effect_EV tmp_Condition_effect
end
% interaction effects
if LIMO.design.fullfactorial == 1
for i=1:length(LIMO.design.nb_interactions)
name = sprintf('Interaction_effect_%g_EV',i);
if length(LIMO.design.nb_interactions) == 1
for t=1:size(Yr,2); Interaction_effect_EV(:,t) = real(tmp_Interaction_effect{t}.EV); end
save(name,'Interaction_effect_EV','-v7.3')
name = sprintf('Interaction_effect_%g',i);
for t=1:size(Yr,2); Interaction_effect(t,:) = [tmp_Interaction_effect{t}.Roy.F tmp_Interaction_effect{t}.Roy.p tmp_Interaction_effect{t}.Pillai.F tmp_Interaction_effect{t}.Pillai.p]; end
save(name,'Interaction_effect','-v7.3')
else
for t=1:size(Yr,2); Interaction_effect_EV(:,t) = real(tmp_Interaction_effect{t}(i).EV); end
save(name,'Interaction_effect_EV','-v7.3')
name = sprintf('Interaction_effect_%g',i);
for t=1:size(Yr,2); Interaction_effect(t,:) = [tmp_Interaction_effect{t}(i).Roy.F tmp_Interaction_effect{t}(i).Roy.p tmp_Interaction_effect{t}(i).Pillai.F tmp_Interaction_effect{t}(i).Pillai.p]; end
save(name,'Interaction_effectV','-v7.3')
end
end
clear Interaction_effect Interaction_effect_EV tmp_Interaction_effect
end
if LIMO.design.nb_continuous ~=0
for i=1:LIMO.design.nb_continuous
name = sprintf('Covariate_effect_%g_EV',i);
if LIMO.design.nb_continuous == 1
for t=1:size(Yr,2); Covariate_effect_EV(:,t) = real(tmp_Covariate_effect{t}.EV); end
save(name,'Covariate_effect_EV','-v7.3')
name = sprintf('Covariate_effect_%g',i);
for t=1:size(Yr,2); Covariate_effect(t,:) = [tmp_Covariate_effect{t}.Roy.F tmp_Covariate_effect{t}.Roy.p tmp_Covariate_effect{t}.Pillai.F tmp_Covariate_effect{t}.Pillai.p]; end
save(name,'Covariate_effect','-v7.3')
else
for t=1:size(Yr,2); Covariate_effect_EV(:,t) = real(tmp_Covariate_effect{t}(i).EV); end
save(name,'Covariate_effect_EV','-v7.3')
name = sprintf('Covariate_effect_%g',i);
for t=1:size(Yr,2); Covariate_effect(t,:) = [tmp_Covariate_effect{t}(i).Roy.F tmp_Covariate_effect{t}(i).Roy.p tmp_Covariate_effect{t}(i).Pillai.F tmp_Covariate_effect{t}(i).Pillai.p]; end
save(name,'Covariate_effect','-v7.3')
end
end
clear Covariate_effect Covariate_effect_EV tmp_Covariate_effect
end
clear file electrode filename model reg dir i W
end
% if bootsrrap
if LIMO.design.bootstrap == 1
end
% TFCE if requested
if LIMO.design.tfce == 1
end
end
warning on;
case{5}
%% ------------------------------------------------------------------------
% Results
% ------------------------------------------------------------------------
% short cut to limo_results
% check which files are there
% -------------------------
try
load('LIMO.mat');
catch
[file,dir_path] = uigetfile('LIMO.mat','select a LIMO.mat file');
if file ==0
return
else
cd (dir_path); load LIMO.mat;
end
end
cd (LIMO.dir);
% R2
% ---
if LIMO.design.bootstrap == 1
if LIMO.design.tfce == 1
limo_display_results(1,'R2.mat',pwd,0.05,5,LIMO,0);
else
limo_display_results(1,'R2.mat',pwd,0.05,2,LIMO,0);
end
else
limo_display_results(1,'R2.mat',pwd,0.05,1,LIMO,0);
end
saveas(gcf, 'R2.fig','fig'); close(gcf)
clear R2.mat
% conditions
if prod(LIMO.design.nb_conditions) ~=0
for i=1:length(LIMO.design.nb_conditions)
name = sprintf('Condition_effect_%g.mat',i);
if LIMO.design.bootstrap == 1
if LIMO.design.tfce == 1
limo_display_results(1,name,pwd,0.05,5,LIMO,0);
else
limo_display_results(1,name,pwd,0.05,2,LIMO,0);
end
else
limo_display_results(1,name,pwd,0.05,1,LIMO,0);
end
savename = sprintf('Condition_effect_%g.fig',i);
saveas(gcf, savename,'fig'); close(gcf)