JDiaz
JDiaz

Reputation: 147

How to create a discrete normal distribution in R?

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.

Upvotes: 7

Views: 9230

Answers (3)

Mainul Islam
Mainul Islam

Reputation: 1276

library(extraDistr)
set.seed(12)
rdnorm(10)

Upvotes: 0

roman
roman

Reputation: 1370

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)

Upvotes: 5

LyzandeR
LyzandeR

Reputation: 37879

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.

Upvotes: 7

Related Questions