-
Notifications
You must be signed in to change notification settings - Fork 28
/
palm_core.m
3802 lines (3517 loc) · 164 KB
/
palm_core.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 palm_core(varargin)
% This is the core PALM function.
%
% _____________________________________
% Anderson M. Winkler
% FMRIB / University of Oxford
% Oct/2014
% http://brainder.org
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% PALM -- Permutation Analysis of Linear Models
% Copyright (C) 2015 Anderson M. Winkler
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% any later version.
%
% This program is distributed in the hope that it will be useful
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Uncomment the line below for debugging:
clear global plm opts; global plm opts;
% Take the arguments. Save a small log if needed.
ticI = tic;
[opts,plm] = palm_takeargs(varargin{:});
% Variables to store stuff for later.
if opts.missingdata
nY = plm.nY; else, nY = 1;
plm.Ymissp = cell(plm.nY,1);
end
tmp = cell(nY,1);
for y = 1:nY
tmp{y} = cell(plm.nM,1);
for m = 1:plm.nM
tmp{y}{m} = cell(plm.nC(m),1);
end
end; clear('y');
plm.X = tmp; % effective regressors
plm.Z = tmp; % nuisance regressors
plm.eCm = tmp; % effective contrast (for Mp)
plm.eCx = tmp; % effective contrast (for the effective regressors only)
plm.eC = tmp; % final effective contrast (depends on the method)
plm.Mp = tmp; % partitioned model, joined
plm.nEV = tmp; % number of regressors
plm.Hm = tmp; % hat (projection) matrix
plm.Rm = tmp; % residual forming matrix
plm.dRm = tmp; % diagonal elements of the residual forming matrix
plm.rM = tmp; % rank of the design matrix
plm.Gname = cell(plm.nM,1); % name of the statistic for each contrast
plm.nP = cell(plm.nM,1); % number of permutations for each contrast
for m = 1:plm.nM
plm.Gname{m} = cell (plm.nC(m),1);
plm.nP{m} = zeros(plm.nC(m),1);
end; clear('m');
G = cell(plm.nY,1); % to store G at each permutation
df2 = cell(plm.nY,1); % to store df2 at each permutation
plm.Gpperm = cell(plm.nY,1); % counter, for the permutation p-value
plm.G = cell(plm.nY,1); % for the unpermuted G (and to be saved)
plm.df2 = cell(plm.nY,1); % for the unpermuted df2 (and to be saved)
plm.Gmax = cell(plm.nY,1); % to store the max statistic (Y collapses)
for y = 1:plm.nY
G {y} = cell(plm.nM,1);
df2 {y} = cell(plm.nM,1);
plm.Gpperm {y} = cell(plm.nM,1);
plm.G {y} = cell(plm.nM,1);
plm.df2 {y} = cell(plm.nM,1);
plm.Gmax {y} = cell(plm.nM,1);
if opts.designperinput, loopM = y; else, loopM = 1:plm.nM; end
for m = loopM
G {y}{m} = cell(plm.nC(m),1);
df2 {y}{m} = cell(plm.nC(m),1);
plm.Gpperm {y}{m} = cell(plm.nC(m),1);
plm.G {y}{m} = cell(plm.nC(m),1);
plm.df2 {y}{m} = cell(plm.nC(m),1);
plm.Gmax {y}{m} = cell(plm.nC(m),1);
for c = 1:plm.nC(m)
plm.G {y}{m}{c} = zeros(1,plm.Ysiz(y));
plm.Gpperm {y}{m}{c} = zeros(1,plm.Ysiz(y));
end
end
end
if opts.accel.negbin
plm.Gppermp = plm.Gpperm; % number of perms done, for the negative binomial mode
end
if opts.saveuncorrected && (opts.accel.tail || opts.accel.gamma) || opts.FDR
plm.Gperms = plm.G; % The 1xYsiz(1) will be replaced by nPxYsiz(1) later below
end
if opts.accel.lowrank
kappa = G; % To store the kappa constant (see paper)
B = G; % To store B (see paper)
S = G; % To store Sigma (see paper)
Bperms = G; % To store the initial full permutations (betas)
Sperms = G; % To store the initial full permutations (variances)
plm.nJ = plm.rC; % To store the number of full permutations to do
plm.Bbasis = G; % Basis for the betas
plm.Sbasis = G; % Basis for the variances
end
if opts.savemetrics
plm.metr = plm.Gname; % to store permutation metrics
end
% Spatial stats, univariate
if opts.cluster.uni.do
plm.Gclu = cell(plm.nY,1); % to store cluster statistic
plm.Gclumax = cell(plm.nY,1); % for the max cluster statistic
for y = 1:plm.nY
plm.Gclu {y} = cell(plm.nM,1);
plm.Gclumax {y} = cell(plm.nM,1);
if opts.designperinput, loopM = y; else, loopM = 1:plm.nM; end
for m = loopM
plm.Gclu {y}{m} = cell(plm.nC(m),1);
plm.Gclumax {y}{m} = cell(plm.nC(m),1);
end
end
end
if opts.tfce.uni.do
Gtfce = cell(plm.nY,1); % to store TFCE at each permutation
plm.Gtfcepperm = cell(plm.nY,1); % counter, for the TFCE p-value
plm.Gtfce = cell(plm.nY,1); % to store TFCE statistic
plm.Gtfcemax = cell(plm.nY,1); % for the max TFCE statistic
for y = 1:plm.nY
Gtfce {y} = cell(plm.nM,1);
plm.Gtfcepperm {y} = cell(plm.nM,1);
plm.Gtfce {y} = cell(plm.nM,1);
plm.Gtfcemax {y} = cell(plm.nM,1);
if opts.designperinput, loopM = y; else, loopM = 1:plm.nM; end
for m = loopM
Gtfce {y}{m} = cell(plm.nC(m),1);
plm.Gtfcepperm{y}{m} = cell(plm.nC(m),1);
plm.Gtfce {y}{m} = cell(plm.nC(m),1);
plm.Gtfcemax {y}{m} = cell(plm.nC(m),1);
end
end
if opts.saveuncorrected && (opts.accel.tail || opts.accel.gamma)
plm.Gtfceperms = plm.Gtfce; % The 1xYsiz(1) will be replaced by nPxYsiz(1) later below
end
end
% Variables for NPC
if opts.NPC
plm.npcstr = '_npc_'; % default string for the filenames.
if opts.npcmod && ~ opts.npccon
Gnpc = cell(1); % to store the G-stats ready for NPC
df2npc = cell(1); % to store the df2 ready for NPC
Gnpc {1} = zeros(plm.nY,plm.Ysiz(1));
df2npc{1} = zeros(plm.nY,plm.Ysiz(1));
T = cell(plm.nM,1); % to store T at each permutation
plm.Tpperm = cell(plm.nM,1); % counter, for the combined p-value
Tppara = cell(plm.nM,1); % for the combined parametric p-value
plm.Tmax = cell(plm.nM,1); % to store the max combined statistic
elseif ~ opts.npcmod && opts.npccon
Gnpc = cell(plm.nY,1);
df2npc = cell(plm.nY,1);
if opts.designperinput
for y = 1:plm.nY
Gnpc {y} = zeros(plm.nC(y),plm.Ysiz(y));
df2npc{y} = zeros(plm.nC(y),plm.Ysiz(y));
end
else
for y = 1:plm.nY
Gnpc {y} = zeros(sum(plm.nC),plm.Ysiz(y));
df2npc{y} = zeros(sum(plm.nC),plm.Ysiz(y));
end
end
T = cell(plm.nY,1);
plm.Tpperm = cell(plm.nY,1);
Tppara = cell(plm.nY,1);
plm.Tmax = cell(plm.nY,1);
elseif opts.npcmod && opts.npccon
Gnpc = cell(1);
df2npc = cell(1);
if opts.designperinput
Gnpc {1} = zeros(plm.nY*plm.nC(1),plm.Ysiz(1));
df2npc{1} = zeros(plm.nY*plm.nC(1),plm.Ysiz(1));
else
Gnpc {1} = zeros(plm.nY*sum(plm.nC),plm.Ysiz(1));
df2npc{1} = zeros(plm.nY*sum(plm.nC),plm.Ysiz(1));
end
T = cell(1);
plm.Tpperm = cell(1);
Tppara = cell(1);
plm.Tmax = cell(1);
end
% Spatial stats, NPC
if opts.cluster.npc.do
plm.Tclu = plm.Tmax; % to store cluster NPC statistic
plm.Tclumax = plm.Tmax; % for the max cluster NPC
end
if opts.tfce.npc.do
Ttfce = plm.Tmax; % to store TFCE at each permutation
plm.Ttfcepperm = plm.Tmax; % counter, for the TFCE p-value
plm.Ttfce = plm.Tmax; % for the unpermuted TFCE
plm.Ttfcemax = plm.Tmax; % to store the max TFCE statistic
if opts.saveuncorrected && (opts.accel.tail || opts.accel.gamma)
plm.Ttfceperms = plm.Ttfce;
end
end
end
% Variables for MV
if opts.MV
plm.mvstr = '_mv'; % default string for the filenames.
Q = cell(plm.nM,1); % to store MV G at each permutation
plm.Qname = cell(plm.nM,1);
Qdf2 = cell(plm.nM,1); % to store MV df2 at each permutation
plm.Qpperm = cell(plm.nM,1); % counter, for the MV permutation p-value
Qppara = cell(plm.nM,1); % for the MV parametric p-value
fastmv = cell(plm.nM,1);
pparamv = cell(plm.nM,1);
plm.Qmax = cell(plm.nM,1); % to store the max multivariate statistic
plm.mvrev = cell(plm.nM,1); % reverse the direction in which a stat is significant?
% Spatial stats, multivariate
if opts.cluster.mv.do
plm.Qclu = cell(plm.nM,1); % to store cluster MV statistic
plm.Qclumax = cell(plm.nM,1); % for the max cluster MV
end
if opts.tfce.mv.do
Qtfce = cell(plm.nM,1); % to store TFCE at each permutation
plm.Qtfcepperm = cell(plm.nM,1); % counter, for the TFCE p-value
plm.Qtfce = cell(plm.nM,1); % for the unpermuted TFCE
plm.Qtfcemax = cell(plm.nM,1); % to store the max TFCE statistic
end
% Lower levels of these variables
for m = 1:plm.nM
Q{m} = cell(plm.nC(m),1);
plm.Qname{m} = cell(plm.nC(m),1);
Qdf2{m} = cell(plm.nC(m),1);
plm.Qpperm{m} = cell(plm.nC(m),1);
Qppara{m} = cell(plm.nC(m),1);
fastmv{m} = cell(plm.nC(m),1);
pparamv{m} = cell(plm.nC(m),1);
plm.Qmax{m} = cell(plm.nC(m),1);
plm.mvrev{m} = cell(plm.nC(m),1);
if opts.cluster.mv.do
plm.Qclu{m} = cell(plm.nC(m),1);
plm.Qclumax{m} = cell(plm.nC(m),1);
end
if opts.tfce.mv.do
Qtfce{m} = cell(plm.nC(m),1);
plm.Qtfcepperm{m} = cell(plm.nC(m),1);
plm.Qtfce{m} = cell(plm.nC(m),1);
plm.Qtfcemax{m} = cell(plm.nC(m),1);
end
for c = 1:plm.nC(m)
plm.Q{m}{c} = zeros(1,plm.Ysiz(1));
plm.Qpperm{m}{c} = zeros(1,plm.Ysiz(1));
end
end
if opts.accel.negbin
plm.Qppermp = plm.Qpperm; % number of perms done, for the negative binomial mode
end
if opts.saveuncorrected && (opts.accel.tail || opts.accel.gamma)
plm.Qperms = plm.Q; % The 1xYsiz(1) will be replaced by nPxYsiz(1) later below
if opts.tfce.mv.do
plm.Qtfceperms = plm.Qtfce;
end
end
end
% Variables for CCA
if opts.CCA || opts.PLS
plm.mvstr = ''; % default string for the filenames.
end
% Functions and strings for spatial statistics
switch opts.cluster.stat
case 'extent'
clusterfunc = @palm_clustere;
opts.cluster.str = '_clustere';
case 'mass'
clusterfunc = @palm_clusterm;
opts.cluster.str = '_clusterm';
case 'density'
clusterfunc = @palm_clusterd;
opts.cluster.str = '_clusterd';
case 'tippett'
clusterfunc = @palm_clustert;
opts.cluster.str = '_clustert';
case 'pivotal'
clusterfunc = @palm_clusterp;
opts.cluster.str = '_clusterp';
end
switch opts.tfce.stat
case 'tfce'
tfcefunc = @palm_tfce;
opts.tfce.str = '_tfce';
case 'density'
tfcefunc = @palm_tfde;
opts.tfce.str = '_tfde';
end
clear y m c;
% Inital strings to save the file names later.
plm.ystr = cell(plm.nY,1);
for y = 1:plm.nY
plm.ystr{y} = '';
end
plm.mstr = cell(plm.nM,1);
plm.cstr = cell(plm.nM,1);
for m = 1:plm.nM
plm.mstr{m} = '';
plm.cstr{m} = cell(plm.nC(m));
for c = 1:plm.nC(m)
plm.cstr{m}{c} = '';
end
end
clear y m c;
% Create the function handles for the NPC and function overloading
% for the missing data cases.
if opts.NPC
plm.Tname = lower(opts.npcmethod);
[plm.fastnpc,plm.pparanpc,plm.npcrev,...
plm.npcrel,plm.npcextr] = npchandles(plm.Tname,opts.concordant);
end
if opts.missingdata
[plm.fastnpcmiss,plm.pparanpcmiss] = npchandles(opts.npcmethodmiss,opts.concordant);
plm.mldiv = @mldiv;
plm.mrdiv = @mrdiv;
else
plm.mldiv = @mldivide;
plm.mrdiv = @mrdivide;
end
tocI = toc(ticI);
fprintf('Elapsed time parsing inputs: ~ %g seconds.\n',tocI);
% For each design matrix and contrast:
prepglm = cell(plm.nM,1);
fastpiv = cell(plm.nM,1);
for m = 1:plm.nM
prepglm{m} = cell(plm.nC(m),1);
fastpiv{m} = cell(plm.nC(m),1);
for c = 1:plm.nC(m)
% If there are voxelwise EVs:
if opts.evperdat
fprintf('Doing maths for -evperdat before model fitting: [Design %d/%d, Contrast %d/%d] (may take several minutes)\n',m,plm.nM,c,plm.nC(m));
end
% Partition the model, now using the method chosen by the user
if opts.designperinput, loopY = m; else, loopY = 1:plm.nY; end
if opts.missingdata
if opts.showprogress
fprintf('Preparing designs for missing data [Design: %d/%d, Contrast %d/%d]\n',m,plm.nM,c,plm.nC(m));
end
% Partition the design
for y = loopY
[plm.X{y}{m}{c},plm.Z{y}{m}{c},...
plm.eCm{y}{m}{c},plm.eCx{y}{m}{c},...
plm.Ymissp{y},...
plm.imov{y}{m}{c},plm.ifix{y}{m}{c},...
plm.isdiscrete{y}{m}{c},plm.istwotail{y}{m}{c}] = ...
palm_misspart(plm.Mset{m},plm.Cset{m}{c},...
opts.pmethodr,plm.Ymiss{y},plm.Mmiss{m},opts.mcar,opts.rmethod);
for o = 1:numel(plm.X{y}{m}{c})
plm.Mp{y}{m}{c}{o} = cat(2,plm.X{y}{m}{c}{o},plm.Z{y}{m}{c}{o});
end
end
else % not missing data
% Partition the design
y = m; o = 1;
[plm.X{y}{m}{c}{o},plm.Z{y}{m}{c}{o},plm.eCm{y}{m}{c}{o},plm.eCx{y}{m}{c}{o}] = ...
palm_partition(plm.Mset{m},plm.Cset{m}{c},opts.pmethodr);
plm.Mp{y}{m}{c}{o} = cat(2,plm.X{y}{m}{c}{o},plm.Z{y}{m}{c}{o});
for y = loopY
if y ~= m
plm.X{y}{m}{c}{o} = plm.X{m}{m}{c}{o};
plm.Z{y}{m}{c}{o} = plm.Z{m}{m}{c}{o};
plm.eCm{y}{m}{c}{o} = plm.eCm{m}{m}{c}{o};
plm.eCx{y}{m}{c}{o} = plm.eCx{m}{m}{c}{o};
plm.Mp{y}{m}{c}{o} = plm.Mp{m}{m}{c}{o};
end
end
clear y o;
end
for y = loopY
if opts.missingdata, loopO = 1:numel(plm.Mp{y}{m}{c}); else, loopO = 1; end
for o = loopO
% To avoid rank deficiency issues after partitioning, remove
% columns that are all equal to zero. This won't be done for
% evperdat because it's too slow and can make the designs too
% different if EVs are dropped from just some of the tests.
if ~ opts.evperdat
idx = all(plm.X{y}{m}{c}{o} == 0,1);
plm.X{y}{m}{c}{o}(:,idx) = [];
plm.eCx{y}{m}{c}{o}(idx,:) = [];
idx = all(plm.Z{y}{m}{c}{o} == 0,1);
plm.Z{y}{m}{c}{o}(:,idx) = [];
idx = all(plm.Mp{y}{m}{c}{o} == 0,1);
plm.Mp{y}{m}{c}{o}(:,idx) = [];
plm.eCm{y}{m}{c}{o}(idx,:) = [];
end
% Residual-forming matrix. This is used by the ter Braak method and
% also to compute some of the stats later. Note that, even though the
% residual-forming matrix changes at every permutation, the trace
% for each VG remains unchanged, hence it's not necessary to recompute
% it for every permutation.
if plm.nVG == 1
if strcmpi(opts.rmethod,'terbraak')
[N,~,nT] = size(plm.Mp{y}{m}{c}{o});
I = eye(N);
plm.Hm{y}{m}{c}{o} = zeros(N,N,nT);
plm.Rm{y}{m}{c}{o} = zeros(N,N,nT);
for t = 1:nT
plm.Hm{y}{m}{c}{o}(:,:,t) = plm.Mp{y}{m}{c}{o}(:,:,t)*pinv(plm.Mp{y}{m}{c}{o}(:,:,t));
plm.Rm{y}{m}{c}{o}(:,:,t) = I - plm.Hm{y}{m}{c}{o}(:,:,t);
end
plm.rM{y}{m}{c}{o} = size(plm.Mp{y}{m}{c}{o},1) - round(sum(diag(plm.Rm{y}{m}{c}{o}(:,:,1)))); % this is faster than rank(M)
else
plm.rM{y}{m}{c}{o} = rank(plm.Mp{y}{m}{c}{o}(:,:,1));
end
else % that is, if plm.nVG > 1
if strcmpi(opts.rmethod,'terbraak')
[N,~,nT] = size(plm.Mp{y}{m}{c}{o});
I = eye(N);
plm.Hm{y}{m}{c}{o} = zeros(N,N,nT);
plm.Rm{y}{m}{c}{o} = zeros(N,N,nT);
plm.dRm{y}{m}{c}{o} = zeros(N,nT);
for t = 1:nT
plm.Hm{y}{m}{c}{o}(:,:,t) = plm.Mp{y}{m}{c}{o}(:,:,t)*pinv(plm.Mp{y}{m}{c}{o}(:,:,t));
plm.Rm{y}{m}{c}{o}(:,:,t) = I - plm.Hm{y}{m}{c}{o}(:,:,t);
plm.dRm{y}{m}{c}{o}(:,t) = diag(plm.Rm{y}{m}{c}{o}(:,:,t)); % this is used for the pivotal statistic
end
else
[N,~,nT] = size(plm.Mp{y}{m}{c}{o});
I = eye(N);
plm.dRm{y}{m}{c}{o} = zeros(N,nT);
for t = 1:nT
plm.dRm{y}{m}{c}{o}(:,t) = diag(I - plm.Mp{y}{m}{c}{o}(:,:,t)*pinv(plm.Mp{y}{m}{c}{o}(:,:,t))); % this is used for the pivotal statistic
end
end
plm.rM{y}{m}{c}{o} = size(plm.Mp{y}{m}{c}{o},1) - round(sum(plm.dRm{y}{m}{c}{o}(:,1))); % this is faster than rank(M)
end
plm.nEV{y}{m}{c}{o} = size(plm.Mp{y}{m}{c}{o},2);
end
end
clear y o;
% Some methods don't work well if Z is empty, and there is no point in
% using any of them all anyway.
if opts.designperinput, loopY = m; else, loopY = 1:plm.nY; end
for y = loopY
if opts.missingdata, loopO = 1:numel(plm.Mp{y}{m}{c}); else, loopO = 1; end
for o = loopO
if isempty(plm.Z{y}{m}{c}{o})
plm.rmethod{y}{m}{c}{o} = 'noz';
else
plm.rmethod{y}{m}{c}{o} = opts.rmethod;
end
end
end
clear y o;
% MV/CCA
%%% DOUBLE-CHECK THE DEGREES-OF-FREEDOM!!
if opts.MV
y = 1; o = 1;
% Make the 3D dataset
if opts.accel.negbin
plm.Yq{m}{c} = cat(3,plm.Yset{:});
end
% Define the functions for the stats. Note that none is
% available if nVG > 1, and this should have been
% checked when taking the arguments.
if plm.rC{m}(c) == 1 && any(strcmpi(opts.mvstat,{'auto','hotellingtsq'}))
plm.Qname{m}{c} = '_hotellingtsq';
pparamv {m}{c} = @(Q)fasttsqp(Q,plm.N-plm.rM{y}{m}{c}{o},plm.nY);
plm.mvrev{m}{c} = false;
else
switch lower(opts.mvstat)
case {'wilks','auto'}
plm.Qname{m}{c} = '_wilks';
plm.qfun = @(H,E)wilks(H,E);
pparamv{m}{c} = @(Q)wilksp(Q, ...
plm.rC{m}(c),plm.N-plm.rM{y}{m}{c}{o},plm.nY);
plm.mvrev{m}{c} = true;
case {'lawley','lawley-hotelling'}
plm.Qname{m}{c} = '_lawley-hotelling';
plm.qfun = @(H,E)lawley(H,E);
pparamv{m}{c} = @(Q)lawleyp(Q, ...
plm.rC{m}(c),plm.N-plm.rM{y}{m}{c}{o},plm.nY);
plm.mvrev{m}{c} = false;
case 'pillai'
plm.Qname{m}{c} = '_pillai';
plm.qfun = @(H,E)pillai(H,E);
pparamv{m}{c} = @(Q)pillaip(Q, ...
plm.rC{m}(c),plm.N-plm.rM{y}{m}{c}{o},plm.nY);
plm.mvrev{m}{c} = false;
case {'roy-ii','roy'}
plm.Qname{m}{c} = '_roy-ii';
plm.qfun = @(H,E)roy_ii(H,E);
pparamv{m}{c} = @(Q)roy_iip(Q, ...
plm.rC{m}(c),plm.N-plm.rM{y}{m}{c}{o},plm.nY);
plm.mvrev{m}{c} = false;
case 'roy-iii'
plm.Qname{m}{c} = '_roy-iii';
plm.qfun = @(H,E)roy_iii(H,E);
plm.mvrev{m}{c} = false;
end
end
% For the MV methods in which the most significant stats are the
% smallest, rather than the largest, use reverse comparisons.
if plm.mvrev{m}{c}
mvrel = @le;
mvextr = @min;
else
mvrel = @ge;
mvextr = @max;
end
elseif opts.CCA
% Output string, statistic function, and side to test
plm.Qname{m}{c} = sprintf('_cca%d',opts.ccaorplsparm);
plm.qfun = @cca;
plm.mvrev{m}{c} = false;
elseif opts.PLS
% Output string, statistic function, and side to test
plm.Qname{m}{c} = sprintf('_pls%d',opts.ccaorplsparm);
plm.qfun = @simpls;
plm.mvrev{m}{c} = false;
end
% Effective rank of the matrix nP by Ysiz(y) used for the
% low rank approximation. This number varies according to
% the permutation and regression strategies, but it's
% roughly as below:
if opts.accel.lowrank
plm.nJ{m}(c) = plm.N*(plm.N+1)/2;
end
% Decide which method is going to be used for the regression and
% permutations, compute some useful matrices for later and create
% the appropriate function handle to prepare for the model fit.
% Each of these small functions is a replacement for the generic
% prototype function 'permglm.m', which is far slower.
% Note that this swich needs to remain inside the for-loops over
% designs and contrasts, because they vary. Nonetheless
% this all runs just for the 1st permutation.
isterbraak = false;
if opts.evperdat
if opts.designperinput, loopY = m; else, loopY = 1:plm.nY; end
for y = loopY
if opts.missingdata,loopO = 1:numel(plm.Mp{y}{m}{c}); else, loopO = 1; end
tmp = cell(numel(loopO),1);
plm.eC{y}{m}{c} = tmp;
plm.Hz{y}{m}{c} = tmp;
plm.Rz{y}{m}{c} = tmp;
for o = loopO
% Pick the regression/permutation method
N = size(plm.Mp{y}{m}{c}{o},1);
switch lower(plm.rmethod{y}{m}{c}{o})
case 'noz'
if ~ opts.missingdata && ~ opts.designperinput && y > 1
plm.eC{y}{m}{c}{o} = plm.eC{1}{m}{c}{1};
else
plm.eC{y}{m}{c}{o} = plm.eCx{y}{m}{c}{o};
end
prepglm{m}{c} = @noz3d;
case 'exact'
if ~ opts.missingdata && ~ opts.designperinput && y > 1
plm.eC{y}{m}{c}{o} = plm.eC{1}{m}{c}{1};
else
plm.eC{y}{m}{c}{o} = plm.eCx{y}{m}{c}{o};
end
prepglm{m}{c} = @exact3d;
case 'draper-stoneman'
if ~ opts.missingdata && ~ opts.designperinput && y > 1
plm.eC{y}{m}{c}{o} = plm.eC{1}{m}{c}{1};
else
plm.eC{y}{m}{c}{o} = plm.eCm{y}{m}{c}{o};
end
prepglm{m}{c} = @draperstoneman3d;
case 'still-white'
if ~ opts.missingdata && ~ opts.designperinput && y > 1
plm.Rz{y}{m}{c}{o} = plm.Rz{1}{m}{c}{1};
plm.eC{y}{m}{c}{o} = plm.eC{1}{m}{c}{1};
else
plm.Rz{y}{m}{c}{o} = zeros(N,N,size(plm.Mset{m},3));
I = eye(N);
for t = 1:size(plm.Mset{m},3)
plm.Rz{y}{m}{c}{o}(:,:,t) = I - plm.Z{y}{m}{c}{o}(:,:,t)*pinv(plm.Z{y}{m}{c}{o}(:,:,t));
end
plm.eC{y}{m}{c}{o} = plm.eCx{y}{m}{c}{o};
end
prepglm{m}{c} = @stillwhite3d;
case 'freedman-lane'
if ~ opts.missingdata && ~ opts.designperinput && y > 1
plm.Hz{y}{m}{c}{o} = plm.Hz{1}{m}{c}{1};
plm.Rz{y}{m}{c}{o} = plm.Rz{1}{m}{c}{1};
plm.eC{y}{m}{c}{o} = plm.eCm{1}{m}{c}{o};
else
plm.Hz{y}{m}{c}{o} = zeros(N,N,size(plm.Mset{m},3));
plm.Rz{y}{m}{c}{o} = plm.Hz{y}{m}{c}{o};
I = eye(N);
for t = 1:size(plm.Mset{m},3)
plm.Hz{y}{m}{c}{o}(:,:,t) = plm.Z{y}{m}{c}{o}(:,:,t)*pinv(plm.Z{y}{m}{c}{o}(:,:,t));
plm.Rz{y}{m}{c}{o}(:,:,t) = I - plm.Hz{y}{m}{c}{o}(:,:,t);
end
plm.eC{y}{m}{c}{o} = plm.eCm{y}{m}{c}{o};
end
prepglm{m}{c} = @freedmanlane3d;
case 'terbraak'
isterbraak = true;
if ~ opts.missingdata && ~ opts.designperinput && y > 1
plm.eC{y}{m}{c}{o} = plm.eC{1}{m}{c}{1};
else
plm.eC{y}{m}{c}{o} = plm.eCm{y}{m}{c}{o};
end
prepglm{m}{c} = @terbraak3d;
case 'kennedy'
if ~ opts.missingdata && ~ opts.designperinput && y > 1
plm.Rz{y}{m}{c}{o} = plm.Rz{1}{m}{c}{1};
plm.eC{y}{m}{c}{o} = plm.eC{1}{m}{c}{1};
else
plm.Rz{y}{m}{c}{o} = zeros(N,N,size(plm.Mset{m},3));
I = eye(N);
for t = 1:size(plm.Mset{m},3)
plm.Rz{y}{m}{c}{o}(:,:,t) = I - plm.Z{y}{m}{c}{o}(:,:,t)*pinv(plm.Z{y}{m}{c}{o}(:,:,t));
end
plm.eC{y}{m}{c}{o} = plm.eCx{y}{m}{c}{o};
end
prepglm{m}{c} = @kennedy3d;
case 'manly'
if ~ opts.missingdata && ~ opts.designperinput && y > 1
plm.eC{y}{m}{c}{o} = plm.eC{1}{m}{c}{1};
else
plm.eC{y}{m}{c}{o} = plm.eCm{y}{m}{c}{o};
end
prepglm{m}{c} = @manly; % same as the usual Manly
case 'huh-jhun'
if ~ opts.missingdata && ~ opts.designperinput && y > 1
plm.Rz{y}{m}{c}{o} = plm.Rz{1}{m}{c}{1};
plm.hj{y}{m}{c}{o} = plm.hj{1}{m}{c}{1};
plm.eC{y}{m}{c}{o} = plm.eC{1}{m}{c}{1};
else
plm.Rz{y}{m}{c}{o} = zeros(N,N,size(plm.Mset{m},3));
I = eye(N);
for t = 1:size(plm.Mset{m},3)
plm.Rz{y}{m}{c}{o}(:,:,t) = I - plm.Z{y}{m}{c}{o}(:,:,t)*pinv(plm.Z{y}{m}{c}{o}(:,:,t));
[Q,D] = schur(plm.Rz{y}{m}{c}{o}(:,:,t));
D = abs(diag(D)) < 10*eps;
Q(:,D) = [];
if t == 1
plm.hj{y}{m}{c}{o} = zeros([size(Q) size(plm.Mset{m},3)]);
end
plm.hj{y}{m}{c}{o}(:,:,t) = Q;
end
plm.eC{y}{m}{c}{o} = plm.eCx{y}{m}{c}{o};
end
prepglm{m}{c} = @huhjhun3d;
case 'dekker'
if ~ opts.missingdata && ~ opts.designperinput && y > 1
plm.Rz{y}{m}{c}{o} = plm.Rz{1}{m}{c}{1};
plm.eC{y}{m}{c}{o} = plm.eC{1}{m}{c}{1};
else
plm.Rz{y}{m}{c}{o} = zeros(N,N,size(plm.Mset{m},3));
I = eye(N);
for t = 1:size(plm.Mset{m},3)
plm.Rz{y}{m}{c}{o}(:,:,t) = I - plm.Z{y}{m}{c}{o}(:,:,t)*pinv(plm.Z{y}{m}{c}{o}(:,:,t));
end
plm.eC{y}{m}{c}{o} = plm.eCm{y}{m}{c}{o};
end
prepglm{m}{c} = @dekker3d;
end
% Pick a name for the function that will compute the statistic
% and the name to save the files later.
if opts.pearson
if plm.rC{m}(c) == 1
fastpiv{m}{c} = @fastr3d;
elseif plm.rC{m}(c) > 1
fastpiv{m}{c} = @fastrsq3d;
end
else
if plm.rC{m}(c) == 1 && plm.nVG == 1
fastpiv{m}{c} = @fastt3d;
elseif plm.rC{m}(c) > 1 && plm.nVG == 1
fastpiv{m}{c} = @fastf3d;
elseif plm.rC{m}(c) == 1 && plm.nVG > 1
fastpiv{m}{c} = @fastv3d;
elseif plm.rC{m}(c) > 1 && plm.nVG > 1
fastpiv{m}{c} = @fastg3d;
end
end
end
end
clear y o;
% MV/CCA/PLS/Noperm
if opts.MV && ~ opts.accel.noperm
if plm.rC{m}(c) == 1 && any(strcmpi(opts.mvstat,{'auto','hotellingtsq'}))
fastmv{m}{c} = @(M,psi,res)fasttsq3d(M,psi,res,m,c,plm);
else
fastmv{m}{c} = @(M,psi,res)fastq3d(M,psi,res,m,c,plm);
end
end
if opts.CCA || opts.PLS || opts.accel.noperm
y = 1; o = 1;
% Residual forming matrix (Z only)
plm.Rz{y}{m}{c}{o} = zeros(plm.N,plm.N,plm.Ysiz(1));
if isempty(plm.Z{y}{m}{c}{o})
plm.Rz{y}{m}{c}{o} = bsxfun(@plus,eye(plm.N),plm.Rz{y}{m}{c}{o});
elseif ~ any(strcmpi(opts.rmethod,{ ...
'still-white','freedman-lane', ...
'kennedy','huh-jhun','dekker'}))
I = eye(plm.N);
for t = 1:plm.Ysiz(1)
plm.Rz{y}{m}{c}{o}(:,:,t) = I - plm.Z{y}{m}{c}{o}(:,:,t)*pinv(plm.Z{y}{m}{c}{o}(:,:,t));
end
clear('I');
end
% Make the 3D dataset & residualise wrt Z
plm.Yq{m}{c} = cat(3,plm.Yset{:});
plm.Yq{m}{c} = permute(plm.Yq{m}{c},[1 3 2]);
for t = 1:plm.Ysiz(1)
plm.Yq{m}{c}(:,:,t) = plm.Rz{y}{m}{c}{o}(:,:,t)*plm.Yq{m}{c}(:,:,t);
end
end
clear y o;
else % i.e., if not evperdat
if opts.designperinput, loopY = m; else, loopY = 1:plm.nY; end
for y = loopY
if opts.missingdata, loopO = 1:numel(plm.Mp{y}{m}{c}); else, loopO = 1; end
tmp = cell(numel(loopO),1);
plm.eC{y}{m}{c} = tmp;
plm.Hz{y}{m}{c} = tmp;
plm.Rz{y}{m}{c} = tmp;
for o = loopO
% Pick the regression/permutation method
N = size(plm.Mp{y}{m}{c}{o},1);
switch lower(plm.rmethod{y}{m}{c}{o})
case 'noz'
if ~ opts.missingdata && ~ opts.designperinput && y > 1
plm.eC{y}{m}{c}{o} = plm.eC{1}{m}{c}{1};
else
plm.eC{y}{m}{c}{o} = plm.eCx{y}{m}{c}{o};
end
if opts.missingdata
prepglm{m}{c}{o} = @nozm;
else
prepglm{m}{c} = @noz;
end
case 'exact'
if ~ opts.missingdata && ~ opts.designperinput && y > 1
plm.eC{y}{m}{c}{o} = plm.eC{1}{m}{c}{1};
else
plm.eC{y}{m}{c}{o} = plm.eCx{y}{m}{c}{o};
end
if opts.missingdata
prepglm{m}{c}{o} = @exactm;
else
prepglm{m}{c} = @exact;
end
case 'draper-stoneman'
if ~ opts.missingdata && ~ opts.designperinput && y > 1
plm.eC{y}{m}{c}{o} = plm.eC{1}{m}{c}{1};
else
plm.eC{y}{m}{c}{o} = plm.eCm{y}{m}{c}{o};
end
if opts.missingdata
prepglm{m}{c}{o} = @draperstonemanm;
else
prepglm{m}{c} = @draperstoneman;
end
case 'still-white'
if ~ opts.missingdata && ~ opts.designperinput && y > 1
plm.Rz{y}{m}{c}{o} = plm.Rz{1}{m}{c}{1};
plm.eC{y}{m}{c}{o} = plm.eC{1}{m}{c}{1};
else
plm.Rz{y}{m}{c}{o} = eye(N) - plm.Z{y}{m}{c}{o}*pinv(plm.Z{y}{m}{c}{o});
plm.eC{y}{m}{c}{o} = plm.eCx{y}{m}{c}{o};
end
if opts.missingdata
prepglm{m}{c}{o} = @stillwhitem;
else
prepglm{m}{c} = @stillwhite;
end
case 'freedman-lane'
if ~ opts.missingdata && ~ opts.designperinput && y > 1
plm.Hz{y}{m}{c}{o} = plm.Hz{1}{m}{c}{1};
plm.Rz{y}{m}{c}{o} = plm.Rz{1}{m}{c}{1};
plm.eC{y}{m}{c}{o} = plm.eC{1}{m}{c}{1};
else
plm.Hz{y}{m}{c}{o} = plm.Z{y}{m}{c}{o}*pinv(plm.Z{y}{m}{c}{o});
plm.Rz{y}{m}{c}{o} = eye(N) - plm.Hz{y}{m}{c}{o};
plm.eC{y}{m}{c}{o} = plm.eCm{y}{m}{c}{o};
end
if opts.missingdata
prepglm{m}{c}{o} = @freedmanlanem;
else
prepglm{m}{c} = @freedmanlane;
end
case 'terbraak'
isterbraak = true;
if ~ opts.missingdata && ~ opts.designperinput && y > 1
plm.eC{y}{m}{c}{o} = plm.eC{1}{m}{c}{1};
else
plm.eC{y}{m}{c}{o} = plm.eCm{y}{m}{c}{o};
end
if opts.missingdata
prepglm{m}{c}{o} = @terbraakm;
else
prepglm{m}{c} = @terbraak;
end
case 'kennedy'
if ~ opts.missingdata && ~ opts.designperinput && y > 1
plm.Rz{y}{m}{c}{o} = plm.Rz{1}{m}{c}{1};
plm.eC{y}{m}{c}{o} = plm.eC{1}{m}{c}{1};
else
plm.Rz{y}{m}{c}{o} = eye(N) - plm.Z{y}{m}{c}{o}*pinv(plm.Z{y}{m}{c}{o});
plm.eC{y}{m}{c}{o} = plm.eCx{y}{m}{c}{o};
end
if opts.missingdata
prepglm{m}{c}{o} = @kennedym;
else
prepglm{m}{c} = @kennedy;
end
case 'manly'
if ~ opts.missingdata && ~ opts.designperinput && y > 1
plm.eC{y}{m}{c}{o} = plm.eC{1}{m}{c}{1};
else
plm.eC{y}{m}{c}{o} = plm.eCm{y}{m}{c}{o};
end
if opts.missingdata
prepglm{m}{c}{o} = @manlym;
else
prepglm{m}{c} = @manly;
end
case 'huh-jhun'
if ~ opts.missingdata && ~ opts.designperinput && y > 1
plm.Rz{y}{m}{c}{o} = plm.Rz{1}{m}{c}{1};
plm.hj{y}{m}{c}{o} = plm.hj{1}{m}{c}{1};
plm.eC{y}{m}{c}{o} = plm.eC{1}{m}{c}{1};
else
plm.Rz{y}{m}{c}{o} = eye(N) - plm.Z{y}{m}{c}{o}*pinv(plm.Z{y}{m}{c}{o});
[plm.hj{y}{m}{c}{o},D] = schur(plm.Rz{y}{m}{c}{o});
D = abs(diag(D)) < 10*eps;
plm.hj{y}{m}{c}{o}(:,D) = [];
plm.eC{y}{m}{c}{o} = plm.eCx{y}{m}{c}{o};
end
if opts.missingdata
prepglm{m}{c}{o} = @huhjhunm;
else
prepglm{m}{c} = @huhjhun;
end
case 'dekker'
if ~ opts.missingdata && ~ opts.designperinput && y > 1
plm.Rz{y}{m}{c}{o} = plm.Rz{1}{m}{c}{1};
plm.eC{y}{m}{c}{o} = plm.eC{1}{m}{c}{1};
else
plm.Rz{y}{m}{c}{o} = eye(N) - plm.Z{y}{m}{c}{o}*pinv(plm.Z{y}{m}{c}{o});
plm.eC{y}{m}{c}{o} = plm.eCm{y}{m}{c}{o};
end
if opts.missingdata
prepglm{m}{c}{o} = @dekkerm;
else
prepglm{m}{c} = @dekker;
end
end
% Pick a name for the function that will compute the statistic
% and the name to save the files later.
if opts.pearson
if plm.rC{m}(c) == 1
fastpiv{m}{c} = @fastr;
elseif plm.rC{m}(c) > 1
fastpiv{m}{c} = @fastrsq;
end
elseif opts.SwE
if plm.rC{m}(c) == 1 && plm.nVG == 1
fastpiv{m}{c} = @fasttswe;
elseif plm.rC{m}(c) > 1 && plm.nVG == 1
fastpiv{m}{c} = @fastfswe;
elseif plm.rC{m}(c) == 1 && plm.nVG > 1
fastpiv{m}{c} = @fastvswe;
elseif plm.rC{m}(c) > 1 && plm.nVG > 1
fastpiv{m}{c} = @fastgswe;
end
else
if plm.rC{m}(c) == 1 && plm.nVG == 1
fastpiv{m}{c} = @fastt;
elseif plm.rC{m}(c) > 1 && plm.nVG == 1
fastpiv{m}{c} = @fastf;
elseif plm.rC{m}(c) == 1 && plm.nVG > 1
fastpiv{m}{c} = @fastv;
elseif plm.rC{m}(c) > 1 && plm.nVG > 1
fastpiv{m}{c} = @fastg;
end
end
end
end
clear y o;
% MV/CCA/Noperm
if opts.MV && ~ opts.accel.noperm
if plm.rC{m}(c) == 1 && any(strcmpi(opts.mvstat,{'auto','hotellingtsq'}))
fastmv{m}{c} = @(M,psi,res)fasttsq(M,psi,res,m,c,plm);
else
fastmv{m}{c} = @(M,psi,res)fastq(M,psi,res,m,c,plm);
end
end
if opts.CCA || opts.PLS || opts.accel.noperm
y = 1; o = 1;
% Residual forming matrix (Z only)
if isempty(plm.Z{y}{m}{c}{o})
plm.Rz{y}{m}{c}{o} = eye(plm.N);
elseif ~ any(strcmpi(opts.rmethod,{ ...
'still-white','freedman-lane', ...
'kennedy','huh-jhun','dekker'}))
plm.Rz{y}{m}{c}{o} = eye(plm.N) - plm.Z{y}{m}{c}{o}*pinv(plm.Z{y}{m}{c}{o});
end
% Make the 3D dataset & residualise wrt Z
plm.Yq{m}{c} = cat(3,plm.Yset{:});
for y = 1:plm.nY
plm.Yq{m}{c}(:,:,y) = plm.Rz{1}{m}{c}{o}*plm.Yq{m}{c}(:,:,y);
end; clear y
plm.Yq{m}{c} = permute(plm.Yq{m}{c},[1 3 2]);
end
end
% Pick a name to save the files later.
if opts.pearson || opts.accel.noperm
if plm.rC{m}(c) == 1
plm.Gname{m}{c} = '_rstat';
elseif plm.rC{m}(c) > 1
plm.Gname{m}{c} = '_rsqstat';
end
else
if plm.rC{m}(c) == 1 && plm.nVG == 1
plm.Gname{m}{c} = '_tstat';
elseif plm.rC{m}(c) > 1 && plm.nVG == 1
plm.Gname{m}{c} = '_fstat';
elseif plm.rC{m}(c) == 1 && plm.nVG > 1
plm.Gname{m}{c} = '_vstat';
elseif plm.rC{m}(c) > 1 && plm.nVG > 1
plm.Gname{m}{c} = '_gstat';
end
end
end
end
% Create the permutation set, while taking care of the synchronized
% permutations (see the inner loop below)
if opts.syncperms
if ~ opts.accel.noperm
if isempty(plm.EB)
if opts.savemetrics
[plm.Pset,plm.nP{1}(1),plm.metr{1}{1}] = ...
palm_shuffree(plm.seq{1}{1},opts.nP0, ...
opts.cmcp,opts.EE,opts.ISE,false);