How to use ranova?

73 views (last 30 days)
Tyler Tuttle
Tyler Tuttle on 28 Apr 2021
Edited: Scott MacKenzie on 28 Jul 2021
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.
close all
sub=table([1 2 3]','VariableNames',{'Condition'});
subject1=['1'; '2'; '3'; '4'; '5'];
rm1=fitrm(t1,'A-C ~ subject','WithinDesign',sub);
subject2=['1'; '1'; '1'; '1'; '1'];
rm2=fitrm(t2,'A-C ~ subject','WithinDesign',sub);

Answers (1)

Scott MacKenzie
Scott MacKenzie on 28 Jul 2021
Edited: Scott MacKenzie on 28 Jul 2021
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) = {''};
if c{i,5} == .5
c(i,5) = {''};
% 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};
% 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];





Community Treasure Hunt

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

Start Hunting!

Translated by