|
8 | 8 | %
|
9 | 9 | % MATLAB version
|
10 | 10 | %
|
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 | + % |
22 | 26 | % 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 | + % |
27 | 30 | %
|
28 | 31 | % EXAMPLE USAGE:
|
29 | 32 | % Data is in a 2D matrix, i.e., variable X observation (e.g., voxels X subjects or connections X subjects)
|
|
35 | 38 | % Specify default parameters (if keyword arguments not provided)
|
36 | 39 | default_nullmean = 0;
|
37 | 40 | default_alpha = .05;
|
38 |
| - default_tail = 1; |
| 41 | + default_tail = 0; |
39 | 42 | default_permutations = 1000;
|
40 | 43 | default_nproc = 1;
|
41 | 44 | addRequired(p, 'diff_arr');
|
|
59 | 62 | maxT_dist = zeros(permutations,1);
|
60 | 63 | parfor (i=1:permutations, nproc)
|
61 | 64 | seed = seeds(i);
|
62 |
| - maxT_dist(i) = runPermutation(diff_arr, nullmean, seed); |
| 65 | + maxT_dist(i) = runPermutation(diff_arr, nullmean, tail, seed); |
63 | 66 | end
|
64 | 67 |
|
65 | 68 | % Obtain real t-values
|
66 | 69 | [H, P, CI, STATS] = ttest(diff_arr,nullmean,'dim',2);
|
67 | 70 | realT = STATS.tstat;
|
68 | 71 |
|
69 | 72 | % Find threshold for alpha
|
70 |
| - maxT_dist_sorted = sort(maxT_dist) |
| 73 | + maxT_dist_sorted = sort(maxT_dist); |
71 | 74 | % Specify which tail we want
|
72 | 75 | if tail == 1
|
73 | 76 | topPercVal_maxT_inx = length(maxT_dist_sorted)*(1-alpha);
|
74 | 77 | elseif tail == -1
|
75 | 78 | topPercVal_maxT_inx = length(maxT_dist_sorted)*(alpha);
|
| 79 | + elseif tail == 0 |
| 80 | + topPercVal_maxT_inx = length(maxT_dist_sorted)*(1-alpha); |
76 | 81 | end
|
77 | 82 |
|
78 | 83 | maxT_thresh = maxT_dist_sorted(topPercVal_maxT_inx+1);
|
|
91 | 96 | end
|
92 | 97 |
|
93 | 98 |
|
94 |
| -function maxT = runPermutation(diff_arr,nullmean,seed) |
| 99 | +function maxT = runPermutation(diff_arr,nullmean,tail,seed) |
95 | 100 | % Helper function to perform a single permutation
|
96 | 101 |
|
97 | 102 | % Set random seed
|
|
110 | 115 |
|
111 | 116 | % Take t-test against 0 for each independent test
|
112 | 117 | [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 |
114 | 125 |
|
115 | 126 | end
|
0 commit comments