-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistatis.m
402 lines (371 loc) · 14.1 KB
/
distatis.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
function varargout = distatis(data,nPCs,group3,groupcolor3,groupsym3,groupsiz3,grouplabel3,compcoding,compcolor,compsym,compsiz,complabel,savepng)
% Perform DISTATIS on a 3D distance (dissimilarity) matrix
% FORMAT [eigval,eigvector,fscore,eigval3,eigvector3,fscore3] = distatis(data)
% INPUTS:
% data - Distance (dissimilarity) matrix [n x n x m]
%
% OPTIONAL INPUTS (for plots):
% nPCs - The number of components to plot (default: 2)
% group3 - Group coding for dim 3 (eg, [1 1 2 2] % 2 groups)
% groupcolor3 - Group color for dim 3 (eg, [1 0 0; 0 0 1] % red, blue)
% groupsym3 - Group symbol for dim 3 (eg, 'x.' % x symbol, filled circle)
% groupsiz3 - Group size for dim 3 (eg, [4 6] % 2 groups)
% grouplabel3 - Group label for dim 3 (eg, {'pixels','measures','ratings','pairwise'})
% compcoding - Compromise coding for dim 1/2 (eg, [1 1 1 2 2 2] % 2 groups)
% compcolor - Compromise color for dim 1/2 (eg, [0 1 0; 1 0 0] % green, red)
% compsym - Compromise symbol for dim 1/2 (eg, 'x.' % x symbol, filled circle)
% compsiz - Compromise size for dim 1/2 (eg, [4 8] % 2 groups)
% complabel - Compromise label for dim 1/2 (eg, {'M1','M2','M3','F1','F2','F3'})
% savepng - Save figures as PNG files (default = 'FALSE')
%
% OUTPUTS:
% eigval - Eigenvalues for compromise
% eigvector - Eigenvector for compromise
% fscore - Factor score for compromise
% eigval3 - Eigenvalues for dim3 data
% eigvector3 - Eigenvector for dim3 data
% fscore3 - Factor score for dim3 data
%
% REFERENCE:
% Abdi H, Toole AJO, Valentin D, Edelman B (2005)
% DISTATIS: The analysis of multiple distance matrices.
% IEEE CVPR, 42-47.
%
% Abdi H, Valentin D, Chollet S, Chrea C (2007)
% Analyzing assessors and products in sorting tasks: DISTATIS, theory and applications.
% Food Qual Prefer 18:627-640.
%
% REQUIREMENTS:
% Statistics and Machine Learning Toolbox
% plot_arrow.m
% https://www.mathworks.com/matlabcentral/fileexchange/3345-plot-arrowhead
%__________________________________________________________________________
% Copyright (C) 2020-2022 Daisuke MATSUYOSHI
% $Id: distatis.m 0008 2022-04-29Z $
%% Data check
[nx,ny,nz] = size(data);
if nx ~= ny
error('Invalid dimension (input matrix)!');
end
%% Settings
if nargin < 2 || isempty(nPCs)
nPCs = 2;
end
if nargin < 3 || isempty(group3)
group3 = 1:nz;
end
if nargin < 4 || isempty(groupcolor3)
groupcolor3 = [];
end
if nargin < 5 || isempty(groupsym3)
groupsym3 = [];
end
if nargin < 6
groupsiz3 = [];
end
if nargin < 7 || isempty(grouplabel3)
grouplabel3 = cellstr(num2str(transpose(1:nz)));
end
if nargin < 8 || isempty(compcoding)
compcoding = 1:nx;
end
if nargin < 9 || isempty(compcolor)
compcolor = [];
end
if nargin < 10 || isempty(compsym)
compsym = [];
end
if nargin < 11
compsiz = [];
end
if nargin < 12 || isempty(complabel)
complabel = cellstr(num2str(transpose(1:nx)));
end
if nargin < 13 || isempty(savepng)
savepng = 'FALSE';
end
%% Initialize
matI = eye(nx);
m = ones(nx,1)/nx;
M = matI - ones(nx,1) * m';
%% Calc
% Cross product
Snorm = zeros(size(data));
for i=1:nz
S = (-1/2) * M * data(:,:,i) * M';
eigSt = eig(S);
eigS = sort(eigSt,'descend');
Snorm(:,:,i) = S/eigS(1);
end
X = reshape(Snorm,[],size(Snorm,3)); % complete data, normalized cross-product vectors
% Compromise matrix
A = X' * X; % scalar product matrix
N = diag(diag(A));
n = zeros(size(X,2),1);
for i=1:size(X,2)
n(i) = norm(X(:,i),2); % L2
end
C = A ./ (n(:)*n(:).'); % Cosine matrix % N^(-1/2)*A*N^(-1/2)
Rv = A ./ sqrt(diag(A)*diag(A).'); % Congruence coefficients
% Eig Decompose
[V,D] = eig(C);
[d,idx] = sort(diag(D),'descend');
Ds = D(idx,idx);
Vs = V(:,idx);
G = Vs * diag(d.^(1/2));
varargout{4} = d;
varargout{5} = Vs;
varargout{6} = G;
% Optimal weights
P = Vs;
alpha = P ./ pinv((ones(length(P),1)/length(P))'*P)';
Splus = reshape(X * alpha(:,1),nx,nx);
[V,D] = eig(Splus);
[dSplus,idx] = sort(diag(D),'descend');
eigSplusD = D(idx,idx);
eigSplusV = V(:,idx);
Fscore = eigSplusV * diag(dSplus.^(1/2));
varargout{1} = dSplus;
varargout{2} = eigSplusV;
varargout{3} = Fscore;
%% Plots
% Components Plot1 (Component1 & Component2) | Dim 3
PCx = 1; PCy = 2;
figure
hold on
gscatter(G(:,1),G(:,2),group3,groupcolor3,groupsym3,groupsiz3,'off') % weighted with eigenvalue
text(G(:,1), G(:,2), grouplabel3);
plot([0,0],ylim,':k','HandleVisibility','off')
plot([0,0],ylim,':k','HandleVisibility','off') % draw twice to plot at full length
plot(xlim,[0,0],':k','HandleVisibility','off')
xlabel(sprintf('LatentComponent%d (%.1f%%)',PCx,d(1)/sum(d)*100))
ylabel(sprintf('LatentComponent%d (%.1f%%)',PCy,d(2)/sum(d)*100))
hold off
if strcmpi(savepng,'TRUE')
saveas(gcf,sprintf('%s_distatis_1.png',inputname(1)))
end
% Components Plot2 (Component1 & Component2) | Compromise Dim 1/2
figure
hold on
gscatter(eigSplusV(:,PCx),eigSplusV(:,PCy),compcoding,compcolor,compsym,compsiz,'off')
text(eigSplusV(:,PCx), eigSplusV(:,PCy), complabel);
plot([0,0],ylim,':k','HandleVisibility','off')
plot([0,0],ylim,':k','HandleVisibility','off') % draw twice to plot at full length
plot(xlim,[0,0],':k','HandleVisibility','off')
xlabel(sprintf('LatentComponent%d (%.1f%%)',PCx,dSplus(PCx)/sum(dSplus)*100))
ylabel(sprintf('LatentComponent%d (%.1f%%)',PCy,dSplus(PCy)/sum(dSplus)*100))
hold off
if strcmpi(savepng,'TRUE')
saveas(gcf,sprintf('%s_distatis_2.png',inputname(1)))
end
% Components Plot3 (Component1 & Component2) | Compromise Dim 1/2 & 3
Fs = zeros(size(data));
for i=1:size(Snorm,3)
Fs(:,:,i) = Snorm(:,:,i) * eigSplusV * diag(dSplus.^(-1/2));
end
if isempty(groupcolor3)
cols = [1 0 0; 0 1 0; 0 0 1; 1 1 0; 1 0 1; 0 1 1; 0 0 0];
if nz < 15
cols = [cols; lines];
else
cols = [winter(ceil(nz/2));autumn(ceil(nz/2))];
end
else
Gidx = unique(group3);
nGroup = numel(unique(group3));
nPerGroup = zeros(nGroup,1);
for i=1:nGroup
nPerGroup(i) = sum(group3==Gidx(i));
end
cols = zeros(nz,3);
for i=1:nGroup
cols((i-1)*nPerGroup(i)+1:i*nPerGroup(i),:) = repmat(groupcolor3(i,:),nPerGroup(i),1);
end
end
figure
hold on
gscatter(eigSplusV(:,PCx),eigSplusV(:,PCy),compcoding,compcolor,compsym,compsiz,'off')
text(eigSplusV(:,PCx), eigSplusV(:,PCy), complabel);
plot(xlim,[0,0],':k','HandleVisibility','off')
for j=1:size(Fs,3)
for i=1:length(eigSplusV)
x0 = eigSplusV(i,PCx);
y0 = eigSplusV(i,PCy);
x1 = Fs(i,PCx,j);
y1 = Fs(i,PCy,j);
plot_arrow(x0, y0, x1, y1,'color',cols(j,:),'facecolor',cols(j,:));
end
end
plot([0,0],ylim,':k','HandleVisibility','off')
plot([0,0],ylim,':k','HandleVisibility','off') % draw twice to plot at full length
xlabel(sprintf('LatentComponent%d (%.1f%%)',PCx,dSplus(PCx)/sum(dSplus)*100))
ylabel(sprintf('LatentComponent%d (%.1f%%)',PCy,dSplus(PCy)/sum(dSplus)*100))
hold off
if strcmpi(savepng,'TRUE')
saveas(gcf,sprintf('%s_distatis_3.png',inputname(1)))
end
if nPCs == 3
% 1st
PCx = 1; PCy = 3;
% Components Plot | Compromise Dim 3
figure
hold on
gscatter(G(:,PCx),G(:,PCy),group3,groupcolor3,groupsym3,groupsiz3,'off') % weighted with eigenvalue
text(G(:,PCx), G(:,PCy), grouplabel3);
plot([0,0],ylim,':k','HandleVisibility','off')
plot([0,0],ylim,':k','HandleVisibility','off') % draw twice to plot at full length
plot(xlim,[0,0],':k','HandleVisibility','off')
xlabel(sprintf('LatentComponent%d (%.1f%%)',PCx,d(PCx)/sum(d)*100))
ylabel(sprintf('LatentComponent%d (%.1f%%)',PCy,d(PCy)/sum(d)*100))
hold off
if strcmpi(savepng,'TRUE')
saveas(gcf,sprintf('%s_distatis_%d.png',inputname(1),4))
end
% Components Plot | Compromise Dim 1/2
figure
hold on
gscatter(eigSplusV(:,PCx),eigSplusV(:,PCy),compcoding,compcolor,compsym,compsiz,'off')
text(eigSplusV(:,PCx), eigSplusV(:,PCy), complabel);
plot([0,0],ylim,':k','HandleVisibility','off')
plot([0,0],ylim,':k','HandleVisibility','off') % draw twice to plot at full length
plot(xlim,[0,0],':k','HandleVisibility','off')
xlabel(sprintf('LatentComponent%d (%.1f%%)',PCx,dSplus(PCx)/sum(dSplus)*100))
ylabel(sprintf('LatentComponent%d (%.1f%%)',PCy,dSplus(PCy)/sum(dSplus)*100))
hold off
if strcmpi(savepng,'TRUE')
saveas(gcf,sprintf('%s_distatis_%d.png',inputname(1),5))
end
% Components Plot | Compromise Dim 1/2 & 3
figure
hold on
gscatter(eigSplusV(:,PCx),eigSplusV(:,PCy),compcoding,compcolor,compsym,compsiz,'off')
text(eigSplusV(:,PCx), eigSplusV(:,PCy), complabel);
plot(xlim,[0,0],':k','HandleVisibility','off')
for j=1:size(Fs,3)
for i=1:length(eigSplusV)
x0 = eigSplusV(i,PCx);
y0 = eigSplusV(i,PCy);
x1 = Fs(i,PCx,j);
y1 = Fs(i,PCy,j);
plot_arrow(x0, y0, x1, y1,'color',cols(j,:),'facecolor',cols(j,:));
end
end
plot([0,0],ylim,':k','HandleVisibility','off')
plot([0,0],ylim,':k','HandleVisibility','off') % draw twice to plot at full length
xlabel(sprintf('LatentComponent%d (%.1f%%)',PCx,dSplus(PCx)/sum(dSplus)*100))
ylabel(sprintf('LatentComponent%d (%.1f%%)',PCy,dSplus(PCy)/sum(dSplus)*100))
hold off
if strcmpi(savepng,'TRUE')
saveas(gcf,sprintf('%s_distatis_%d.png',inputname(1),6))
end
% 2nd
PCx = 2; PCy = 3;
% Components Plot | Compromise Dim 3
figure
hold on
gscatter(G(:,PCx),G(:,PCy),group3,groupcolor3,groupsym3,groupsiz3,'off') % weighted with eigenvalue
text(G(:,PCx), G(:,PCy), grouplabel3);
plot([0,0],ylim,':k','HandleVisibility','off')
plot([0,0],ylim,':k','HandleVisibility','off') % draw twice to plot at full length
plot(xlim,[0,0],':k','HandleVisibility','off')
xlabel(sprintf('LatentComponent%d (%.1f%%)',PCx,d(PCx)/sum(d)*100))
ylabel(sprintf('LatentComponent%d (%.1f%%)',PCy,d(PCy)/sum(d)*100))
hold off
if strcmpi(savepng,'TRUE')
saveas(gcf,sprintf('%s_distatis_%d.png',inputname(1),7))
end
% Components Plot | Compromise Dim 1/2
PCx = 2; PCy = 3;
figure
hold on
gscatter(eigSplusV(:,PCx),eigSplusV(:,PCy),compcoding,compcolor,compsym,compsiz,'off')
text(eigSplusV(:,PCx), eigSplusV(:,PCy), complabel);
plot([0,0],ylim,':k','HandleVisibility','off')
plot([0,0],ylim,':k','HandleVisibility','off') % draw twice to plot at full length
plot(xlim,[0,0],':k','HandleVisibility','off')
xlabel(sprintf('LatentComponent%d (%.1f%%)',PCx,dSplus(PCx)/sum(dSplus)*100))
ylabel(sprintf('LatentComponent%d (%.1f%%)',PCy,dSplus(PCy)/sum(dSplus)*100))
hold off
if strcmpi(savepng,'TRUE')
saveas(gcf,sprintf('%s_distatis_%d.png',inputname(1),8))
end
% Components Plot | Compromise Dim 1/2 & 3
figure
hold on
gscatter(eigSplusV(:,PCx),eigSplusV(:,PCy),compcoding,compcolor,compsym,compsiz,'off')
text(eigSplusV(:,PCx), eigSplusV(:,PCy), complabel);
plot(xlim,[0,0],':k','HandleVisibility','off')
for j=1:size(Fs,3)
for i=1:length(eigSplusV)
x0 = eigSplusV(i,PCx);
y0 = eigSplusV(i,PCy);
x1 = Fs(i,PCx,j);
y1 = Fs(i,PCy,j);
plot_arrow(x0, y0, x1, y1,'color',cols(j,:),'facecolor',cols(j,:));
end
end
plot([0,0],ylim,':k','HandleVisibility','off')
plot([0,0],ylim,':k','HandleVisibility','off') % draw twice to plot at full length
xlabel(sprintf('LatentComponent%d (%.1f%%)',PCx,dSplus(PCx)/sum(dSplus)*100))
ylabel(sprintf('LatentComponent%d (%.1f%%)',PCy,dSplus(PCy)/sum(dSplus)*100))
hold off
if strcmpi(savepng,'TRUE')
saveas(gcf,sprintf('%s_distatis_%d.png',inputname(1),9))
end
elseif nPCs > 3
nLoop = floor((nPCs-2)/2);
for i = 1:nLoop
PCx = i*2+1; PCy = i*2+2;
% Components Plot | Dim 3
figure
hold on
gscatter(G(:,PCx),G(:,PCy),group3,groupcolor3,groupsym3,groupsiz3,'off') % weighted with eigenvalue
text(G(:,PCx), G(:,PCy), grouplabel3);
plot([0,0],ylim,':k','HandleVisibility','off')
plot([0,0],ylim,':k','HandleVisibility','off') % draw twice to plot at full length
plot(xlim,[0,0],':k','HandleVisibility','off')
xlabel(sprintf('LatentComponent%d (%.1f%%)',PCx,d(PCx)/sum(d)*100))
ylabel(sprintf('LatentComponent%d (%.1f%%)',PCy,d(PCy)/sum(d)*100))
hold off
if strcmpi(savepng,'TRUE')
saveas(gcf,sprintf('%s_distatis_%d.png',inputname(1),3+i*3-2))
end
% Components Plot | Compromise Dim 1/2
figure
hold on
gscatter(eigSplusV(:,PCx),eigSplusV(:,PCy),compcoding,compcolor,compsym,compsiz,'off')
text(eigSplusV(:,PCx), eigSplusV(:,PCy), complabel);
plot([0,0],ylim,':k','HandleVisibility','off')
plot([0,0],ylim,':k','HandleVisibility','off') % draw twice to plot at full length
plot(xlim,[0,0],':k','HandleVisibility','off')
xlabel(sprintf('LatentComponent%d (%.1f%%)',PCx,dSplus(PCx)/sum(dSplus)*100))
ylabel(sprintf('LatentComponent%d (%.1f%%)',PCy,dSplus(PCy)/sum(dSplus)*100))
hold off
if strcmpi(savepng,'TRUE')
saveas(gcf,sprintf('%s_distatis_%d.png',inputname(1),3+i*3-1))
end
% Components Plot | Compromise Dim 1/2 & 3
figure
hold on
gscatter(eigSplusV(:,PCx),eigSplusV(:,PCy),compcoding,compcolor,compsym,compsiz,'off')
text(eigSplusV(:,PCx), eigSplusV(:,PCy), complabel);
plot(xlim,[0,0],':k','HandleVisibility','off')
for j=1:size(Fs,3)
for k=1:length(eigSplusV)
x0 = eigSplusV(k,PCx);
y0 = eigSplusV(k,PCy);
x1 = Fs(k,PCx,j);
y1 = Fs(k,PCy,j);
plot_arrow(x0, y0, x1, y1,'color',cols(j,:),'facecolor',cols(j,:));
end
end
plot([0,0],ylim,':k','HandleVisibility','off')
plot([0,0],ylim,':k','HandleVisibility','off') % draw twice to plot at full length
xlabel(sprintf('LatentComponent%d (%.1f%%)',PCx,dSplus(PCx)/sum(dSplus)*100))
ylabel(sprintf('LatentComponent%d (%.1f%%)',PCy,dSplus(PCy)/sum(dSplus)*100))
hold off
if strcmpi(savepng,'TRUE')
saveas(gcf,sprintf('%s_distatis_%d.png',inputname(1),3+i*3))
end
end
end