How to create a discrete normal distribution in R?

╄→尐↘猪︶ㄣ 提交于 2020-01-03 20:57:57

问题


I am trying to create a discrete normal distribution using something such as

x <- rnorm(1000, mean = 350, sd = 20) 

but I don't think the rnorm function has a built in "discrete numbers only" option. I have spent a few hours trying to search this on StackOverflow, Google and R documentation but have yet to find anything.


回答1:


Obviously, there is no discrete normal distribution as by default it is continuous. However, as mentioned here (Wikipedia is not the best possible source but this is correct anyway):

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

This can be seen with a quick example:

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)))

Plot:

The graph on the left (binomial) certainly approximates the right (normal) and this will get more obvious as n goes to Inf.

As you will see rbinom(1000, 30, p=0.25) will produce discrete values (integers). Also, density is probably not the best function to use on a discrete variable, but it proves the point here.




回答2:


if you want to generate a set of random integers following a normal distribution you could simply round them like so...

    round(rnorm(10, 5, 1), 0)


来源:https://stackoverflow.com/questions/32764816/how-to-create-a-discrete-normal-distribution-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!