如何在 R 中创建离散正态分布?

How to create a discrete normal distribution in R?

我正在尝试使用诸如

之类的东西来创建离散正态分布
x <- rnorm(1000, mean = 350, sd = 20) 

但我认为 rnorm 函数没有内置 "discrete numbers only" 选项。我花了几个小时试图在 Whosebug、Google 和 R 文档上搜索它,但还没有找到任何东西。

显然,没有离散的正态分布,默认情况下它是连续的。然而,如前所述 here(维基百科不是最好的来源,但无论如何这是正确的):

If n is large enough, then the skew of the distribution is not too great. In this case a reasonable approximation to B(n, p) is given by the normal distribution

这可以通过一个简单的例子看出:

par(mfrow=c(1,2) )
#values generated by a binomial distribution
plot(density(rbinom(1000, 30, p=0.25)))
#values generated by a normal distribution
plot(density(rnorm(1000)))

剧情:

左边的图(二项式)肯定近似于右边的(正态)并且随着 n goes to Inf.

这会变得更加明显

如您所见,rbinom(1000, 30, p=0.25) 将产生离散值(整数)。此外,密度可能不是用于离散变量的最佳函数,但它证明了这一点。

如果你想生成一组服从正态分布的随机整数,你可以像这样简单地将它们四舍五入...

    round(rnorm(10, 5, 1), 0)
library(extraDistr)
set.seed(12)
rdnorm(10)