MATLAB Answers

Matlab repeated measures anova - different result than in other packages

13 views (last 30 days)
Rose Potter
Rose Potter on 20 Jun 2019
Commented: Jingwei Sun on 7 Jul 2019
Hello everyone,
I have a problem with repeated measures anovas. Is it possible that Matlab uses a different model unlike other packages and programs? I got a very unlikely result in the anova and then ran it again with SPSS and R which both yielded the same result (and not the same as Matlab). I don't see any mistake in my Matlab code and get no error message, just this really unlikely and apparently wrong result. Has anyone else come across this problem or is there a mistake in my code after all which I just did not spot? Thanks in advance!


Sign in to comment.

Answers (1)

Jeff Miller
Jeff Miller on 21 Jun 2019
Hi again Rose,
After playing around with this some more, I think the secret is to tell MATLAB that any numerically-coded categories, like this:
% How to reproduce an SPSS mixed ANOVA in MATLAB.
% Here is a toy data set with 6 groups defined by 2 Gender (m/f) x 3 Age (2 people per group).
% Each person is measured 6 times defined by 3 Seasons x 2 Time points.
data = readtable('Sample2x3x3x2.txt'); % File attached
Season = [1,1,2,2,3,3]';
Time = [1,2,1,2,1,2]';
WithinFactors = table(Season,Time);
% CRITICAL: You must indicate that Age, Season, and Time are to be treated
% as categorical indicators rather than as numerical (i.e., regression-type) predictors.
% MATLAB assumes the latter by default.
data.Age = categorical(data.Age);
WithinFactors.Season = categorical(WithinFactors.Season);
WithinFactors.Time = categorical(WithinFactors.Time);
% Run the ANOVA
rm = fitrm(data, 'Seas1Time1-Seas3Time2~Gender*Age','WithinDesign',WithinFactors,'WithinModel','Season*Time');
ranovatable = ranova(rm,'WithinModel','Season*Time');

Translated by