Home > DAVB > corrmap.m

corrmap

PURPOSE ^

CORRMAP Correlation map with variable grouping

SYNOPSIS ^

function corrmap(data,labels,reord)

DESCRIPTION ^

CORRMAP Correlation map with variable grouping
  CORRMAP produces a pseudocolor map which shows the
  amount of correlation of between variables in a data
  set. The function will reorder the variables by KNN
  clustering if desired. The inputs are the data set 
  (data) and an optional variable containing the labels
  corresponding the variables. Another optional variable
  (reord) will cause the function to keep the original
  ordering of the variables if set to 0.

 The I/O syntax is: corrmap(data,labels,reord)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function corrmap(data,labels,reord)
0002 %CORRMAP Correlation map with variable grouping
0003 %  CORRMAP produces a pseudocolor map which shows the
0004 %  amount of correlation of between variables in a data
0005 %  set. The function will reorder the variables by KNN
0006 %  clustering if desired. The inputs are the data set
0007 %  (data) and an optional variable containing the labels
0008 %  corresponding the variables. Another optional variable
0009 %  (reord) will cause the function to keep the original
0010 %  ordering of the variables if set to 0.
0011 %
0012 % The I/O syntax is: corrmap(data,labels,reord)
0013 
0014 % Copyright 1997
0015 % Eigenvector Research, Inc.
0016 % by Barry M. Wise
0017 if nargin < 3
0018   reord = 1;
0019 end 
0020 [m,n] = size(data);
0021 if reord  ~= 0
0022   dist = -corrcoef(data);
0023   for i = 1:n
0024     dist(i,i) = inf;
0025   end
0026   for k = 1:n-1
0027     [min1,ind1] = min(dist);
0028     [min2,ind2] = min(min1);
0029     r = ind1(ind2);
0030     c = ind2;
0031     % Segment to order samples here
0032     if k == 1
0033       groups = zeros(round(n/2),n);
0034       groups(1,1:2) = [c r]; gi = 1;
0035     else
0036       % does r belong to an existing group?
0037       [zr1,zr2] = find(groups==r);
0038       % does c belong to an existing group?
0039       [zc1,zc2] = find(groups==c);
0040       % If neither c nor r belong to a group they form their own
0041       if isempty(zr1)   %r doesn't belong to a group
0042         if isempty(zc1) %c doesn't belong to a group
0043           gi = gi+1;
0044           groups(gi,1:2) = [c r];
0045         else   % r doesn't belong but c does, add r to group c
0046           sgc = size(find(groups(zc1(1),:)));   %how big is group c
0047           % Figure out what side to add to
0048           cgc = groups(zc1(1),1:sgc(2));
0049           [mindg,inddg] = min([dist(cgc(1),r) dist(cgc(sgc(2)),r)]);
0050           if inddg == 2
0051             groups(zc1(1),sgc(2)+1) = r;
0052           else
0053             groups(zc1(1),1:sgc(2)+1) = [r groups(zc1(1),1:sgc(2))];
0054           end
0055         end
0056       else   %r does belong to a group
0057         if isempty(zc1) %c doesn't belong to a group, add c to group r
0058           sgr = size(find(groups(zr1(1),:)));   %how big is group r
0059           % Figure out what side to add to
0060           cgr = groups(zr1(1),1:sgr(2));
0061           [mindg,inddg] = min([dist(cgr(1),c) dist(cgr(sgr(2)),c)]);
0062           if inddg == 2
0063             groups(zr1(1),sgr(2)+1) = c;
0064           else
0065             groups(zr1(1),1:sgr(2)+1) = [c groups(zr1(1),1:sgr(2))];
0066           end
0067         else  %both c and r belong to groups, add group c to group r
0068           sgr = size(find(groups(zr1(1),:)));  %size of group r
0069           sgc = size(find(groups(zc1(1),:)));  %size of group c
0070           % Figure out what side to add to
0071           cgc = groups(zc1(1),1:sgc(2));  % current group c
0072           cgr = groups(zr1(1),1:sgr(2));  % current group r
0073           [mindg,inddg] = min([dist(cgc(1),cgr(1)) dist(cgc(1),cgr(sgr(2))) ...
0074                  dist(cgc(sgc(2)),cgr(1)) dist(cgc(sgc(2)),cgr(sgr(2)))]);
0075           if inddg == 1
0076             % flop group c and add to the left of r
0077             groups(zr1(1),1:sgr(2)+sgc(2)) = [cgc(sgc(2):-1:1) cgr];
0078           elseif inddg == 2
0079             % add group c to the right of group r
0080             groups(zr1(1),sgr(2)+1:sgr(2)+sgc(2)) = cgc;
0081           elseif inddg == 3
0082             % add group c to the left of group r
0083             groups(zr1(1),1:sgr(2)+sgc(2)) = [cgc cgr];
0084           else
0085             % flop group c and add to the right of group r
0086             groups(zr1(1),1:sgr(2)+sgc(2)) = [cgr cgc(sgc(2):-1:1)];
0087           end
0088           groups(zc1,:) = zeros(1,n);
0089         end
0090       end
0091     end
0092     dist(r,c) = inf;
0093     dist(c,r) = inf;
0094     z1 = find(dist(r,:)==inf);
0095     z2 = find(dist(c,:)==inf);
0096     z1n = z1(find(z1~=r));
0097     z1n = z1n(find(z1n~=c));
0098     z2n = z2(find(z2~=c));
0099     z2n = z2n(find(z2n~=r));
0100     z = [z1 z2];
0101     sz = size(z);
0102     for j = 1:max(sz);
0103       for k = 1:max(sz);
0104         dist(z(j),z(k)) = inf;
0105       end  
0106     end
0107   end
0108 end
0109 if reord ~= 0
0110   order = groups(find(groups(:,1)),:);
0111 else
0112   order = 1:n;
0113 end
0114 if nargin > 1
0115   [ml,nl] = size(labels);
0116   if ml == n
0117     lflag = 1;
0118   else
0119     lflag = 0;
0120     nl = 2;
0121   end
0122 else
0123   lflag = 0;
0124   nl = 2;
0125 end
0126 sim = corrcoef(data(:,order)); 
0127 sim = [sim zeros(n,1); [zeros(1,n) -1]];
0128 pcolor(0.5:1:n+0.5,0.5:1:n+0.5,sim), colormap('hot')
0129 set(gca,'Ydir','reverse')
0130 set(gca,'YTickLabel',[],'YTick',[])
0131 set(gca,'XTickLabel',[],'XTick',[])
0132 axis('square')
0133 if n > 50
0134   fs = 7;
0135   os = 0.20*nl/8;
0136 elseif n > 20
0137   fs = 9;
0138   os = 0.20*nl/6;
0139 else
0140   fs = 12;
0141   os = 0.20*nl/4;  %Increase this to make white area around fig bigger
0142 end
0143 if lflag == 1
0144   text(-n*os*ones(n,1)+0.5*ones(n,1),1:n,labels(order,:))
0145   z = get(gca,'Children');
0146   set(z(1:n),'FontSize',fs)
0147   text(1:n,zeros(1,n),labels(order,:))
0148   z = get(gca,'Children');
0149   set(z(1:n),'Rotation',90);
0150   set(z(1:n),'FontSize',fs)
0151 else
0152   for i = 1:n
0153     text(-n*os+0.5,i,int2str(order(i)));
0154   end
0155   z = get(gca,'Children');
0156   set(z(1:n),'FontSize',fs)
0157   for i = 1:n
0158     text(i,0,int2str(order(i)));
0159   end
0160   z = get(gca,'Children');
0161   set(z(1:n),'Rotation',90);
0162   set(z(1:n),'FontSize',fs)
0163 end
0164 hold on
0165 plot([-n*os n+0.5],[-n*os -n*os],'-k');
0166 plot([-n*os n+0.5],[n+.5 n+.5],'-k');
0167 plot([-n*os -n*os],[-n*os n+.5],'-k');
0168 plot([n+0.5 n+0.5],[-n*os n+.5],'-k');
0169 hold off
0170 axis image
0171 colorbar
0172 if reord ~= 0
0173   title('Correlation Map, Variables Regrouped by Similarity')
0174 else
0175   title('Correlation Map, Variables in Original Order')
0176 end
0177 xlabel('Scale Gives Value of R for Each Variable Pair')

Generated on Thu 02-Sep-2010 12:44:42 by m2html © 2005