help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: correlation with nan?


From: Stefan Neumann
Subject: Re: correlation with nan?
Date: Fri, 21 May 2010 16:02:27 +0200

2010/5/20 James Sherman Jr. <address@hidden>
If you're just looking to calculate the correlation coefficient between two variables, couldn't you just do something like:
nan_indices = or(isnan(x), isnan(y));
xhat = x(~nan_indices);
yhat = y(~nan_indices);

correlation_coeff = mean((xhat-mean(xhat)).*(yhat-
mean(yhat)))./(std(xhat)*std(yhat));

Hope this helps.

Hi James,

thank you.
I the data I have several variables.
If there is no other way to get a correlation-matrix then your suggestion plus a loop will probably be the way.






2010/5/20 Søren Hauberg <address@hidden>
tor, 20 05 2010 kl. 18:39 +0200, skrev Stefan Neumann:
> is there a way to calculate a correlation when the data contains NaN ?
>
> The nan-toolbox does not seem to have this.

The attached function gives you the covariance matrix; that might be
helpful.

Søren


Hi Søren,

thank you for the file.
I used it in the following script and had the result appended below.
I'm confused. Shouldn't there be a value for every cell of the covariance_matrix?

% --- script --- %
Data = "">4.2500e+01   6.8400e+01   2.0800e+01
5.3200e+01          NaN          NaN
4.5800e+01          NaN          NaN
5.8600e+01          NaN   2.6700e+01
5.1300e+01   7.3600e+01   2.6500e+01
       NaN   7.6200e+01   3.1600e+01
       NaN   7.3100e+01   3.2000e+01
       NaN   7.8200e+01   3.6600e+01
       NaN   7.0800e+01   3.5200e+01
       NaN          NaN   2.4100e+01
       NaN   8.5600e+01   2.9400e+01
       NaN   9.4300e+01   3.6200e+01
5.9500e+01   9.6800e+01   3.2400e+01
4.6400e+01   8.5900e+01   3.7900e+01
0.0000e+00          NaN   3.2200e+01
] ;

avg    = nanmean(Data)
stddev = nanstd(Data)
covariance_matrix = nancov(Data)

% --- result --- %
avg =   44.663   80.290   30.892

stddev =   19.0303    9.8743    5.1737

covariance_matrix =
   NaN   NaN   NaN
   NaN   NaN   NaN
   NaN   NaN   NaN


THX
Stefan


reply via email to

[Prev in Thread] Current Thread [Next in Thread]