SciPy 如何计算 pearsonr() 函数中的 p 值?
How SciPy calculates the p-value in pearsonr() function?
我搜索了很多,但没有解释 SciPy 如何计算相关系数的 p 值以及为什么它不可靠(由函数页面上的 SciPy 开始)小于500的数据集
scipy.stats.pearsonr
computes the p value using the t distribution. (You can check the source code in the file stats.py
on github.) 这绝对应该在文档字符串中提及。
这是一个例子。首先,导入pearsonr
和scipy的t分布实现:
In [334]: from scipy.stats import pearsonr, t as tdist
为此示例定义 x
和 y
:
In [335]: x = np.array([0, 1, 2, 3, 5, 8, 13])
In [336]: y = np.array([1.2, 1.4, 1.6, 1.7, 2.0, 4.1, 6.6])
计算此数据的 r
和 p
:
In [337]: r, p = pearsonr(x, y)
In [338]: r
Out[338]: 0.9739566302403544
In [339]: p
Out[339]: 0.0002073053505382502
现在再次计算 p
,首先计算 t 统计量,然后找到该 t 值的两倍生存函数:
In [340]: df = len(x) - 2
In [341]: t = r * np.sqrt(df/(1 - r**2))
In [342]: 2*tdist.sf(t, df) # This is the p value.
Out[342]: 0.0002073053505382502
我们得到了与预期相同的 p 值。
我不知道声明的来源"The p-values are not entirely reliable but are probably reasonable for datasets larger than 500 or so"。如果有人知道可引用的参考文献,则应将其添加到 pearsonr
文档字符串中。
我搜索了很多,但没有解释 SciPy 如何计算相关系数的 p 值以及为什么它不可靠(由函数页面上的 SciPy 开始)小于500的数据集
scipy.stats.pearsonr
computes the p value using the t distribution. (You can check the source code in the file stats.py
on github.) 这绝对应该在文档字符串中提及。
这是一个例子。首先,导入pearsonr
和scipy的t分布实现:
In [334]: from scipy.stats import pearsonr, t as tdist
为此示例定义 x
和 y
:
In [335]: x = np.array([0, 1, 2, 3, 5, 8, 13])
In [336]: y = np.array([1.2, 1.4, 1.6, 1.7, 2.0, 4.1, 6.6])
计算此数据的 r
和 p
:
In [337]: r, p = pearsonr(x, y)
In [338]: r
Out[338]: 0.9739566302403544
In [339]: p
Out[339]: 0.0002073053505382502
现在再次计算 p
,首先计算 t 统计量,然后找到该 t 值的两倍生存函数:
In [340]: df = len(x) - 2
In [341]: t = r * np.sqrt(df/(1 - r**2))
In [342]: 2*tdist.sf(t, df) # This is the p value.
Out[342]: 0.0002073053505382502
我们得到了与预期相同的 p 值。
我不知道声明的来源"The p-values are not entirely reliable but are probably reasonable for datasets larger than 500 or so"。如果有人知道可引用的参考文献,则应将其添加到 pearsonr
文档字符串中。