Xcorr (for autocorrelation) with NaN values

I would like to autocorrelate some data, but it has some missing values, is there a quick way to do this in Matlab? xcorr returns an array of NaN if any of the inputs are NaN.

eg.

data = [1 2 3 4 NaN 2 3 4 1 2 3 4]; xc = xcorr(data, 'biased'); 
+1
source share
3 answers

With some understanding from Nzbuu, the following works:

 data = [1 2 3 4 NaN 2 3 4 5]; scaled = (data - nanmean(data)) / nanstd(data); scaled(isnan(data)) = 0; corr = xcorr(scaled); 

You need to insert zeros after scaling the data, and not earlier, because otherwise it will affect the value of mu and std used in xcorr. This is better than just working with xcorr directly, since the fft approach used in xcorr is much faster for large datasets.

+4
source

I would prefer to exclude pairs with NaN from the correlation instead of introducing zeros. In this case, I would use the following code in matlab based on corr (Pearson autocorrelation coefficients).

 out=zeros(nlags,1); out(1)=1; for i=2:nlags+1 out(i)=corr(data(i:end),data(1:end-i+1),'rows','complete'); end stem(0:nlags,out) title('sample ACF') 

Hope this helps

+3
source

Of course. You can use indexing to select only elements that are not NaN and invoke xcorr .

 data = [1 2 3 4 NaN 2 3 4 1 2 3 4]; xc = xcorr(data(~isnan(data)), 'biased'); 
+2
source

All Articles