Ranksum - Wilcoxon rank sum test - Get more statistics using a for loop

Hello everyone!
I am running some statistics and I need to use the Wilcoxon rank sum test.
I have 2 matrices and I need to compare 25 vector pairs.
If I run the test in a for loop, I get only the p-values - I would love to get more statistics but I have not been able to find a way.
That is what I have now:
Matrix1 = n x 25
Matrix2 = m x 25
for i = 1:25
testresults(i) = ranksum(Matrix1.(i),Matrix2(i),'method','exact')
end
If I run the test like this, I will get as a results just the pval, any clue how to get [p,h,stats] in a for loop?
I know how to run this out of the for loop.
Thank you all in advance for your help!

 Respuesta aceptada

Adam Danz
Adam Danz el 22 de Jun. de 2020
Editada: Adam Danz el 22 de Jun. de 2020
p = nan(1,n);
h = false(1,n);
stats = cell(1,n);
for i = 1:n
[p(i),h(i),stats{i}] = ranksum(x,y,__)
end

7 comentarios

Thank you very much Adam, is there a way to name the result matrix?
Background is that I will need to perform the same test multiple times for different for loops and I guess the results are going to be overwritten everytime.
Can I just call p, h, and stats differently everytime, or it is important that the name stays p, h, and stats?
Two idea.
1) you could a version of my answer into its own function so you can store the outputs in their own variables.
% very rough example.
[p1,h1,stats1] = localRankSum(x1,y1);
[p2,h2,stats2] = localRankSum(x2,y2);
[p3,h3,stats3] = localRankSum(x3,y3);
function [p,h,stats] = localRankSum(x,y)
p = nan(1,n);
h = false(1,n);
stats = cell(1,n);
for i = 1:n
[p(i),h(i),stats{i}] = ranksum(x,y,__)
end
2) you could add a nested loop
% very rough example
p = nan(m,n);
h = false(m,n);
stats = cell(m,n);
for i = 1:n
for j = 1:m
[p(i,j),h(i,j),stats{i,j}] = ranksum(x,y,__);
end
end
Thank you, Adam - Really helpful
Adam Danz
Adam Danz el 22 de Jun. de 2020
Editada: Adam Danz el 22 de Jun. de 2020
Glad I could help!
The critical lesson to learn is to avoid creating a bunch of different variables that store the same type of data when the data can be stored in a vector / matrix / array. Indexing is the superpower of Matlab and nearly all of its functions are designed to operate on arrays in a fast and efficient manner.
Thanks Adam,
I have similar code but nested for loop never finishes.
% CTL2008E1:(177,162)
% Xrel:(177,162,15)
% Yctl:(177,162,15)
% Zidl:(177,162,15)
% ranksum(Xrel(:,:,1:15),Yctl(:,:,1:15))
pRC = nan(size(CTL2008E1));
hRC = false(size(CTL2008E1));
statsRC = cell(size(CTL2008E1));
pIC = nan(size(CTL2008E1));
hIC = false(size(CTL2008E1));
statsIC = cell(size(CTL2008E1));
% do only for non NaN pixels.
for i = 1:size(CTL2008E1,1)
for j = 1:size(CTL2008E1,2)
while ~isnan(CTL2008E1(i,j))
[pRC(i,j),hRC(i,j)] = ranksum(squeeze(Xrel(i,j,:)),squeeze(Yctl(i,j,:)));
[pIC(i,j),hIC(i,j)] = ranksum(squeeze(Zidl(i,j,:)),squeeze(Yctl(i,j,:)));
end
end
end
I am running on Macbook pro 6 cores. Is it possible to use multicore? (2,6 GHz 6-Core Intel Core i7, 16 GB 2400 MHz DDR4)
Thank you,Y
The loop gets stuck in the while-loop at the first non-NaN value in CTL2008E1.
I think you want a condition rather than a while-loop.
if ~isnan(CTL2008E1(i,j))
[pRC(i,j),hRC(i,j)] = ranksum(squeeze(Xrel(i,j,:)),squeeze(Yctl(i,j,:)));
[pIC(i,j),hIC(i,j)] = ranksum(squeeze(Zidl(i,j,:)),squeeze(Yctl(i,j,:)));
end
Thanks! good catch!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 22 de Jun. de 2020

Comentada:

el 31 de En. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by