-
Notifications
You must be signed in to change notification settings - Fork 17
/
Spearman.m
210 lines (179 loc) · 7.69 KB
/
Spearman.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
function [r,t,pval,hboot,CI] = Spearman(X,Y,fig_flag,level)
% Computes the Spearman correlation with its bootstrap CI.
%
% FORMAT: [r,t,p] = Spearman(X,Y)
% [r,t,p] = Spearman(X,Y,fig_flag,level)
% [r,t,p,hboot,CI] = Spearman(X,Y,fig_flag,level)
%
% INPUTS: X and Y are 2 vectors or matrices, in the latter case,
% correlations are computed column-wise
% fig_flag indicates to plot (1 - default) the data or not (0)
% level is the desired alpha level (5/100 is the default)
%
% OUTPUTS: r is the Spearman correlation
% t is the associated t value
% pval is the corresponding p value
% hboot 1/0 declares the test significant based on CI
% CI is the percentile bootstrap confidence interval
%
% If X and Y are matrices of size [n p], p correlations are computed
% and the CIs are adjusted at the alpha/p level (Bonferonni
% correction); hboot is based on these adjusted CIs but pval remains
% uncorrected.
%
% This function requires the tiedrank.m function from the matlab stat toolbox.
%
% See also TIEDRANK.
% Cyril Pernet v1
% ---------------------------------
% Copyright (C) Corr_toolbox 2012
%% data check
% if X a vector and Y a matrix,
% repmat X to perform multiple tests on Y (or the other around)
if size(X,1) == 1 && size(X,2) > 1; X = X'; end
if size(Y,1) == 1 && size(Y,2) > 1; Y = Y'; end
if size(X,2) == 1 && size(Y,2) > 1
X = repmat(X,1,size(Y,2));
elseif size(Y,2) == 1 && size(X,2) > 1
Y = repmat(Y,1,size(X,2));
end
if sum(size(X)~=size(Y)) ~= 0
error('X and Y must have the same size')
end
%% parameters
if nargin < 2
error('two inputs requested');
elseif nargin == 2
fig_flag = 1;
level = 5/100;
elseif nargin == 3
level = 5/100;
end
[n p] = size(X);
%% basic Spearman
% compute r (default)
Xrank = tiedrank(X,0);
Yrank = tiedrank(Y,0);
r = sum(detrend(Xrank,'constant').*detrend(Yrank,'constant')) ./ ...
(sum(detrend(Xrank,'constant').^2).*sum(detrend(Yrank,'constant').^2)).^(1/2);
t = r.*(sqrt(n-2)) ./ sqrt((1-r.^2));
pval = 2*tcdf(-abs(t),n-2);
% The corr function in the stat toolbox uses
% permutations for n<10 and some other fancy
% things when n>10 and there are no ties among
% ranks - we just do the standard way.
if nargout > 3
nboot = 1000;
if p > 1
level = level / p;
end
low = round((level*nboot)/2);
if low == 0
error('adjusted CI cannot be computed, too many tests for the number of observations')
else
high = nboot - low;
end
% bootstrap
table = randi(n,n,nboot);
for B=1:nboot
xrank = tiedrank(X(table(:,B),:),0);
yrank = tiedrank(Y(table(:,B),:),0);
rb(B,:) = sum(detrend(xrank,'constant').*detrend(yrank,'constant')) ./ ...
(sum(detrend(xrank,'constant').^2).*sum(detrend(yrank,'constant').^2)).^(1/2);
for c=1:size(X,2)
b = pinv([xrank(:,c) ones(n,1)])*yrank(:,c);
slope(B,c) = b(1); intercept(B,c) = b(2,:);
end
end
rb = sort(rb,1);
slope = sort(slope,1);
intercept = sort(intercept,1);
% CI and h
adj_nboot = nboot - sum(isnan(rb));
adj_low = round((level*adj_nboot)/2);
adj_high = adj_nboot - adj_low;
for c=1:size(X,2)
CI(:,c) = [rb(adj_low(c),c) ; rb(adj_high(c),c)];
hboot(c) = (rb(adj_low(c),c) > 0) + (rb(adj_high(c),c) < 0);
CIslope(:,c) = [slope(adj_low(c),c) ; slope(adj_high(c),c)];
CIintercept(:,c) = [intercept(adj_low(c),c) ; intercept(adj_high(c),c)];
end
end
%% plots
if fig_flag ~= 0
answer = [];
if p > 1
answer = questdlg(['plots all ' num2str(p) ' correlations'],'Plotting option','yes','no','yes');
else
if fig_flag == 1
figure('Name','Spearman correlation');
set(gcf,'Color','w');
end
if nargout>3
subplot(1,2,1);
M = sprintf('Spearman corr r=%g \n %g%%CI [%g %g]',r,(1-level)*100,CI(1),CI(2));
else
M = sprintf('Spearman corr r=%g p=%g',r,pval);
end
scatter(Xrank,Yrank,100,'filled'); grid on
xlabel('X Rank','FontSize',14); ylabel('Y Rank','FontSize',14);
title(M,'FontSize',16);
h=lsline; set(h,'Color','r','LineWidth',4);
box on;set(gca,'FontSize',14,'Layer','Top')
if nargout >3
y1 = refline(CIslope(1),CIintercept(1)); set(y1,'Color','r');
y2 = refline(CIslope(2),CIintercept(2)); set(y2,'Color','r');
y1 = get(y1); y2 = get(y2);
xpoints=[[y1.XData(1):y1.XData(2)],[y2.XData(2):-1:y2.XData(1)]];
step1 = y1.YData(2)-y1.YData(1); step1 = step1 / (y1.XData(2)-y1.XData(1));
step2 = y2.YData(2)-y2.YData(1); step2 = step2 / (y2.XData(2)-y2.XData(1));
filled=[[y1.YData(1):step1:y1.YData(2)],[y2.YData(2):-step2:y2.YData(1)]];
hold on; fillhandle=fill(xpoints,filled,[1 0 0]);
set(fillhandle,'EdgeColor',[1 0 0],'FaceAlpha',0.2,'EdgeAlpha',0.8);%set edge color
box on;set(gca,'FontSize',14)
subplot(1,2,2); hold on
k = round(1 + log2(length(rb))); hist(rb,k); grid on;
title({'Bootstrapped correlations';['h=',num2str(hboot)]},'FontSize',16)
xlabel('boot correlations','FontSize',14);ylabel('frequency','FontSize',14)
plot(repmat(CI(1),max(hist(rb,k)),1),[1:max(hist(rb,k))],'r','LineWidth',4);
plot(repmat(CI(2),max(hist(rb,k)),1),[1:max(hist(rb,k))],'r','LineWidth',4);
axis tight; colormap([.4 .4 1])
box on;set(gca,'FontSize',14,'Layer','Top')
end
end
if strcmp(answer,'yes')
for f = 1:p
figure('Color','w','Name',[num2str(f) ' Spearman correlation'])
if nargout >3
subplot(1,2,1);
M = sprintf('Spearman corr r=%g \n %g%%CI [%g %g]',r(f),(1-level)*100,CI(1,f),CI(2,f));
else
M = sprintf('Spearman corr r=%g p=%g',r(f),pval(f));
end
scatter(Xrank(:,f),Yrank(:,f),100,'filled'); grid on
xlabel('X Rank','FontSize',14); ylabel('Y Rank','FontSize',14);
title(M,'FontSize',16);
h=lsline; set(h,'Color','r','LineWidth',4);
if nargout >3
y1 = refline(CIslope(1,f),CIintercept(1,f)); set(y1,'Color','r');
y2 = refline(CIslope(2,f),CIintercept(2,f)); set(y2,'Color','r');
y1 = get(y1); y2 = get(y2);
xpoints=[[y1.XData(1):y1.XData(2)],[y2.XData(2):-1:y2.XData(1)]];
step1 = y1.YData(2)-y1.YData(1); step1 = step1 / (y1.XData(2)-y1.XData(1));
step2 = y2.YData(2)-y2.YData(1); step2 = step2 / (y2.XData(2)-y2.XData(1));
filled=[[y1.YData(1):step1:y1.YData(2)],[y2.YData(2):-step2:y2.YData(1)]];
hold on; fillhandle=fill(xpoints,filled,[1 0 0]);
set(fillhandle,'EdgeColor',[1 0 0],'FaceAlpha',0.2,'EdgeAlpha',0.8);%set edge color
box on;set(gca,'FontSize',14)
subplot(1,2,2); hold on
k = round(1 + log2(length(rb(:,f)))); hist(rb(:,f),k); grid on;
plot(repmat(CI(1,f),max(hist(rb(:,f),k)),1),[1:max(hist(rb(:,f),k))],'r','LineWidth',4);
plot(repmat(CI(2,f),max(hist(rb(:,f),k)),1),[1:max(hist(rb(:,f),k))],'r','LineWidth',4);
title({'Bootstrapped correlations';['h=',num2str(hboot(f))]},'FontSize',16)
xlabel('boot correlations','FontSize',14);ylabel('frequency','FontSize',14)
axis tight; colormap([.4 .4 1])
box on;set(gca,'FontSize',14,'Layer','Top')
end
end
end
end