gpt4 book ai didi

python - seaborn 置信区间计算正确吗?

转载 作者:太空狗 更新时间:2023-10-30 02:38:44 25 4
gpt4 key购买 nike

首先,我必须承认我的统计学知识充其量只是生锈了:即使它是崭新的,它也不是我特别喜欢的学科,这意味着我很难理解它。

不过,我查看了 barplot 图是如何计算误差线的,并且惊讶地发现使用“置信区间”(CI) 而不是(更常见的)标准偏差。研究更多 CI 使我想到了这个 wikipedia article,它似乎在说,基本上,CI 计算如下:

mean minus 1.96 times stdev over sqrt(n)

mean plus 1.96 times stdev over sqrt(n)

或者,在伪代码中:

def ci_wp(a):
"""calculate confidence interval using Wikipedia's formula"""
m = np.mean(a)
s = 1.96*np.std(a)/np.sqrt(len(a))
return m - s, m + s

但是我们在seaborn/utils.py中发现的是:

def ci(a, which=95, axis=None):
"""Return a percentile range from an array of values."""
p = 50 - which / 2, 50 + which / 2
return percentiles(a, p, axis)

现在也许我完全错过了这一点,但这似乎与维基百科提出的计算完全不同。谁能解释这种差异?

再举个例子,从评论中,为什么我们得到的结果如此不同:

 >>> sb.utils.ci(np.arange(100))
array([ 2.475, 96.525])

>>> ci_wp(np.arange(100))
[43.842250270646467,55.157749729353533]

并与其他统计工具进行比较:

 def ci_std(a):
"""calculate margin of error using standard deviation"""
m = np.mean(a)
s = np.std(a)
return m-s, m+s

def ci_sem(a):
"""calculate margin of error using standard error of the mean"""
m = np.mean(a)
s = sp.stats.sem(a)
return m-s, m+s

这给了我们:

>>> ci_sem(np.arange(100))
(46.598850802411796, 52.401149197588204)

>>> ci_std(np.arange(100))
(20.633929952277882, 78.366070047722118)

或者使用随机样本:

rng = np.random.RandomState(10)
a = rng.normal(size=100)
print sb.utils.ci(a)
print ci_wp(a)
print ci_sem(a)
print ci_std(a)

...产生:

[-1.9667006   2.19502303]
(-0.1101230745774124, 0.26895640045116026)
(-0.017774461397903049, 0.17660778727165088)
(-0.88762281417683186, 1.0464561400505796)

为什么 Seaborn 的数字与其他结果截然不同?

最佳答案

你使用这个维基百科公式的计算是完全正确的。 Seaborn 只是使用了另一种方法:https://en.wikipedia.org/wiki/Bootstrapping_(statistics) . Dragicevic [1] 对此进行了很好的描述:

[It] consists of generating many alternative datasets from the experimental data by randomly drawing observations with replacement. The variability across these datasets is assumed to approximate sampling error and is used to compute so-called bootstrap confidence intervals. [...] It is very versatile and works for many kinds of distributions.

在 Seaborn 的源代码中,一个 barplot使用 estimate_statistic它引导数据然后计算它的置信区间:

>>> sb.utils.ci(sb.algorithms.bootstrap(np.arange(100)))
array([43.91, 55.21025])

结果与你的计算一致。

[1] Dragicevic, P. (2016)。 HCI 中的公平统计通信。在 HCI 的现代统计方法中(第 291-330 页)。施普林格,查姆。

关于python - seaborn 置信区间计算正确吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46125182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com