spm_make_contrasts.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. function Con = spm_make_contrasts(k)
  2. % Make contrasts for one, two or three-way ANOVAs
  3. % FORMAT Con = spm_make_contrasts(k)
  4. %
  5. % k - vector where the ith entry is the number of levels of factor i
  6. %
  7. % Con - struct array with fields:
  8. % Con(c).c - Contrast matrix
  9. % .name - Name
  10. %
  11. % This function computes contrasts for a generic k(1)-by-k(2)-by-k(3)
  12. % design. It is assumed that the levels of the first factor change slowest.
  13. %
  14. % For one-way ANOVAs set k=L, where L is the number of
  15. % levels, for two-way ANOVAs set k=[L1 L2], for three way set k=[L1 L2 L3]
  16. %
  17. % This function generates (transposed) contrast matrices to test
  18. % average effect, main effect of each factor and interactions.
  19. %__________________________________________________________________________
  20. %
  21. % Reference:
  22. %
  23. % For details of Kronecker operations, see section 5 of
  24. % http://www.fil.ion.ucl.ac.uk/~wpenny/publications/rik_anova.pdf
  25. %__________________________________________________________________________
  26. % Copyright (C) 2005-2012 Wellcome Trust Centre for Neuroimaging
  27. % Will Penny
  28. % $Id: spm_make_contrasts.m 6157 2014-09-05 18:17:54Z guillaume $
  29. %-Number of factors
  30. %--------------------------------------------------------------------------
  31. nf = numel(k);
  32. k = [k(:)' 1 1 1];
  33. if nf > 3
  34. fprintf('spm_make_contrasts not written for %d-way ANOVAs.\n',nf);
  35. Con = [];
  36. return
  37. end
  38. %-Get common and differential vectors
  39. %--------------------------------------------------------------------------
  40. C1 = ones(k(1),1);
  41. C2 = ones(k(2),1);
  42. C3 = ones(k(3),1);
  43. C4 = ones(k(4),1);
  44. D1 = -diff(eye(k(1)))';
  45. D2 = -diff(eye(k(2)))';
  46. D3 = -diff(eye(k(3)))';
  47. D4 = -diff(eye(k(4)))';
  48. %-Average effect, main effects and interactions contrast vectors
  49. %--------------------------------------------------------------------------
  50. Con(1).c = kron(kron(kron(C1,C2),C3),C4)';
  51. Con(2).c = kron(kron(kron(D1,C2),C3),C4)';
  52. Con(1).name = 'Average effect of condition';
  53. Con(2).name = 'Main effect of factor 1';
  54. if nf > 1
  55. Con(3).c = kron(kron(kron(C1,D2),C3),C4)';
  56. Con(4).c = kron(kron(kron(D1,D2),C3),C4)';
  57. Con(3).name = 'Main effect of factor 2';
  58. Con(4).name = 'Interaction, factor 1 x 2';
  59. end
  60. if nf > 2
  61. Con(5).c = kron(kron(kron(C1,C2),D3),C4)';
  62. Con(6).c = kron(kron(kron(D1,C2),D3),C4)';
  63. Con(7).c = kron(kron(kron(C1,D2),D3),C4)';
  64. Con(8).c = kron(kron(kron(D1,D2),D3),C4)';
  65. Con(5).name = 'Main effect of factor 3';
  66. Con(6).name = 'Interaction, factor 1 x 3';
  67. Con(7).name = 'Interaction, factor 2 x 3';
  68. Con(8).name = 'Interaction, factor 1 x 2 x 3';
  69. end
  70. if nf > 3
  71. Con(9).c = kron(kron(kron(C1,C2),C3),D4)';
  72. Con(10).c = kron(kron(kron(D1,C2),C3),D4)';
  73. Con(11).c = kron(kron(kron(C1,D2),C3),D4)';
  74. Con(12).c = kron(kron(kron(C1,C2),D3),D4)';
  75. Con(13).c = kron(kron(kron(D1,D2),C3),D4)';
  76. Con(14).c = kron(kron(kron(D1,C2),D3),D4)';
  77. Con(15).c = kron(kron(kron(C1,D2),D3),D4)';
  78. Con(16).c = kron(kron(kron(D1,D2),D3),D4)';
  79. Con(9).name = 'Main effect of factor 4';
  80. Con(10).name = 'Interaction, factor 1 x 4';
  81. Con(11).name = 'Interaction, factor 2 x 4';
  82. Con(12).name = 'Interaction, factor 3 x 4';
  83. Con(13).name = 'Interaction, factor 1 x 2 x 4';
  84. Con(14).name = 'Interaction, factor 1 x 3 x 4';
  85. Con(15).name = 'Interaction, factor 2 x 3 x 4';
  86. Con(16).name = 'Interaction, factor 1 x 2 x 3 x 4';
  87. end