MATLAB: How to make all possible pairwise comparisons for the three-way repeated measures ANOVA

fitrmMATLABpairwise comparisonsranovarepeated measuresStatistics and Machine Learning Toolbox

I have run a three-way repeated measures ANOVA with 1 within-subject factor and 2 between-subject factors using Statistics and Machine Learning Toolbox. I can make the pairwise comparisons for the 2-way interactions, but I can not do the same for 3-way interactions. I looked at a similar question but the answer provided there works for 3 within-subject factors, not for my case:
https://www.mathworks.com/matlabcentral/answers/140799-3-way-repeated-measures-anova-pairwise-comparisons-using-multcompare
Here is my code:
% generate random data for the example
data = randn(168,3);
% define a variable that stores the treatment information
Treatment = cell(168, 1);
for i=1:25;Treatment{i} = 'control';end;
for i=26:52;Treatment{i} = 'treatment';end;
for i=53:78;Treatment{i} = 'control';end;
for i=79:98;Treatment{i} = 'treatment';end;
for i=99:137;Treatment{i} = 'control';end;
for i=138:168;Treatment{i} = 'treatment';end;
% define a variable that stores the environment type
Etype = cell(168, 1);
for i=1:52;Etype{i} = 'A';end;
for i=53:98;Etype{i} = 'B';end;
for i=99:168;Etype{i} = 'C';end;
% Store the data in a proper table format to do repeated measures analysis
all_table = table(Etype, Treatment, data(:, 1), data(:, 2), ...
data(:, 3), ...
'VariableNames', {'Etype', 'Treatment', 'pre', 'during', 'post'});
% Define the within subject parameter (pre, during, post)
Time = [0 1 2]; % 0: pre, 1: during, 2: post
% Fit repetitive model to data
rm = fitrm(all_table, 'pre-post ~ Treatment*Etype', ...
'WithinModel', Time, 'WithinModel', 'separatemeans');
% Run repetitive measures ANOVA on model:
ranova(rm)
% make pairwise comparisons for the two-way interactions
multcompare(rm, 'Time', 'By', 'Etype')
multcompare(rm, 'Time', 'By', 'Treatment')
multcompare(rm, 'Treatment', 'By', 'Etype')
% but how can I make pairwise comparisons for the 3-way interaction?
% These do not work:
multcompare(rm, 'Time', 'By', 'Treatment', 'Etype')
multcompare(rm, 'Time', 'By', {'Treatment', 'Etype'})
Thanks

Best Answer

Hi Ehsan,
I recently came across a really similar problem with my research. I suggest a solution could be found by adapting the answer of Matt Mollison in:
In the mentioned case they wanted to test all the possible combinations of levels of the within subject factors, while in this case we both have between-subject factors (Etype and Treatment) and within-subject factors (Time).
I would suggest working around your problem with the following code.
% 1. Convert the between-subject factors to categorical.
etype_cat=categorical(Etype);
treatment_cat=categorical(Treatment);
% 2. Create an interaction factor capturing each combination of levels % of Etype and Treatment (you can check with the function "catogories()")
interaction_cat=etype_cat.*treatment_cat;
% 3. Call fitrm with the modified between design.
t2 = table(interaction_cat, data(:, 1), data(:, 2), data(:, 3), ...
'VariableNames', {'interaction_etype_treat', 'pre', 'during', 'post'});
rm2 = fitrm(t2, 'pre-post ~ interaction_etype_treat', ...
'WithinModel', Time, 'WithinModel', 'separatemeans');
% 4. Use interaction factor interaction_cat as the first variable in % multcompare, 'By' Time
tbl2=multcompare(rm2,'interaction_etype_treat','By','Time');