nestetPermutationTest.m 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. function [obsDiff, pVal] = nestetPermutationTest(sample1, sample2, ...
  2. groupsCell, reorderedFlyIndsCell, ...
  3. permutations, sidedness, plotresult)
  4. allobservations = [sample1, sample2];
  5. observeddifference = nanmean(groupsCell{1}) - ...
  6. nanmean(groupsCell{2});
  7. effectsize = observeddifference / ...
  8. nanmean([nanstd(groupsCell{1}), nanstd(groupsCell{2})]);
  9. exact = 0;
  10. showprogress = permutations / 10;
  11. for n = 1:permutations
  12. if showprogress && mod(n,showprogress) == 0, display(sprintf('Permutation %d of %d', n, permutations)); end
  13. % selecting either next combination, or random permutation
  14. if exact, permutation = [allcombinations(n,:), setdiff(1:numel(allobservations), allcombinations(n,:))];
  15. else, permutation = randperm(length(allobservations)); end
  16. % dividing into two samples
  17. randomSample1 = cell(1, numel(sample1));
  18. randomSample2 = cell(1, numel(sample2));
  19. jCount = 1;
  20. for iElem = permutation
  21. % First fill sample1, then check if the sample coes from first or
  22. % second genotype.
  23. if jCount <= length(sample1)
  24. if iElem > length(sample1)
  25. sampleInds = reorderedFlyIndsCell{2} == iElem - length(sample1);
  26. randomSample1{jCount} = groupsCell{2}(sampleInds);
  27. else
  28. sampleInds = reorderedFlyIndsCell{1} == iElem;
  29. randomSample1{jCount} = groupsCell{1}(sampleInds);
  30. end
  31. else
  32. if iElem > length(sample1)
  33. sampleInds = reorderedFlyIndsCell{2} == iElem - length(sample1);
  34. randomSample2{jCount - numel(sample1)} = groupsCell{2}(sampleInds);
  35. else
  36. sampleInds = reorderedFlyIndsCell{1} == iElem;
  37. randomSample2{jCount - numel(sample1)} = groupsCell{1}(sampleInds);
  38. end
  39. end
  40. jCount = jCount + 1;
  41. end
  42. % saving differences between the two samples
  43. randomdifferences(n) = nanmean(cat(2, randomSample1{:})) - ...
  44. nanmean(cat(2, randomSample2{:}));
  45. end
  46. % getting probability of finding observed difference from random permutations
  47. if strcmp(sidedness, 'both')
  48. p = (length(find(abs(randomdifferences) > abs(observeddifference)))+1) / (permutations+1);
  49. elseif strcmp(sidedness, 'smaller')
  50. p = (length(find(randomdifferences < observeddifference))+1) / (permutations+1);
  51. elseif strcmp(sidedness, 'larger')
  52. p = (length(find(randomdifferences > observeddifference))+1) / (permutations+1);
  53. end
  54. % plotting result
  55. if plotresult
  56. figure;
  57. hist(randomdifferences, 20);
  58. hold on;
  59. xlabel('Random differences');
  60. ylabel('Count')
  61. od = plot(observeddifference, 0, '*r', 'DisplayName', sprintf('Observed difference.\nEffect size: %.2f,\np = %f', effectsize, p));
  62. legend(od);
  63. end
  64. obsDiff = observeddifference;
  65. pVal = p;
  66. end