bootmean.m 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. function varargout=bootmean(varargin)
  2. % [p,ci]=bootmean(A,B,['boots'])
  3. % When passed in a single vector, tests whether the mean is different from 0
  4. % When passed in two vectors does a permutation test to see whether the means
  5. % are significantly different
  6. % Optional inputs:
  7. % 'boots' the number of shuffles to perform (2000)
  8. if nargin>1 && isnumeric(varargin{2})
  9. A=varargin{1};
  10. B=varargin{2};
  11. one_dist=false;
  12. if nargin>2
  13. varargin=varargin(3:end);
  14. else
  15. varargin={};
  16. end
  17. else
  18. A=varargin{1};
  19. one_dist=true;
  20. if nargin>1
  21. varargin=varargin(2:end);
  22. else
  23. varargin={};
  24. end
  25. end
  26. boots=2000;
  27. utils.overridedefaults(who,varargin);
  28. if one_dist
  29. % assume test whether mean of the population is differenct from zero.
  30. dist=A;
  31. if isvector(dist)
  32. dist=dist(:);
  33. end
  34. n=size(dist,1);
  35. [B]=bootstrp(boots, @nanmean, dist);
  36. ps=[0:0.01:100];
  37. sd_ps=prctile(B,ps);
  38. sd_p= stats.get_p(0,B);
  39. varargout{1}=sd_p;
  40. varargout{2}=prctile(B,50);
  41. varargout{3}=B;
  42. else
  43. sA=size(A,1);
  44. sB=size(B,1);
  45. sd=nanmean(A)-nanmean(B);
  46. ALL_DATA=[A;B];
  47. boot_score=zeros(boots,size(ALL_DATA,2));
  48. for bx=1:boots
  49. shuff_d=ALL_DATA(randperm(sA+sB),:);
  50. A=shuff_d(1:sA,:);
  51. B=shuff_d(sA+1:end,:);
  52. boot_score(bx,:)=nanmean(A)-nanmean(B);
  53. end
  54. sd_p= stats.get_p(sd, boot_score);
  55. end
  56. varargout{1}=sd_p;
  57. varargout{2}=prctile(B,[2.5 97.5]);