corr
statistics: rho = corr (x)
statistics: rho = corr (x, y)
statistics: rho = corr (…, name, value)
statistics: [rho, pval] = corr (…)
Linear or rank correlation coefficients.
rho = corr (x) returns the matrix of pairwise correlation
coefficients between the columns of x, whose rows are observations and
whose columns are variables. For an n-by-k matrix x,
rho is k-by-k and rho(i,j) is the
correlation between the i-th and the j-th column of x.
rho = corr (x, y) returns the correlations between
the columns of x and the columns of y. For an
n-by-k1 matrix x and an n-by-k2 matrix
y, rho is k1-by-k2 and rho(i,j) is the
correlation between the i-th column of x and the j-th
column of y. x and y must have the same number of rows.
The following Name-Value pairs are supported:
| Name | Value |
|---|---|
'Type' | The coefficient to compute: 'Pearson'
(default) for the linear correlation coefficient, 'Kendall' for
Kendall’s tau-b, or 'Spearman' for Spearman’s rho. |
'Rows' | How missing values are handled: 'all'
(default) uses every row, so an entry is NaN whenever either of its
columns holds a NaN; 'complete' first removes every row
holding a NaN in any column; 'pairwise' computes each entry
from the rows where its own pair of columns is present. |
'Tail' | The alternative hypothesis of the test reported in
pval: 'both' (default) for a correlation different from zero,
'right' for a positive one, or 'left' for a negative one. |
'Weights' | A column vector of n nonnegative observation weights. The default weights every observation equally. |
Source Code: corr
Option values are matched without regard to case, and an unambiguous
abbreviation is accepted, so 'spear' selects 'Spearman'.
[rho, pval] = corr (…) also returns pval, the
p-value of a test of the null hypothesis that the corresponding correlation
is zero, against the alternative named by 'Tail'. A pval entry
is NaN wherever its rho entry is, and every entry is
NaN when 'Weights' is given.
The p-value is computed as follows:
'Pearson': from a Student’s t distribution with n-2
degrees of freedom applied to
t = rho * sqrt ((n - 2) / (1 - rho^2)), which is
exact when the data are normally distributed.
'Kendall': from the exact permutation distribution when
n < 10, and for a sample of fewer than 50 observations holding no
tied values; otherwise from a normal approximation with a continuity
correction and the usual correction for ties.
'Spearman': from the exact permutation distribution when
n < 10; from the approximation of Best and Roberts (algorithm AS 89)
for a larger sample holding no tied values; and from the same Student’s t
transformation as the Pearson coefficient for a larger sample that does
hold them.
The exact p-value is the proportion of the n! orderings of one variable giving a coefficient as extreme as the observed one, so tied values need no special treatment there.
A column of constant values has no defined correlation, so its entries,
the diagonal one included, are NaN. Inf makes a Pearson
coefficient NaN, while the rank coefficients order it like any other
value.
Two deviations from MATLAB. A character array is refused, where MATLAB
correlates the character codes; its acceptance there is incidental,
correlation having no meaning for text. An array of more than two
dimensions is refused, where MATLAB refuses most of them from inside a
matrix multiplication but flattens a leading singleton dimension and
answers NaN; core Octave’s corr answers NaN there
too.
References:
See also: corrcoef, cov, partialcorr, tiedrank, kendall, spearman
Source Code: corr
Correlation between the columns of a matrix, with p-values
x = [1 2; 3 5; 4 4; 7 8; 9 6]; [rho, pval] = corr (x)
rho = 1.0000 0.8052 0.8052 1.0000 pval = 1.0000 0.1002 0.1002 1.0000
Spearman's rank correlation, which a monotone relation makes exact
x = [1; 2; 3; 4; 5]; y = [1; 4; 9; 16; 25]; [rho, pval] = corr (x, y, 'Type', 'Spearman')
rho = 1 pval = 0.016667