-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclusterPermuteTtest.asv
More file actions
73 lines (61 loc) · 1.74 KB
/
Copy pathclusterPermuteTtest.asv
File metadata and controls
73 lines (61 loc) · 1.74 KB
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
function [h, p] = clusterPermuteTtest(A, B, varargin)
% function [h, p] = clusterPermuteTtest(data1, data2)
% data matrices have time along the x, and trial along the y
plotdistb = 0;
if isempty(varargin)
alphathresh = 0.05;
else
alphathresh = varargin{1};
end
nrep = 1000;
ntrials = [size(A, 1), size(B,1)];
%mintrials = min(ntrials);
[h, p, ~, stats] = ttest2(A, B, 'alpha', alphathresh); %operates on each column
clusts = bwlabel(h);
nclust = length(unique(clusts))-1;
groupt = []; ts=[];
for jj=1:nclust
ts = sum([stats.tstat(clusts == (jj+1))]);
groupt = cat(1,groupt, ts);
end
groupt = abs(groupt);
C = cat(1, A, B); %make a single pool of all trials to permute
nrows = size(C,1);
tsums = [];
for ii=1:nrep
order = randperm(nrows);
A1 = C(order(1:ntrials(1)), :);
bi = order((ntrials(1)+1):nrows);
B1 = C(bi, :);
[h1, ~, ~, stat1] = ttest2(A1,B1, 'alpha', alphathresh);
clust1 = bwlabel(h1);
nclust = length(unique(clust1))-1;
for jj=1:nclust
ts(jj) = sum([stat1.tstat(clust1 == (jj+1))]);
%tsums = cat(1,tsums, ts);
end
tsums = cat(1,tsums, max(abs(ts)));
clear ts;
end
tsums = tsums(tsums ~= 0 & ~isnan(tsums)); %get rid of the zeros in this vector
tsums = abs(tsums);
if ~isempty(tsums)
sig_thresh = quantile(tsums, .95);
else
tsums = 1;
end
hgroup = groupt >= sig_thresh; %above or below permuted significance
for ii=1:length(hgroup)
h(clusts == ii) = hgroup(ii);
p(clusts == ii) = p(clusts == ii);
end
if plotdistb && ~isempty(tsums)
figure;
%[f, stepx] = ecdf(tsums);
%stairs(stepx,f, 'k'); hold on;
hist(tsums, 100); hold on;
ah = gca;
for ii=1:length(groupt)
plot([groupt(ii), groupt(ii)], ah.YLim,'r');
end
end