Home > utils > scatter_w_stats.m

scatter_w_stats

PURPOSE ^

Generates scatter plot and includes regression lines and statistics

SYNOPSIS ^

function h = scatter_w_stats(xdata,ydata,params)

DESCRIPTION ^

 Generates scatter plot and includes regression lines and statistics

 h = scatter_w_stats(xdata,ydata,params)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function h = scatter_w_stats(xdata,ydata,params)
0002 % Generates scatter plot and includes regression lines and statistics
0003 %
0004 % h = scatter_w_stats(xdata,ydata,params)
0005 
0006 % 12Feb2015 Petr Janata
0007 
0008 if ~all(size(xdata) == size(ydata))
0009   error('xdata and ydata must be the same size')
0010 end
0011 
0012 if size(xdata,2) == 1
0013   xdata = xdata(:);
0014   ydata = ydata(:);
0015 end
0016 
0017 ncols = size(xdata,2);
0018 
0019 % Generate the scatter plot
0020 for icol = 1:ncols
0021   h(icol) = scatter(xdata(:,icol), ydata(:,icol));
0022   hold 'on'
0023 end
0024 
0025 % Do the stats
0026 for icol = 1:ncols
0027   [b,~,~,~,stats] = regress(ydata(:,icol),[ones(size(xdata(:,1))) xdata(:,icol)]);
0028   
0029   xlims = get(gca,'xlim');
0030   l(icol) = line(xlims,xlims*b(2)+b(1),'Color',get(h(icol),'CData'));
0031   
0032   Rsqr = stats(1);
0033   r(icol) = sqrt(Rsqr)*sign(b(2));
0034   p(icol) = stats(3);
0035   
0036   % Add the text
0037   if sign(b(2)) > 0
0038     horizontalAlign = 'right';
0039   else
0040     horizontalAlign = 'left';
0041   end
0042   verticalAlign = 'bottom';
0043   
0044   t(icol) = text(mean(xlims), mean(xlims)*b(2)+b(1), sprintf('r(%d) = %.2f, p = %.4f', ...
0045     sum(~isnan(ydata(:,icol)))-1, r(icol), p(icol)), ...
0046     'horizontalAlign', horizontalAlign, ...
0047     'verticalAlign', verticalAlign);
0048 end
0049 
0050 end

Generated on Thu 23-May-2019 04:00:50 by m2html © 2003