bundles / scipy latest / scipy / stats / _hypotests / somersd
function
scipy.stats._hypotests:somersd
source: /scipy/stats/_hypotests.py :775
Signature
def somersd ( x , y = None , alternative = two-sided ) Summary
Calculates Somers' D, an asymmetric measure of ordinal association.
Extended Summary
Like Kendall's , Somers' is a measure of the correspondence between two rankings. Both statistics consider the difference between the number of concordant and discordant pairs in two rankings and , and both are normalized such that values close to 1 indicate strong agreement and values close to -1 indicate strong disagreement. They differ in how they are normalized. To show the relationship, Somers' can be defined in terms of Kendall's :
Suppose the first ranking has distinct ranks and the second ranking has distinct ranks. These two lists of rankings can also be viewed as an contingency table in which element is the number of rank pairs with rank in ranking and rank in ranking . Accordingly, somersd also allows the input data to be supplied as a single, 2D contingency table instead of as two separate, 1D rankings.
Note that the definition of Somers' is asymmetric: in general, . somersd(x, y) calculates Somers' : the "row" variable is treated as an independent variable, and the "column" variable is dependent. For Somers' , swap the input lists or transpose the input table.
Parameters
x: array_like1D array of rankings, treated as the (row) independent variable. Alternatively, a 2D contingency table.
y: array_like, optionalIf
xis a 1D array of rankings,yis a 1D array of rankings of the same length, treated as the (column) dependent variable. Ifxis 2D,yis ignored.alternative: {'two-sided', 'less', 'greater'}, optionalDefines the alternative hypothesis. Default is 'two-sided'. The following options are available: * 'two-sided': the rank correlation is nonzero * 'less': the rank correlation is negative (less than zero) * 'greater': the rank correlation is positive (greater than zero)
Returns
res: SomersDResultA SomersDResult object with the following fields:
statistic
statistic
pvalue
pvalue
table
table
Notes
This function follows the contingency table approach of [2] and [3]. p-values are computed based on an asymptotic approximation of the test statistic distribution under the null hypothesis .
Theoretically, hypothesis tests based on Kendall's and Somers' should be identical. However, the p-values returned by kendalltau are based on the null hypothesis of independence between and (i.e. the population from which pairs in and are sampled contains equal numbers of all possible pairs), which is more specific than the null hypothesis used here. If the null hypothesis of independence is desired, it is acceptable to use the p-value returned by kendalltau with the statistic returned by somersd and vice versa. For more information, see [2].
Contingency tables are formatted according to the convention used by SAS and R: the first ranking supplied (x) is the "row" variable, and the second ranking supplied (y) is the "column" variable. This is opposite the convention of Somers' original paper [1].
Array API Standard Support
somersd has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.
==================== ==================== ==================== Library CPU GPU ==================== ==================== ==================== NumPy ✅ n/a CuPy n/a ⛔ PyTorch ⛔ ⛔ JAX ⛔ ⛔ Dask ⛔ n/a ==================== ==================== ====================
See
dev-arrayapifor more information.
Examples
We calculate Somers' D for the example given in [4]_, in which a hotel chain owner seeks to determine the association between hotel room cleanliness and customer satisfaction. The independent variable, hotel room cleanliness, is ranked on an ordinal scale: "below average (1)", "average (2)", or "above average (3)". The dependent variable, customer satisfaction, is ranked on a second scale: "very dissatisfied (1)", "moderately dissatisfied (2)", "neither dissatisfied nor satisfied (3)", "moderately satisfied (4)", or "very satisfied (5)". 189 customers respond to the survey, and the results are cast into a contingency table with the hotel room cleanliness as the "row" variable and customer satisfaction as the "column" variable. +-----+-----+-----+-----+-----+-----+ | | (1) | (2) | (3) | (4) | (5) | +=====+=====+=====+=====+=====+=====+ | (1) | 27 | 25 | 14 | 7 | 0 | +-----+-----+-----+-----+-----+-----+ | (2) | 7 | 14 | 18 | 35 | 12 | +-----+-----+-----+-----+-----+-----+ | (3) | 1 | 3 | 2 | 7 | 17 | +-----+-----+-----+-----+-----+-----+ For example, 27 customers assigned their room a cleanliness ranking of "below average (1)" and a corresponding satisfaction of "very dissatisfied (1)". We perform the analysis as follows.from scipy.stats import somersd table = [[27, 25, 14, 7, 0], [7, 14, 18, 35, 12], [1, 3, 2, 7, 17]] res = somersd(table)✓
res.statistic res.pvalue✗
See also
- kendalltau
Calculates Kendall's tau, another correlation measure.
- pearsonr
Calculates a Pearson correlation coefficient.
- spearmanr
Calculates a Spearman rank-order correlation coefficient.
- weightedtau
Computes a weighted version of Kendall's tau.
Aliases
-
scipy.stats.somersd