Python比例测试类似于R中的prop.test

2024-04-18 07:08:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找Python中的一个测试,它可以执行以下操作:

> survivors <- matrix(c(1781,1443,135,47), ncol=2)
> colnames(survivors) <- c('survived','died')
> rownames(survivors) <- c('no seat belt','seat belt')
> survivors
             survived died
no seat belt     1781  135
seat belt        1443   47
> prop.test(survivors)

    2-sample test for equality of proportions with continuity correction

data:  survivors
X-squared = 24.3328, df = 1, p-value = 8.105e-07
alternative hypothesis: two.sided
95 percent confidence interval:
 -0.05400606 -0.02382527
sample estimates:
   prop 1    prop 2 
0.9295407 0.9684564 

我最感兴趣的是p-value计算。

示例采用here格式


Tags: samplenotestforvaluematrixseatbelt
2条回答

我想我明白了:

In [11]: from scipy import stats

In [12]: import numpy as np

In [13]: survivors = np.array([[1781,135], [1443, 47]])

In [14]: stats.chi2_contingency(survivors)
Out[14]: 
(24.332761232771361,       # x-squared
 8.1048817984512269e-07,   # p-value
 1,
 array([[ 1813.61832061,   102.38167939],
       [ 1410.38167939,    79.61832061]]))

添加到@Akavall的答案中:如果您没有显式的“失败”计数(在您的示例中是死亡的计数),R的prop.test允许您只指定试验的总数,例如prop.test(c(1781, 1443), c(1781+135, 1443+47))将给出与您构建的列联表相同的结果。

Scipy的chi2_contingency明确要求提供失败计数和完整的列联表。如果您没有明确的失败计数,只想检查两个样本的成功率在总数中所占的比例是否相等,那么可以使用

survivors = np.array([[1781, total1 - 1781], [1443, total2 - 47]])
chi2_contingency(survivors)

# Result:
(24.332761232771361, 8.1048817984512269e-07, 1,
array([[ 1813.61832061,   102.38167939],
           [ 1410.38167939,    79.61832061]]))

我花了点时间才弄明白。希望它能帮助别人。

相关问题 更多 >