Set up Repeated Measures Anova function MATLAB
조회 수: 7 (최근 30일)
이전 댓글 표시
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
Scott MacKenzie
2022년 7월 25일
@Jacob Jacobo, thanks for the clarification. I just posted an answer. Good luck.
채택된 답변
Scott MacKenzie
2022년 7월 25일
편집: Scott MacKenzie
2022년 7월 25일
@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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Repeated Measures and MANOVA에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!