Running repeated anova on all elements of large matrix
3 views (last 30 days)
Show older comments
Leonardo Tozzi
on 3 Dec 2019
Answered: Leonardo Tozzi
on 4 Dec 2019
Dear Experts,
I have the following problem. I have a large (3344) stack of matrices, stacked along the 3rd dimension. Each of these matrices is symmetric (319*319). The matrices are repeated measurements of 4 conditions for 836 subjects (therefore they are 836*4=3344).
Now, I would like to run a repeated measures ANOVA for each element of these matrices to obtain a final 319*319 matrix which is the F test value for each element for the factor "condition" across subjects. I understand that an easy way of doing this would be to loop through each element of the matrix, build a table where each of the 4 conditions becomes a column and each subject is a row and then use fitrm and ranova to obtain an F value. However, this method is computationally very slow and I was wondering if someone had suggestions to speed it up (besides running it only on half of the matrix of course). The reason for this is that the resulting F matrix needs to be the input in a permutation test, so ideally this would have to be repeated hundreds/thousands of times.
So far the (slow) code I have is this:
stack is the 3344 matrices.
G is a vector which determines which condition each 319*319 matrix belongs to.
Fmat=nan(size(stack, 1), size(stack, 2));
for i=1:size(stack, 1)
for j=1:size(stack, 1)
if i==j
Fmat(i,j)=nan; % the diagonal is always nan
else
% format the data as required by matlab
t = table([subidx],squeeze(stack(i, j, G==1)), squeeze(stack(i, j, G==2)), squeeze(stack(i, j, G==3)), squeeze(stack(i, j, G==4)), 'VariableNames',{'IDs','d1', 'd2', 'd3', 'd4'});
Meas = dataset([1 2 3 4]','VarNames',{'Measurements'});
% run repeated measures ANOVA
rm = fitrm(t,'d1-d4 ~ IDs-1','WithinDesign',Meas);
ranovatbl = ranova(rm);
%save output
Fmat(i,j)=ranovatbl.F(1);
end
end
end
Thank you very much for any suggestions,
Leonardo Tozzi
0 Comments
Accepted Answer
Jeff Miller
on 4 Dec 2019
If you really just want the F value, you can probably get it much faster by looking up the formulas for the one-factor within-Ss ANOVA and computing the F directly from those--that is, forget about fitrm and ranova. fitrm and ranova are doing a lot more work than you need, starting with decoding the model specification from a string and figuring out what computations need to be done. For example, this seems to have the relevant computational formulas: link
0 Comments
More Answers (1)
See Also
Categories
Find more on Repeated Measures and MANOVA in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!