How to use ranova?

63 次查看(过去 30 天)
Tyler Tuttle
Tyler Tuttle 2021-4-28
I have a set of data. The data is from 5 subjects, the same measure is taken from each subject 3 times. Each measure has a different condition applied. I want to look for differences between conditions.
Because I have repeated measures from each subject, I assume I should use ranova. However, it seems like I need a variable that is not "within subject" to get ranova to work. For example, if I had the same experiemental design but male and female subjects then ranova would work. I don't want to use anova1 because to my understanding that treats every measure as a different subject. Any help would be greatly appreciated.
I have tried using ranova with all subjects as 1 groups, and each subject as a different group. Neither has worked for me.
clear,clc
close all
Measurement=[101.053914979877,39.9882799952772,1.10267662721688;216.136311110424,70.1362721192365,1.25119134782814;26.5334559925966,81.1211148269482,1.54372037819131;231.177535625968,135.886529812943,0.890020347986787;140.395359593013,37.2109788038507,0.00626713939985805];
sub=table([1 2 3]','VariableNames',{'Condition'});
subject1=['1'; '2'; '3'; '4'; '5'];
t1=table(subject1,Measurement(:,1),Measurement(:,2),Measurement(:,3),'VariableNames',{'subject','A','B','C'});
rm1=fitrm(t1,'A-C ~ subject','WithinDesign',sub);
ranovatbl1=ranova(rm1,'WithinModel','Condition');
c3=multcompare(rm1,'Condition');
subject2=['1'; '1'; '1'; '1'; '1'];
t2=table(subject2,Measurement(:,1),Measurement(:,2),Measurement(:,3),'VariableNames',{'subject','A','B','C'});
rm2=fitrm(t2,'A-C ~ subject','WithinDesign',sub);
ranovatbl2=ranova(rm2,'WithinModel','Condition');
c4=multcompare(rm2,'Condition');

回答(1 个)

Scott MacKenzie
Scott MacKenzie 2021-7-28
编辑:Scott MacKenzie 2021-7-28
You note: it seems like I need a variable that is not "within subject" to get ranova to work.
Actually, that's not the case. You can use ranova for a design that has just one within-subjects factor, as is the case with your data.
Here's what I put together for the data in your question. I used the labels "Condition" for the independent variable and "Measure" for the dependent variable. As seen in the anova table below, there is a significant effect of Condition on Measure: F(2,8) = 10.439, p < .01.
The code below includes a function I wrote that creates a more conventional anova table from the overly-complicated table produced by ranova.
% data from question (1 within-subjects factor with 3 levels, 5 participants)
M = [1 101.05 39.99 1.10;
2 216.14 70.14 1.25;
3 26.53 81.12 1.54;
4 231.18 135.89 0.89;
5 140.40 37.21 0.01];
% organize the data in a table
T = array2table(M(:,2:end));
T.Properties.VariableNames = {'A' 'B' 'C'};
% create the within-subjects design
withinDesign = table([1 2 3]','VariableNames',{'Condition'});
withinDesign.Condition = categorical(withinDesign.Condition);
% create the repeated measures model and do the anova
rm = fitrm(T,'A-C ~ 1','WithinDesign',withinDesign);
AT = ranova(rm,'WithinModel','Condition'); % remove comma to see ranova's table
% output a conventional anova table
disp(anovaTable(AT, 'Measure (units)'));
ANOVA table for Measure (units) =============================================================================== Effect df SS MS F p ------------------------------------------------------------------------------- Participant 4 15513.713 3878.428 Condition 2 50484.917 25242.459 10.439 0.0059 Participant(Condition) 8 19344.377 2418.047 ===============================================================================
% ---------------------------------------------------------------------
% Scott's 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; % field needed for df, SS, MS, F, and p columns
barDouble = sprintf('%s\n', repmat('=', 1, fieldWidth1 + fieldWidth2));
barSingle = sprintf('%s\n', 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 barDouble];
s = [s sprintf('%-*s %4s %11s %14s %9s %9s\n', fieldWidth1, '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

类别

Help CenterFile Exchange 中查找有关 Analysis of Variance and Covariance 的更多信息

标签

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by