-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclustperm.m
65 lines (56 loc) · 2.1 KB
/
clustperm.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
function [clusters] = clustperm(dat, thrsh, clustthr, nits, nonparam)
% 1-dimensional cluster-based permutation test against ~ 0
% dat: conditions x samples x subjects; clusters: conditions x samples;
% (c) Bernhard Spitzer, 2016
clusters=NaN(size(dat,1),size(dat,2));
for i=1:size(dat,1)
disp(['cluster-based permutation test ' num2str(i) '/' num2str(size(dat,1))]);
itclust=zeros(1,nits);
for iter=1:nits
itstats=zeros(1,size(dat,2));
flipper=randsample([-1 1],size(dat,3),1)';
for t=1:size(dat,2)
if nonparam
[p,h,stats] = signrank(squeeze(dat(i,t,:)).*flipper);
else
[h p stats] = ttest(squeeze(dat(i,t,:)).*flipper);
end
itstats(t)=p;
end
% find clusters (iterations)
array=find(itstats<thrsh);
temp = abs(diff(array));
indx = find(temp > 1);
indx = [0 indx length(array)];
iclust=zeros(length(indx)-1,1);
for ii = 1:(length(indx)-1)
iclust(ii) = length(array((indx(ii)+1):indx(ii+1)));
end
itclust(iter)=max(iclust);
end
itclust=sort(itclust,2,'descend');
ctoff=clustthr.*nits;
thrclust=itclust(ctoff);
actstats=zeros(1,size(dat,2));
for t=1:size(dat,2)
if nonparam
[p,h,stats] = signrank(squeeze(dat(i,t,:)));
else
[h p stats] = ttest(squeeze(dat(i,t,:)));
end
actstats(t)=p;
end
% find clusters (data)
array=find(actstats<thrsh);
temp = abs(diff(array));
indx = find(temp > 1);
indx = [0 indx length(array)];
actclust={};
for ii = 1:(length(indx)-1)
actclust{ii} = array((indx(ii)+1):indx(ii+1));
if length(actclust{ii})>thrclust
clusters(i,actclust{ii})=i;
end
end
end
end