运行 卡方检验与观察和期望计数并获得置信区间
Run a chi square test with observation and expectation counts and get confidence interval
我是卡方检验的新手,我试图弄清楚 运行 卡方检验的 'standard' 方式,并获得成功率差异的 95% 置信区间在两个实验中。
我的数据是这样的:
Condition A: 25 75 100
Condition B: 100 100 200
Total: 125 175
这些数字代表实验期间观察到的计数。如您所见,条件 A 与条件 B 的样本数量不同。
我想得到的是:
- 表明条件 A 的 33% 的成功率是否与条件 B 的 50% 的成功率在统计上不同的检验统计量。
- 我还想知道两个成功率之间差异的 95% 置信区间。
似乎 scipy.stats.chisquare
期望用户调整 'expected' 计数,以便它们看起来与 'observed' 计数从相同的样本量中取出。这是我需要做的唯一转变吗?如果没有,我还需要做什么?最后,我将如何计算比例差异的 95% 置信区间?
你有一个 contingency table. To perform the χ2 test on this data, you can use scipy.stats.chi2_contingency
:
In [31]: from scipy.stats import chi2_contingency
In [32]: obs = np.array([[25, 75], [100, 100]])
In [33]: obs
Out[33]:
array([[ 25, 75],
[100, 100]])
In [34]: chi2, p, dof, expected = chi2_contingency(obs)
In [35]: p
Out[35]: 5.9148695289823149e-05
你的偶然性table是2x2,所以你可以使用Fisher's exact test. This is implemented in scipy as scipy.stats.fisher_exact
:
In [148]: from scipy.stats import fisher_exact
In [149]: oddsr, pval = fisher_exact(obs)
In [150]: pval
Out[150]: 3.7175015403965242e-05
scipy 没有更多的意外事件 table。看起来 statsmodels
的下一个版本将有更多的工具来分析意外事件 table,但现在这没有帮助。
编写一些代码来计算比例差异及其 95% 置信区间并不难。这是一种方法:
# Include this if you are using Python 2.7. Or tweak the code in the
# function to ensure that division uses floating point.
from __future__ import division
def diffprop(obs):
"""
`obs` must be a 2x2 numpy array.
Returns:
delta
The difference in proportions
ci
The Wald 95% confidence interval for delta
corrected_ci
Yates continuity correction for the 95% confidence interval of delta.
"""
n1, n2 = obs.sum(axis=1)
prop1 = obs[0,0] / n1
prop2 = obs[1,0] / n2
delta = prop1 - prop2
# Wald 95% confidence interval for delta
se = np.sqrt(prop1*(1 - prop1)/n1 + prop2*(1 - prop2)/n2)
ci = (delta - 1.96*se, delta + 1.96*se)
# Yates continuity correction for confidence interval of delta
correction = 0.5*(1/n1 + 1/n2)
corrected_ci = (ci[0] - correction, ci[1] + correction)
return delta, ci, corrected_ci
例如,
In [22]: obs
Out[22]:
array([[ 25, 75],
[100, 100]])
In [23]: diffprop(obs)
Out[23]:
(-0.25,
(-0.35956733089748971, -0.14043266910251032),
(-0.36706733089748972, -0.13293266910251031))
返回的第一个值是比例差异delta
。接下来的两对是 delta
的 Wald 95% 置信区间和具有 Yates 连续性校正的 Wald 95% 置信区间。
如果你不喜欢那些负值,你可以先反转行:
In [24]: diffprop(obs[::-1])
Out[24]:
(0.25,
(0.14043266910251032, 0.35956733089748971),
(0.13293266910251031, 0.36706733089748972))
为了比较,下面是 R 中的类似计算:
> obs
[,1] [,2]
[1,] 25 75
[2,] 100 100
> prop.test(obs, correct=FALSE)
2-sample test for equality of proportions without continuity
correction
data: obs
X-squared = 17.1429, df = 1, p-value = 3.467e-05
alternative hypothesis: two.sided
95 percent confidence interval:
-0.3595653 -0.1404347
sample estimates:
prop 1 prop 2
0.25 0.50
> prop.test(obs, correct=TRUE)
2-sample test for equality of proportions with continuity correction
data: obs
X-squared = 16.1297, df = 1, p-value = 5.915e-05
alternative hypothesis: two.sided
95 percent confidence interval:
-0.3670653 -0.1329347
sample estimates:
prop 1 prop 2
0.25 0.50
我只想在 中添加 n1 = float(n1)
和 n2 = float(n2)
。它们应该被转换为浮点数(对于 Python 2 个用户),否则除法将简单地产生 0。
我是卡方检验的新手,我试图弄清楚 运行 卡方检验的 'standard' 方式,并获得成功率差异的 95% 置信区间在两个实验中。
我的数据是这样的:
Condition A: 25 75 100
Condition B: 100 100 200
Total: 125 175
这些数字代表实验期间观察到的计数。如您所见,条件 A 与条件 B 的样本数量不同。
我想得到的是:
- 表明条件 A 的 33% 的成功率是否与条件 B 的 50% 的成功率在统计上不同的检验统计量。
- 我还想知道两个成功率之间差异的 95% 置信区间。
似乎 scipy.stats.chisquare
期望用户调整 'expected' 计数,以便它们看起来与 'observed' 计数从相同的样本量中取出。这是我需要做的唯一转变吗?如果没有,我还需要做什么?最后,我将如何计算比例差异的 95% 置信区间?
你有一个 contingency table. To perform the χ2 test on this data, you can use scipy.stats.chi2_contingency
:
In [31]: from scipy.stats import chi2_contingency
In [32]: obs = np.array([[25, 75], [100, 100]])
In [33]: obs
Out[33]:
array([[ 25, 75],
[100, 100]])
In [34]: chi2, p, dof, expected = chi2_contingency(obs)
In [35]: p
Out[35]: 5.9148695289823149e-05
你的偶然性table是2x2,所以你可以使用Fisher's exact test. This is implemented in scipy as scipy.stats.fisher_exact
:
In [148]: from scipy.stats import fisher_exact
In [149]: oddsr, pval = fisher_exact(obs)
In [150]: pval
Out[150]: 3.7175015403965242e-05
scipy 没有更多的意外事件 table。看起来 statsmodels
的下一个版本将有更多的工具来分析意外事件 table,但现在这没有帮助。
编写一些代码来计算比例差异及其 95% 置信区间并不难。这是一种方法:
# Include this if you are using Python 2.7. Or tweak the code in the
# function to ensure that division uses floating point.
from __future__ import division
def diffprop(obs):
"""
`obs` must be a 2x2 numpy array.
Returns:
delta
The difference in proportions
ci
The Wald 95% confidence interval for delta
corrected_ci
Yates continuity correction for the 95% confidence interval of delta.
"""
n1, n2 = obs.sum(axis=1)
prop1 = obs[0,0] / n1
prop2 = obs[1,0] / n2
delta = prop1 - prop2
# Wald 95% confidence interval for delta
se = np.sqrt(prop1*(1 - prop1)/n1 + prop2*(1 - prop2)/n2)
ci = (delta - 1.96*se, delta + 1.96*se)
# Yates continuity correction for confidence interval of delta
correction = 0.5*(1/n1 + 1/n2)
corrected_ci = (ci[0] - correction, ci[1] + correction)
return delta, ci, corrected_ci
例如,
In [22]: obs
Out[22]:
array([[ 25, 75],
[100, 100]])
In [23]: diffprop(obs)
Out[23]:
(-0.25,
(-0.35956733089748971, -0.14043266910251032),
(-0.36706733089748972, -0.13293266910251031))
返回的第一个值是比例差异delta
。接下来的两对是 delta
的 Wald 95% 置信区间和具有 Yates 连续性校正的 Wald 95% 置信区间。
如果你不喜欢那些负值,你可以先反转行:
In [24]: diffprop(obs[::-1])
Out[24]:
(0.25,
(0.14043266910251032, 0.35956733089748971),
(0.13293266910251031, 0.36706733089748972))
为了比较,下面是 R 中的类似计算:
> obs
[,1] [,2]
[1,] 25 75
[2,] 100 100
> prop.test(obs, correct=FALSE)
2-sample test for equality of proportions without continuity
correction
data: obs
X-squared = 17.1429, df = 1, p-value = 3.467e-05
alternative hypothesis: two.sided
95 percent confidence interval:
-0.3595653 -0.1404347
sample estimates:
prop 1 prop 2
0.25 0.50
> prop.test(obs, correct=TRUE)
2-sample test for equality of proportions with continuity correction
data: obs
X-squared = 16.1297, df = 1, p-value = 5.915e-05
alternative hypothesis: two.sided
95 percent confidence interval:
-0.3670653 -0.1329347
sample estimates:
prop 1 prop 2
0.25 0.50
我只想在 n1 = float(n1)
和 n2 = float(n2)
。它们应该被转换为浮点数(对于 Python 2 个用户),否则除法将简单地产生 0。