Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

binbino.m 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. function [binc, mu, lowci, highci, n, ytob]=binbino(x,y, varargin)
  2. % [binc, mu, se, n]=binbino(x,y,bin_e)
  3. % Takes a vector x and a vector y and returns mean and binomial ci of
  4. % values of y for bins of x.
  5. %
  6. % Input:
  7. % x 1xn vector of x values to bin
  8. % y 1xn vector of y values to average
  9. %
  10. % Optional Input:
  11. %
  12. % n_bins Optional # of bins. Default 10
  13. % bin_e Optional bin edges. Otherwise will return n_bins bins with equal # of samples from
  14. % min(x) to max(x)
  15. %
  16. % Output:
  17. % binc 1xm bin centers
  18. % mu 1xm The average value of y at that bin
  19. % se 1xm The standard error of y at that bin
  20. % n 1xm The number of values of y in this bin
  21. bin_e=[];
  22. plot_it=false;
  23. plot_fit='';
  24. plot_fit_x0=[0 1 0 10];
  25. n_bins=10;
  26. alpha=0.01;
  27. clr='k';
  28. func=@nanmean;
  29. utils.overridedefaults(who,varargin);
  30. x=x(:);
  31. y=y(:);
  32. bad = isnan(x) | isnan(y);
  33. x(bad)=[];
  34. y(bad)=[];
  35. ytob=nan+y;
  36. if isempty(bin_e)
  37. pbins=linspace(0,100,n_bins+1);
  38. bin_e=prctile(x,pbins);
  39. end
  40. binc=(bin_e(2:end)+bin_e(1:end-1))/2;
  41. mu=nan(size(binc));
  42. se=nan(size(binc));
  43. assert(all(y==0 | y==1 | isnan(y)),'This is for binomial data, use binned instead');
  44. [n,~,yx]=histcounts(x,bin_e);
  45. for nx=1:(numel(n))
  46. tmp=func(y(yx==nx));
  47. if isempty(tmp)
  48. tmp=nan;
  49. end
  50. mu(nx)=tmp;
  51. ytob(yx==nx)=nx;
  52. [~,ci]=binofit(nansum(y(yx==nx)),sum(yx==nx), alpha);
  53. lowci(nx)=ci(1);
  54. highci(nx)=ci(2);
  55. % this is bad for low n.
  56. end
  57. if plot_it
  58. draw.errorplot(gca,binc, mu, se,'Marker','o','Color',clr);
  59. if ~isempty(plot_fit)
  60. beta=nlinfit(x,y,plot_fit,plot_fit_x0);
  61. xx=linspace(binc(1),binc(end),1000);
  62. yy=plot_fit(beta,xx);
  63. hold on;
  64. plot(xx,yy,'-','Color',clr);
  65. end
  66. if binomial_data
  67. ylim([0 1]);
  68. end
  69. end