Within Subjects Repeated Measures ANOVA - fitting a model

23 views (last 30 days)
Hi, I'm having trouble fitting a model; I keep getting an error and I don't know why.
I have 2 groups of 20 subjects each, and I have a 'pre' and a 'post' measurement for all. These are stored in 4 vectors: GroupA_pre, GroupA_post, GroupB_pre, GroupB_post. Each is a 20 x 1 of type 'double'.
I want to run a within-subjects repeated measures ANOVA to get significance of Time (within subjects: pre vs post) and of Group (between subjects).
So far I have the following code, but I get an error when I fitrm that says "The model formula contains names not recognized as predictor or response names."
I tried to instead specify the model as pre_controls,pre_meditators,post_controls,post_meditators~1. The error disappears but the results I get are incorrect (significance levels way too high, I presume because it's not fitting the between-subjects component. Any help much appreciated! : )
% Construct datatable with the 4 clusters of data and name the variables.
datatable = table(GroupA_pre, GroupA_post, GroupB_pre, GroupB_post);
datatable.Properties.VariableNames = {'pre_controls','pre_meditators','post_controls','post_meditators'};
% Table to indicate the levels on each factor for each of the different variables.
WithinStructure = table([1 1 2 2]',[1 2 1 2]','VariableNames',{'Time','Group'});
% Fit rm model
rm = fitrm(datatable, 'pre_controls,pre_meditators,post_controls,post_meditators~Group*Time','WithinDesign',WithinStructure);
% Specify the repeated-measures factors again when you call ranova:
ranovatable = ranova(rm,'WithinModel','Time*Group')
rm.Coefficients
rm.Covariance
I have also tried running it this other way, which does output results that are close to what SPSS says, but not exactly the same. I'd like to know why the top code does not work and if possible what is the difference between the bottom and the model SPSS uses.
% Create Group var of size = #subjects, control = 0, medit = 1
A = [0];
X = num_precontrols;
C = repmat(A, X, 1);
A = [1];
X = num_premeditators;
M = repmat(A, X, 1);
clear A;
clear X;
% define data
dataTable = array2table([GroupA_pre,GroupA_post,C;GroupB_pre,GroupB_post,M]);
% convert to table format
dataTable.Properties.VariableNames = {'Pre','Post','Group'};
% convert group to categorical
dataTable.Group = categorical(dataTable.Group);
% define levels of within subject factor
wsVariable = table([0 1]','VariableNames',{'Treatment'});
% run RANOVA (within subjects)
rm = fitrm(dataTable,'Pre,Post~Group','WithinDesign',wsVariable)
ranovatbl = ranova(rm)
% run ANOVA (between subjects)
anovatbl = anova(rm)
Thank you!!

Answers (1)

Jeff Miller
Jeff Miller on 7 Sep 2020
The first data table definitely isn't in the format that fitrm expects. Each row of the table should be a single subject from one group or the other.
Your second try is close, but I think you need to include the treatment factor, something like this:
rm = fitrm(dataTable,'Pre,Post~Group*Treatment','WithinDesign',wsVariable)
All of the dfs for this design should be 1 or 38 (19 df for subjects in each group * 2 groups).
  1 Comment
Alejandro Jinich
Alejandro Jinich on 9 Sep 2020
Adding treatment factor there gives me an error message.
I found that my second try works for within-subjects effects, and then to get between-subjects I just add:
anovatbl = anova(rm);
-Thank you! This was helpful :)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!