How to do ANOVA repeated measures?

I have 12 Female and 11 Male rats which participate in a sucrose tracking experiment for 4 different levels of sucrose concentration. I have approach rate data (between 0 and 1) for each animal for 4 different concentration. I have attached the figure. I want to do ANOVA repeated measures for the experiment to check the effect of concentration as well as gender. I wrote this following code. But I'm not sure if this is correct or how to interpret the result.
dataForFemale = loadFile.featureForEachSubjectId{1};
dataForMale = loadFile.featureForEachSubjectId{2};
for i = 1:4
dataMatrix(:,i) = [dataForFemale{i} dataForMale{i}]';
end
Gender = [repmat({'F'},1,length(dataForFemale{1})),repmat({'M'},1,length(dataForMale{1}))]';
t = [Gender, array2table(dataMatrix)];
t.Properties.VariableNames = {'Gender','c1','c2','c3','c4'};
rm = fitrm(t, 'c1-c4 ~ Gender', 'WithinDesign', table([1, 2, 3, 4]','VariableNames', {'Concentration'}));
result = ranova(rm);
multcompare(rm, 'Concentration', 'By', 'Gender');

3 commentaires

Scott MacKenzie
Scott MacKenzie le 30 Avr 2023
Modifié(e) : Scott MacKenzie le 30 Avr 2023
Your ANOVA code is correct.
Bear in mind that you have a mixed design with one within-subjects factor (concentration with four levels) and one between-subjects factor (gender with two levels). The "repeated-measures" aspect of the ANOVA only pertains to concentration.
Atanu
Atanu le 1 Mai 2023
Thanks for your response!
Scott MacKenzie
Scott MacKenzie le 1 Mai 2023
Modifié(e) : Scott MacKenzie le 1 Mai 2023
You're welcome. I just had another look and notice that you omitted the within-subjects IV (sucrose concentration) in the ranova function. I've added it, rearranged the code slightly, and posted an answer. Hope this helps.

Connectez-vous pour commenter.

 Réponse acceptée

Scott MacKenzie
Scott MacKenzie le 1 Mai 2023
Modifié(e) : Scott MacKenzie le 1 Mai 2023
Here's a script for the ANOVA that includes both the within-subjects IV (sucrose concentration and the between-subjects IV (gender).
As you can see, the effect of gender on approach_avoid was not statistically significant (F1,21 = 3.83, p > .05), but the effect of sucrose_concentration on approach_avoid was (F3,63 = 118.5, p < .001). If you examine m (the output from multcompare), you'll see which pairwise differences by gender were statistically significant. There were lots!
Note: I don't like the ANOVA table generated by ranova. The script below below includes a function to create a more conventional ANOVA table from the ranova output. Of course, for your research, "Participants" is "Rats".
load(websave('tmp', 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1370074/BLsexDiffApproachavoid.mat'));
dataForFemale = featureForEachSubjectId{1};
dataForMale = featureForEachSubjectId{2};
for i = 1:4
dataMatrix(:,i) = [dataForFemale{i} dataForMale{i}]';
end
Gender = [repmat({'F'},1,length(dataForFemale{1})),repmat({'M'},1,length(dataForMale{1}))]';
t = [Gender, array2table(dataMatrix)];
t.Properties.VariableNames = {'Gender','c1','c2','c3','c4'};
% setup and do the ANOVA
withinDesign = table([1 2 3 4]', 'VariableNames', {'Sucrose_Concentration'});
withinDesign.Sucrose_Concentration = categorical(withinDesign.Sucrose_Concentration);
rm = fitrm(t,'c1-c4 ~ Gender', 'WithinDesign', withinDesign);
result = ranova(rm, 'WithinModel', 'Sucrose_Concentration');
% do post hoc multiple comparisons (check m for significant pairwise differences)
m = multcompare(rm, 'Sucrose_Concentration', 'By', 'Gender');
% output a conventional ANOVA table
disp(anovaTable(result, 'Approach_Avoid'));
ANOVA table for Approach_Avoid =========================================================================================== Effect df SS MS F p ------------------------------------------------------------------------------------------- Gender 1 0.07668 0.07668 3.827 0.0639 Participant 21 0.42081 0.02004 Sucrose_Concentration 3 7.05085 2.35028 118.527 0.0000 Gender:Sucrose_Concentration 3 0.13201 0.04400 2.219 0.0946 Participant(Sucrose_Concentration) 63 1.24924 0.01983 ===========================================================================================
% -------------------------------------------------------------------------
% function to create a conventional ANOVA table from the overly-complicated
% and confusing anova table created by the ranova function.
function [s] = anovaTable(AT, dvName)
c = table2cell(AT);
% remove erroneous entries in F and p columns
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
% use conventional labels in Effect column
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 table
fieldWidth1 = max(cellfun('length', effect)); % width of Effect column
fieldWidth2 = 57; % width for df, SS, MS, F, and p columns
barDouble = repmat('=', 1, fieldWidth1 + fieldWidth2);
barSingle = repmat('-', 1, fieldWidth1 + fieldWidth2);
% re-organize the data
c = c(2:end,[2 1 3 4 5]);
c = [num2cell(repmat(fieldWidth1, size(c,1), 1)), effect(2:end), c]';
% create the ANOVA table
s = sprintf('ANOVA table for %s\n', dvName);
s = [s sprintf('%s\n', barDouble)];
s = [s sprintf('%-*s %4s %11s %14s %9s %9s\n', fieldWidth1, 'Effect', 'df', 'SS', 'MS', 'F', 'p')];
s = [s sprintf('%s\n', barSingle)];
s = [s sprintf('%-*s %4d %14.5f %14.5f %10.3f %10.4f\n', c{:})];
s = [s sprintf('%s\n', barDouble)];
end

5 commentaires

Thank you for the detailed explanation. However, for me output of ranova table is fine, cause I am not very familiar with either of them. I was wondering why it is necessary to use "WithinModel",'Concentration' in ranova like the following?
result = ranova(rm,"WithinModel",'Concentration');
It produces the following table.
Whereas,
result = ranova(rm);
produces the following result:
What does the extra first 3 rows stand for in the first table? And which p-values should I report for effect of concentration and gender? Is it the p-values on row 4 and 5 in the first table?
Scott MacKenzie
Scott MacKenzie le 2 Mai 2023
The WithinModel option with ranova is needed to include the within-subjects independent varariable in the analysis. Your design is a 2 x 4 mixed design with one between-subjects factor (gender with 2 levels) and one within-subjects factor (concentration with 4 levels). So, there are three results of interest: (1) the main effect of gender on approach_avoid, (2) the main effect of concentration on approach_avoid, and (3) the Gender x Concentration interaction effect on approach_avoid. That's why there are three F-statistics in the ANOVA table in my answer. If the WithinModel option is omitted, the analysis is only for the between-subjects factor and only one F-statistic appears.
Note that in both the tables in your comment, the ranova output table includes an extra F-statistic for the intercept. This is rarely of interest and is typically not included in the ANOVA table.
Atanu
Atanu le 3 Mai 2023
Modifié(e) : Atanu le 3 Mai 2023
Okay, I understand. Thank you very much for the explanation and code again!
Scott MacKenzie
Scott MacKenzie le 3 Mai 2023
You're welcome. Good luck with your research.
Atanu
Atanu le 3 Mai 2023
Thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by