python-3.x binom_test结果与正态分布检验统计量的假设检验比较

k2arahey  于 7个月前  发布在  Python
关注(0)|答案(1)|浏览(98)

我有两个样本,每个样本的试验次数不同,每个样本的成功次数也不同。我试图比较两个样本之间的成功率,看看是否有显著差异。我似乎得到了非常不同的结果,这取决于我是否使用scipy.stats中的binom_test或下面的函数,该函数假设检验统计量是正态分布的。
有人能告诉我,如果我应用binom_test不正确,或者如果有一个错误/我不正确地使用下面的函数?
我从一个SO帖子中得到了这个函数,似乎hatP可能是不正确的。
我有样本数据和binom_test和下面的函数的结果。binom_test得到的p值基本上是舍入到0,而函数得到的p值是1.82,这甚至没有意义。
来自SO post https://stats.stackexchange.com/questions/81091/is-it-possible-to-do-a-test-of-significance-for-a-string-occurrence-in-two-datas的功能

# 2 sample binom

def fnDiffProp(x1, x2, n1, n2):
    '''
    inputs:
    x1: the number of successes in the first sample
    x2: the number of successes in the second sample
    n1: the total number of 'trials' in the first sample
    n2: the total number of 'trials' in the second sample
    output:
    the test statistic, and the p-value as a tuple
    '''
    
    import math
    import scipy.stats as stats
    
    hatP = (x1 + x2)/(n1 + n2)
    hatQ = 1 - hatP
    hatP1 = x1/n1
    hatP2 = x1/n2
    Z = (hatP1 - hatP2)/(math.sqrt(hatP*hatQ*(1/n1 + 1/n2)))
    pVal = 2*(1 - stats.norm.cdf(Z))
    return((Z, pVal))


sample 1

195 successes
135779 trials

sample 2

5481 successes
81530 trials

results from binom_test

binom_test(x=5481, n=81530, p=0.0014, alternative='greater')

0.0

binom_test(x=5481, n=81530, p=0.0014, alternative='two-sided')
0.0

fnDiffProp(x1=195, x2=5481, n1=135779, n2=81530)

(-1.3523132192521408, 1.82372486268966)

字符串
最新消息:
我从statsmodels运行了proportions_ztest,得到了下面的结果,类似于binom_test的结果。在下面的一个测试中,我从两组中随机抽取了相等的样本。在任何一种情况下,p值都很小,四舍五入为0。

number_of_successes = [5481, 195]
total_sample_sizes = [81530, 135779]
# Calculate z-test statistic and p-value
test_stat, p_value = proportions_ztest(number_of_successes, total_sample_sizes, alternative='larger')

print(str(test_stat))
print(str(p_value))

93.10329278601503
0.0

number_of_successes = [5389, 119]
total_sample_sizes = [80000, 80000]
# Calculate z-test statistic and p-value
test_stat, p_value = proportions_ztest(number_of_successes, total_sample_sizes, alternative='larger')

print(str(test_stat))
print(str(p_value))

72.26377467032772
0.0

093gszye

093gszye1#

有人能告诉我,如果我应用binom_test不正确,或者如果有一个错误/我不正确地使用下面的函数?
scipy.stats.binom_test检验零假设,即给定样本是从具有假设成功概率的二项分布中抽取的。这是一种单样本检验,将样本与假设的概率分布进行比较。
你说“我想比较两个样本之间的成功率,看看是否有显著差异。”所以你想要一个双样本检验,评估两个样本是否来自同一个二项分布(成功概率未知)。
这两种情况完全不同,因此scipy.stats.binom_test不能用来解决您的问题。
在你的other issue中,我展示了如何修正你的自定义测试fnDiffProp来解决你的问题。事实上,它产生的统计数据和p值与proportions_ztest相同。你可以在这个上下文中考虑的其他测试在那篇文章中列出。
https://stackoverflow.com/a/77422932/6036253

相关问题