MATLAB Answers

ranova with two within factors

46 views (last 30 days)
Hello everyone,
I have a question about how to do a repeated measures anova in Matlab. I have data from 20 people who were tested in 4 conditions each: treatment A pre test, treatment A post test, treatment B pre test, treatment B post test. I want to use time and treatment as factors. However I only managed an anova with one factor. Here's what I got so far:
datatable = cell2table(struct2cell([pre.A, pre.B, post.A, post.B]'));
datatable.Properties.VariableNames = {'pre_A','pre_B','post_A','post_B'};
rm = fitrm(datatable, 'pre_A,pre_B,post_A,post_B~1','WithinDesign',[1:4]);
ranovatable = ranova(rm)
How do I introduce the treatment as a second within factor? I read the documentation but did not find anything I could make use of. Thank you so much in advance!


Sign in to comment.

Accepted Answer

Jeff Miller
Jeff Miller on 16 Jun 2019
datatable.Properties.VariableNames = {'pre_A','pre_B','post_A','post_B'};
% When you have more than one repeated-measures factor, you must set up a table
% to indicate the levels on each factor for each of your different variables.
% Here is the command you need for this case:
WithinStructure = table([1 1 2 2]',[1 2 1 2]','VariableNames',{'PrePost','TreatAB'});
% The 4 different rows of the WithinStructure table correspond to the 4 different
% columns, 'pre_A','pre_B','post_A','post_B', respectively, in your data table.
% Each 'pre_A','pre_B','post_A','post_B' column is coded as 1/2 on the PrePost factor
% and as 1/2 on the TreatAB factor.
% Now pass the WithinStructure table to fitrm so that it knows how the different
% columns correspond to the different levels of the repeated-measures factors.
rm = fitrm(datatable, 'pre_A,pre_B,post_A,post_B~1','WithinDesign',WithinStructure);
% Finally, you need to specify the repeated-measures factors again when you call ranova, like this:
ranovatable = ranova(rm,'WithinModel','PrePost*TreatAB');


Show 4 older comments
Jeff Miller
Jeff Miller on 19 Jun 2019
Hmmm...I also tried a numerical example and got different results. Although MATLAB produces an ANOVA table with the same structure and df's as other packages, the SS's, MS's, and F's are different, so I am really not sure what underlying model MATLAB is using. Well, maybe I've just got the specifications wrong.
As far as the ~1 notation, this is used to indicate that there are no between-subjects factors. For example, if you did have between-subjects factors like gender and treatment (with columns having those names in the original datatable), you would use a specification like 'pre_A,pre_B,post_A,post_B~Gender*Treatment'
Rose Potter
Rose Potter on 20 Jun 2019
Ok thanks a lot, now it makes sense! I also believe Matlab uses some other model or calculation because several people here tried it now and it does not seem to produce the correct results. Thanks for your help!
Florian Binder
Florian Binder on 8 Jul 2019
The differences to SPSS and R may be because the variable type in the WithinStructure is not categorical.
You have to make the variables in the WithinStructure categorical if they are not metric.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by