Density, distribution function, quantile function and random
generation for the Gamma distribution with parameters
1 2 3 4 5 6
vector of quantiles.
vector of probabilities.
number of observations. If
an alternative way to specify the scale.
shape and scale parameters. Must be positive,
logical; if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x].
scale is omitted, it assumes the default value of
The Gamma distribution with parameters
shape = a
scale = s has density
f(x)= 1/(s^a Gamma(a)) x^(a-1) e^-(x/s)
for x ≥ 0, a > 0 and s > 0.
(Here Gamma(a) is the function implemented by R's
gamma() and defined in its help. Note that a = 0
corresponds to the trivial distribution with all mass at point 0.)
The mean and variance are E(X) = a*s and Var(X) = a*s^2.
The cumulative hazard H(t) = - log(1 - F(t)) is
Note that for smallish values of
shape (and moderate
scale) a large parts of the mass of the Gamma distribution is
on values of x so near zero that they will be represented as
zero in computer arithmetic. So
rgamma may well return values
which will be represented as zero. (This will also happen for very
large values of
scale since the actual generation is done for
scale = 1.)
dgamma gives the density,
pgamma gives the distribution function,
qgamma gives the quantile function, and
rgamma generates random deviates.
Invalid arguments will result in return value
NaN, with a warning.
The length of the result is determined by
rgamma, and is the maximum of the lengths of the
numerical arguments for the other functions.
The numerical arguments other than
n are recycled to the
length of the result. Only the first elements of the logical
arguments are used.
The S (Becker et al (1988) parametrization was via
rate: S had no
scale parameter. It is an error
to supply and
pgamma is closely related to the incomplete gamma function. As
defined by Abramowitz and Stegun 6.5.1 (and by ‘Numerical
Recipes’) this is
P(a,x) = 1/Gamma(a) integral_0^x t^(a-1) exp(-t) dt
P(a, x) is
pgamma(x, a). Other authors (for example
Karl Pearson in his 1922 tables) omit the normalizing factor,
defining the incomplete gamma function γ(a,x) as
integral_0^x t^(a-1) exp(-t) dt, i.e.,
pgamma(x, a) * gamma(a).
Yet other use the ‘upper’ incomplete gamma function,
Gamma(a,x) = integral_x^Inf t^(a-1) exp(-t) dt,
which can be computed by
pgamma(x, a, lower = FALSE) * gamma(a).
Note however that
pgamma(x, a, ..) currently requires a > 0,
whereas the incomplete gamma function is also defined for negative
a. In that case, you can use
Γ(a,x)) from package gsl.
dgamma is computed via the Poisson density, using code contributed
by Catherine Loader (see
pgamma uses an unpublished (and not otherwise documented)
algorithm ‘mainly by Morten Welinder’.
qgamma is based on a C translation of
Best, D. J. and D. E. Roberts (1975). Algorithm AS91. Percentage points of the chi-squared distribution. Applied Statistics, 24, 385–388.
plus a final Newton step to improve the approximation.
shape >= 1 uses
Ahrens, J. H. and Dieter, U. (1982). Generating gamma variates by a modified rejection technique. Communications of the ACM, 25, 47–54,
0 < shape < 1 uses
Ahrens, J. H. and Dieter, U. (1974). Computer methods for sampling from gamma, beta, Poisson and binomial distributions. Computing, 12, 223–246.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
Shea, B. L. (1988) Algorithm AS 239, Chi-squared and incomplete Gamma integral, Applied Statistics (JRSS C) 37, 466–473.
Abramowitz, M. and Stegun, I. A. (1972) Handbook of Mathematical Functions. New York: Dover. Chapter 6: Gamma and Related Functions.
NIST Digital Library of Mathematical Functions. http://dlmf.nist.gov/, section 8.2.
gamma for the gamma function.
Distributions for other standard distributions, including
dbeta for the Beta distribution and
for the chi-squared distribution which is a special case of the Gamma
1 2 3 4 5 6 7 8 9 10 11
-log(dgamma(1:4, shape = 1)) p <- (1:9)/10 pgamma(qgamma(p, shape = 2), shape = 2) 1 - 1/exp(qgamma(p, shape = 1)) # even for shape = 0.001 about half the mass is on numbers # that cannot be represented accurately (and most of those as zero) pgamma(.Machine$double.xmin, 0.001) pgamma(5e-324, 0.001) # on most machines 5e-324 is the smallest # representable non-zero number table(rgamma(1e4, 0.001) == 0)/1e4
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.