forked from neurodebian/spm8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spm_P_RF.m
194 lines (160 loc) · 6.16 KB
/
spm_P_RF.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
function [P,p,Ec,Ek] = spm_P_RF(c,k,Z,df,STAT,R,n)
% Returns the [un]corrected P value using unifed EC theory
% FORMAT [P p Ec Ek] = spm_P_RF(c,k,z,df,STAT,R,n)
%
% c - cluster number
% k - extent {RESELS}
% z - height {minimum over n values}
% df - [df{interest} df{error}]
% STAT - Statistical field
% 'Z' - Gaussian field
% 'T' - T - field
% 'X' - Chi squared field
% 'F' - F - field
% R - RESEL Count {defining search volume}
% n - number of component SPMs in conjunction
%
% P - corrected P value - P(C >= c | K >= k}
% p - uncorrected P value
% Ec - expected number of clusters (maxima)
% Ek - expected number of resels per cluster
%
%__________________________________________________________________________
%
% spm_P_RF returns the probability of c or more clusters with more than
% k resels in volume process of R RESELS thresholded at u. All p values
% can be considered special cases:
%
% spm_P_RF(1,0,z,df,STAT,1,n) = uncorrected p value
% spm_P_RF(1,0,z,df,STAT,R,n) = corrected p value {based on height z)
% spm_P_RF(1,k,u,df,STAT,R,n) = corrected p value {based on extent k at u)
% spm_P_RF(c,k,u,df,STAT,R,n) = corrected p value {based on number c at k and u)
% spm_P_RF(c,0,u,df,STAT,R,n) = omnibus p value {based on number c at u)
%
% If n > 1 a conjunction probility over the n values of the statistic
% is returned
%__________________________________________________________________________
%
% References:
%
% [1] Hasofer AM (1978) Upcrossings of random fields
% Suppl Adv Appl Prob 10:14-21
% [2] Friston KJ et al (1994) Assessing the Significance of Focal Activations
% Using Their Spatial Extent
% Human Brain Mapping 1:210-220
% [3] Worsley KJ et al (1996) A Unified Statistical Approach for Determining
% Significant Signals in Images of Cerebral Activation
% Human Brain Mapping 4:58-73
%__________________________________________________________________________
% Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
% Karl Friston
% $Id: spm_P_RF.m 3994 2010-07-13 15:53:30Z guillaume $
% get expectations
%==========================================================================
% get EC densities
%--------------------------------------------------------------------------
D = find(R,1,'last');
R = R(1:D);
G = sqrt(pi)./gamma(([1:D])/2);
EC = spm_ECdensity(STAT,Z,df);
EC = max(EC(1:D),eps);
% corrected p value
%--------------------------------------------------------------------------
P = triu(toeplitz(EC'.*G))^n;
P = P(1,:);
EM = (R./G).*P; % <maxima> over D dimensions
Ec = sum(EM); % <maxima>
EN = P(1)*R(D); % <resels>
Ek = EN/EM(D); % Ek = EN/EM(D);
% get P{n > k}
%==========================================================================
% assume a Gaussian form for P{n > k} ~ exp(-beta*k^(2/D))
% Appropriate for SPM{Z} and high d.f. SPM{T}
%--------------------------------------------------------------------------
D = D - 1;
if ~k || ~D
p = 1;
elseif STAT == 'Z'
beta = (gamma(D/2 + 1)/Ek)^(2/D);
p = exp(-beta*(k^(2/D)));
elseif STAT == 'T'
beta = (gamma(D/2 + 1)/Ek)^(2/D);
p = exp(-beta*(k^(2/D)));
elseif STAT == 'X'
beta = (gamma(D/2 + 1)/Ek)^(2/D);
p = exp(-beta*(k^(2/D)));
elseif STAT == 'F'
beta = (gamma(D/2 + 1)/Ek)^(2/D);
p = exp(-beta*(k^(2/D)));
end
% Poisson clumping heuristic {for multiple clusters}
%==========================================================================
P = 1 - spm_Pcdf(c - 1,(Ec + eps)*p);
% set P and p = [] for non-implemented cases
%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if k > 0 && n > 1
P = []; p = [];
end
if k > 0 && (STAT == 'X' || STAT == 'F')
P = []; p = [];
end
%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
%==========================================================================
% spm_ECdensity
%==========================================================================
function [EC] = spm_ECdensity(STAT,t,df)
% Returns the EC density
%__________________________________________________________________________
%
% Reference : Worsley KJ et al 1996, Hum Brain Mapp. 4:58-73
%
%--------------------------------------------------------------------------
% EC densities (EC}
%--------------------------------------------------------------------------
t = t(:)';
if STAT == 'Z'
% Gaussian Field
%----------------------------------------------------------------------
a = 4*log(2);
b = exp(-t.^2/2);
EC(1,:) = 1 - spm_Ncdf(t);
EC(2,:) = a^(1/2)/(2*pi)*b;
EC(3,:) = a/((2*pi)^(3/2))*b.*t;
EC(4,:) = a^(3/2)/((2*pi)^2)*b.*(t.^2 - 1);
elseif STAT == 'T'
% T - Field
%----------------------------------------------------------------------
v = df(2);
a = 4*log(2);
b = exp(gammaln((v+1)/2) - gammaln(v/2));
c = (1+t.^2/v).^((1-v)/2);
EC(1,:) = 1 - spm_Tcdf(t,v);
EC(2,:) = a^(1/2)/(2*pi)*c;
EC(3,:) = a/((2*pi)^(3/2))*c.*t/((v/2)^(1/2))*b;
EC(4,:) = a^(3/2)/((2*pi)^2)*c.*((v-1)*(t.^2)/v - 1);
elseif STAT == 'X'
% X - Field
%----------------------------------------------------------------------
v = df(2);
a = (4*log(2))/(2*pi);
b = t.^(1/2*(v - 1)).*exp(-t/2-gammaln(v/2))/2^((v-2)/2);
EC(1,:) = 1 - spm_Xcdf(t,v);
EC(2,:) = a^(1/2)*b;
EC(3,:) = a*b.*(t-(v-1));
EC(4,:) = a^(3/2)*b.*(t.^2-(2*v-1)*t+(v-1)*(v-2));
elseif STAT == 'F'
% F Field
%----------------------------------------------------------------------
k = df(1);
v = df(2);
a = (4*log(2))/(2*pi);
b = gammaln(v/2) + gammaln(k/2);
EC(1,:) = 1 - spm_Fcdf(t,df);
EC(2,:) = a^(1/2)*exp(gammaln((v+k-1)/2)-b)*2^(1/2)...
*(k*t/v).^(1/2*(k-1)).*(1+k*t/v).^(-1/2*(v+k-2));
EC(3,:) = a*exp(gammaln((v+k-2)/2)-b)*(k*t/v).^(1/2*(k-2))...
.*(1+k*t/v).^(-1/2*(v+k-2)).*((v-1)*k*t/v-(k-1));
EC(4,:) = a^(3/2)*exp(gammaln((v+k-3)/2)-b)...
*2^(-1/2)*(k*t/v).^(1/2*(k-3)).*(1+k*t/v).^(-1/2*(v+k-2))...
.*((v-1)*(v-2)*(k*t/v).^2-(2*v*k-v-k-1)*(k*t/v)+(k-1)*(k-2));
end