all_comps.m 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. function [p,ps]=all_comps(M,varargin)
  2. % [p,ps]=all_comps(M,varargin)
  3. %
  4. % Does series of ttests or permutation tests between N groups of samples using
  5. % Bonferroni-Holm correction for multiple comparisons.
  6. %
  7. % Input
  8. % M A matrix or a cell array of vectors. Comparisons are between
  9. % colums or between elements of the cell array
  10. %
  11. % Optional Inputs
  12. %
  13. % adjust_p=true; If false does not correct for multiple comparisons
  14. % use_ttest=true; If false uses permutation tests (slower)
  15. % nboots=2500; The number of permutations to use if not using ttests
  16. % do_median=false; If true does a permutation test comparing medians.
  17. % Overides use_ttest
  18. % Output
  19. % p The p-values for each comparison
  20. % ps An (n choose 2) x 2 matrix which describes which p
  21. % values correspond to which comparison. Always
  22. % in numerical order, so this is just for convenience.
  23. adjust_p=true;
  24. use_ttest=true;
  25. nboots=2500;
  26. do_median=false;
  27. utils.overridedefaults(who,varargin);
  28. if isnumeric(M)
  29. n_cols=size(M,2);
  30. ps=nchoosek(1:n_cols,2);
  31. p=nan(1,size(ps,1));
  32. for px=1:size(ps,1)
  33. V=M(:,ps(px,1))-M(:,ps(px,2));
  34. if do_median
  35. p(px)=bootmedian(V,'boots',nboots);
  36. else
  37. if use_ttest
  38. [~,p(px)]=ttest(V);
  39. else
  40. p(px)=bootmean(V,'boots',nboots);
  41. end
  42. end
  43. end
  44. else
  45. % M is a cell array
  46. n_cols=numel(M);
  47. ps=nchoosek(1:n_cols,2);
  48. p=nan(1,size(ps,1));
  49. if do_median
  50. for px=1:size(ps,1)
  51. p(px)=bootmedian(M{ps(px,1)},M{ps(px,2)});
  52. end
  53. else
  54. for px=1:size(ps,1)
  55. p(px)=bootmean(M{ps(px,1)},M{ps(px,2)});
  56. end
  57. end
  58. end
  59. if adjust_p
  60. p=bonf_holm(p);
  61. end