Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

スピアマンの順位相関係数(Spearman’s rho)

順序尺度 × 順序尺度 あるいは 任意の変数を順位に置き換えて測る相関(順位ベースのピアソン相関)

線形性や正規生の仮定を必要としない

2つの変数X,YX, Ynn個の観測値の各ペア(xi,yi)(x_i, y_i)の順位の差did_iを求め

ρ=16di2n(n21)\rho=1-\frac{6 \sum d_i^2}{n\left(n^2-1\right)}
x = [1, 1, 2, 2, 3, 3]
y = [0, 0, 0, 1, 1, 1]

from scipy.stats import spearmanr
print(f"{spearmanr(x, y).statistic=:.3f}")

from ordinalcorr import polychoric
print(f"{polychoric(x, y)=:.3f}")
spearmanr(x, y).statistic=0.816
polychoric(x, y)=0.999
x = np.linspace(-10, 10, 15)
y = 1 / (1 + np.exp(-x))

plt.scatter(x, y)

from scipy.stats import spearmanr, pearsonr
from ordinalcorr import polychoric
print(f"{pearsonr(x, y).statistic=:.3f}")
print(f"{spearmanr(x, y).statistic=:.3f}")
print(f"{polychoric(x, y)=:.3f}")
pearsonr(x, y).statistic=0.933
spearmanr(x, y).statistic=1.000
polychoric(x, y)=1.000
<Figure size 640x480 with 1 Axes>