How to perform a repeated measures three-way ANOVA
12 views (last 30 days)
Show older comments
Hi all,
I am trying to perform a repeated measures three-way ANOVA to explore the interaction between three independent variables on a dependent variable.
The database consists of 5 columns: Duration (dependent variable), Size (independent variable), Distance (independent variable), Filters (independent variable) and SubjectID (subject identification number). Attached you can see the database: 'Data.xlsx'
Basically, 17 subjects participated in the experiment (1 to 18; subject 10 was excluded due to exclusion criteria).
Each subject had to perform a task under 2 size conditions, 2 distance conditions, and 6 filter conditions. The experiment consisted of measuring task duration under the different conditions. In total, each subject performed 72 trials, which meant 3 trials for each combination of conditions (e.g. subject 1 performed 3 times the combination: Size (1), Distance (50), Filter (0.6)).
So my goal is to perform a repeated measures three-way ANOVA to explore the interaction between the independent variables (Size, Distance, Filter) on the dependent variable (Duration), however, I am not sure how to make the 'within subject factor' table to subsequently perform the repeated measures model (under fitrm) and finally perform ranova.
Any help would be appreciated. I would also appreciate help in the interpretation of the ranova table.
Thanks in advance!
Here the code:
%% Read Data
% Duration = dependent variable
% Size, Distance, Filter = independent variables
% SubjectID = subject identification number
Table = readtable('Data.xlsx');
%% Organize dependent variable
Duration = array2table(reshape(Table.Duration, 51, []));
%% Column names
varNames = {'V1','V2','V3','V4','V5','V6','V7','V8','V9','V10','V11','V12',...
'V13','V14','V15','V16','V17','V18','V19','V20','V21','V22','V23','V24'};
Duration.Properties.VariableNames = varNames;
%% Create within table (within subject factors)
%% Create repeated measure model
%% Apply Ranova
0 Comments
Answers (1)
Elle
on 18 Nov 2022
Edited: Elle
on 18 Nov 2022
I agree that the r-by-k WithinDesign matrix description is confusing and I'm bringing it up with my team.
For now I'm suggesting the following solution because I understand that you're wanting to test the statistical significance of the independent variables Size, Distance, and Filter on the dependent variable Duration.
First, load the data and sort the rows.
tbl = readtable('Data.xlsx');
tbl = sortrows(tbl,[2,3,4]); % The array input argument contains the indices of the independent variables
Now let's reformat the table so that each of the repeated measurements is in its own column. That is to say, let's make a table with columns Size, Distance, Filter, SubjectID, Duration1, Duration2, Duration3, where each of the duration variables corresponds to a trial. One missing variable from your data set is the trial number, so I'm going to take the creative liberty of assigning trial numbers based on the order that the rows appear when we sorted Size, Distance, and Filter from smallest to biggest.
formattedtbl = table();
for i=1:3:1224 % The way the rows in tbl were ordered meant that the repeated measures appeared in groups of three
independent = tbl(i,["Size","Distance","Filter","SubjectID"]);
dependent = tbl(i:i+2,"Duration");
dependent = rows2vars(dependent);
newrow = [independent,dependent];
formattedtbl = [formattedtbl;newrow];
end
I doubt I did the above in the most elegant way possible because formattedtbl now has an extra variable OriginalVariableNames which I'll drop while I'm renaming the dependent variables.
formattedtbl.OriginalVariableNames = []; % drop the unwanted variable
formattedtbl = renamevars(formattedtbl,["Var1","Var2","Var3"],["Duration1","Duration2","Duration3"])
Duration1, Duration2, and Duration3 contain the data for the repeated measurements. The input table is now in the right format.
Finally, the moment we've all been waiting for. Let's give each repeated measure a fun name and fit the model.
WithinDesignMatrix = table(["Trial1" "Trial2" "Trial3"]',VariableNames="Trial");
rmdl = fitrm(formattedtbl,"Duration1-Duration3 ~ Size+Distance+Filter",WithinDesign=WithinDesignMatrix)
You actually don't need the WithinDesign vector because it's actually the modelspec that tells fitrm what the dependent and independent variables are. Take a look at the Wilkinson Notation page to understand the notation I'm using.
Finally, we can perform an RANOVA.
ranova(rmdl)
I hope this helps!
1 Comment
Jeff Miller
on 19 Nov 2022
Hmmm...I would interpret the OP's description of the design as calling for a somewhat different analysis than that shown by @Elle.
For example, I would expect lots of different error terms, such as subjects*Distance, subjects*Filter, subject*Filter*Distance, etc--in fact, a different error term for each of the experimental sources.
For what it is worth, here is the ANOVA I would compute:
% Excerpt from the output file Within3Fac.fpr:
% ANOVA For Duration
% Source df MS dfe MSe F pEta^2 P G-G P
% Mean 1 441.08 16 0.27812 1585.9 0.990 0.000 ***
% Filter 5 0.02350 80 0.00745 3.1540 0.165 0.012 0.024 **
% Distance 1 10.218 16 0.05463 187.04 0.921 0.000 ***
% FD 5 0.00935 80 0.00388 2.4103 0.131 0.043 0.059 **
% Size 1 0.01894 16 0.01268 1.4938 0.085 0.239
% FS 5 0.00798 80 0.00402 1.9886 0.111 0.089 0.115 *
% DS 1 0.01986 16 0.00937 2.1189 0.117 0.165
% FDS 5 0.00568 80 0.00378 1.5028 0.086 0.198 0.218
% The above ANOVA table was computed using the following commands,
% which rely on some (so far) Windows-only code at
% https://github.com/milleratotago/RawRT
Trials = readtable('Within3FacData.xlsx');
Dependent = 'Duration';
% Names of table variables specifying levels of within-Ss factors
BetweenFacs = {}; % there are no between-Ss factors
WithinFacs = {'Size','Distance','Filter'};
outFileName = 'Within3Fac';
CallMrf(Trials,Dependent,BetweenFacs,WithinFacs,'SubjectID',outFileName);
This ANOVA only considers the average duration (across the 3 trials) for each subject in each of the 24 conditions, which would be appropriate as I understood the OP's description. My analysis is based on the premise that the different subjects are a random sample from a larger population, with the research goal being to generalize from these subjects to the overall population averages.
See Also
Categories
Find more on Analysis of Variance and Covariance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!