-
Notifications
You must be signed in to change notification settings - Fork 1
/
spmrt_compcorr.m
268 lines (228 loc) · 10.2 KB
/
spmrt_compcorr.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
function [CIP,pP,CIC,pC]=spmrt_compcorr(pair1,pair2,mask,metric,figout,threshold,alpha_level)
% Implementation of a percentile bootstrap of the difference of correlation
% for dependent measures - think of comparing the reliability (corr) of two MRI
% sequences or two paradigms.
%
% FORMAT [diffP,CIP,diffC,CIC]=spmrt_compcorr
% [diffP,CIP]=spmrt_compcorr(pair1,pair2,mask,'Pearson',figout,threshold,alpha_level)
% [diffC,CIC]=spmrt_compcorr(pair1,pair2,mask,'Concordance',figout,threshold,alpha_level)
%
% INPUT if no input the user is prompted
% pair1 is a char array with the filenames of 2 images to correlate (see spm_select)
% pair2 is a char array with the filenames of 2 images to correlate (see spm_select)
% mask is the filename of a mask in same space as image1 and image2
% metric is 'Pearson', 'Concordance', or 'both'
% figout 1/0 (default) to get correlation figure out
% threshold (optional) if mask is not binary, threshold to apply
% alpha_level is the level used to compute the confidence interval (default is 5%)
%
% OUTPUT CIP is the 95% confidence interval of the difference in Pearson correlation coefficient (if 0 not included then significant)
% pP is the p value associated with CIP
% CIC is the 95% confidence interval of the difference in concordance correlation coefficient (if 0 not included then significant)
% pC is the p value associated with CIC
%
% Reference: Wilcox, R.R. (2016) Comparing dependent robust correlations.
% Brit J Math Stat Psy, 69, 215-224. http://onlinelibrary.wiley.com/doi/10.1111/bmsp.12069/full
%
% Cyril Pernet
% --------------------------------------------------------------------------
% Copyright (C) spmrt
pC = []; CIP = [];
pP = []; CIC = [];
nboot = 1000; % a thousand bootstraps
pn1 = pair1(1,:);
lc = strfind(pn1,'.');
lc2 = strfind(pn1,'/');
inName11 = pn1(max(lc2)+1:max(lc)-1);
pn2 = pair1(2,:);
lc = strfind(pn2,'.');
lc2 = strfind(pn2,'/');
inName12 = pn2(max(lc2)+1:max(lc)-1);
pn1 = pair2(1,:);
lc = strfind(pn1,'.');
lc2 = strfind(pn1,'/');
inName21 = pn1(max(lc2)+1:max(lc)-1);
pn2 = pair2(2,:);
lc = strfind(pn2,'.');
lc2 = strfind(pn2,'/');
inName22 = pn2(max(lc2)+1:max(lc)-1);
%% check inputs
spm('defaults', 'FMRI');
if nargin < 7; threshold = []; alpha_level = 5/100; end
if nargin < 5; figout = 0; end
if nargin < 4; metric = 'both'; end
if nargin == 0
[pair1,sts] = spm_select(2,'image','select images for pair 1',{},pwd,'.*',1);
if sts == 0
return
end
[pair2,sts] = spm_select(2,'image','select images for pair 2',{},pwd,'.*',1);
if sts == 0
return
end
[mask,sts] = spm_select(1,'image','select mask image',{},pwd,'.*',1);
if sts == 0
return
end
figout = 1; % if user if prompt to enter data then return a figure
end
%% Get the data
if exist('threshold','var') && ~isempty(threshold)
X = spmrt_getdata(pair1(1,:),pair1(2,:),mask,threshold);
Y = spmrt_getdata(pair2(1,:),pair2(2,:),mask,threshold);
else
X = spmrt_getdata(pair1(1,:),pair1(2,:),mask);
Y = spmrt_getdata(pair2(1,:),pair2(2,:),mask);
end
nx = size(X,1);
if any(sum(X,1) == 0)
error('at least one image in the 1st pair is empty')
end
ny = size(Y,1);
if any(sum(Y,1) == 0)
error('at least one image in the 2nd pair is empty')
end
if nx ~= ny
error('unexpectedly the number of voxels from each pairs differ despite using the same mask')
end
%% Pearson correlation
if strcmpi(metric,'Pearson') || strcmpi(metric,'Both')
disp('computing Pearson correlation differences');
table = randi(nx,nx,nboot);
rP1 = sum(detrend(reshape(X(table,1),[nx nboot]),'constant').*detrend(reshape(X(table,2),[nx nboot]),'constant')) ./ ...
(sum(detrend(reshape(X(table,1),[nx nboot]),'constant').^2).*sum(detrend(reshape(X(table,2),[nx nboot]),'constant').^2)).^(1/2);
rP2 = sum(detrend(reshape(Y(table,1),[nx nboot]),'constant').*detrend(reshape(Y(table,2),[nx nboot]),'constant')) ./ ...
(sum(detrend(reshape(Y(table,1),[nx nboot]),'constant').^2).*sum(detrend(reshape(Y(table,2),[nx nboot]),'constant').^2)).^(1/2);
bootdiffP = sort(rP1 - rP2);
bootdiffP(isnan(bootdiffP)) = [];
adj_nboot = length(bootdiffP);
low = round((alpha_level*adj_nboot)/2); % lower bound
high = adj_nboot - low; % upper bound
CIP = [bootdiffP(low) bootdiffP(high)];
pvalue = mean(bootdiffP < 0);
pP = 2*min(pvalue,1-pvalue);
if pP == 0
pP = 1/nboot;
end
end
%% Concordance
if strcmpi(metric,'Concordance') || strcmpi(metric,'Both')
if strcmpi(metric,'Concordance')
table = randi(nx,nx,nboot); % otherwise reuse the one from above = same sampling scheme
end
bootdiffC = zeros(1,nboot);
for b=1:nboot
S = cov(X(table(:,b),:),1); Var1 = S(1,1); Var2 = S(2,2); S = S(1,2);
ybar = mean(X(table(:,b),:));
r1 = (2.*S) ./ ( Var1 +Var2 + (ybar(1)-ybar(2))^2);
S = cov(Y(table(:,b),:),1); Var1 = S(1,1); Var2 = S(2,2); S = S(1,2);
ybar = mean(Y(table(:,b),:));
r2 = (2.*S) ./ ( Var1 +Var2 + (ybar(1)-ybar(2))^2);
bootdiffC(b) = r1-r2;
end
bootdiffC = sort(bootdiffC,1);
bootdiffC(isnan(bootdiffC)) = [];
adj_nboot = length(bootdiffC);
low = round((alpha_level*adj_nboot)/2); % lower bound
high = adj_nboot - low; % upper bound
CIC = [bootdiffC(low) bootdiffC(high)];
pvalue = mean(bootdiffC < 0);
pC = 2*min(pvalue,1-pvalue);
if pC == 0
pC = 1/nboot;
end
end
%% figure
if figout == 1
figure('Name','images correlation')
set(gcf,'Color','w','InvertHardCopy','off', 'units','normalized','outerposition',[0 0 1 1])
% left plot
[rP1,CIP1,rC1,CIC1] = spmrt_corr(pair1(1,:),pair1(2,:),mask,'both',0,threshold,alpha_level);
if strcmpi(metric,'Pearson')
subplot(1,3,1);
mytitle = sprintf('Pearson corr (blue) =%g \n CI [%g %g]',rP1,CIP1(1),CIP1(2));
elseif strcmpi(metric,'Concordance')
subplot(1,3,1);
mytitle = sprintf('Concordance corr (red) =%g \n CI [%g %g]',rC1,CIC1(1),CIC1(2));
else
subplot(4,3,[4 7]);
mytitle = sprintf('Pearson corr (blue) =%g \n CI [%g %g] \n Concordance corr (red) =%g \n CI [%g %g]',rP1,CIP1(1),CIP1(2),rC1,CIC1(1),CIC1(2));
end
scatter(X(:,1),X(:,2),50); grid on % plot observations pair 1
xlabel(['pair1 ' inName11],'FontSize',14); ylabel(['pair1 ' inName12],'FontSize',14); % label
h=lsline; set(h,'Color','b','LineWidth',2); % add the least square line
box on; set(gca,'Fontsize',12); axis square; hold on
v = axis;
idxMax = find(v==max(abs(v)));
idxMin = find(v==min(abs(v)));
identity = v(idxMin):v(idxMax);
idplot = plot(identity,identity,'k--','LineWidth',2); % Identity line % add diagonal
title(mytitle,'Fontsize',12)
if strcmp(metric,'Concordance') || strcmp(metric,'both')
concplot = plot(identity,identity*scaleC + shiftC,'r','LineWidth',2);
end
% right plot
[rP2,CIP2,rC2,CIC2] = spmrt_corr(pair2(1,:),pair2(2,:),mask,'both',0,threshold,alpha_level);
if strcmpi(metric,'Pearson')
subplot(1,3,3);
mytitle = sprintf('Pearson corr (blue) =%g \n CI [%g %g]',rP2,CIP2(1),CIP2(2));
elseif strcmpi(metric,'Concordance')
subplot(1,3,3);
mytitle = sprintf('Concordance corr (red) =%g \n CI [%g %g]',rC2,CIC2(1),CIC2(2));
else
subplot(4,3,[6 9]);
mytitle = sprintf('Pearson corr (blue) =%g \n CI [%g %g] \n Concordance corr (red) =%g \n CI [%g %g]',rP2,CIP2(1),CIP2(2),rC2,CIC2(1),CIC2(2));
end
scatter(Y(:,1),Y(:,2),50); grid on % plot observations pair 1
xlabel(['pair2 ' inName21],'FontSize',14); ylabel(['pair2 ' inName22],'FontSize',14); % label
h2=lsline; set(h2,'Color','b','LineWidth',2); % add the least square line
box on; set(gca,'Fontsize',12); axis square; hold on
vv = axis;
idxMax = find(vv==max(abs(vv)));
idxMin = find(vv==min(abs(vv)));
identity = vv(idxMin):vv(idxMax);
idplot = plot(identity,identity,'k--','LineWidth',2); % Identity line % add diagonal
title(mytitle,'Fontsize',12)
if strcmp(metric,'Concordance') || strcmp(metric,'both')
concplot = plot(identity,identity*scaleC + shiftC,'r','LineWidth',2);
end
% middle plot
if strcmpi(metric,'Pearson')
subplot(1,3,1);
subplot(4,3,[2 5]);
k = round(1 + log2(length(bootdiffP)));
[n,x]=hist(bootdiffP,k); h = x(2) - x(1);
bar(x,n/(length(bootdiffP)*h),1, ...
'FaceColor',[0.5 0.5 1],'EdgeColor',[0 0 0], ...
'FaceAlpha',0.9,'EdgeAlpha',1); grid on; box on
ylabel('Freq.','FontSize',12)
title(sprintf('Differences in Pearsons'' corr \n %g CI [%g %g]',rP1-rP2, CIP(1),CIP(2)),'Fontsize',12)
elseif strcmpi(metric,'Concordance')
subplot(1,3,1);
subplot(4,3,[8 11]);
k = round(1 + log2(length(bootdiffC)));
[n,x]=hist(bootdiffC,k); h = x(2) - x(1);
bar(x,n/(length(bootdiffC)*h),1, ...
'FaceColor',[0.5 0.5 1],'EdgeColor',[0 0 0], ...
'FaceAlpha',0.9,'EdgeAlpha',1); grid on; box on
xlabel('differences','FontSize',12); ylabel('Freq.','FontSize',12)
title(sprintf('Differences in Concordance corr \n %g CI [%g %g]',rC1-rC2, CIC(1),CIC(2)),'Fontsize',12)
else
subplot(4,3,[2 5]);
k = round(1 + log2(length(bootdiffP)));
[n,x]=hist(bootdiffP,k); h = x(2) - x(1);
bar(x,n/(length(bootdiffP)*h),1, ...
'FaceColor',[0.5 0.5 1],'EdgeColor',[0 0 0], ...
'FaceAlpha',0.9,'EdgeAlpha',1); grid on; box on
ylabel('Freq.','FontSize',12)
title(sprintf('Differences in Pearsons'' corr \n %g CI [%g %g]',rP1-rP2, CIP(1),CIP(2)),'Fontsize',12)
subplot(4,3,[8 11]);
k = round(1 + log2(length(bootdiffC)));
[n,x]=hist(bootdiffC,k); h = x(2) - x(1);
bar(x,n/(length(bootdiffC)*h),1, ...
'FaceColor',[0.5 0.5 1],'EdgeColor',[0 0 0], ...
'FaceAlpha',0.9,'EdgeAlpha',1); grid on; box on
xlabel('differences','FontSize',12); ylabel('Freq.','FontSize',12)
title(sprintf('Differences in Concordance corr \n %g CI [%g %g]',rC1-rC2, CIC(1),CIC(2)),'Fontsize',12)
end
end