Saturday, October 8, 2022

[FIXED] How to create a discrete normal distribution in R?

Issue

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.


Solution

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:

enter image description here

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.



Answered By - LyzandeR
Answer Checked By - Timothy Miller (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.