forked from LIMO-EEG-Toolbox/limo_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
limo_OrthogContrasts.m
90 lines (78 loc) · 2.53 KB
/
limo_OrthogContrasts.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
function [M,MList]=limo_OrthogContrasts(nTreats)
%
% Generates coefficients corresponding to one particular set of
% orthogonal contrasts for a set of variables
%
% FORMAT
% [contrasts, names]= limo_OrthogContrasts([2 2])
%
% INPUT
% nTreats is a vector with the levels of the various factors in an ANOVA
%
% OUTPUT
% M is a cell with the various contrast in it
% Mlist is a series of set names
%
% Taken from Matthew Nelson GenOrthogComps.m
% simplified by Cyril Pernet 16-09-2009
% ------------------------------
% Copyright (C) LIMO Team 2019
if length(nTreats) == 1
error('there must be at least 2 factors')
end
alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
dfs=nTreats-1;
nFacs=length(nTreats);
nCombs=prod(nTreats);
% Conpute main effects contrasts
M=cell(1,nFacs);
for iFac=1:nFacs
M{iFac}=repmat(0,dfs(iFac),nCombs);
if iFac==nFacs;
nLowCombs=1;
else
nLowCombs=prod(nTreats(iFac+1:end));
end
if iFac==1
nUpCombs=1;
else
nUpCombs=prod(nTreats(1:iFac-1));
end
for idf=1:dfs(iFac)
tmpComps=repmat( 0,1,nLowCombs*nTreats(iFac) );
tmpComps( 1:nLowCombs*idf )=1/idf;
tmpComps( nLowCombs*idf+1:nLowCombs*(idf+1) )=-1;
tmpComps=repmat(tmpComps,1,nUpCombs);
M{iFac}(idf, : )=tmpComps;
MList{iFac}=alphabet(iFac);
end
end
% Do interactions as products of the main effect contrasts
if nFacs>1
for curnFacs=2:nFacs %start with 2 way interactions and go up to nFacs-way interactions
for initFac=1:nFacs-(curnFacs-1)
[M MList]=RLoop(curnFacs,initFac,repmat(1,1,nCombs), length(M)+1, '', M,MList,dfs,alphabet);
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [M MList]=RLoop(FacRem,curFac,curRow,curMNum, nextList, M,MList,dfs,alphabet)
%given the initial inputs, and the initial curFac, this should calc all the
%downstream M vals for that initial curFac...
FacRem=FacRem-1;
for idf=1:dfs(curFac)
if FacRem==0
if curMNum>length(M);
M{curMNum}=[];
MList{curMNum}=[nextList alphabet(curFac)];
end
M{curMNum}(end+1,:)=curRow.*M{curFac}(idf,:);
else
nextRow=curRow.*M{curFac}(idf,:);
nextList(end+1)=alphabet(curFac);
baseFacNum=curFac+1; %this is needed for the numbering of the outputs...
for iNextFac=curFac+1:length(dfs)-FacRem+1
[M MList]=RLoop( FacRem,iNextFac,nextRow,curMNum+(iNextFac-baseFacNum), nextList, M,MList,dfs,alphabet );
end
end
end