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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With