bundles / scipy 1.18.0 / scipy / stats / _stats_py / spearmanr
function
scipy.stats._stats_py:spearmanr
source: /scipy/stats/_stats_py.py:5203
Signature
def spearmanr(a, b = None, axis = 0, nan_policy = propagate, alternative = two-sided)Summary
Calculate a Spearman correlation coefficient with associated p-value.
Extended Summary
The Spearman rank-order correlation coefficient is a nonparametric measure of the monotonicity of the relationship between two datasets. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Correlations of -1 or +1 imply an exact monotonic relationship. Positive correlations imply that as x increases, so does y. Negative correlations imply that as x increases, y decreases.
The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Spearman correlation at least as extreme as the one computed from these datasets. Although calculation of the p-value does not make strong assumptions about the distributions underlying the samples, it is only accurate for very large samples (>500 observations). For smaller sample sizes, consider a permutation test (see Examples section below).
Parameters
a, b: 1D or 2D array_like, b is optionalOne or two 1-D or 2-D arrays containing multiple variables and observations. When these are 1-D, each represents a vector of observations of a single variable. For the behavior in the 2-D case, see under
axis, below. Both arrays need to have the same length in theaxisdimension.axis: int or None, optionalIf axis=0 (default), then each column represents a variable, with observations in the rows. If axis=1, the relationship is transposed: each row represents a variable, while the columns contain observations. If axis=None, then both arrays will be raveled.
nan_policy: {'propagate', 'raise', 'omit'}, optionalDefines how to handle when input contains nan. The following options are available (default is 'propagate'):
'propagate': returns nan
'raise': throws an error
'omit': performs the calculations ignoring nan values
alternative: {'two-sided', 'less', 'greater'}, optionalDefines the alternative hypothesis. Default is 'two-sided'. The following options are available:
'two-sided': the correlation is nonzero
'less': the correlation is negative (less than zero)
'greater': the correlation is positive (greater than zero)
Returns
res: SignificanceResultAn object containing attributes:
statistic
Spearman correlation matrix or correlation coefficient (if only 2 variables are given as parameters). Correlation matrix is square with length equal to total number of variables (columns or rows) in
aandbcombined.pvalue
The p-value for a hypothesis test whose null hypothesis is that two samples have no ordinal correlation. See
alternativeabove for alternative hypotheses.pvaluehas the same shape asstatistic.
Raises
: ValueErrorIf
axisis not 0, 1 or None, or if the number of dimensions ofais greater than 2, or ifbis None and the number of dimensions ofais less than 2.
Warns
: `~scipy.stats.ConstantInputWarning`Raised if an input is a constant array. The correlation coefficient is not defined in this case, so
np.nanis returned.
Examples
import numpy as np from scipy import stats res = stats.spearmanr([1, 2, 3, 4, 5], [5, 6, 7, 8, 7])✓
res.statistic res.pvalue✗
np.float64(0.08858700531354384)
rng = np.random.default_rng() x2n = rng.standard_normal((100, 2)) y2n = rng.standard_normal((100, 2)) res = stats.spearmanr(x2n)✓
res.statistic, res.pvalue
✗(np.float64(0.054917491749174915), np.float64(0.5873499892795326))
res = stats.spearmanr(x2n[:, 0], x2n[:, 1])
✓res.statistic, res.pvalue
✗(np.float64(0.054917491749174915), np.float64(0.5873499892795326))
res = stats.spearmanr(x2n, y2n)
✓res.statistic res.pvalue✗
array([[0. , 0.58735, 0.22151, 0.00587],
[0.58735, 0. , 0.932 , 0.86421],
[0.22151, 0.932 , 0. , 0.64532],
[0.00587, 0.86421, 0.64532, 0. ]])
res = stats.spearmanr(x2n.T, y2n.T, axis=1)
✓res.statistic
✗array([[ 1. , 0.05492, -0.12334, 0.27366],
[ 0.05492, 1. , -0.00864, -0.01732],
[-0.12334, -0.00864, 1. , -0.04659],
[ 0.27366, -0.01732, -0.04659, 1. ]])
res = stats.spearmanr(x2n, y2n, axis=None)
✓res.statistic, res.pvalue
✗(np.float64(-0.07393234830870772), np.float64(0.29813911281371225))
res = stats.spearmanr(x2n.ravel(), y2n.ravel())
✓res.statistic, res.pvalue
✗(np.float64(-0.07393234830870772), np.float64(0.29813911281371225))
rng = np.random.default_rng() xint = rng.integers(10, size=(100, 2)) res = stats.spearmanr(xint)✓
res.statistic, res.pvalue
✗(np.float64(-0.0751530532255754), np.float64(0.4574007060277257))For small samples, consider performing a permutation test instead of relying on the asymptotic p-value. Note that to calculate the null distribution of the statistic (for all possibly pairings between observations in sample ``x`` and ``y``), only one of the two inputs needs to be permuted.
x = [1.76405235, 0.40015721, 0.97873798, 2.2408932, 1.86755799, -0.97727788] y = [2.71414076, 0.2488, 0.87551913, 2.6514917, 2.01160156, 0.47699563]✓
def statistic(x): # permute only `x` return stats.spearmanr(x, y).statistic res_exact = stats.permutation_test((x,), statistic, permutation_type='pairings') res_asymptotic = stats.spearmanr(x, y)✓
res_exact.pvalue, res_asymptotic.pvalue # asymptotic pvalue is too low
✗(np.float64(0.10277777777777777), np.float64(0.07239650145772594))For a more detailed example, see :ref:`hypothesis_spearmanr`.
See also
- hypothesis_spearmanr
Extended example
Aliases
scipy.stats.spearmanr