我正在尝试使用 scipy 来获取 Pearson R 相关性的置信区间。 Scipy 文档指出
scipy.stats.pearsonr
返回一个带有方法 confidence_interval()
的对象。
当我运行他们的示例代码时,我得到一个元组作为结果,因此无法计算置信区间。
我在 Windows 10 上使用 scipy 版本 1.7.3 和 python 3.7.0
我做错了什么?
最小可重现示例:
import numpy as np
from scipy import stats
x, y = [1, 2, 3, 4, 5, 6, 7], [10, 9, 2.5, 6, 4, 3, 2]
res = stats.pearsonr(x, y)
res
应该给出:
PearsonRResult(statistic=-0.828503883588428, pvalue=0.021280260007523286)
但我得到:
(-0.8285038835884277, 0.021280260007523345)
当我尝试时:
res.confidence_interval(confidence_level=0.9)
根据 scipy 文档示例,我当然得到一个错误
"AttributeError: 'tuple' object has no attribute 'confidence_interval'"
在 Scipy 版本 1.7 中,
pearsonr
返回元组而不是对象。这是您的版本的文档:
https://docs.scipy.org/doc/scipy-1.7.1/reference/reference/ generated/scipy.stats.pearsonr.html
看起来像在 1.9 版本中引入的那样返回一个对象。