spm_uc_FDR.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. function [u,Ps,Ts] = spm_uc_FDR(q,df,STAT,n,Vs,Vm)
  2. % False Discovery critical height threshold
  3. % FORMAT [u,Ps,Ts] = spm_uc_FDR(q,df,STAT,n,Vs[,Vm])
  4. %
  5. % q - critical expected False Discovery Rate
  6. % df - [df{interest} df{residuals}]
  7. % STAT - Statistical field (see comments below about FWER and EFDR)
  8. % 'Z' - Gaussian field
  9. % 'T' - T - field
  10. % 'X' - Chi squared field
  11. % 'F' - F - field
  12. % 'P' - P - value
  13. % n - Conjunction number
  14. % Vs - Mapped statistic image(s)
  15. % -or-
  16. % Vector of sorted p-values, p1<p2<... (saves i/o w/ repeated calls)
  17. % Vm - Mask in 1 of 3 forms
  18. % o Scalar, indicating implicit mask value in statistic image(s)
  19. % o Vector of indicies of elements within mask
  20. % o Mapped mask image
  21. % (Ignored if Vs is a vector.)
  22. %
  23. % u - critical height
  24. % Ps - Sorted p-values
  25. % Ts - Sorted statistic values
  26. %
  27. %__________________________________________________________________________
  28. %
  29. % The Benjamini & Hochberch (1995) False Discovery Rate (FDR) procedure
  30. % finds a threshold u such that the expected FDR is at most q.
  31. % spm_uc_FDR returns this critical threshold u.
  32. %
  33. % For repeated use of a given statistic image, return Ps in the place
  34. % of Vs:
  35. % [P Ps] = spm_uc_FDR(Z1,df,STAT,n,Vs); %-Initialization, image read
  36. % P = spm_uc_FDR(Z2,df,STAT,n,Ps); %-Image not read, Ps used
  37. %
  38. % Note that a threshold of Inf is possible if q is very small. This
  39. % means that there is no threshold such that FDR is controlled at q.
  40. %
  41. %
  42. % Background
  43. %
  44. % For a given threshold on a statistic image, the False Discovery Rate
  45. % is the proportion of suprathreshold voxels which are false positives.
  46. % Recall that the thresholding of each voxel consists of a hypothesis
  47. % test, where the null hypothesis is rejected if the statistic is larger
  48. % than threshold. In this terminology, the FDR is the proportion of
  49. % rejected tests where the null hypothesis is actually true.
  50. %
  51. % A FDR proceedure produces a threshold that controls the expected FDR
  52. % at or below q. The FDR adjusted p-value for a voxel is the smallest q
  53. % such that the voxel would be suprathreshold.
  54. %
  55. % In comparison, a traditional multiple comparisons proceedure
  56. % (e.g. Bonferroni or random field methods) controls Familywise Error
  57. % rate (FWER) at or below alpha. FWER is the *chance* of one or more
  58. % false positives anywhere (whereas FDR is a *proportion* of false
  59. % positives). A FWER adjusted p-value for a voxel is the smallest alpha
  60. % such that the voxel would be suprathreshold.
  61. %
  62. % If there is truely no signal in the image anywhere, then a FDR
  63. % proceedure controls FWER, just as Bonferroni and random field methods
  64. % do. (Precisely, controlling E(FDR) yeilds weak control of FWE). If
  65. % there *is* some signal in the image, a FDR method should be more powerful
  66. % than a traditional method.
  67. %
  68. %
  69. % References
  70. %
  71. % Benjamini & Hochberg (1995), "Controlling the False Discovery Rate: A
  72. % Practical and Powerful Approach to Multiple Testing". J Royal Stat Soc,
  73. % Ser B. 57:289-300.
  74. %
  75. % Benjamini & Yekutieli (2001), "The Control of the false discovery rate
  76. % in multiple testing under dependency". To appear, Annals of Statistics.
  77. % Available at http://www.math.tau.ac.il/~benja
  78. %__________________________________________________________________________
  79. % Copyright (C) 2002-2015 Wellcome Trust Centre for Neuroimaging
  80. % Thomas Nichols
  81. % $Id: spm_uc_FDR.m 6643 2015-12-11 16:57:25Z guillaume $
  82. if (nargin<6), Vm = []; end
  83. % Set Benjamini & Yeuketeli cV for independence/PosRegDep case
  84. %--------------------------------------------------------------------------
  85. cV = 1;
  86. % Load, mask & sort statistic image (if needed)
  87. %--------------------------------------------------------------------------
  88. if isstruct(Vs)
  89. Ts = spm_data_read(Vs(1));
  90. for i=2:numel(Vs)
  91. Ts = min(Ts,spm_data_read(Vs(i)));
  92. end
  93. if ~isempty(Vm)
  94. if isstruct(Vm)
  95. Ts(spm_data_read(Vm)==0) = [];
  96. elseif numel(Vm)==1
  97. Ts(Ts==Vm) = [];
  98. else
  99. Ts = Ts(Vm);
  100. end
  101. end
  102. Ts(isnan(Ts)) = [];
  103. Ts = sort(Ts(:));
  104. if STAT ~= 'P', Ts = flipud(Ts); end
  105. end
  106. % Calculate p values of image (if needed)
  107. %--------------------------------------------------------------------------
  108. if isstruct(Vs)
  109. Ps = spm_z2p(Ts,df,STAT,n);
  110. else
  111. Ps = Vs;
  112. end
  113. S = length(Ps);
  114. % Calculate FDR inequality RHS
  115. %--------------------------------------------------------------------------
  116. Fi = (1:S)'/S*q/cV;
  117. % Find threshold
  118. %--------------------------------------------------------------------------
  119. I = find(Ps<=Fi, 1, 'last' );
  120. if isempty(I)
  121. if STAT == 'P'
  122. u = 0;
  123. else
  124. u = Inf;
  125. end
  126. else
  127. if isstruct(Vs)
  128. u = Ts(I);
  129. else
  130. % We don't have original statistic values; determine from p-value...
  131. if STAT == 'Z'
  132. u = spm_invNcdf(1-Ps(I).^(1/n));
  133. elseif STAT == 'T'
  134. u = spm_invTcdf(1-Ps(I).^(1/n),df(2));
  135. elseif STAT == 'X'
  136. u = spm_invXcdf(1-Ps(I).^(1/n),df(2));
  137. elseif STAT == 'F'
  138. u = spm_invFcdf(1-Ps(I).^(1/n),df);
  139. % ... except in case when we want a P-value
  140. elseif STAT == 'P'
  141. u = Ps(I);
  142. end
  143. end
  144. end