-
Notifications
You must be signed in to change notification settings - Fork 28
/
palm_quickperms.m
142 lines (130 loc) · 4.89 KB
/
palm_quickperms.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
function varargout = palm_quickperms(varargin)
% Create a set of permutations that is left in the Octave/Matlab workspace
% for later use, or to be exported to other programs.
%
% [Pset,VG] = palm_quickperms(M,EB,P,EE,ISE,CMCx,CMCp)
%
% Inputs (to skip an argument, use an empty array, []):
% M : Design matrix. It can be the full, unpartitioned design, or
% if there are nuisance, simply the part that contains the EVs
% of interest. This distinction is only relevant if there are
% discrete EVs of interest and nuisance variables that are
% continuous. You may consider a partitioning as in the
% function palm_partition.m.
% If you have no idea what to use, it is in general, it is in
% general safe to use as M simply a vector (1:N)'.
% You can also simply leave it empty ([]) if EB is supplied, and
% by default it will be (1:N)'. If an EB isn't supplied, you can
% simply use N and by default it will be (1:N)'.
% EB : Exchangeability blocks (can be multi-level). For freely
% exchangeable data, use ones(N,1). You can also leave it
% empty ([]) if a valid, non-empty M was supplied.
% P : Desired number of permutations. The actual number may be
% smaller if N is too small. Use 0 for exhaustive.
% Default is 10000.
% EE : True/False indicating whether to assume exchangeable errors,
% which allow permutations.
% ISE : True/False indicating whether to assume independent and
% symmetric errors, which allow sign-flippings.
% CMCx : True/False indicating whether repeated rows in the design
% should be be ignored. Default is false.
% CMCp : True/False indicating whether repeated permutations should
% be ignored. Default is false.
%
% Outputs:
% Pset : Permutation set. It contains one permutation per column.
% VG : Variance groups (VGs), to be used with a statistic that is
% robust to heteroscedasticity.
%
% _____________________________________
% Anderson M. Winkler
% FMRIB / University of Oxford
% Sep/2015
% 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/>.
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Take the inputs:
v.M = [];
v.EB = [];
v.P = 10000;
v.EE = true;
v.ISE = false;
v.CMCx = false;
v.CMCp = false;
fields = fieldnames(v);
for a = 1:nargin,
if ~ isempty(varargin{a}),
v.(fields{a}) = varargin{a};
end
end
if ~ v.EE && ~ v.ISE,
error('At least one of EE or ISE must be given as "true".');
end
if v.P < 0,
error('P must not be negative');
end
if isempty(v.M ), v.M = 0; end
if isempty(v.EB), v.EB = 0; end
% Number of subjects
N = max(size(v.M,1),size(v.EB,1));
if N == 1,
N = max(v.M,v.EB);
elseif N == 0,
error('Design matrix and exchangeability blocks cannot be both empty.');
end
if v.M == 0, v.M = N; end
if v.EB == 0, v.EB = N; end
if ...
(size(v.M,1) > 1 && N ~= size(v.M,1)) || ...
(isscalar(v.M) && N ~= v.M) || ...
(size(v.EB,1) > 1 && N ~= size(v.EB,1)) || ...
(isscalar(v.EB) && N ~= v.EB),
error('Design matrix and exchangeability blocks of incompatible sizes');
end
% Design matrix:
if isempty(v.M) || isscalar(v.M) || v.CMCx,
v.M = (1:N)';
end
% Create the shufflings:
if ...
isempty(v.EB) || ...
isscalar(v.EB) || ...
(isvector(v.EB) && numel(unique(v.EB)) == 1),
% If no EBs were given, use simple shuffling:
simpleshuf = true;
Pset = palm_shuffree(v.M,v.P,v.CMCp,v.EE,v.ISE,true);
else
% Or use the multi-level blocks. Begin by reindexing the leaves:
simpleshuf = false;
v.EB = palm_reindex(v.EB,'fixleaves');
% Then create the permutation tree:
Ptree = palm_tree(v.EB,v.M);
% Then the set of permutations:
Pset = palm_shuftree(Ptree,v.P,v.CMCp,v.EE,v.ISE,true);
end
varargout{1} = Pset;
% Define the variance groups (for heteroscedasticity, if needed):
if nargout == 2,
% Create the VGs:
if simpleshuf,
VG = ones(N,1);
else
VG = palm_ptree2vg(Ptree);
end
varargout{2} = VG;
end