Struggles with Two way Anova
13 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Jared Kimble
le 27 Juin 2022
Commenté : Scott MacKenzie
le 1 Juil 2022
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 commentaires
Scott MacKenzie
le 29 Juin 2022
Modifié(e) : Scott MacKenzie
le 29 Juin 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.)
Réponse acceptée
Scott MacKenzie
le 30 Juin 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)
% 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
% 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)'));
% -------------------------------------------------------------------------
% 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 commentaires
Scott MacKenzie
le 1 Juil 2022
@Adam Danz, thanks for the kinds words. Hopefully, my answer is helpful to @Jared Kimble and others.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Repeated Measures and MANOVA dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!