Why do I get correlation result NaN?

7 ビュー (過去 30 日間)
Bharath kumar boyanapalli
Bharath kumar boyanapalli 2021 年 8 月 13 日
コメント済み: Walter Roberson 2021 年 8 月 17 日
I have two matrices A is of 1*1058 and B is 1*1058, both matrices have some NaN values included in them. Is there any way to get correlation between these two matrices.

採用された回答

Ive J
Ive J 2021 年 8 月 15 日
Unless you have a good reason to impute your missing data, you can remove missing values from both vectors.
nanidx = isnan(A) | isnan(B);
corr(A(~nanidx), B(~nanidx))
  9 件のコメント
Ive J
Ive J 2021 年 8 月 16 日
Yes that's the whole idea!
% step 0-create two sample vectors with 5 missing values
sz = 100;
A = rand(sz, 1);
B = rand(sz, 1);
A(randperm(sz, 5)) = nan;
B(randperm(sz, 5)) = nan;
% step 1-find missing values in both vectors
nanidx = isnan(A) | isnan(B);
% step 2- remove the indices in step 1
cleanA = A(~nanidx);
cleanB = B(~nanidx);
% step 3- calculate the correlation coeff.
R = corr(cleanA, cleanB); % NOTE: by default Pearson correlation is used in corr function
% step 4- report it
fprintf('Pearson R is %.2f\n', R)
Pearson R is -0.08
Walter Roberson
Walter Roberson 2021 年 8 月 16 日
That's what Ive J's code does: removes all locations for which X is nan or Y is nan.

サインインしてコメントする。

その他の回答 (1 件)

Chunru
Chunru 2021 年 8 月 13 日
Use "fillmissing" to fill up the nans before computing the correlation. doc fillmissing for more details.
  7 件のコメント
Chunru
Chunru 2021 年 8 月 17 日
Anyway, if you have data with so many NANs, you need to doubt your data first before doubting the processing techniques. There is not fool proof technique for filling missing data. It all depends on what data you have and what you want.
Walter Roberson
Walter Roberson 2021 年 8 月 17 日
Mathematically, if you have vectors A and B, then
cAB = corr(A,B);
P = randperm(numel(A));
pA = A(P);
pB = B(P);
cpAB = corr(pA, pB);
then cAB needs to equal cpAB to within round-off. The order of the elements relative to each other in their same vectors do not matter: only the correspondance between the two vectors matter.
If, though, you were to fillmissing(A) and compare that to fillmissing(pA) then you would get different results, because fillmissing works based upon nearby values, under the assumption there is some kind of smooth continuity. This is not really compatible with the mathematics of correlation which does not care about order within the sequence.
If you have some prediction function for your vectors, then Yes, it might make sense to apply that prediction function. It might even make sense to apply something like narx to predict in some cases. But that would have to be done based upon knowledge of what the vectors represent. fillmissing() has no knowledge of what they represent.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDescriptive Statistics についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by