Set up Repeated Measures Anova function MATLAB
8 views (last 30 days)
Show older comments
Jacob Jacobo
on 21 Jul 2022
Commented: Scott MacKenzie
on 28 Jul 2022
Hello Everyone,
I want to run an ANOVA comparing 3 treatments (labeled as 1, 2, and 3) and then run a post-hoc comparison between treatments if the ANOVA shows a difference. I have 16 individual patients who all receive 3 different treatments and each treatment has 3 independent measurements taken at the same time. Here is the code I have so far:
data = readtable('Data.xlsx') ;
% Removing the 'Patients' column
t = data(:,2:end) ;
% Within design
WithinDesign = table((1:3)','VariableNames',{'Measurements'}) ;
% Repeated measures model
rm = fitrm(t,'AA-FE~Treatment','WithinDesign',WithinDesign) ;
% Sphericity test
rm.mauchly
% Anova
ranova(rm)
I understand that the data does not pass the sphericity test caclulated by rm.mauchly, but I would still like to know whether or not my ANOVA set-up represents what I wanted to acquire from ANOVA since I would like to do this in the future.
3 Comments
Scott MacKenzie
on 25 Jul 2022
@Jacob Jacobo, thanks for the clarification. I just posted an answer. Good luck.
Accepted Answer
Scott MacKenzie
on 25 Jul 2022
Edited: Scott MacKenzie
on 25 Jul 2022
@Jacob Jacobo, your setup for fitrm is slightly wrong, since you only have a single within-subjects factor. Below is what I put together for the AA dependent variable. The effect of treatment on AA was statistically significant, F(2,30) = 31.3, p < .0001. All six of the pairwise comparisons are also significant. You'll get similar results for the IE and FE dependent variables.
M = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1073380/Data.xlsx');
% extract and reorganize data for the AA dependent variable (1 row per subject, 1 column per treatment)
AA = reshape(M(:,3), [], 3)
% put the AA data into a table
T = array2table(AA, 'VariableNames', {'T1', 'T2', 'T3'});
withinDesign = table([1 2 3]', 'VariableNames', {'Treatment'});
withinDesign.Treatment = categorical(withinDesign.Treatment);
rm = fitrm(T, 'T1-T3 ~ 1', 'WithinDesign', withinDesign);
AT = ranova(rm, 'WithinModel', 'Treatment');
% output a conventional ANOVA table
disp(anovaTable(AT, 'AA'));
% do the pairwise comparisons (3 treatments, therefore 6 comparisons)
multcompare(rm, 'Treatment')
% -------------------------------------------------------------------------
% Function to create a conventional ANOVA table from the overly-complicated
% and confusing anova table created by the ranova function.
function [s] = anovaTable(AT, dvName)
c = table2cell(AT);
% remove erroneous entries in F and p columns
for i=1:size(c,1)
if c{i,4} == 1
c(i,4) = {''};
end
if c{i,5} == .5
c(i,5) = {''};
end
end
% use conventional labels in Effect column
effect = AT.Properties.RowNames;
for i=1:length(effect)
tmp = effect{i};
tmp = erase(tmp, '(Intercept):');
tmp = strrep(tmp, 'Error', 'Participant');
effect(i) = {tmp};
end
% determine the required width of the table
fieldWidth1 = max(cellfun('length', effect)); % width of Effect column
fieldWidth2 = 57; % width for df, SS, MS, F, and p columns
barDouble = repmat('=', 1, fieldWidth1 + fieldWidth2);
barSingle = repmat('-', 1, fieldWidth1 + fieldWidth2);
% re-organize the data
c = c(2:end,[2 1 3 4 5]);
c = [num2cell(repmat(fieldWidth1, size(c,1), 1)), effect(2:end), c]';
% create the ANOVA table
s = sprintf('ANOVA table for %s\n', dvName);
s = [s sprintf('%s\n', barDouble)];
s = [s sprintf('%-*s %4s %11s %14s %9s %9s\n', fieldWidth1, 'Effect', 'df', 'SS', 'MS', 'F', 'p')];
s = [s sprintf('%s\n', barSingle)];
s = [s sprintf('%-*s %4d %14.5f %14.5f %10.3f %10.4f\n', c{:})];
s = [s sprintf('%s\n', barDouble)];
end
2 Comments
More Answers (0)
See Also
Categories
Find more on Repeated Measures and MANOVA in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!