How to do ANOVA repeated measures?

10 次查看(过去 30 天)
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 个评论
Scott MacKenzie
Scott MacKenzie 2023-5-1
编辑:Scott MacKenzie 2023-5-1
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.

请先登录,再进行评论。

采纳的回答

Scott MacKenzie
Scott MacKenzie 2023-5-1
编辑:Scott MacKenzie 2023-5-1
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 个评论
Scott MacKenzie
Scott MacKenzie 2023-5-3
You're welcome. Good luck with your research.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Repeated Measures and MANOVA 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by