How to make Anova table for data from a factorial experiment

I'd like to know how MATLAB can be used to create an Anova table like this
% ANOVA_table_for_Entry Speed (wpm)
% =================================================================================
% Effect df SS MS F p
% ---------------------------------------------------------------------------------
% Participant 15 1424.418 94.961
% Layout 1 103.298 103.298 12.038 0.0034
% Layout_x_Par 15 128.717 8.581
% Trial 4 87.443 21.861 6.108 0.0003
% Trial_x_Par 60 214.745 3.579
% Layout_x_Trial 4 17.638 4.409 1.003 0.4130
% Layout_x_Trial_x_Par 60 263.701 4.395
% =================================================================================
from a data set like this:
11.44 14.93 12.00 12.54 16.85 8.21 11.21 9.67 8.55 9.87
15.76 13.86 14.27 14.72 13.90 7.08 9.97 12.29 11.34 14.21
...
10.15 9.51 12.28 15.27 12.98 9.42 8.70 10.18 13.94 11.10
The data form a 16 x 10 table. The full data set is in the attached file.
About the data: The data are from a 2 x 5 within-subjects factorial experiment with 16 participants. Each value is the measured entry speed (in words per minute) for a particular participant and test condition. There are 16 rows of data, one for each participant. There are 10 columns of data, one for each test conditions, as now described. The were two independent variables: Layout with 2 levels (Opti and Metropolis) and Trial with 5 levels (T1, T2, T3, T4, T5). The trial data are nested within the layout data. In other words, the layout data are in columns 1-5 (Opti) and 6-10 (Metropolis), and within each set are the data for the five trials.
About the Anova table: The Anova table was generated from this data set, but not using MATLAB.
I'm using MATLAB more and more these days for many analyses and I'm wondering if I can extend my use of MATLAB into the realm of hypothesis testing. Hence this question. Thanks in advance for your help.

 Accepted Answer

x = readtable('mack_data.txt');
x.Properties.VariableNames = {'o1','o2','o3','o4','o5','m1','m2','m3','m4','m5'};
withinDesign = table([1 1 1 1 1 2 2 2 2 2]',[1:5 1:5]','VariableNames',{'Layout','Trial'});
withinDesign.Layout = categorical(withinDesign.Layout);
withinDesign.Trial = categorical(withinDesign.Trial);
rm = fitrm(x,'o1-m5~1','WithinDesign',withinDesign);
ranova(rm,'WithinModel','Layout*Trial')

10 Comments

Thanks Jeff. This is spot on. I now have more confidence that I can actually use MATLAB for analyses like this. Thanks again.
BTW Jeff, may I ask your opinion on a simple yes/no question: Can ranova analyse data from an experiment with a single between-subjects factor?
Yes, you can also include one or more between-Ss factors. These would be coded with one extra categorical column per factor in the data matrix x, and they are included on the right side of the model statement, e.g.
rm = fitrm(x,'o1-m5~AgeGroup','WithinDesign',withinDesign);
but then not mentioned in the ranova command (so it does not change).
Look at the "Longitudinal Data" example here.
Actually, I've already done that successfully -- add a categorical column for a between-subjects factor. And your code to analyse the data I posted here was a great help. Thanks.
But, what I'm stuck on is using ranova when there is only a between-subjects factor; i.e., no within-subjects factor. Consider a data set such as this:
25.6 LH
23.4 LH
19.4 LH
28.1 LH
25.9 LH
14.3 RH
22.0 RH
30.4 RH
21.1 RH
19.3 RH
There is just one between-subjects factor, Handeness, with 5 left-handed participants (LH) and 5 right-handed participants (RH). The Anova table should look like this:
I've fiddled with ranova, but just can't seem to get is to do this analyses. Can ranova do an analysis like this?
Sorry, Scott, I don't know the answer to your question about ranova. For a between-Ss design, though, you can use anovan (or even ttest2 for one between-Ss factor with 2 levels).
OK, thanks for considering this. I've done a single-factor between-subjects analysis with anovan, but I'm hoping for a one-stop-shopping approach -- using a single MATLAB anova function for any factorial design. It's a work in progress! Thanks again.
These lines perform a single-factor between-subject ANOVA using ranova. It gives the same results as with anova1 function.
T = table([25.6 23.4 19.4 28.1 25.9 14.3 22.0 30.4 21.1 19.3]',...
{'LH','LH','LH','LH','LH','RH','RH','RH','RH','RH'}','VariableNames',{'Var','Handeness'});
rm = fitrm(T,'Var~Handeness');
tab1 = rm.ranova('WithinModel','Time');
disp(tab1)
[p, tab2] = anova1(T.Var,T.Handeness,'off');
disp(tab2)
Thanks, Christophe. I've done this with anova1 and other tools, but not with ranova. You've solved the puzzle. Great. However, I'm scratching my head over part of your solution. In the documentation, I don't see any reference to the string literal 'Time' for the 'WithinModel' property. Where/how did you determine that this is needed?
Thanks Scott. By default, the WithinDesign property of the RepeatedMeasuresModel object is a table with a single variable named 'Time', taking integer values from 1 to the number of repeated measures. So :
rm=fitrm(T,'Var~Handeness');
is equivalent to :
rm = fitrm(T,'Var~Handeness','WithinDesign',table([1],'VariableNames',{'Time'}));
Thanks for the extra details, Christophe. I'd have never figured that out. Cheers.

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!