Search code examples
rrandomstatisticsprobabilitydistribution

Sample from the Erlang distribution in R


The Erlang distribution has two parameters: natural number k (shape parameter) and real number lambda (rate parameter). How can you take a random sample of size n from the Erlang distribution, using R without exotic packages?


Solution

  • The trick to this question is to note that the Erlang distribution is a special case of the Gamma distribution. Sampling from the gamma distribution is implemented in the stats package.

    The following function returns a sample of size n from the Erlang(k, lambda) distribution in a standard R vector:

    rgamma(n, shape=k, rate = lambda)

    n is the size of the sample.

    shape is the parameter k. This is the shape parameter of the Erlang distribution (for Erlang, this must be a natural number >=1).

    rate is the rate parameter for the Erlang distribution.