spm_t2z.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. function [z,t1,z1] = spm_t2z(t,df,Tol)
  2. % Student's t to standard Normal (z-score) distribution
  3. % FORMAT [z,t1,z1] = spm_t2z(t,df,Tol)
  4. % t - t values
  5. % df - degrees of freedom
  6. % Tol - minimum tail probability for direct computation
  7. % Defaults to 10^(-16), a z of about 8.2
  8. % t1 - (absolute) t-value where linear extrapolation starts
  9. % empty if no extrapolation
  10. % z1 - Equivalent standard Normal ordinate to t-value t1
  11. %__________________________________________________________________________
  12. %
  13. % spm_t2z implements a distributional transformation from the Student's
  14. % t to the unit Gaussian using incomplete Beta functions and the
  15. % inverse error function.
  16. %
  17. % Returns z as deviates from the standard Normal (Gaussian)
  18. % distribution with lower tail probability equal to that of the
  19. % supplied t statistics with df degrees of freedom.
  20. %
  21. % The standard normal distribution approximates Student's
  22. % t-distribution for large degrees of freedom. In univariate
  23. % situations, conventional wisdom states that 30 degrees of freedom is
  24. % sufficient for such an approximation. In the imaging context, the
  25. % multiple comparisons problem places emphasis on the extreme tails of
  26. % the distribution. For PET neuroimaging simulation suggests that 120
  27. % degrees of freedom are required before the distribution of the
  28. % maximal voxel value in a t-statistic image is adequately approximated
  29. % by that of the maxima of a gaussian statistic image (these
  30. % distributions usually being approximated using the theory of
  31. % continuous random fields) (KJW - private communication). For fMRI
  32. % with it's higher resolution, it is likely that even greater degrees
  33. % of freedom are required for such an approximation.
  34. %
  35. % *No* one-one approximation is made in this code for high df: This is
  36. % because the t2z accuracy reduces as t increases in absolute value
  37. % (particularly in the extrapolation region, underestimating the true
  38. % z. In this case imposing a one-one relationship for df>d say would
  39. % give a jump from df=d-1 to df=d.
  40. %
  41. % For t deviates with very small tail probabilities (< Tol = 10^(-10),
  42. % corresponding to a z of about 6), the corresponding z is computed by
  43. % extrapolation of the t2z relationship z=f(t). This extrapolation
  44. % takes the form of z = log(t-t1+l0) + (z1-log(l0)). Here (t1,z1) is
  45. % the t & z ordinates with tail probability Tol. l0 is chosen such that
  46. % at the point where extrapolation takes over (t1,z1), continuity of
  47. % the first derivative is maintained. Thus, the gradient of the f(t) at
  48. % t1 is estimated as m using six points equally spaced to t1-0.5, and
  49. % l0 is then 1/m. Experience suggests that this underestimates z,
  50. % especially for ludicrously high t and/or high df, giving conservative
  51. % (though still significant) results.
  52. %
  53. %__________________________________________________________________________
  54. % Copyright (C) 1994-2015 Wellcome Trust Centre for Neuroimaging
  55. % Andrew Holmes
  56. % $Id: spm_t2z.m 6654 2015-12-22 12:55:36Z spm $
  57. %-Initialisation
  58. %===========================================================================
  59. % p-value tolerance: t-values with tail probabilities less than Tol are
  60. % `converted' to z by extrapolation
  61. %---------------------------------------------------------------------------
  62. if nargin<3, Tol = 10^(-10); end
  63. %-Argument range and size checks
  64. %---------------------------------------------------------------------------
  65. if nargin<2, error('insufficient arguments'), end
  66. if length(df)~=1, error('df must be a scalar'), end
  67. if df<=0, error('df must be strictly positive'), end
  68. %-Computation
  69. %===========================================================================
  70. z = zeros(size(t));
  71. %-Mask out t == 0 (z==0) and t == +/- Inf (z==+/- Inf), where
  72. % betainc(1,*,*) and betainc(0,*,*) warn "Log of zero"
  73. %---------------------------------------------------------------------------
  74. Qi = find(isinf(t));
  75. if ~isempty(Qi), z(Qi)=t(Qi); end
  76. tmp = df./(df + t.^2);
  77. Q = find(tmp~=1 & ~isinf(t));
  78. if isempty(Q), return; end
  79. %-Mask out at +/- t1 for interpolation
  80. %---------------------------------------------------------------------------
  81. t1 = -spm_invTcdf(Tol,df);
  82. mQb = abs(t(Q)) > t1;
  83. %-t->z using Tcdf & invNcdf for abs(t)<=t1
  84. %===========================================================================
  85. if any(~mQb)
  86. QQnb = find(~mQb);
  87. %-Compute (smaller) tail probability
  88. %-Chunk up to avoid convergence problems for long vectors in betacore
  89. p = zeros(size(QQnb));
  90. tmp = [1,[501:500:length(QQnb)],length(QQnb)+1];
  91. for i = 1:length(tmp)-1
  92. p(tmp(i):tmp(i+1)-1) = ...
  93. betainc(df./(df + t(Q(QQnb(tmp(i):tmp(i+1)-1))).^2),df/2,.5)/2;
  94. end
  95. %-Compute standard normal deviate lower tail prob equal to p
  96. z(Q(QQnb)) = sqrt(2)*erfinv(2*p - 1);
  97. end
  98. %-Compute standard normal deviates for large t where p-value under/overflows
  99. %-Use logarithmic function for extrapolation, fitted such that first
  100. % derivative is continuous. Estimate gradient from the last 0.5 (t) of
  101. % the (computable) t2z relationship.
  102. %===========================================================================
  103. if any(mQb)
  104. z1 =-sqrt(2)*erfinv(2*Tol-1);
  105. t2 =t1-[1:5]/10;
  106. z2 =spm_t2z(t2,df);
  107. %-least squares line through ([f1,t2],[z1,z2]) : z = m*f + c
  108. mc = [[t1,t2]',ones(length([t1,t2]),1)] \ [z1,z2]';
  109. %-------------------------------------------------------------------
  110. %-Logarithmic extrapolation
  111. %-------------------------------------------------------------------
  112. l0=1/mc(1);
  113. %-Perform logarithmic extrapolation, negate z for positive t-values
  114. QQ = Q(mQb); % positions of t-values left to process
  115. z(QQ) = - ( log( (2*(t(QQ)>0)-1).*t(QQ) -t1 + l0 ) + (z1-log(l0)) );
  116. %-------------------------------------------------------------------
  117. % %-------------------------------------------------------------------
  118. % %-Linear extrapolation
  119. % %-------------------------------------------------------------------
  120. % %-adjust c for line through (t1,z1)
  121. % mc(2) = z1-mc(1)*t1;
  122. %
  123. % %-Perform extrapolation, negate positive t-values
  124. % QQ = Q(mQb); % positions of t-values left to process
  125. % z(QQ) = - ( (2*(t(QQ)>0)-1).*t(QQ)*mc(1) + mc(2) );
  126. % %-------------------------------------------------------------------
  127. end
  128. %-Negate (the negative) z-scores corresponding to positive t-values
  129. %---------------------------------------------------------------------------
  130. z(t>0)=-z(t>0);