Running repeated anova on all elements of large matrix
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Jeff Miller
2019-12-4
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 个评论
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Repeated Measures and MANOVA 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!