binned2.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. function [xbinc, ybinc, mu, se, n]=binned2(x,y,z, varargin)
  2. % [binc, mu, se, n]=binned2(x,y,z,bin_e)
  3. % Takes a vector x and a vector y and returns mean and standard error of
  4. % values of z for bins of x and y.
  5. %
  6. % Input:
  7. % x 1xn vector of x values to bin
  8. % y 1xn vector of y values to bin
  9. % z 1xn vector of z values to average in each bin
  10. %
  11. % Optional Input [=default]:
  12. %
  13. % n_bins=10 Optional # of bins.
  14. % n_x_bins=n_bins Specify # of x bins. Overrides n_bins
  15. % n_y_bins=n_bins Specify # of y bins. Overrides n_bins
  16. % even_bins=false By default bins have equal # of data points. If this is
  17. % true then bins are evenly spaced and have uneven sample
  18. % sizes
  19. %
  20. % xbin_e=[] Optional bin edges for the x-axis. Overrides all
  21. % earlier options
  22. %
  23. % ybin_e=[]; Optional bin edges for the y-axis. Overrides all
  24. % earlier options
  25. % plot_it=false;
  26. % marker if plot_it then use this marker ['o']
  27. % linestyle if plot_it then use this linestyle ['-']
  28. % ax=[]; if plot_it=true, plot to this axis
  29. %
  30. % Output:
  31. % binc 1xm bin centers
  32. % mu 1xm The average value of y at that bin
  33. % se 1xm The standard error of y at that bin
  34. % n 1xm The number of values of y in this bin
  35. check_inputs(x,y,z)
  36. xbin_e=[];
  37. ybin_e=[];
  38. ax=[];
  39. plot_it=false;
  40. n_bins=7;
  41. n_x_bins=n_bins;
  42. n_y_bins=n_bins;
  43. even_bins=false;
  44. func=@nanmean;
  45. linestyle = '-';
  46. marker = 'o';
  47. utils.overridedefaults(who,varargin);
  48. if isempty(ax) && plot_it
  49. ax=gca;
  50. end
  51. if isempty(xbin_e)
  52. if even_bins
  53. xbin_e=linspace(min(x),max(x),n_x_bins+1);
  54. else
  55. pbins=linspace(0,100,n_x_bins+1);
  56. xbin_e=unique(prctile(x,pbins));
  57. end
  58. end
  59. if isempty(ybin_e)
  60. if even_bins
  61. ybin_e=linspace(min(y),max(y),n_y_bins+1);
  62. else
  63. pbins=linspace(0,100,n_y_bins+1);
  64. ybin_e=unique(prctile(y,pbins));
  65. end
  66. end
  67. clr=cool(numel(ybin_e)-1);
  68. xbinc=(xbin_e(2:end)+xbin_e(1:end-1))/2;
  69. ybinc=(ybin_e(2:end)+ybin_e(1:end-1))/2;
  70. mu=repmat(nan,numel(ybinc),numel(xbinc));
  71. se=mu;
  72. x=x(:);
  73. y=y(:);
  74. z=z(:);
  75. % split the data up by y
  76. [~,yind]=histc(y,ybin_e);
  77. [~,xind]=histc(x,xbin_e);
  78. if all(z==1 | z==0)
  79. binomial_data = true;
  80. else
  81. binomial_data = false;
  82. end
  83. for ny=1:numel(ybinc)
  84. for nx=1:numel(xbinc)
  85. tmp = func(z(xind==nx & yind==ny));
  86. if isempty(tmp)
  87. tmp=nan;
  88. end
  89. mu(ny,nx)=tmp;
  90. if binomial_data
  91. sigma = sum(z(xind==nx & yind==ny));
  92. count = sum(xind==nx & yind==ny);
  93. [~,ci]= binofit(sigma,count);
  94. se(ny,nx)=max(abs(ci-mu(ny,nx)));
  95. else
  96. se(ny,nx)=stats.nanstderr(z(xind==nx & yind==ny));
  97. end
  98. n(ny,nx)=sum(~isnan(z(xind==nx & yind==ny)));
  99. end
  100. end
  101. if plot_it
  102. for ny=1:numel(ybinc)
  103. hh(ny,:)=draw.errorplot(ax,xbinc, mu(ny,:), se(ny,:),'Marker',marker,'Color',clr(ny,:));
  104. set(hh(ny,2),'LineStyle',linestyle,'LineWidth',2)
  105. end
  106. end
  107. function check_inputs(x,y,z)
  108. if ~iscolumn(x) || ~iscolumn(y) || ~iscolumn(z)
  109. error('x,y & z must all be column vectors');
  110. end
  111. if ~isequal(size(x), size(y)) || ~isequal(size(x), size(z)) || ~isequal(size(y), size(z))
  112. error('x, y & z must all be column vectors of equal length');
  113. end