简体   繁体   English

R binom.test舍入错误?

[英]R binom.test roundoff error?

I'm confused about the operation of binom.test. 我对binom.test的操作感到困惑。

Say I want to test a sample of 4/10 success against p=0.5. 假设我想测试4/10成功的样本对p = 0.5。 The P value should be: P值应为:

P(X <= 4) + P(X >=6) or P(X <= 4) + 1-P(X <= 5) P(X <= 4)+ P(X> = 6)或P(X <= 4)+ 1-P(X <= 5)

and indeed: 确实:

>pbinom(4,10,p=0.5) + 1-pbinom(5,10,0.5)
[1] 0.7539063

or: 要么:

>binom.test(4,10,p=0.5)

Exact binomial test

data:  4 and 10
number of successes = 4, number of trials = 10, p-value = 0.7539

But now I want to test a sample of 95/150 against p=0.66 Here, the expected value is 99, so the P value should be 但现在我想测试95/150的样本对p = 0.66这里,期望值是99,所以P值应该是

P(X <= 95) + P(X >= 103) or P(X <= 95) + 1-P(X <= 102) P(X <= 95)+ P(X> = 103)或P(X <= 95)+ 1-P(X <= 102)

which is 是的

>pbinom(95,150,.66) + 1-pbinom(102,150,.66)
[1] 0.5464849

but

>binom.test(95,150,.66)

    Exact binomial test

data:  95 and 150
number of successes = 95, number of trials = 150, p-value = 0.4914

In fact, the difference in the two P-values is exactly dbinom(103,150,.66) . 实际上,两个P值的差异恰好是dbinom(103,150,.66) So it seems R has failed to include X=103. 所以似乎R未能包括X = 103。

The only explanation I can guess for this is that there is roundoff error due to the inexact representation of .66 causing R to just miss X=103. 我可以猜到的唯一解释是,由于.66的不精确表示导致R仅错过X = 103,因此存在舍入误差。 Is this all it is, or is there something else going on? 这就是全部,还是还有其他事情发生?

Here is the code for computing the p-value in binom.test(x = 95, n = 150, p= 0.66) 这是用于计算binom.test中p值的代码(x = 95,n = 150,p = 0.66)

relErr <- 1 + 1e-07
d <- dbinom(x, n, p)
m <- n * p
i <- seq.int(from = ceiling(m), to = n)
y <- sum(dbinom(i, n, p) <= d * relErr)
pbinom(x, n, p) + pbinom(n - y, n, p, lower.tail = FALSE)

So, binom.test doesn't look symmetrically about the expected value. 因此,binom.test看起来并不是对称的预期值。 It looks for the first integer C such that C is bigger than or equal to the expected value and the probability of getting exactly C successes is less than or equal to the probability of getting exactly x successes, up to the fudge factor in relErr. 它寻找第一个整数C,使得C大于或等于预期值,并且获得精确C成功的概率小于或等于获得正好x次成功的概率,直到relErr中的软糖因子。 So, instead of saying that p is the probability of getting "at least that far away from the expected value", they say that p is the probability that the probability is at least as small as the value that you obtained. 因此,他们不是说p是“至少远离预期值”的概率,而是说p是概率至少与您获得的值一样小的概率。

In this case, 在这种情况下,

dbinom(95,n,p)

is 0.05334916. 是0.05334916。 So, binom.test looks for the values of x such that dbinom(x,n,p) is less than 0.05334916. 因此,binom.test查找x的值,使得dbinom(x,n,p)小于0.05334916。 It turns out that those are 0:95 and 104:150. 事实证明那些是0:95和104:150。 So, binom.test returns the value of 所以,binom.test返回值

sum(dbinom(0:95,n,p)) + sum(dbinom(104:150,n,p))

which is 0.4914044. 这是0.4914044。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM