为什么在pbinom()中np的小舍入误差会导致计算的p值出现相对较大的误差?

huangapple go评论90阅读模式
英文:

Why does a small rounding error for np in pbinom() generate a relatively large error in the calculated p?

问题

我有一个计算二项分布概率的函数。用户可以输入一个样本概率。假设他们有35次试验中的25次成功。他们计算出概率为0.7143,并将其输入到函数中。

由于我使用了pbinom()函数,我将把该概率乘以样本大小以获得分位数。看一下当我在这里重新计算数学时的差异,首先是用计算的分位数,然后是实际计数。

> 2*pbinom(.7143*35,35,.5,lower.tail = FALSE)+dbinom(.7143*35,35,.5)
[1] 0.005988121
Warning message:
In dbinom(0.7143 * 35, 35, 0.5) : non-integer x = 25.000500

> 2*pbinom(25,35,.5,lower.tail = FALSE)+dbinom(25,35,.5)
[1] 0.01133098

尾部的这种差异可能会导致不同的结论。

这里有一个远离尾部的例子,有35次试验中的18次成功:

> 2*pbinom(.5143*35,35,.5,lower.tail = FALSE)+dbinom(.5143*35,35,.5)
[1] 0.7358788
Warning message:
In dbinom(0.5143 * 35, 35, 0.5) : non-integer x = 18.000500
> 2*pbinom(18,35,.5,lower.tail = FALSE)+dbinom(18,35,.5)
[1] 0.8679394

这是p值的13%差异。

我理解为什么会出现警告消息,我可以通过四舍五入轻松修复它,但为什么分位数中的微小误差0.0005会如此显著地影响计算出的p值呢?

英文:

I have a function to calculate probabilities for a binomial distribution. The user could enter a sample probability. Let's say they had 25 out of 35. They calculate a probability as 0.7143 and enter that into the function.

Since I am using pbinom() I'll multiply that probability by the sample size to get the quantile. Look at the differences when I reproduce the math here, first with a calculated quantile and second with the actual count.

> 2*pbinom(.7143*35,35,.5,lower.tail = FALSE)+dbinom(.7143*35,35,.5)
[1] 0.005988121
Warning message:
In dbinom(0.7143 * 35, 35, 0.5) : non-integer x = 25.000500

> 2*pbinom(25,35,.5,lower.tail = FALSE)+dbinom(25,35,.5)
[1] 0.01133098

A difference of that amount in the tail could lead to a different conclusion.

Here is an example away from the tails with 18 out of 35:

> 2*pbinom(.5143*35,35,.5,lower.tail = FALSE)+dbinom(.5143*35,35,.5)
[1] 0.7358788
Warning message:
In dbinom(0.5143 * 35, 35, 0.5) : non-integer x = 18.000500
> 2*pbinom(18,35,.5,lower.tail = FALSE)+dbinom(18,35,.5)
[1] 0.8679394

That is a difference of 13% in the p-value.

I understand why the warning message appears and I can easily fix it by rounding, but why does a tiny error of 0.0005 in the quantile affect the calculated p-value so much?

答案1

得分: 3

如果x的元素不是整数,dbinom的结果会是零,并伴有警告。这不是数值错误,而是如果你给定一个非整数给dbinom,它将始终返回0,并附带警告。因此,确保输入是整数是解决方案。

英文:

There is a comment in the documentation that directly addresses this:

If an element of x is not integer, the result of dbinom is zero, with a warning.

So it's not numerical error, per se, it's that if you give dbinom a non-integer it will always return 0, with a warning. So yes, the solution is to always check your inputs to ensure they are integers.

huangapple
  • 本文由 发表于 2023年5月25日 01:57:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定