-
Notifications
You must be signed in to change notification settings - Fork 6
/
permloads.m
executable file
·285 lines (275 loc) · 8.09 KB
/
permloads.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
function varargout = permloads(varargin)
% Permutation inference for canonical correlation
% analysis (CCA).
%
% Usage:
% [pfwer,r,A,B,U,V,pA,pB] = permloads(Y,X,nP,Z,W,Sel,partial,Pset,nK)
%
% Inputs:
% - Y : Left set of variables, size N by P.
% - X : Right set of variables, size N by Q.
% - nP : An integer representing the number
% of permutations.
% Default is 1000 permutations.
% - Z : (Optional) Nuisance variables for
% both (partial CCA) or left side
% (part CCA) only.
% - W : (Optional) Nuisance variables for the
% right side only (bipartial CCA).
% - Sel : (Optional) Selection matrix or a selection vector,
% to use Theil's residuals instead of Huh-Jhun's
% projection. If specified as a vector, it can be
% made of integer indices or logicals.
% The R unselected rows of Z (S of W) must be full
% rank. Use -1 to randomly select N-R (or N-S) rows.
% - partial : (Optional) Boolean indicating whether
% this is partial (true) or part (false) CCA.
% Default is true, i.e., partial CCA.
% - Pset : Predefined set of permutations (e.g., that respect
% exchangeability blocks). For information on how to
% generate these, see:
% https://fsl.fmrib.ox.ac.uk/fsl/fslwiki/PALM
% If a selection matrix is provided (for the Theil method),
% Pset will have to have fewer rows than the original N, i.e.,
% it will have as many rows as the effective number of
% subjects determined by the selection matrix.
% - nK : When correcting the loadings, consider only the first nK
% canonical modes. If not supplied, it will be uncorrected.
%
% Outputs:
% - p : p-values, FWER corrected via closure.
% - r : Canonical correlations.
% - A : Canonical coefficients, left side.
% - B : Canonical coefficients, right side.
% - U : Canonical variables, left side.
% - V : Canonical variables, right side.
% - pA : P-values for the loadings, left side. If nK was supplied, it's
% FWER-corrected only up to nK-th component, otherwise it's
% uncorrected.
% - pB : P-values for the loadings, right side. If nK was supplied, it's
% FWER-corrected only up to nK-th component, otherwise it's
% uncorrected.
%
% ___________________________________________
% AM Winkler, O Renaud, SM Smith, TE Nichols
% NIH - Univ. of Geneva - Univ. of Oxford
% May/2021
% Read input arguments
narginchk(2,9)
Y = varargin{1};
X = varargin{2};
if nargin >= 3
nP = varargin{3};
end
if nargin >= 4
Z = varargin{4};
else
Z = [];
end
if nargin >= 5
W = varargin{5};
else
W = [];
end
if nargin >= 6
Sel = varargin{6};
else
Sel = [];
end
if nargin >= 7
partial = varargin{7};
else
partial = true;
end
if nargin >= 8
Pset = varargin{8};
else
Pset = false;
end
if nargin >= 9
nK = varargin{9};
else
nK = false;
end
Ny = size(Y,1);
Nx = size(X,1);
if Ny ~= Nx
error('Y and X do not have same number of rows.')
end
N = Ny; clear Ny Nx
I = eye(N);
% Residualise Y wrt Z
if isempty(Z)
Qz = I;
else
Z = center(Z);
Qz = semiortho(Z,Sel);
end
Y = center(Y);
Y = Qz'*Y;
Ny = size(Y,1);
R = size(Z,2);
% Residualise X wrt W
if isempty(W)
if partial
W = Z;
Qw = Qz;
else
Qw = I;
end
else
W = center(W);
Qw = semiortho(W,Sel);
end
X = center(X);
X = Qw'*X;
Nx = size(X,1);
S = size(W,2);
% Initial CCA
[A,B,r] = cca(Qz*Y,Qw*X,R,S);
K = numel(r);
U = Y*[A null(A')];
V = X*[B null(B')];
% Initialise counter
cnt = zeros(1,K);
Lcnt = zeros(size(Y,2),K);
Rcnt = zeros(size(X,2),K);
lW = zeros(1,K);
% For each permutation
for p = 1:nP
fprintf('Permutation %d/%d: ',p,nP);
% If user didn't supply a set of permutations,
% permute randomly both Y and X.
% Otherwise, use the permtuation set to shuffle
% one side only.
if islogical(Pset)
% First permutation is no permutation
if p == 1
idxY = (1:Ny);
idxX = (1:Nx);
else
idxY = randperm(Ny);
idxX = randperm(Nx);
end
else
idxY = Pset(:,p);
idxX = (1:Nx);
end
% For each canonical variable
Uperm = zeros(size(U,1),K);
Vperm = zeros(size(V,1),K);
for k = 1:K
fprintf('%d ',k);
[Aperm,Bperm,rperm] = cca(Qz*U(idxY,k:end),Qw*V(idxX,k:end),R,S);
lWtmp = -fliplr(cumsum(fliplr(log(1-rperm.^2))));
lW(k) = lWtmp(1);
Uperm(:,k) = U(:,k:end)*Aperm(:,1);
Vperm(:,k) = V(:,k:end)*Bperm(:,1);
end
Lload = abs(corr(Y,Uperm));
Rload = abs(corr(X,Vperm));
if p == 1
lW1 = lW;
Lload1 = Lload;
Rload1 = Rload;
end
cnt = cnt + (lW >= lW1);
if nK
% FWER-correction if nK is supplied
idx = 1:nK;
Lcnt = Lcnt + (max(max(Lload(:,idx),[],1),[],2) >= Lload1);
Rcnt = Rcnt + (max(max(Rload(:,idx),[],1),[],2) >= Rload1);
else
% Uncorrected if nK == 0
Lcnt = Lcnt + (Lload >= Lload1);
Rcnt = Rcnt + (Rload >= Rload1);
end
fprintf('\n');
end
punc = cnt/nP;
pA = Lcnt/nP;
pB = Rcnt/nP;
varargout{1} = cummax(punc); % pfwer
varargout{2} = r; % canonical correlations
varargout{3} = A; % canonical weights (left)
varargout{4} = B; % canonical weights (right)
varargout{5} = Qz*Y*A; % canonical variables (left)
varargout{6} = Qw*X*B; % canonical variables (right)
varargout{7} = pA; % p-value for the individual weights, left side.
varargout{8} = pB; % p-value for the individual weights, right side.
% =================================================================
function Q = semiortho(Z,Sel)
% Compute a semi-orthogonal matrix according to
% the Huh-Jhun or Theil methods. Note that, due to a
% simplification of HJ, input here is Z, not Rz.
if isempty(Sel)
% If Sel is empty, do Huh-Jhun
% HJ here is simplified as in Winkler et al, 2020 (see the Appendix text of the paper)
[Q,D,~] = svd(null(Z'),'econ');
Q = Q*D;
else
% Theil
[N,R] = size(Z);
if isvector(Sel)
% If Sel is a vector of logical or integer indices
if islogical(Sel)
Sel = find(Sel);
end
if Sel(1) > 0
% If Sel is a column of indices
unSel = setdiff(1:N,Sel);
if rank(Z(unSel,:)) < R
error('Selected rows of nuisance not full rank')
end
else
% If Sel is -1 or anything else but empty [].
% Try first with a faster approach
Sel = true(N,1);
Zs = bsxfun(@rdivide,Z,mean(Z,2));
[~,~,iU] = unique(Zs,'rows');
nU = max(iU);
for r = randperm(nU,R)
idx = find(iU == r);
idx = idx(randperm(numel(idx)));
Sel(idx(1)) = false;
end
% but it it fails, go with another one
if rank(Z(~Sel,:)) < R
foundSel = false;
while ~ foundSel
Sel = sort(randperm(N,N-R));
unSel = setdiff(1:N,Sel);
if rank(Z(unSel,:)) == R
foundSel = true;
end
end
end
end
S = eye(N);
S = S(:,Sel);
else
% Sel is a matrix proper
S = Sel;
end
Rz = eye(N) - Z*pinv(Z);
Q = Rz*S*sqrtm(inv(S'*Rz*S));
end
% =================================================================
function [A,B,cc] = cca(Y,X,R,S)
% Compute CCA.
N = size(Y,1);
[Qy,Ry,iY] = qr(Y,0);
[Qx,Rx,iX] = qr(X,0);
K = min(rank(Y),rank(X));
[L,D,M] = svds(Qy'*Qx,K);
cc = min(max(diag(D(:,1:K))',0),1);
A = Ry\L(:,1:K)*sqrt(N-R);
B = Rx\M(:,1:K)*sqrt(N-S);
A(iY,:) = A;
B(iX,:) = B;
% =================================================================
function X = center(X)
% Mean center a matrix and remove constant columns.
icte = sum(diff(X,1,1).^2,1) == 0;
X = bsxfun(@minus,X,mean(X,1));
X(:,icte) = [];