nets_glm.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. %
  2. % [p_uncorrected,p_corrected] = nets_glm(netmats,design_matrix,contrast_matrix,view_output);
  3. % [p_uncorrected,p_corrected] = nets_glm(netmats,design_matrix,contrast_matrix,view_output,nperms);
  4. % Steve Smith and Ludo Griffanti - 2013-2014
  5. %
  6. % do cross-subject GLM on a set of netmats, giving uncorrected and corrected (1-p) values
  7. % randomise(permutation testing) is used to get corrected 1-p-values (i.e., correcting for multiple comparisons
  8. % across the NxN netmat elements)
  9. %
  10. % design_matrix and contrast_matrix are strings pointing to randomise-compatible design and contrast matrix files
  11. %
  12. % view_output (0 or 1) determines whether to bring up the graphical display of the results.
  13. % below the diagonals is shown the 1-corrected-p,
  14. % above the diagonal is the same thing, but thresholded at 0.95 i.e. corrected-p < 0.05
  15. %
  16. function [p_uncorrected,p_corrected] = nets_glm(ts_dir,netmats,des,con,view,varargin);
  17. nperms=5000;
  18. if nargin==6
  19. nperms=varargin{1};
  20. end
  21. XXX=size(netmats,2);
  22. TTT=size(netmats,1);
  23. Nf=sqrt(XXX);
  24. N=round(Nf);
  25. ShowSquare=0;
  26. if (N==Nf)
  27. grot=reshape(mean(netmats),N,N);
  28. if sum(sum(abs(grot-grot')))<0.00000001 % is netmat square and symmetric
  29. ShowSquare=1;
  30. end
  31. end
  32. %fname=tempname;
  33. fname=[ts_dir '/tempo'];
  34. save_avw(reshape(netmats',XXX,1,1,TTT),fname,'f',[1 1 1 1]);
  35. system(sprintf('randomise -i %s -o %s -d %s -t %s -x --uncorrp -n %d',fname,fname,des,con,nperms));
  36. % how many contrasts were run?
  37. [grot,ncon]=system(sprintf('imglob %s_vox_corrp_tstat*.* | wc -w',fname));
  38. ncon=str2num(ncon);
  39. if view==1
  40. figure('position',[100 100 600*ncon 500]);
  41. end
  42. gap=0.05; cw=0.1; xw=(1-gap*(ncon+2))/(ncon+0.1);
  43. for i=1:ncon
  44. p_uncorrected(i,:)= read_avw(sprintf('%s_vox_p_tstat%d',fname,i));
  45. p_corrected(i,:)= read_avw(sprintf('%s_vox_corrp_tstat%d',fname,i));
  46. [grot,FDRthresh]=system(sprintf('fdr -i %s_vox_p_tstat%d -q 0.05 --oneminusp | grep -v Probability',fname,i));
  47. FDRthresh=str2num(FDRthresh);
  48. sprintf('contrast %d, best values: uncorrected_p=%f FWE_corrected_p=%f. \nFDR-correction-threshold=%f (to be applied to uncorrected p-values)',i,1-max(p_uncorrected(i,:)),1-max(p_corrected(i,:)),FDRthresh)
  49. if view==1
  50. if ShowSquare==1
  51. if (i<ncon)
  52. subplot('Position',[ (i-1)*xw+i*gap gap xw 1-2*gap ]);
  53. else
  54. subplot('Position',[ (i-1)*xw+i*gap gap xw+0.1 1-2*gap ]);
  55. end
  56. grot=reshape(p_corrected(i,:),N,N);
  57. [groti,grotj]=find(grot==max(grot(:)));
  58. sprintf('optimal corrected p=%.5f at edge between nodes %d and %d\n',1-max(grot(:)),groti(1),grotj(1))
  59. imagesc(grot.*(triu(grot,1)>0.95) + tril(grot)); % delete non-significant entries above the diag
  60. colormap('jet');
  61. if (i==ncon), colorbar; end;
  62. else
  63. subplot('Position',[ (i-1)*xw+i*gap gap xw 1-2*gap ]);
  64. plot(p_corrected(i,:));
  65. end
  66. title(sprintf('contrast %d',i));
  67. end
  68. end