MATLAB Examples

corrmap

corrmap returns an (N-1)-dimensional array of correlation coefficients between y and A along dimension dim of A.

Contents

Syntax

r = corrmap(A,y)
r = corrmap(A,y,dim)
r = corrmap(A,y,dim,varargin)
[r,p] = corrmap(...)
[rm,pm,rlom,rupm] = corrmap(...)

Description

r = corrmap(A,y) returns the correlation coefficient between data matrix A and series y. This method will attempt to solve along a dimension of A whose length matches the length of y.

r = corrmap(A,y,dim) returns correlation coefficients along dimension dim of A. If dim is not declared, correlation coefficients will be calculated along the first dimension whose length equals the length of y.

r = corrmap(A,y,dim,varargin) allows parameter values described in the corrcoef documentation.

[r,p] = corrmap(...) also returns p, a matrix of p-values for testing the hypothesis of no correlation. Each p-value is the probability of getting a correlation as large as the observed value by random chance, when the true correlation is zero. If p(i,j) is small, say, less than 0.05, then the correlation r(i,j) is significant.

[r,p,rlo,rup] = corrmap(...) also returns matrices rlo and rup, of the same size as r, containing lower and upper bounds for a 95% confidence interval for each coefficient.

Example 1: A simple correlation coefficient between two time series.

Consider a case where you have some observational data, such as the sea surface temperature (SST) at a single location, measured daily for three years. You would like to compare SSTs in your research location to a new climate index because you think your new index does a better job of describing SSTs than the El Nino Southern Oscillation Index.

t = datenum(2000,0,1:1:3*365)';                             % time vector
SST = 20 + 8*cos(t*2*pi/365) + 3*rand(size(t)) + .2*sin(t); % measured temperature data
myIndex = cos(2*pi*t/365);                                  % your new brilliant climate index

figure
subplot(2,1,1)
plot(t,SST)
datetick
ylabel('sea surface temp {\circ}C')

subplot(2,1,2)
plot(t,myIndex)
datetick
ylabel('my new climate index')
xlabel('time')

As you can see, your new climate index seems to be highly correlated with SSTs at your location of interest. Just how correlated?

corrmap(SST,myIndex)
ans =

    0.9879

A correlation coefficient of ~0.99 is pretty high. But is it statistically significant?

[~,p] = corrmap(SST,myIndex)
p =

     0

A p value of 0 indicates that yes, this correlation may be significant.

Example 2: Compare a time series to a 3D gridded data set.

You may have some climate reanalyis data in a big 3D data set. Instead of looping through all of your lats and lons to calculate the correlation coefficient for each grid box, this function will be cleaner, performing the operation all at once for every grid box.

Let's start this example by generating some data to work with:

t = datenum(2000,0,1:1:3*365)';                             % time vector
y = sin(t);                                                 % some index akin to ENSO

A = rand(3,4,length(t));
A(1,1,:) = y;
A(1,2,:) = y.*rand(size(t));
A(1,3,:) = y+rand(size(t));
A(1,4,:) = 5*y + cos(t);
A(2,1,:) = sin(.7*t);
A(2,2,:) = -y;

Above, A is a 3D gridded data set. Dimensions 1 and 2 may correspond to x and y values, or latitudes and longitudes. Dimension 3 corresponds to time, any given slice A(:,:,n) represents a snapshot of the field of data at time n. Because of the way we populated A, half of the grid boxes in A should show some significant correlation with y.

[r,p] = corrmap(A,y);
r
r =

    1.0000    0.8589    0.9238    0.9805
   -0.0003   -1.0000   -0.0136   -0.0147
   -0.0378    0.0011   -0.0340    0.0123

figure
imagesc(r)
cb = colorbar;
caxis([-1 1])
ylabel(cb,'correlation coefficient')
title('observational data and index correlation')

Indeed, above we see a correlation in the top row as we planned. Here are the corresponding p values:

p
p =

         0    0.0000         0         0
    0.9924         0    0.6530    0.6261
    0.2119    0.9723    0.2613    0.6834

figure
imagesc(p)
cb = colorbar;
caxis([0 1])
ylabel(cb,'p value')
title('a measure of statistical significance')

Author Info

This script was written by Chad A. Greene, April 2014.

See also

corrcoef