Repeated measure ANOVA in MATLAB
2 views (last 30 days)
Show older comments
Sarfaraz Ahmed
on 16 Jan 2022
Commented: Sarfaraz Ahmed
on 24 Jan 2022
Hi Everyone,
I am trying to do ANOVA statistical analysis on the attached data set. This is just example data set to know how to apply anova on the given data. There are 4 cases (t0 ~ t4) and in each case we do 1 observation for each subject using two methods.
Can anyone help please how to generate the below statistical results on the given data sets from MATLAB ?
1) Boxplot with 95% confidance interval (as attached snap) .
2) Sphercity test and histrograph for each method.
3) P values fo each case between method 1 and method 2.
it would be great if someone can help on the script code of such statistical analysis.
Thanks in the anticipation.
0 Comments
Accepted Answer
Scott MacKenzie
on 24 Jan 2022
What you have is a 2 x 4 within-subjects design. The independent variables are "Method" (2 levels) and "Case" (4 levels). You didn't provide a name for the dependent variable. Let's call it "Measurement".
To simplify the analysis, I reorganized your data (see attached), positioning the measurements for each participant on the same row.
The script below performs the anova usning MATLAB's ranova function and produces a conventional ANOVA table using a custom function, given at the end.
Results: There is a significant effect of Method on Measurement (F(1,7) = 33.58, p = .0007). There is also a significant effect of Case on Measurement (F(3,21) = 39.44, p < .0001). However, the Method x Case interaction effect on Measurement is not significant (F(3,21) = 1.54, p = .233).
For the other parts to your question, visit the examples provided in the documention for MATLAB's boxplot, boxchart, mauchly (for a spericity test), and histogram functions.
T = readtable('data2.xlsx', 'range', 'B3');
T.Properties.VariableNames = {'v1', 'v2', 'v3', 'v4', 'v5', 'v6', 'v7', 'v8'};
withinDesign = table([1 1 1 1 2 2 2 2]',[1 2 3 4 1 2 3 4]','VariableNames',{'Method','Case'});
withinDesign.Method = categorical(withinDesign.Method);
withinDesign.Case = categorical(withinDesign.Case);
rm = fitrm(T, 'v1-v8 ~ 1', 'WithinDesign', withinDesign);
%mauchly(rm)
AT = ranova(rm, 'WithinModel', 'Method*Case');
disp(anovaTable(AT, 'Measurement'));
% -------------------------------------------------------------------------
% 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
More Answers (0)
See Also
Categories
Find more on Analysis of Variance and Covariance in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!