\Header{GammaDist}{The Gamma Distribution}
\alias{dgamma}{GammaDist}
\alias{pgamma}{GammaDist}
\alias{qgamma}{GammaDist}
\alias{rgamma}{GammaDist}
\keyword{distribution}{GammaDist}
\begin{Description}\relax
Density, distribution function, quantile function and random
generation for the Gamma distribution with parameters \code{shape} and
\code{scale}.
\end{Description}
\begin{Usage}
\begin{verbatim}
dgamma(x, shape, rate = 1, scale = 1/rate, log = FALSE)
pgamma(q, shape, rate = 1, scale = 1/rate, lower.tail = TRUE, log.p = FALSE)
qgamma(p, shape, rate = 1, scale = 1/rate, lower.tail = TRUE, log.p = FALSE)
rgamma(n, shape, rate = 1, scale = 1/rate)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{x, q}] vector of quantiles.
\item[\code{p}] vector of probabilities.
\item[\code{n}] number of observations. If \code{length(n) > 1}, the length
is taken to be the number required.
\item[\code{rate}] an alternative way to specify the scale.
\item[\code{shape, scale}] shape and scale parameters.
\item[\code{log, log.p}] logical; if TRUE, probabilities p are given as log(p).
\item[\code{lower.tail}] logical; if TRUE (default), probabilities are
\eqn{P[X \le x]}{P[X <= x]}, otherwise, \eqn{P[X > x]}{P[X > x]}.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
If \code{scale} is omitted, it assumes the default value of \code{1}.

The Gamma distribution with parameters \code{shape} \eqn{=\alpha}{= a}
and \code{scale} \eqn{=\sigma}{= s} has density
\deqn{
f(x)= \frac{1}{{\sigma}^{\alpha}\Gamma(\alpha)} {x}^{\alpha-1} e^{-x/\sigma}
}{f(x)= 1/(s^a Gamma(a)) x^(a-1) e^-(x/s)}
for \eqn{x > 0}{}, \eqn{\alpha > 0}{a > 0} and \eqn{\sigma > 0}{s > 0}.
The mean and variance are
\eqn{E(X) = \alpha\sigma}{E(X) = a*s} and
\eqn{Var(X) = \alpha\sigma^2}{Var(X) = a*s^2}.

\code{pgamma()} uses algorithm AS 239, see the references.
\end{Details}
\begin{Value}
\code{dgamma} gives the density,
\code{pgamma} gives the distribution function
\code{qgamma} gives the quantile function, and
\code{rgamma} generates random deviates.
\end{Value}
\begin{Note}\relax
The S parametrization is via \code{shape} and \code{rate}: S has no
\code{scale} parameter.

The cumulative hazard \eqn{H(t) = - \log(1 - F(t))}{H(t) = - log(1 - F(t))}
is \code{-pgamma(t, ..., lower = FALSE, log = TRUE)}.

\code{pgamma} is closely related to the incomplete gamma function.  As
defined by Abramowitz and Stegun 6.5.1
\deqn{P(a,x) = \frac{1}{\Gamma(a)} \int_0^x t^{a-1} e^{-t} dt}{P(a,x) =
1/Gamma(a) integral_0^x t^(a-1) exp(-t) dt}
\eqn{P(a, x)}{} is \code{pgamma(x, a)}.  Other authors (for example
Karl Pearson in his 1922 tables) omit the normalizing factor,
defining the incomplete gamma function as \code{pgamma(x, a) * gamma(a)}.
\end{Note}
\begin{References}\relax
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
\emph{The New S Language}.
Wadsworth \& Brooks/Cole.

Shea, B. L. (1988)
Algorithm AS 239,  Chi-squared and Incomplete Gamma Integral,
\emph{Applied Statistics (JRSS C)} \bold{37}, 466--473.

Abramowitz, M. and Stegun, I. A. (1972)
\emph{Handbook of Mathematical Functions.} New York: Dover.
Chapter 6: Gamma and Related Functions.
\end{References}
\begin{SeeAlso}\relax
\code{\Link{gamma}} for the Gamma function, \code{\Link{dbeta}} for
the Beta distribution and \code{\Link{dchisq}} for the chi-squared
distribution which is a special case of the Gamma distribution.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
-log(dgamma(1:4, shape=1))
p <- (1:9)/10
pgamma(qgamma(p,shape=2), shape=2)
1 - 1/exp(qgamma(p, shape=1))
\end{ExampleCode}
\end{Examples}

