forked from ChristianGaser/BrainAGE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBA_ttest2.m
358 lines (322 loc) · 13.3 KB
/
BA_ttest2.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
function [h,p,ci,stats] = BA_ttest2(x,y,alpha,tail,vartype,dim)
%TTEST2 Two-sample t-test with pooled or unpooled variance estimate.
% H = TTEST2(X,Y) performs a t-test of the hypothesis that two
% independent samples, in the vectors X and Y, come from distributions
% with equal means, and returns the result of the test in H. H=0
% indicates that the null hypothesis ("means are equal") cannot be
% rejected at the 5% significance level. H=1 indicates that the null
% hypothesis can be rejected at the 5% level. The data are assumed to
% come from normal distributions with unknown, but equal, variances. X
% and Y can have different lengths.
%
% This function performs an unpaired two-sample t-test. For a paired
% test, use the TTEST function.
%
% X and Y can also be matrices or N-D arrays. For matrices, TTEST2
% performs separate t-tests along each column, and returns a vector of
% results. X and Y must have the same number of columns. For N-D
% arrays, TTEST2 works along the first non-singleton dimension. X and Y
% must have the same size along all the remaining dimensions.
%
% TTEST2 treats NaNs as missing values, and ignores them.
%
% H = TTEST2(X,Y,ALPHA) performs the test at the significance level
% (100*ALPHA)%. ALPHA must be a scalar.
%
% H = TTEST2(X,Y,ALPHA,TAIL) performs the test against the alternative
% hypothesis specified by TAIL:
% 'both' -- "means are not equal" (two-tailed test)
% 'right' -- "mean of X is greater than mean of Y" (right-tailed test)
% 'left' -- "mean of X is less than mean of Y" (left-tailed test)
% TAIL must be a single string.
%
% H = TTEST2(X,Y,ALPHA,TAIL,VARTYPE) allows you to specify the type of
% test. When VARTYPE is 'equal', TTEST2 performs the default test
% assuming equal variances. When VARTYPE is 'unequal', TTEST2 performs
% the test assuming that the two samples come from normal distributions
% with unknown and unequal variances. This is known as the Behrens-Fisher
% problem. TTEST2 uses Satterthwaite's approximation for the effective
% degrees of freedom. VARTYPE must be a single string.
%
% [H,P] = TTEST2(...) returns the p-value, i.e., the probability of
% observing the given result, or one more extreme, by chance if the null
% hypothesis is true. Small values of P cast doubt on the validity of
% the null hypothesis.
%
% [H,P,CI] = TTEST2(...) returns a 100*(1-ALPHA)% confidence interval for
% the true difference of population means.
%
% [H,P,CI,STATS] = TTEST2(...) returns a structure with the following fields:
% 'tstat' -- the value of the test statistic
% 'df' -- the degrees of freedom of the test
% 'sd' -- the pooled estimate of the population standard deviation
% (for the equal variance case) or a vector containing the
% unpooled estimates of the population standard deviations
% (for the unequal variance case)
%
% [...] = TTEST2(X,Y,ALPHA,TAIL,VARTYPE,DIM) works along dimension DIM of
% X and Y. Pass in [] to use default values for ALPHA, TAIL, or VARTYPE.
%
% See also TTEST, RANKSUM, VARTEST2, ANSARIBRADLEY.
% References:
% [1] E. Kreyszig, "Introductory Mathematical Statistics",
% John Wiley, 1970, section 13.4. (Table 13.4.1 on page 210)
% Copyright 1993-2010 The MathWorks, Inc.
% $Revision: 1.1.8.2 $ $Date: 2010/04/24 18:31:38 $
% ______________________________________________________________________
%
% Christian Gaser
% Structural Brain Mapping Group (https://neuro-jena.github.io)
% Departments of Neurology and Psychiatry
% Jena University Hospital
% ______________________________________________________________________
% $Id$
if nargin < 2
error('stats:ttest2:TooFewInputs','Requires at least two input arguments');
end
if nargin < 3 || isempty(alpha)
alpha = 0.05;
elseif ~isscalar(alpha) || alpha <= 0 || alpha >= 1
error('stats:ttest2:BadAlpha','ALPHA must be a scalar between 0 and 1.');
end
if nargin < 4 || isempty(tail)
tail = 0;
elseif ischar(tail) && (size(tail,1)==1)
tail = find(strncmpi(tail,{'left','both','right'},length(tail))) - 2;
end
if ~isscalar(tail) || ~isnumeric(tail)
error('stats:ttest2:BadTail', ...
'TAIL must be one of the strings ''both'', ''right'', or ''left''.');
end
if nargin < 5 || isempty(vartype)
vartype = 1;
elseif ischar(vartype) && (size(vartype,1)==1)
vartype = find(strncmpi(vartype,{'equal','unequal'},length(vartype)));
end
if ~isscalar(vartype) || ~isnumeric(vartype)
error('stats:ttest2:BadVarType', ...
'VARTYPE must be one of the strings ''equal'' or ''unequal''.');
end
if nargin < 6 || isempty(dim)
% Figure out which dimension mean will work along by looking at x. y
% will have be compatible. If x is a scalar, look at y.
dim = find(size(x) ~= 1, 1);
if isempty(dim), dim = find(size(y) ~= 1, 1); end
if isempty(dim), dim = 1; end
% If we haven't been given an explicit dimension, and we have two
% vectors, then make y the same orientation as x.
if isvector(x) && isvector(y)
if dim == 2
y = y(:)';
else % dim == 1
y = y(:);
end
end
end
% Make sure all of x's and y's non-working dimensions are identical.
sizex = size(x); sizex(dim) = 1;
sizey = size(y); sizey(dim) = 1;
if ~isequal(sizex,sizey)
error('stats:ttest2:InputSizeMismatch',...
'The data in a 2-sample t-test must be commensurate.');
end
xnans = isnan(x);
if any(xnans(:))
nx = sum(~xnans,dim);
else
nx = size(x,dim); % a scalar, => a scalar call to tinv
end
ynans = isnan(y);
if any(ynans(:))
ny = sum(~ynans,dim);
else
ny = size(y,dim); % a scalar, => a scalar call to tinv
end
s2x = nanvar(x,[],dim);
s2y = nanvar(y,[],dim);
difference = nanmean(x,dim) - nanmean(y,dim);
if vartype == 1 % equal variances
dfe = nx + ny - 2;
sPooled = sqrt(((nx-1) .* s2x + (ny-1) .* s2y) ./ dfe);
se = sPooled .* sqrt(1./nx + 1./ny);
ratio = difference ./ se;
if (nargout>3)
stats = struct('tstat', ratio, 'df', cast(dfe,class(ratio)), ...
'sd', sPooled);
if isscalar(dfe) && ~isscalar(ratio)
stats.df = repmat(stats.df,size(ratio));
end
end
elseif vartype == 2 % unequal variances
s2xbar = s2x ./ nx;
s2ybar = s2y ./ ny;
dfe = (s2xbar + s2ybar) .^2 ./ (s2xbar.^2 ./ (nx-1) + s2ybar.^2 ./ (ny-1));
se = sqrt(s2xbar + s2ybar);
ratio = difference ./ se;
if (nargout>3)
stats = struct('tstat', ratio, 'df', cast(dfe,class(ratio)), ...
'sd', sqrt(cat(dim, s2x, s2y)));
if isscalar(dfe) && ~isscalar(ratio)
stats.df = repmat(stats.df,size(ratio));
end
end
% Satterthwaite's approximation breaks down when both samples have zero
% variance, so we may have gotten a NaN dfe. But if the difference in
% means is non-zero, the hypothesis test can still reasonable results,
% that don't depend on the dfe, so give dfe a dummy value. If difference
% in means is zero, the hypothesis test returns NaN. The CI can be
% computed ok in either case.
if se == 0, dfe = 1; end
else
error('stats:ttest2:BadVarType',...
'VARTYPE must be ''equal'' or ''unequal'', or 1 or 2.');
end
% Compute the correct p-value for the test, and confidence intervals
% if requested.
if tail == 0 % two-tailed test
p = 2 * spm_Tcdf(-abs(ratio),dfe);
if nargout > 2
spread = tinv(1 - alpha ./ 2, dfe) .* se;
ci = cat(dim, difference-spread, difference+spread);
end
elseif tail == 1 % right one-tailed test
p = spm_Tcdf(-ratio,dfe);
if nargout > 2
spread = tinv(1 - alpha, dfe) .* se;
ci = cat(dim, difference-spread, Inf(size(p)));
end
elseif tail == -1 % left one-tailed test
p = spm_Tcdf(ratio,dfe);
if nargout > 2
spread = tinv(1 - alpha, dfe) .* se;
ci = cat(dim, -Inf(size(p)), difference+spread);
end
else
error('stats:ttest2:BadTail',...
'TAIL must be ''both'', ''right'', or ''left'', or 0, 1, or -1.');
end
% Determine if the actual significance exceeds the desired significance
h = cast(p <= alpha, class(p));
h(isnan(p)) = NaN; % p==NaN => neither <= alpha nor > alpha
function y = nanvar(x,w,dim)
%NANVAR Variance, ignoring NaNs.
% Y = NANVAR(X) returns the sample variance of the values in X, treating
% NaNs as missing values. For a vector input, Y is the variance of the
% non-NaN elements of X. For a matrix input, Y is a row vector
% containing the variance of the non-NaN elements in each column of X.
% For N-D arrays, NANVAR operates along the first non-singleton dimension
% of X.
%
% NANVAR normalizes Y by N-1 if N>1, where N is the sample size of the
% non-NaN elements. This is an unbiased estimator of the variance of the
% population from which X is drawn, as long as X consists of independent,
% identically distributed samples, and data are missing at random. For
% N=1, Y is normalized by N.
%
% Y = NANVAR(X,1) normalizes by N and produces the second moment of the
% sample about its mean. NANVAR(X,0) is the same as NANVAR(X).
%
% Y = NANVAR(X,W) computes the variance using the weight vector W. The
% length of W must equal the length of the dimension over which NANVAR
% operates, and its non-NaN elements must be nonnegative. Elements of X
% corresponding to NaN elements of W are ignored.
%
% Y = NANVAR(X,W,DIM) takes the variance along dimension DIM of X.
%
% See also VAR, NANSTD, NANMEAN, NANMEDIAN, NANMIN, NANMAX, NANSUM.
% Copyright 1984-2005 The MathWorks, Inc.
% $Revision: 1.1.8.1 $ $Date: 2010/03/16 00:15:55 $
if nargin < 2 || isempty(w), w = 0; end
sz = size(x);
if nargin < 3 || isempty(dim)
% The output size for [] is a special case when DIM is not given.
if isequal(x,[]), y = NaN(class(x)); return; end
% Figure out which dimension sum will work along.
dim = find(sz ~= 1, 1);
if isempty(dim), dim = 1; end
elseif dim > length(sz)
sz(end+1:dim) = 1;
end
% Need to tile the mean of X to center it.
tile = ones(size(sz));
tile(dim) = sz(dim);
if isequal(w,0) || isequal(w,1)
% Count up non-NaNs.
n = sum(~isnan(x),dim);
if w == 0
% The unbiased estimator: divide by (n-1). Can't do this when
% n == 0 or 1, so n==1 => we'll return zeros
denom = max(n-1, 1);
else
% The biased estimator: divide by n.
denom = n; % n==1 => we'll return zeros
end
denom(n==0) = NaN; % Make all NaNs return NaN, without a divideByZero warning
x0 = x - repmat(nanmean(x, dim), tile);
y = nansum(abs(x0).^2, dim) ./ denom; % abs guarantees a real result
% Weighted variance
elseif numel(w) ~= sz(dim)
error('MATLAB:nanvar:InvalidSizeWgts','The length of W must be compatible with X.');
elseif ~(isvector(w) && all(w(~isnan(w)) >= 0))
error('MATLAB:nanvar:InvalidWgts','W must be a vector of nonnegative weights, or a scalar 0 or 1.');
else
% Embed W in the right number of dims. Then replicate it out along the
% non-working dims to match X's size.
wresize = ones(size(sz)); wresize(dim) = sz(dim);
wtile = sz; wtile(dim) = 1;
w = repmat(reshape(w, wresize), wtile);
% Count up non-NaNs.
n = nansum(~isnan(x).*w,dim);
x0 = x - repmat(nansum(w.*x, dim) ./ n, tile);
y = nansum(w .* abs(x0).^2, dim) ./ n; % abs guarantees a real result
end
function m = nanmean(x,dim)
%NANMEAN Mean value, ignoring NaNs.
% M = NANMEAN(X) returns the sample mean of X, treating NaNs as missing
% values. For vector input, M is the mean value of the non-NaN elements
% in X. For matrix input, M is a row vector containing the mean value of
% non-NaN elements in each column. For N-D arrays, NANMEAN operates
% along the first non-singleton dimension.
%
% NANMEAN(X,DIM) takes the mean along dimension DIM of X.
%
% See also MEAN, NANMEDIAN, NANSTD, NANVAR, NANMIN, NANMAX, NANSUM.
% Copyright 1993-2004 The MathWorks, Inc.
% $Revision: 1.1.8.1 $ $Date: 2010/03/16 00:15:50 $
% Find NaNs and set them to zero
nans = isnan(x);
x(nans) = 0;
if nargin == 1 % let sum deal with figuring out which dimension to use
% Count up non-NaNs.
n = sum(~nans);
n(n==0) = NaN; % prevent divideByZero warnings
% Sum up non-NaNs, and divide by the number of non-NaNs.
m = sum(x) ./ n;
else
% Count up non-NaNs.
n = sum(~nans,dim);
n(n==0) = NaN; % prevent divideByZero warnings
% Sum up non-NaNs, and divide by the number of non-NaNs.
m = sum(x,dim) ./ n;
end
function y = nansum(x,dim)
%NANSUM Sum, ignoring NaNs.
% Y = NANSUM(X) returns the sum of X, treating NaNs as missing values.
% For vector input, Y is the sum of the non-NaN elements in X. For
% matrix input, Y is a row vector containing the sum of non-NaN elements
% in each column. For N-D arrays, NANSUM operates along the first
% non-singleton dimension.
%
% Y = NANSUM(X,DIM) takes the sum along dimension DIM of X.
%
% See also SUM, NANMEAN, NANVAR, NANSTD, NANMIN, NANMAX, NANMEDIAN.
% Copyright 1993-2004 The MathWorks, Inc.
% $Revision: 1.1.8.1 $ $Date: 2010/03/16 00:15:54 $
% Find NaNs and set them to zero. Then sum up non-NaNs. Cols of all NaNs
% will return zero.
x(isnan(x)) = 0;
if nargin == 1 % let sum figure out which dimension to work along
y = sum(x);
else % work along the explicitly given dimension
y = sum(x,dim);
end