Skip to content

Commit c1d1cf9

Browse files
author
Eric Fields
committed
add split plot designs to I_ANOVATest
1 parent e525ff9 commit c1d1cf9

2 files changed

Lines changed: 53 additions & 12 deletions

File tree

estimate_epsilon.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
% epsilon - electrode x time point array of epsilon estimate
2626
%
2727
%
28-
%VERSION DATE: 11 June 2020
28+
%VERSION DATE: 13 June 2020
2929
%AUTHOR: Eric Fields
3030
%
3131
%NOTE: This function is provided "as is" and any express or implied warranties
@@ -52,7 +52,7 @@
5252

5353
%This function currently only works for one-way fully repeated measures designs
5454
if ~isempty(cond_subs) && ~isequal(cond_subs, 0) && length(cond_subs) > 1
55-
error('estimate_epsilon curently only implemented for fully repetaed measures ANOVA');
55+
error('estimate_epsilon curently only implemented for fully repeated measures ANOVA');
5656
end
5757
if ndims(reduced_data) ~= 4
5858
error('estimate_epsilon currently does not support more than one factor with more than 2 levels');

testing/I_ANOVATest.m

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
%Author: Eric Fields
44
%Version Date: 12 June 2020
55

6-
%% RB DESIGNS PARAMETRIC ANOVA
7-
86
%Simulation parameters
97
n_electrodes = 32;
108
n_time_pts = 40;
119
n_subs = 16;
1210

11+
%Between subjects designs
12+
cond_subs = [8, 8];
13+
14+
15+
%% RB DESIGNS PARAMETRIC ANOVA
16+
1317
%Designs to test
1418
anova_designs = {5, ...
1519
[2, 2], ...
@@ -34,8 +38,12 @@
3438

3539
wg_design = anova_designs{m};
3640

41+
if sum(wg_design>2) > 2 && ~isempty(cond_subs)
42+
continue
43+
end
44+
3745
%Choose random sphericity correction
38-
if sum(wg_design>2) < 2
46+
if sum(wg_design>2) < 2 && isempty(cond_subs)
3947
sphericity_corr = sphericity_corrections{randi(numel(sphericity_corrections))};
4048
else
4149
sphericity_corr = 'none';
@@ -58,14 +66,16 @@
5866
%MATLAB ANOVA
5967
oneway_data = reshape(data, n_electrodes, n_time_pts, [], n_subs);
6068
rm_data = squeeze(oneway_data(e,t,:,:))';
61-
[rm, ranovatbl] = matlab_ANOVA(rm_data, wg_design, var_names);
69+
[rm, ranovatbl] = matlab_ANOVA(rm_data, wg_design, cond_subs, var_names);
6270

6371
%Calculate all effects in model and compare to MATLAB ANOVA
6472
for i = 1:length(effects)
6573

74+
%%% Within subjects effects %%%
75+
6676
%FMUT calculations
6777
dims = effects{i}+2;
68-
test_results = calc_param_ANOVA(data, [], dims, 0.05, 'none', sphericity_corr);
78+
test_results = calc_param_ANOVA(data, cond_subs, dims, 0.05, 'none', sphericity_corr);
6979

7080
%Check results for a random electrode and time point
7181
rm_table_row = ['(Intercept):' strrep(effects_labels{i}, 'X', ':')];
@@ -74,14 +84,34 @@
7484
else
7585
suff = sphericity_corr;
7686
end
77-
assert(test_results.p(e,t) - ranovatbl{rm_table_row, ['pValue' suff]} < 1e-9);
87+
assert(abs(test_results.p(e,t) - ranovatbl{rm_table_row, ['pValue' suff]}) < 1e-9);
88+
89+
%%% Between subjects effects
90+
if ~isempty(cond_subs)
91+
92+
%FMUT calculations
93+
dims = [effects{i}+2 ndims(data)];
94+
test_results = calc_param_ANOVA(data, cond_subs, dims, 0.05, 'none', sphericity_corr);
95+
96+
%Check results for a random electrode and time point
97+
rm_table_row = ['group:' strrep(effects_labels{i}, 'X', ':')];
98+
if strcmp(sphericity_corr, 'none')
99+
suff = '';
100+
else
101+
suff = sphericity_corr;
102+
end
103+
assert(abs(test_results.p(e,t) - ranovatbl{rm_table_row, ['pValue' suff]}) < 1e-9);
104+
105+
end
78106

79107
end
80108

81109
end
82110

83111

84-
function [rm, ranovatbl] = matlab_ANOVA(rm_data, wg_design, var_names)
112+
%% ANOVA function
113+
114+
function [rm, ranovatbl] = matlab_ANOVA(rm_data, wg_design, cond_subs, var_names)
85115
%Calculate ANOVA with MATLAB's stats module
86116
%
87117
%INPUTS
@@ -144,17 +174,28 @@
144174
for row = 1:size(withindesign, 1)
145175
col_names{row+1} = strjoin(withindesign{row, :}, '');
146176
end
147-
177+
148178
%Create ANOVA table
149179
T = [cell2table(cellfun(@(x) sprintf('S%d', x), num2cell(1:n_subs)', 'UniformOutput', false), 'VariableNames', {'sub'}) ...
150180
array2table(rm_data)];
151181
T.Properties.VariableNames = col_names;
152-
182+
group = {};
183+
153184
%MATLAB repeated measure ANOVA
154-
model_formula = sprintf('%s-%s~1', T.Properties.VariableNames{2}, T.Properties.VariableNames{end});
185+
if isempty(cond_subs)
186+
model_formula = sprintf('%s-%s~1', T.Properties.VariableNames{2}, T.Properties.VariableNames{end});
187+
else
188+
for g = 1:length(cond_subs)
189+
group = [group; repmat({sprintf('G%d', g)}, cond_subs(g), 1)]; %#ok<AGROW>
190+
end
191+
T(:, 'group') = group;
192+
model_formula = sprintf('%s-%s~1+group', T.Properties.VariableNames{2}, T.Properties.VariableNames{end-1});
193+
end
155194
rm = fitrm(T, model_formula, 'WithinDesign', withindesign);
156195
[~, effects_labels] = get_effects(var_names);
157196
reg_design = strrep(strjoin(effects_labels, '+'), 'X', '*');
158197
ranovatbl = ranova(rm, 'WithinModel', reg_design);
159198

199+
writetable(T, 'outputs/sp_test.csv');
200+
160201
end

0 commit comments

Comments
 (0)