-
Notifications
You must be signed in to change notification settings - Fork 28
/
palm_partition.m
133 lines (126 loc) · 4.65 KB
/
palm_partition.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
function [X,Z,eCm,eCx] = palm_partition(M,C,meth,Y)
% Partition a design matrix into regressors of interest and
% nuisance according to a given contrast.
%
% Usage
% [X,Z] = palm_partition(M,C,meth,Y)
%
% Inputs:
% M : Design matrix, to be partitioned.
% C : Contrast that will define the partitioning.
% meth : Method for the partitioning. It can be:
% - 'Guttman'
% - 'Beckmann'
% - 'Ridgway'
% - 'none' (does nothing, X=M, Z=[])
% Y : (Optional) For the 'Winkler' method only.
%
% Outputs:
% X : Matrix with regressors of interest.
% Z : Matrix with regressors of no interest.
% eCm : Effective contrast, equivalent to the original,
% for the partitioned model [X Z], and considering
% all regressors.
% eCx : Same as above, but considering only X.
%
% References:
% * Guttman I. Linear Models: An Introduction. Wiley,
% New York, 1982.
% * Smith SM, Jenkinson M, Beckmann C, Miller K,
% Woolrich M. Meaningful design and contrast estimability
% in FMRI. Neuroimage 2007;34(1):127-36.
% * Ridgway GR. Statistical analysis for longitudinal MR
% imaging of dementia. PhD thesis. 2009.
% * Winkler AM, Ridgway GR, Webster MG, Smith SM, Nichols TE.
% Permutation inference for the general linear model.
% Neuroimage. 2014 May 15;92:381-97.
% _____________________________________
% A. Winkler, G. Ridgway & T. Nichols
% FMRIB / University of Oxford
% Mar/2012 (1st version)
% Aug/2013 (major revision)
% Dec/2015 (this version)
% 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/>.
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
switch lower(meth)
case 'guttman' % works for evperdat (3D)
idx = any(C~=0,2);
X = M(:,idx,:);
Z = M(:,~idx,:);
eCm = vertcat(C(idx,:),C(~idx,:));
case 'beckmann' % works for evperdat (3D)
Cu = null(C');
for t = 1:size(M,3)
D = pinv(M(:,:,t)'*M(:,:,t));
CDCi = pinv(C'*D*C);
Pc = C*CDCi*C'*D;
Cv = Cu - Pc*Cu;
F3 = pinv(Cv'*D*Cv);
if t == 1
X = zeros(size(M,1),size(CDCi,2),size(M,3));
Z = zeros(size(M,1),size(F3,2),size(M,3));
end
X(:,:,t) = M(:,:,t)*D*C*CDCi;
Z(:,:,t) = M(:,:,t)*D*Cv*F3;
end
eCm = vertcat(eye(size(X,2)),...
zeros(size(Z,2),size(X,2)));
case 'ridgway' % works for evperdat (3D)
rC = rank(C);
rM = rank(M(:,:,ceil(size(M,3)/2)));
rZ = rM - rC;
pinvC = pinv(C');
if rZ > 0
C0 = eye(size(M,2)) - C*pinv(C);
for t = 1:size(M,3)
if t == 1
X = zeros(size(M,1),size(pinvC,2),size(M,3));
Z = zeros(size(M,1),rZ,size(M,3));
end
tmpX = M(:,:,t)*pinvC;
[tmpZ,~,~] = svd(M(:,:,t)*C0);
Z(:,:,t) = tmpZ(:,1:rZ);
X(:,:,t) = tmpX-Z(:,:,t)*pinv(Z(:,:,t))*tmpX;
end
eCm = vertcat(eye(size(X,2)),...
zeros(size(Z,2),size(X,2)));
else
for t = 1:size(M,3)
if t == 1
X = zeros(size(M,1),size(pinvC,2),size(M,3));
Z = zeros(size(M,1),rZ,size(M,3));
end
X = M(:,:,t)*pinvC;
end
eCm = eye(size(X,2));
end
case 'none' % works for evperdat (3D)
X = M;
Z = [];
eCm = C;
case 'sometest' % this is just for testing, and doesn't work with evperdat
D = pinv(M'*M);
X = M*D*C*pinv(C'*D*C);
Z = (M*D*M'-X*pinv(X))*Y;
eCm = vertcat(eye(size(X,2)),...
zeros(size(Z,2),size(X,2)));
otherwise
error('''%s'' - Unknown partitioning scheme',meth);
end
eCx = eCm(1:size(X,2),:);