forked from sashel/permANOVA
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Sim_indepANOVA_a.m
184 lines (151 loc) · 7.06 KB
/
Sim_indepANOVA_a.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
%% Simulations of an increasing main effect in an independent, balanced 2-way ANOVA without interaction
% Uses FtSimLink_indepANOVA, anova2_cell_mod
stemFolder = pwd;
resDir = [stemFolder filesep 'SimData' filesep 'ResultsIndepANOVA_a' filesep];
savePrefix = '2way_indep_a';
if ~exist(resDir,'dir')
mkdir(resDir);
end;
Rep = 1000; % number of Monte Carlo simulations
a = 3; % number of levels in factor A
b = 4; % number of levels in factor B
n = 5; % number of repetitions in each factor level combination
B = [50 -50 20 -20];
B = reshape(repmat(B,a,n),a,b,n);
design = [repmat([1,2,3],1,b*n);repmat([1 1 1 2 2 2 3 3 3 4 4 4],1,n)]; % design matrix
load([stemFolder filesep 'dummy'],'data');
cfg_neighb.method = 'template';
cfg_neighb.template = 'CTF275_neighb.mat';
neighbours = ft_prepare_neighbours(cfg_neighb, data);
Method_List = {'ftest','raw','exact','res'};
Error_List = {'exp','gauss'};
for ee = 1:length(Error_List)
figure
hold on
for mm = 1:length(Method_List)
method = Method_List{mm};
err_dist = Error_List{ee};
if strcmp(err_dist,'exp')
sc = [10000, 40, 20, 12, 10, 8, 6, 4, 3];
elseif strcmp(err_dist,'gauss')
sc = [100000, 200, 100, 70, 50, 40, 35, 30, 25]*3;
end
saveStr = [savePrefix,'_',err_dist,'_',method];
for r = 1:length(sc)
% gradually increase effecr size of factor A
if r ==1
A = [0; 0; 0];
else
A = [50; 0;-50]/sc(r);
end
m_A = mean(A);
par = sqrt(sum(((A - m_A).^2)./a)); % effect size
A = reshape(repmat(A,b,n),a,b,n);
res_A = zeros(Rep,1);
for j = 1:Rep
% put the model together & add errors
if strcmp(err_dist,'exp')
y = A + B + (exprnd(1,a,b,n)).^3;
elseif strcmp(err_dist,'gauss')
y = A + B + randn(a,b,n);
end
switch method
case 'ftest' % standard F-test
% prepare data input for anova2_cell_mod
c{1,1} = squeeze(y(1,1,:))';
c{1,2} = squeeze(y(1,2,:))';
c{1,3} = squeeze(y(1,3,:))';
c{1,4} = squeeze(y(1,4,:))';
c{2,1} = squeeze(y(2,1,:))';
c{2,2} = squeeze(y(2,2,:))';
c{2,3} = squeeze(y(2,3,:))';
c{2,4} = squeeze(y(2,4,:))';
c{3,1} = squeeze(y(3,1,:))';
c{3,2} = squeeze(y(3,2,:))';
c{3,3} = squeeze(y(3,3,:))';
c{3,4} = squeeze(y(3,4,:))';
[FA, FB, FI, dfa, dfb, dfi] = anova2_cell_mod(c); % adapted from the resampling statistic toolbox
res_A(j) = 1 - fcdf(FA, dfa(1), dfa(2));
case 'raw' % permutation of raw data
fac = 'a';
c = reshape(y,1,a*b*n);
exact = 'no';
statfun = 'indepAnova2way';
% the permutation ANOVA is called here
stat = FtSimLink_indepANOVA(data,neighbours,c,design,statfun,fac,exact);
res_A(j) = stat.prob;
case 'exact' % restricted permutations
fac = 'a';
c = reshape(y,1,a*b*n);
exact = 'yes';
statfun = 'indepAnova2way';
stat = FtSimLink_indepANOVA(data,neighbours,c,design,statfun,fac,exact);
res_A(j) = stat.prob;
case 'res' % permutation of residuals
fac = 'a';
c = reshape(y,1,a*b*n);
ncond_a = a;
ncond_b = b;
for nfac_a = 1:ncond_a
for nfac_b = 1:ncond_b
idx_ab = design(1,:) == nfac_a & design(2,:) == nfac_b;
anovaIn{nfac_a,nfac_b} = c(idx_ab);
end
end
for jj = 1:size(anovaIn,2)
tmp = zeros(size(anovaIn{1,1}));
for ii = 1:size(anovaIn,1)
tmp(:,:,ii) = anovaIn{ii,jj};
end
bmean(jj) = squeeze(mean(mean(tmp)));
end
for ii = 1:size(anovaIn,1)
for jj = 1:size(anovaIn,2)
idx_ab = design(1,:) == ii & design(2,:) == jj;
c_new(idx_ab) = c(idx_ab) - bmean(jj);
end
end
exact = 'no';
statfun = 'indepAnova2way';
stat = FtSimLink_indepANOVA(data,neighbours,c_new,design,statfun,fac,exact);
res_A(j) = stat.prob;
end
end
p_val(r) = length(find(res_A <= 0.05))/Rep;
paramA(r) = par;
end
switch method
case 'ftest'
plot(paramA,p_val,':d','color',[0.4 0.4 0.4],'linewidth',2)
case 'raw'
plot(paramA,p_val,'g:d','linewidth',2)
case 'exact'
plot(paramA,p_val,':*','linewidth',2)
case 'res'
plot(paramA,p_val,'m:s','linewidth',2)
end
set(gcf,'color','w')
xlabel('\theta_A','FontSize',10,'FontName','Helvetica')
ylabel('Power','FontSize',10,'FontName','Helvetica')
if strcmp(err_dist,'gauss')
title('Gauss. errors','FontSize',11,'FontName','Helvetica','FontWeight','bold','FontAngle','italic')
elseif strcmp(err_dist,'exp')
title('Exp. errors','FontSize',11,'FontName','Helvetica','FontWeight','bold','FontAngle','italic')
end
set(gca, ...
'Box' , 'off' , ...
'TickDir' , 'out' , ...
'TickLength' , [.02 .02] , ...
'XMinorTick' , 'off' , ...
'YMinorTick' , 'off' , ...
'YGrid' , 'off' , ...
'YTick' , [0.05 0.2:0.2:1], ... %'YTickLabel' , [], ...
'LineWidth' , 1 );
box off
xlim([min(paramA) max(paramA)])
cd(resDir)
save(saveStr,'paramA','p_val*','A','B','sc','method')
clear paramA p_val* c
cd(stemFolder)
end
end