Clear Filters
Clear Filters

Struggles with Two way Anova

11 views (last 30 days)
I am currently trying to get the two way analysis of this set of data, but am unable to confirm if I am formatting correctly as I am not getting the same values in a different program.
PrPoSwingLFAL = [readAllLFPreAL(:,2), readAllLFPostAL(:,2)];
SW1Lin = PrPoSwingLFAL(:);
PrPoSwingLFVE = [readAllLFPreVE(:,2), readAllLFPostVE(:,2)];
SW2Lin = PrPoSwingLFVE(:);
SWall = [SW1Lin, SW2Lin]
ANo = anova2(SWall,2)
0.0800 0.0920
0.0750 0.1010
0.1100 0.1100
0.0870 0.0950
0.1040 0.0930
0.0680 0.0790
0.0830 0.0990
0.1030 0.0960
0.1130 0.1000
0.1010 0.0850
The above stuff was to pull the data from excel and use it here. This is what SWall gives. The first 5 in each column are pre testing and the last 5 of each column are post test. Each subject was done twice in the two groups so there is a pre and post for each subject in the columns. 5 subjects in each column if that makes sense. I just want to know if that would be the correct way to do this comparison when looking at the time and groups, or if I formatted it incorrectly. Any help is appreciated.
  2 Comments
Scott MacKenzie
Scott MacKenzie on 29 Jun 2022
Edited: Scott MacKenzie on 29 Jun 2022
I have a few questions about your data.
What does each data value measure?
Rows 1-5 are the pre-test on 5 subjects and rows 6-10 are the post-test on the same 5 subjects. Is that correct? I get that from your description -- just want to confirm. Time (pre vs. post) is the first factor in your design. It is a within-subjects factor.
What is the difference between the data in the two columns?
Are the columns for different subjects (i.e., 10 subjects total) or the same subjects (i.e., 5 subjects total)? (This is the second factor in your design, but it's not clear whether it is within-subjects or between-subjects.)
Jared Kimble
Jared Kimble on 29 Jun 2022
Sorry I should have been more clear. Each data value is a time result in seconds. Yes, 1-5 are pre and 6-10 are post and the same subjects. The two columns are different groups so it would be 10 subjects total. The difference between the columns is a difference in the type of strain.

Sign in to comment.

Accepted Answer

Scott MacKenzie
Scott MacKenzie on 30 Jun 2022
OK, thanks for the clarification. It seems you have a 2 x 2 mixed design with 10 subjects. The factors are Test Sequence with two levels (Pre, Post) and Strain Group with two levels (S1, S2). Test Sequence is within-subjects and Strain Group is between-subjects (with 5 subjects per group).
Note that anova2 assumes that both factors are between-subjects. This might be the source of the discrepancy you are seeing. So, to do the analysis in MATLAB you need to use a different anova function. There are at least five such functions, so deciding which to use is a challenge. My preference is ranova which can handle any combination of within- and between-subjects factors. See below for the code with comments.
There is a custon function at the bottom to create a conventional anova table from the behemouth produced by ranova. The table should be similar to the table generated by your "different program".
M = readmatrix('testdata.txt') % your data, as in question (10x2)
M = 10×2
0.0800 0.0920 0.0750 0.1010 0.1100 0.1100 0.0870 0.0950 0.1040 0.0930 0.0680 0.0790 0.0830 0.0990 0.1030 0.0960 0.1130 0.1000 0.1010 0.0850
% reorganize data: one row for each of 10 subjects
pre = [M(1:5,1); M(1:5,2)];
post = [M(6:10,1); M(6:10,2)];
M = [pre, post];
% put data in a table and add a column for group codes
T = array2table(M);
T.Strain = [repmat("S1", 5, 1); repmat("S2", 5, 1)];
T.Properties.VariableNames = { 'Pre', 'Post', 'Strain_group'};
T
T = 10×3 table
Pre Post Strain_group _____ _____ ____________ 0.08 0.068 "S1" 0.075 0.083 "S1" 0.11 0.103 "S1" 0.087 0.113 "S1" 0.104 0.101 "S1" 0.092 0.079 "S2" 0.101 0.099 "S2" 0.11 0.096 "S2" 0.095 0.1 "S2" 0.093 0.085 "S2"
% do the analyis of variance (see ranova documentation for details and examples)
withinDesign = table([1 2]','VariableNames',{'Test_sequence'});
withinDesign.Test_sequence = categorical(withinDesign.Test_sequence);
rm = fitrm(T,'Pre-Post ~ Strain_group', 'WithinDesign', withinDesign);
AT = ranova(rm,'WithinModel','Test_sequence');
% create and output conventional anova table
disp(anovaTable(AT, 'Time (s)'));
ANOVA table for Time (s) =================================================================================== Effect df SS MS F p ----------------------------------------------------------------------------------- Strain_group 1 0.00003 0.00003 0.123 0.7347 Participant 8 0.00220 0.00027 Test_sequence 1 0.00002 0.00002 0.274 0.6146 Strain_group:Test_sequence 1 0.00010 0.00010 1.328 0.2825 Participant(Test_sequence) 8 0.00058 0.00007 ===================================================================================
% -------------------------------------------------------------------------
% 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
  2 Comments
Adam Danz
Adam Danz on 30 Jun 2022
Always glad to see your responses to stats questions, @Scott MacKenzie!
Scott MacKenzie
Scott MacKenzie on 1 Jul 2022
@Adam Danz, thanks for the kinds words. Hopefully, my answer is helpful to @Jared Kimble and others.

Sign in to comment.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!