|
3 | 3 | %Author: Eric Fields |
4 | 4 | %Version Date: 12 June 2020 |
5 | 5 |
|
6 | | -%% RB DESIGNS PARAMETRIC ANOVA |
7 | | - |
8 | 6 | %Simulation parameters |
9 | 7 | n_electrodes = 32; |
10 | 8 | n_time_pts = 40; |
11 | 9 | n_subs = 16; |
12 | 10 |
|
| 11 | +%Between subjects designs |
| 12 | +cond_subs = [8, 8]; |
| 13 | + |
| 14 | + |
| 15 | +%% RB DESIGNS PARAMETRIC ANOVA |
| 16 | + |
13 | 17 | %Designs to test |
14 | 18 | anova_designs = {5, ... |
15 | 19 | [2, 2], ... |
|
34 | 38 |
|
35 | 39 | wg_design = anova_designs{m}; |
36 | 40 |
|
| 41 | + if sum(wg_design>2) > 2 && ~isempty(cond_subs) |
| 42 | + continue |
| 43 | + end |
| 44 | + |
37 | 45 | %Choose random sphericity correction |
38 | | - if sum(wg_design>2) < 2 |
| 46 | + if sum(wg_design>2) < 2 && isempty(cond_subs) |
39 | 47 | sphericity_corr = sphericity_corrections{randi(numel(sphericity_corrections))}; |
40 | 48 | else |
41 | 49 | sphericity_corr = 'none'; |
|
58 | 66 | %MATLAB ANOVA |
59 | 67 | oneway_data = reshape(data, n_electrodes, n_time_pts, [], n_subs); |
60 | 68 | 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); |
62 | 70 |
|
63 | 71 | %Calculate all effects in model and compare to MATLAB ANOVA |
64 | 72 | for i = 1:length(effects) |
65 | 73 |
|
| 74 | + %%% Within subjects effects %%% |
| 75 | + |
66 | 76 | %FMUT calculations |
67 | 77 | 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); |
69 | 79 |
|
70 | 80 | %Check results for a random electrode and time point |
71 | 81 | rm_table_row = ['(Intercept):' strrep(effects_labels{i}, 'X', ':')]; |
|
74 | 84 | else |
75 | 85 | suff = sphericity_corr; |
76 | 86 | 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 |
78 | 106 |
|
79 | 107 | end |
80 | 108 |
|
81 | 109 | end |
82 | 110 |
|
83 | 111 |
|
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) |
85 | 115 | %Calculate ANOVA with MATLAB's stats module |
86 | 116 | % |
87 | 117 | %INPUTS |
|
144 | 174 | for row = 1:size(withindesign, 1) |
145 | 175 | col_names{row+1} = strjoin(withindesign{row, :}, ''); |
146 | 176 | end |
147 | | - |
| 177 | + |
148 | 178 | %Create ANOVA table |
149 | 179 | T = [cell2table(cellfun(@(x) sprintf('S%d', x), num2cell(1:n_subs)', 'UniformOutput', false), 'VariableNames', {'sub'}) ... |
150 | 180 | array2table(rm_data)]; |
151 | 181 | T.Properties.VariableNames = col_names; |
152 | | - |
| 182 | + group = {}; |
| 183 | + |
153 | 184 | %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 |
155 | 194 | rm = fitrm(T, model_formula, 'WithinDesign', withindesign); |
156 | 195 | [~, effects_labels] = get_effects(var_names); |
157 | 196 | reg_design = strrep(strjoin(effects_labels, '+'), 'X', '*'); |
158 | 197 | ranovatbl = ranova(rm, 'WithinModel', reg_design); |
159 | 198 |
|
| 199 | + writetable(T, 'outputs/sp_test.csv'); |
| 200 | + |
160 | 201 | end |
0 commit comments