Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a discrete normal distribution in R?

Tags:

r

statistics

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.

like image 624
JDiaz Avatar asked Sep 24 '15 15:09

JDiaz


People also ask

Can you do normal distribution with discrete data?

Normal distribution is strictly only applicable for data that is continuous though in some cases we can use the normal distribution to approximate data that is discrete.

How do you generate a random sample from a normal distribution in R?

Random numbers from a normal distribution can be generated using rnorm() function. We need to specify the number of samples to be generated. We can also specify the mean and standard deviation of the distribution. If not provided, the distribution defaults to 0 mean and 1 standard deviation.

How does Rnorm work in R?

rnorm is the R function that simulates random variates having a specified normal distribution. As with pnorm , qnorm , and dnorm , optional arguments specify the mean and standard deviation of the distribution.


1 Answers

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.

like image 194
LyzandeR Avatar answered Oct 20 '22 17:10

LyzandeR