Skip to content

Commit c06b7c7

Browse files
committed
updated maxT code for two-tailed test
1 parent 2847df1 commit c06b7c7

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

matlabCode/maxT.m

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,25 @@
88
%
99
% MATLAB version
1010
%
11-
% Parameters:
12-
% diff_arr = MxN matrix of set of M independent tests for condition 1 minus condition 2 across N subjects
13-
% diff_arr can also be an array of multiple values (or tests) compared against the nullmean (or null mean)
14-
% Optional parameters:
15-
% nullmean : Expected value of the null hypothesis {default : 0, for a t-test against 0}
16-
% alpha : Corrected alpha level {default : .05)
17-
% tail : [1 or -1] If tail = 1, reject the null hypothesis if the mean of the data is greater than 0 (upper tailed test).
18-
% If tail = -1, reject the null hypothesis if the mean of the data is less than nullmean {default = 1}
19-
% permutations : Number of permutations to perform {default 1000}
20-
% nproc : Number of processes to run in parallel {default 1}
21-
%
11+
% Required Parameters:
12+
% diff_arr = MxN matrix of set of M independent tests for condition 1 minus condition 2 across N subjects
13+
% diff_arr can also be an array of multiple values (or tests) compared against the nullmean (or null mean)
14+
% Optional Parameters:
15+
% nullmean = Expected value of the null hypothesis {default = 0, for a t-test against 0}
16+
% alpha = alpha value to return the maxT threshold {default = .05}
17+
% tail = [0, 1, or -1]
18+
% If tail = 1, reject the null hypothesis if the statistic is greater than the null dist (upper tailed test).
19+
% If tail = -1, reject the null hypothesis if the statistic is less than the null dist (lower tailed test).
20+
% If tail = 0, reject the null hypothesis for a two-tailed test
21+
% {default : 0}
22+
% permutations = Number of permutations to perform {default = 1000}
23+
% nproc = number of processes to run in parallel {default = 1}
24+
% pvals = if True, returns equivalent p-value distribution for all t-values {default = True}
25+
%
2226
% Returns:
23-
% t : Array of T-values of correct contrast map (Mx1 vector, for M tests)
24-
% maxT_thresh : maximum t-value for associated alpha-level
25-
%
26-
% N.B.: Only works for paired one-sample t-tests
27+
% t: Array of T-values of correct contrast map (Mx1 vector, for M tests)
28+
% maxTThreshold : The t-value threshold corresponding to the corrected alpha value. If a two-tailed test is specified, the maxR is provided as an absolute value
29+
%
2730
%
2831
% EXAMPLE USAGE:
2932
% Data is in a 2D matrix, i.e., variable X observation (e.g., voxels X subjects or connections X subjects)
@@ -35,7 +38,7 @@
3538
% Specify default parameters (if keyword arguments not provided)
3639
default_nullmean = 0;
3740
default_alpha = .05;
38-
default_tail = 1;
41+
default_tail = 0;
3942
default_permutations = 1000;
4043
default_nproc = 1;
4144
addRequired(p, 'diff_arr');
@@ -59,20 +62,22 @@
5962
maxT_dist = zeros(permutations,1);
6063
parfor (i=1:permutations, nproc)
6164
seed = seeds(i);
62-
maxT_dist(i) = runPermutation(diff_arr, nullmean, seed);
65+
maxT_dist(i) = runPermutation(diff_arr, nullmean, tail, seed);
6366
end
6467

6568
% Obtain real t-values
6669
[H, P, CI, STATS] = ttest(diff_arr,nullmean,'dim',2);
6770
realT = STATS.tstat;
6871

6972
% Find threshold for alpha
70-
maxT_dist_sorted = sort(maxT_dist)
73+
maxT_dist_sorted = sort(maxT_dist);
7174
% Specify which tail we want
7275
if tail == 1
7376
topPercVal_maxT_inx = length(maxT_dist_sorted)*(1-alpha);
7477
elseif tail == -1
7578
topPercVal_maxT_inx = length(maxT_dist_sorted)*(alpha);
79+
elseif tail == 0
80+
topPercVal_maxT_inx = length(maxT_dist_sorted)*(1-alpha);
7681
end
7782

7883
maxT_thresh = maxT_dist_sorted(topPercVal_maxT_inx+1);
@@ -91,7 +96,7 @@
9196
end
9297

9398

94-
function maxT = runPermutation(diff_arr,nullmean,seed)
99+
function maxT = runPermutation(diff_arr,nullmean,tail,seed)
95100
% Helper function to perform a single permutation
96101

97102
% Set random seed
@@ -110,6 +115,12 @@
110115

111116
% Take t-test against 0 for each independent test
112117
[H, P, CI, STATS] = ttest(diff_arr,nullmean,'dim',2);
113-
maxT = max(STATS.tstat);
118+
if tail == 1
119+
maxT = max(STATS.tstat);
120+
elseif tail == -1
121+
maxT = min(STATS.tstat);
122+
elseif tail == 0
123+
maxT = max(abs(STATS.tstat));
124+
end
114125

115126
end

0 commit comments

Comments
 (0)