two_group_test_coherence.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. function [dz,vdz,Adz]=two_group_test_coherence(J1c1,J2c1,J1c2,J2c2,p,plt,f)
  2. % function [dz,vdz,Adz]=two_group_test_coherence(J1c1,J2c1,J1c2,J2c2,p,plt,f)
  3. % Test the null hypothesis (H0) that data sets J1c1,J2c1,J1c2,J2c2 in
  4. % two conditions c1,c2 have equal population coherence
  5. %
  6. % Usage:
  7. % [dz,vdz,Adz]=two_sample_test_coherence(J1c1,J2c1,J1c2,J2c2,p)
  8. %
  9. % Inputs:
  10. % J1c1 tapered fourier transform of dataset 1 in condition 1
  11. % J2c1 tapered fourier transform of dataset 1 in condition 1
  12. % J1c2 tapered fourier transform of dataset 1 in condition 2
  13. % J2c2 tapered fourier transform of dataset 1 in condition 2
  14. % p p value for test (default: 0.05)
  15. % plt 'y' for plot and 'n' for no plot
  16. % f frequencies (useful for plotting)
  17. %
  18. %
  19. % Dimensions: J1c1,J2c2: frequencies x number of samples in condition 1
  20. % J1c2,J2c2: frequencies x number of samples in condition 2
  21. % number of samples = number of trials x number of tapers
  22. % Outputs:
  23. % dz test statistic (will be distributed as N(0,1) under H0
  24. % vdz Arvesen estimate of the variance of dz
  25. % Adz 1/0 for accept/reject null hypothesis of equal population
  26. % coherences based dz ~ N(0,1)
  27. %
  28. % Note: all outputs are functions of frequency
  29. %
  30. % References: Arvesen, Jackkknifing U-statistics, Annals of Mathematical
  31. % Statisitics, vol 40, no. 6, pg 2076-2100 (1969)
  32. if nargin < 4; error('Need four sets of Fourier transforms'); end;
  33. if nargin < 6 || isempty(plt); plt='n'; end;
  34. %
  35. % Test for matching dimensionalities
  36. %
  37. if size(J1c1)~=size(J2c1) | size(J1c2)~=size(J2c2) | size(J1c1,1)~=size(J1c2,1);
  38. error('Need matching dimensionalities for the Fourier transforms: Check the help file for correct dimensionalities');
  39. else;
  40. m1=size(J1c1,2); % number of samples, condition 1
  41. m2=size(J1c2,2); % number of samples, condition 2
  42. dof1=2*m1; % number of degrees of freedom in the first condition estimates
  43. dof2=2*m2; % number of degrees of freedom in the second condition estimates
  44. end;
  45. if nargin < 7 || isempty(f); f=size(J1c1,1); end;
  46. if nargin < 5 || isempty(p); p=0.05; end; % set the default p value
  47. %
  48. % Compute the individual condition spectra, coherences
  49. %
  50. S12c1=conj(J1c1).*J2c1; % individual sample cross-spectrum, condition 1
  51. S12c2=conj(J1c2).*J2c2; % individual sample cross-spectrum, condition 2
  52. S1c1=conj(J1c1).*J1c1; % individual sample spectrum, data 1, condition 1
  53. S2c1=conj(J2c1).*J2c1; % individual sample spectrum, data 2, condition 1
  54. S1c2=conj(J1c2).*J1c2; % individual sample spectrum, data 1, condition 2
  55. S2c2=conj(J2c2).*J2c2; % individual sample spectrum, data 2, condition 2
  56. Sm12c1=squeeze(mean(S12c1,2)); % mean cross spectrum, condition 1
  57. Sm12c2=squeeze(mean(S12c2,2)); % mean cross spectrum, condition 2
  58. Sm1c1=squeeze(mean(S1c1,2)); % mean spectrum, data 1, condition 1
  59. Sm2c1=squeeze(mean(S2c1,2)); % mean spectrum, data 2, condition 1
  60. Sm1c2=squeeze(mean(S1c2,2)); % mean spectrum, data 1, condition 1
  61. Sm2c2=squeeze(mean(S2c2,2)); % mean spectrum, data 2, condition 1
  62. Cm12c1=abs(Sm12c1./sqrt(Sm1c1.*Sm2c1)); % mean coherence, condition 1
  63. Cm12c2=abs(Sm12c2./sqrt(Sm1c2.*Sm2c2)); % mean coherence, condition 2
  64. Ccm12c1=Cm12c1; % mean coherence saved for output
  65. Ccm12c2=Cm12c2; % mean coherence saved for output
  66. %
  67. % Compute the statistic dz, and the probability of observing the value dz
  68. % given an N(0,1) distribution i.e. under the null hypothesis
  69. %
  70. z1=atanh(Cm12c1)-1/(dof1-2); % Bias-corrected Fisher z, condition 1
  71. z2=atanh(Cm12c2)-1/(dof2-2); % Bias-corrected Fisher z, condition 2
  72. dz=(z1-z2)/sqrt(1/(dof1-2)+1/(dof2-2)); % z statistic
  73. %
  74. % The remaining portion of the program computes Jackknife estimates of the mean (mdz) and variance (vdz) of dz
  75. %
  76. samples1=[1:m1];
  77. samples2=[1:m2];
  78. %
  79. % Leave one out of one sample
  80. %
  81. for i=1:m1;
  82. ikeep=setdiff(samples1,i); % all samples except i
  83. Sm12c1=squeeze(mean(S12c1(:,ikeep),2)); % 1 drop mean cross-spectrum, condition 1
  84. Sm1c1=squeeze(mean(S1c1(:,ikeep),2)); % 1 drop mean spectrum, data 1, condition 1
  85. Sm2c1=squeeze(mean(S2c1(:,ikeep),2)); % 1 drop mean spectrum, data 2, condition 1
  86. Cm12c1(:,i)=abs(Sm12c1./sqrt(Sm1c1.*Sm2c1)); % 1 drop coherence, condition 1
  87. z1i(:,i)=atanh(Cm12c1(:,i))-1/(dof1-4); % 1 drop, bias-corrected Fisher z, condition 1
  88. dz1i(:,i)=(z1i(:,i)-z2)/sqrt(1/(dof1-4)+1/(dof2-2)); % 1 drop, z statistic, condition 1
  89. ps1(:,i)=m1*dz-(m1-1)*dz1i(:,i);
  90. % ps1(:,i)=dof1*dz-(dof1-2)*dz1i(:,i);
  91. end;
  92. ps1m=mean(ps1,2);
  93. for j=1:m2;
  94. jkeep=setdiff(samples2,j); % all samples except j
  95. Sm12c2=squeeze(mean(S12c2(:,jkeep),2)); % 1 drop mean cross-spectrum, condition 2
  96. Sm1c2=squeeze(mean(S1c2(:,jkeep),2)); % 1 drop mean spectrum, data 1, condition 2
  97. Sm2c2=squeeze(mean(S2c2(:,jkeep),2)); % 1 drop mean spectrum, data 2, condition 2
  98. Cm12c2(:,j)=abs(Sm12c2./sqrt(Sm1c2.*Sm2c2)); % 1 drop coherence, condition 2
  99. z2j(:,j)=atanh(Cm12c2(:,j))-1/(dof2-4); % 1 drop, bias-corrected Fisher z, condition 2
  100. dz2j(:,j)=(z1-z2j(:,j))/sqrt(1/(dof1-2)+1/(dof2-4)); % 1 drop, z statistic, condition 2
  101. ps2(:,j)=m2*dz-(m2-1)*dz2j(:,j);
  102. % ps2(:,j)=dof2*dz-(dof2-2)*dz2j(:,j);
  103. end;
  104. %
  105. % Leave one out, both samples
  106. % and pseudo values
  107. % for i=1:m1;
  108. % for j=1:m2;
  109. % dzij(:,i,j)=(z1i(:,i)-z2j(:,j))/sqrt(1/(dof1-4)+1/(dof2-4));
  110. % dzpseudoval(:,i,j)=m1*m2*dz-(m1-1)*m2*dz1i(:,i)-m1*(m2-1)*dz2j(:,j)+(m1-1)*(m2-1)*dzij(:,i,j);
  111. % % dzpseudoval(:,i,j)=dof1*dof2*dz-(dof1-2)*dof2*dz1i(:,i)-dof1*(dof2-2)*dz2j(:,j)+(dof1-2)*(dof2-2)*dzij(:,i,j);
  112. % end;
  113. % end;
  114. % dzah=sum(sum(dzpseudoval,3),2)/(m1*m2);
  115. ps2m=mean(ps2,2);
  116. % dzar=(sum(ps1,2)+sum(ps2,2))/(m1+m2);
  117. 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));
  118. % vdzah=sum(sum((dzpseudoval-dzah(:,ones(1,m1),ones(1,m2))).*(dzpseudoval-dzah(:,ones(1,m1),ones(1,m2))),3),2)/(m1*m2);
  119. %
  120. % Test whether H0 is accepted at the specified p value
  121. %
  122. Adz=zeros(size(dz));
  123. x=norminv([p/2 1-p/2],0,1);
  124. indx=find(dz>=x(1) & dz<=x(2));
  125. Adz(indx)=1;
  126. if strcmp(plt,'y');
  127. if isempty(f) || nargin < 6;
  128. f=linspace(0,1,length(dz));
  129. end;
  130. %
  131. % Compute the coherences
  132. %
  133. S121=mean(conj(J1c1).*J2c1,2);
  134. S122=mean(conj(J1c2).*J2c2,2);
  135. S111=mean(conj(J1c1).*J1c1,2);
  136. S221=mean(conj(J2c1).*J2c1,2);
  137. S112=mean(conj(J1c2).*J1c2,2);
  138. S222=mean(conj(J2c2).*J2c2,2);
  139. C121=abs(S121)./sqrt(S111.*S221);
  140. C122=abs(S122)./sqrt(S112.*S222);
  141. %
  142. % Plot the coherence
  143. %
  144. subplot(311);
  145. plot(f,C121,f,C122); legend('Data 1','Data 2');
  146. set(gca,'FontName','Times New Roman','Fontsize', 16);
  147. ylabel('Coherence');
  148. title('Two group test for coherence');
  149. subplot(312);
  150. plot(f,dz);
  151. set(gca,'FontName','Times New Roman','Fontsize', 16);
  152. ylabel('Test statistic');
  153. conf=norminv(1-p/2,0,1);
  154. line(get(gca,'xlim'),[conf conf]);
  155. line(get(gca,'xlim'),[-conf -conf]);
  156. subplot(313);
  157. plot(f,vdz);
  158. set(gca,'FontName','Times New Roman','Fontsize', 16);
  159. xlabel('frequency'); ylabel('Jackknifed variance');
  160. end;
  161. % Adzar=zeros(size(dzar));
  162. % indx=find(dzar>=x(1) & dzar<=x(2));
  163. % Adzar(indx)=1;
  164. %
  165. % Adzah=zeros(size(dzah));
  166. % indx=find(dzah>=x(1) & dzah<=x(2));
  167. % Adzah(indx)=1;