Can I do a repeated-measures three-way ANOVA with 2 within and one between variable?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Is it possible to perform a three-way ANOVA with repeated-mearues with 2 within variables and 1 between variable?
The two within variables are time (I have measurements from different days) and frequency (I used different frequencies in the measurements). These are repeated-measures. The between variabel is the group allocations (treatment or no treatment).
If so how would I design the table to fit the repeated-measures model to be able to then use the ranova function. Do I use one column or one row for each subject.
Accepted Answer
Scott MacKenzie
on 15 Jun 2021
Edited: Scott MacKenzie
on 15 Jun 2021
Yes. Here's an example script I used recently for such a design. The within-subjects factors are Layout (2 levels) and Trial (5 levels). The between-subjects factor was Group (2 levels). I'm attaching the data file as well.
T = readtable('softkeyboard-h12x10b.txt');
T.Properties.VariableNames = {'o1','o2','o3','o4','o5','q1','q2','q3','q4','q5', 'Group'};
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(T,'o1-q5 ~ Group','WithinDesign',withinDesign);
AT = ranova(rm,'WithinModel','Layout*Trial');
4 Comments
Judith Evers
on 16 Jun 2021
Thank you very much, this got me started
You're welcome. The trick, of course, is to get the input data in the correct format. The header lines in my example are not needed. What you need for the input data is a matrix with n rows (one per participant) and m columns where m = n1 x n2 + 1. For the example, n1 = 5 (number of level of 1st within-subjects factor), n2 = 2 (number of levels of 2nd within-subjects factor) and +1 for an additional column containing the group codes for the between-subjects factor. So, there are 11 columns of data. The nesting of the within-subjects data is important. The data for the 2nd within-subjects factor must be nested inside the data for the 1st within-subjects factor.
BTW, the table gerated by ranova is pretty-well an undecipherable mess (in my view). Here's something you might find helpful. I wrote a function that takes as input the output of ranova (AT, in the code in my solution) and generates a more conventional Anova table. To you use this, put the following statement just after the ranova line in my answer:
disp(anovaTable(AT));
and that copy and paste the function code below into the end of your script (or put it in a file called anovaTable.m in your MATLAB workspace):
function [s] = anovaTable(AT)
c = table2cell(AT);
% The code below replaces the blank(?) entries in the F and p columns
% with empty character strings. Not sure why, but the blank entries are
% actually 1 (F) or 0.05 (p).
%
% The code below can probably be simplified, but I'm not sure exactly how.
%
% An assumption here is that measurements are made on human subjects
% ("Participant"). If the measurements are on some other entity, such as
% "flowers" in the example in the ranova documentation, then Participant,
% below, should be changed accordingly.
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
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 Effects column
fieldWidth = max(cellfun('length', effect));
barDouble = sprintf('%s\n', repmat('=', 1, fieldWidth + 57));
barSingle = sprintf('%s\n', repmat('-', 1, fieldWidth + 57));
% re-organize the data
c = c(2:end,[2 1 3 4 5]);
c = [num2cell(repmat(fieldWidth, size(c,1), 1)), effect(2:end), c]';
% create the ANOVA table
s = barDouble;
s = [s sprintf('%-*s %4s %10s %14s %10s %10s\n', fieldWidth, 'Effect', 'df', 'SS', 'MS', 'F', 'p')];
s = [s barSingle];
s = [s, sprintf('%-*s %4d %14.3f %14.3f %10.3f %10.4f\n', c{:})];
s = [s, barDouble];
end
With this, the following table is generated for the example data set:
==================================================================================
Effect df SS MS F p
----------------------------------------------------------------------------------
Group 1 3.583 3.583 0.037 0.8523
Participant 10 981.236 98.124
Layout 1 6062.238 6062.238 797.018 0.0000
Group:Layout 1 1.903 1.903 0.250 0.6278
Participant(Layout) 10 76.062 7.606
Trial 4 663.696 165.924 50.663 0.0000
Group:Trial 4 3.644 0.911 0.278 0.8904
Participant(Trial) 40 131.003 3.275
Layout:Trial 4 22.466 5.617 2.707 0.0437
Group:Layout:Trial 4 6.856 1.714 0.826 0.5165
Participant(Layout:Trial) 40 83.003 2.075
==================================================================================
This is a typiclal Anova table for this design, and is much more readable (in my view) than the ouput from MATLAB's ranova function. If you have any questions, let me know. Good luck
Hi,
First of all, thank you very much for your answers, it helped me a lot already. However, I am getting stuck at some point.
I have a similar design as ms. Evers, where I have two within variables and none between. I tested the brain activity of mice in different hypoxialevels, and want to compare the correlation between seeds within the same cluster and between different brainclusters. So my "within" variables are the hypoxia levels that I tested the mice in (HLevel) and whether or not the seedpairs were within the same area (Area).
I was able to follow all the steps and everything works well. However, I saw online that one of the assumptions that needs to be met when doing an rm anova is sphericity, and that that can be checked with the mauchly function in matlab. The sphericity in my case does not hold, so I have to do an epsilon correction. However, I cannot give the epsilon correction the interaction between my "within" variables, and am also unsure about the answers it gives. Does the epsilon function do the ranova already? Should I give the ranova function my epsilon values?
Here is my code:
conditions = table(reshape(repmat({'with', 'betw'}, [5 1]), 10, 1),...
reshape([HypoxiaLevels HypoxiaLevels], 10,1),'VariableNames',{'Area','HLevel'});
conditions.Area = categorical(conditions.Area);
conditions.HLevels = categorical(conditions.HLevel);
%we have no between factors, so ~1. Two within factors: 1.within/between
%areas and 2. hypoxia level.
rm = fitrm(wb, 'w1-b5~1', 'WithinDesign', conditions);
mauchlytbl = mauchly(rm); %sphericity does not hold, do epsilon correction
epsilontbl = epsilon(rm);
AT = ranova(rm, 'WithinModel','Area*HLevel');
I also attached the .mat file of my data.
Thanks in advance
Brennan
on 25 Jan 2024
Hello,
I have an extremely similar dataset with 1 between subjects factor (group) and 2 within subjects factor (5 different variables collected at timepoint1 and timepoint2). I have set it up exactly as you have above and my result table indicates a significant p-value for Group:Timepoint:Variable. However, I am now interested in finding which of the 5 variables show significant difference by the group and visit interaction. Such as:
T = multcompare(rm,'Variable','By','Group','By','Timepoint');
or
T = multcompare(rm,'Variable','By','Group*Timepoint');
Neither of which works. Any help on this last step would be appreciated. Thanks!
More Answers (0)
Categories
Find more on Repeated Measures and MANOVA in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)