two_group_test_spectrum.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. function [dz,vdz,Adz]=two_group_test_spectrum(J1,J2,p,plt,f)
  2. % function [dz,vdz,Adz]=two_group_test_spectrum(J1,J2,p,plt,f)
  3. % Test the null hypothesis (H0) that data sets J1, J2 in
  4. % two conditions c1,c2 have equal population spectrum
  5. %
  6. % Usage:
  7. % [dz,vdz,Adz]=two_sample_test_spectrum(J1,J2,p)
  8. %
  9. % Inputs:
  10. % J1 tapered fourier transform in condition 1
  11. % J2 tapered fourier transform in condition 2
  12. % p p value for test (default: 0.05)
  13. % plt 'y' for plot and 'n' for no plot
  14. % f frequencies (useful for plotting)
  15. %
  16. %
  17. % Dimensions: J1: frequencies x number of samples in condition 1
  18. % J2: frequencies x number of samples in condition 2
  19. % number of samples = number of trials x number of tapers
  20. % Outputs:
  21. % dz test statistic (will be distributed as N(0,1) under H0
  22. % vdz Arvesen estimate of the variance of dz
  23. % Adz 1/0 for accept/reject null hypothesis of equal population
  24. % coherences based dz ~ N(0,1)
  25. %
  26. %
  27. % Note: all outputs are functions of frequency
  28. %
  29. % References: Arvesen, Jackkknifing U-statistics, Annals of Mathematical
  30. % Statisitics, vol 40, no. 6, pg 2076-2100 (1969)
  31. if nargin < 2; error('Need four sets of Fourier transforms'); end;
  32. if nargin < 4 || isempty(plt); plt='n'; end;
  33. %
  34. % Test for matching dimensionalities
  35. %
  36. m1=size(J1,2); % number of samples, condition 1
  37. m2=size(J2,2); % number of samples, condition 2
  38. dof1=m1; % degrees of freedom, condition 1
  39. dof2=m2; % degrees of freedom, condition 2
  40. if nargin < 5 || isempty(f); f=size(J1,1); end;
  41. if nargin < 3 || isempty(p); p=0.05; end; % set the default p value
  42. %
  43. % Compute the individual condition spectra, coherences
  44. %
  45. S1=conj(J1).*J1; % spectrum, condition 1
  46. S2=conj(J2).*J2; % spectrum, condition 2
  47. Sm1=squeeze(mean(S1,2)); % mean spectrum, condition 1
  48. Sm2=squeeze(mean(S2,2)); % mean spectrum, condition 2
  49. %
  50. % Compute the statistic dz, and the probability of observing the value dz
  51. % given an N(0,1) distribution i.e. under the null hypothesis
  52. %
  53. bias1=psi(dof1)-log(dof1); bias2=psi(dof2)-log(dof2); % bias from Thomson & Chave
  54. var1=psi(1,dof1); var2=psi(1,dof2); % variance from Thomson & Chave
  55. z1=log(Sm1)-bias1; % Bias-corrected Fisher z, condition 1
  56. z2=log(Sm2)-bias2; % Bias-corrected Fisher z, condition 2
  57. dz=(z1-z2)/sqrt(var1+var2); % z statistic
  58. % Bug fix
  59. % pdz=normpdf(dz,0,1); % probability of observing value dz
  60. pdz = 2*normcdf(-abs(dz),0,1); % probability of observing value dz
  61. %
  62. % The remaining portion of the program computes Jackknife estimates of the mean (mdz) and variance (vdz) of dz
  63. %
  64. samples1=[1:m1];
  65. samples2=[1:m2];
  66. %
  67. % Leave one out of one sample
  68. %
  69. bias11=psi(dof1-1)-log(dof1-1); var11=psi(1,dof1-1);
  70. for i=1:m1;
  71. ikeep=setdiff(samples1,i); % all samples except i
  72. Sm1=squeeze(mean(S1(:,ikeep),2)); % 1 drop mean spectrum, data 1, condition 1
  73. z1i(:,i)=log(Sm1)-bias11; % 1 drop, bias-corrected Fisher z, condition 1
  74. dz1i(:,i)=(z1i(:,i)-z2)/sqrt(var11+var2); % 1 drop, z statistic, condition 1
  75. ps1(:,i)=m1*dz-(m1-1)*dz1i(:,i);
  76. end;
  77. ps1m=mean(ps1,2);
  78. bias21=psi(dof2-1)-log(dof2-1); var21=psi(1,dof2-1);
  79. for j=1:m2;
  80. jkeep=setdiff(samples2,j); % all samples except j
  81. Sm2=squeeze(mean(S2(:,jkeep),2)); % 1 drop mean spectrum, data 2, condition 2
  82. z2j(:,j)=log(Sm2)-bias21; % 1 drop, bias-corrected Fisher z, condition 2
  83. dz2j(:,j)=(z1-z2j(:,j))/sqrt(var1+var21); % 1 drop, z statistic, condition 2
  84. ps2(:,j)=m2*dz-(m2-1)*dz2j(:,j);
  85. end;
  86. %
  87. % Leave one out, both samples
  88. % and pseudo values
  89. % for i=1:m1;
  90. % for j=1:m2;
  91. % dzij(:,i,j)=(z1i(:,i)-z2j(:,j))/sqrt(var11+var21);
  92. % dzpseudoval(:,i,j)=m1*m2*dz-(m1-1)*m2*dz1i(:,i)-m1*(m2-1)*dz2j(:,j)+(m1-1)*(m2-1)*dzij(:,i,j);
  93. % end;
  94. % end;
  95. %
  96. % Jackknife mean and variance
  97. %
  98. % dzah=sum(sum(dzpseudoval,3),2)/(m1*m2);
  99. ps2m=mean(ps2,2);
  100. % dzar=(sum(ps1,2)+sum(ps2,2))/(m1+m2);
  101. vdz=sum((ps1-ps1m(:,ones(1,m1))).*(ps1-ps1m(:,ones(1,m1))),2)/(m1*(m1-1))+sum((ps2-ps2m(:,ones(1,m2))).*(ps2-ps2m(:,ones(1,m2))),2)/(m2*(m2-1));
  102. % vdzah=sum(sum((dzpseudoval-dzah(:,ones(1,m1),ones(1,m2))).*(dzpseudoval-dzah(:,ones(1,m1),ones(1,m2))),3),2)/(m1*m2);
  103. %
  104. % Test whether H0 is accepted at the specified p value
  105. %
  106. Adz=zeros(size(dz));
  107. x=norminv([p/2 1-p/2],0,1);
  108. indx=find(dz>=x(1) & dz<=x(2));
  109. Adz(indx)=1;
  110. % Adzar=zeros(size(dzar));
  111. % indx=find(dzar>=x(1) & dzar<=x(2));
  112. % Adzar(indx)=1;
  113. %
  114. % Adzah=zeros(size(dzah));
  115. % indx=find(dzah>=x(1) & dzah<=x(2));
  116. % Adzah(indx)=1;
  117. if strcmp(plt,'y');
  118. if isempty(f) || nargin < 5;
  119. f=linspace(0,1,length(dz));
  120. end;
  121. %
  122. % Compute the coherences
  123. %
  124. S1=squeeze(mean(conj(J1).*J1,2));
  125. S2=squeeze(mean(conj(J2).*J2,2));
  126. %
  127. % Plot the coherence
  128. %
  129. subplot(311);
  130. plot(f,S1,f,S2); legend('Data 1','Data 2');
  131. set(gca,'FontName','Times New Roman','Fontsize', 16);
  132. ylabel('Spectra');
  133. title('Two group test for spectrum');
  134. subplot(312);
  135. plot(f,dz);
  136. set(gca,'FontName','Times New Roman','Fontsize', 16);
  137. ylabel('Test statistic');
  138. conf=norminv(1-p/2,0,1);
  139. line(get(gca,'xlim'),[conf conf]);
  140. line(get(gca,'xlim'),[-conf -conf]);
  141. subplot(313);
  142. plot(f,vdz);
  143. set(gca,'FontName','Times New Roman','Fontsize', 16);
  144. xlabel('frequency'); ylabel('Jackknifed variance');
  145. end;