how to calculate three way repeated anova?

27 views (last 30 days)
how to perform 3 way repeated anova in Matlab? I have two levels in stress , 2 levels in performers , and 2 levels in feedback as reward / penlaty. Stress and performers are between subject factor and the feedback is within subject factor. I want to apply three way repeated anova ? The data is attached.
  1 Comment
Ayush
Ayush on 1 Mar 2023
To perform a three-way repeated measures ANOVA in Matlab with stress and performers as between-subject factors and feedback as the within-subject factor, you can use the "fitrm" and "ranova" functions from the Statistics and Machine Learning Toolbox.
You can read more about them from the following links:

Sign in to comment.

Answers (1)

Scott MacKenzie
Scott MacKenzie on 1 May 2023
Edited: Scott MacKenzie on 1 May 2023
It seems you have a design with three independent variables. Two are between-subjects and one is within-subjects:
  • Stress (between-subjects)
  • Performance (between-subjects)
  • Feedback (within-subjects)
Below is a script for a three-way ANOVA for your data.
Personally, I don't like the table created by MATLAB's ranova function, so I'm also including a function that creates a more conventional ANOVA output table from the ranova output. The dependent variable is just named "DV" in the table since you didn't name the variable in your question.
% load your data into data table
load(websave('data', 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1305605/data.mat'))
data.Properties.VariableNames = {'Stress', 'Performance', 'Reward', 'Penalty'};
% setup and do the three-way ANOVA
withinDesign = table([1 2]','VariableNames',{'Feedback'});
withinDesign.Feedback = categorical(withinDesign.Feedback);
rm = fitrm(data,'Reward-Penalty ~ Stress*Performance','WithinDesign',withinDesign);
AT = ranova(rm, 'WithinModel', 'Feedback');
% output a conventional ANOVA table from ranova output
disp(anovaTable(AT, 'DV'));
ANOVA table for DV ==================================================================================== Effect df SS MS F p ------------------------------------------------------------------------------------ Stress 1 5.79207 5.79207 0.657 0.4199 Performance 1 5.53506 5.53506 0.628 0.4304 Stress:Performance 1 5.91156 5.91156 0.671 0.4152 Participant 82 722.72762 8.81375 Feedback 1 0.17949 0.17949 0.175 0.6770 Stress:Feedback 1 0.12170 0.12170 0.119 0.7315 Performance:Feedback 1 0.00098 0.00098 0.001 0.9754 Stress:Performance:Feedback 1 0.07972 0.07972 0.078 0.7812 Participant(Feedback) 82 84.21282 1.02699 ====================================================================================
% -------------------------------------------------------------------------
% function to create a conventional ANOVA table from the overly-complicated
% 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

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!