EVOLUTION-MANAGER
Edit File: GammaDist.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: The Gamma Distribution</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for GammaDist {stats}"><tr><td>GammaDist {stats}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>The Gamma Distribution</h2> <h3>Description</h3> <p>Density, distribution function, quantile function and random generation for the Gamma distribution with parameters <code>shape</code> and <code>scale</code>. </p> <h3>Usage</h3> <pre> 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) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x, q</code></td> <td> <p>vector of quantiles.</p> </td></tr> <tr valign="top"><td><code>p</code></td> <td> <p>vector of probabilities.</p> </td></tr> <tr valign="top"><td><code>n</code></td> <td> <p>number of observations. If <code>length(n) > 1</code>, the length is taken to be the number required.</p> </td></tr> <tr valign="top"><td><code>rate</code></td> <td> <p>an alternative way to specify the scale.</p> </td></tr> <tr valign="top"><td><code>shape, scale</code></td> <td> <p>shape and scale parameters. Must be positive, <code>scale</code> strictly.</p> </td></tr> <tr valign="top"><td><code>log, log.p</code></td> <td> <p>logical; if <code>TRUE</code>, probabilities/densities <i>p</i> are returned as <i>log(p)</i>.</p> </td></tr> <tr valign="top"><td><code>lower.tail</code></td> <td> <p>logical; if TRUE (default), probabilities are <i>P[X ≤ x]</i>, otherwise, <i>P[X > x]</i>.</p> </td></tr> </table> <h3>Details</h3> <p>If <code>scale</code> is omitted, it assumes the default value of <code>1</code>. </p> <p>The Gamma distribution with parameters <code>shape</code> <i>= a</i> and <code>scale</code> <i>= s</i> has density </p> <p style="text-align: center;"><i>f(x)= 1/(s^a Gamma(a)) x^(a-1) e^-(x/s)</i></p> <p>for <i>x ≥ 0</i>, <i>a > 0</i> and <i>s > 0</i>. (Here <i>Gamma(a)</i> is the function implemented by <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span>'s <code><a href="../../base/html/Special.html">gamma</a>()</code> and defined in its help. Note that <i>a = 0</i> corresponds to the trivial distribution with all mass at point 0.) </p> <p>The mean and variance are <i>E(X) = a*s</i> and <i>Var(X) = a*s^2</i>. </p> <p>The cumulative hazard <i>H(t) = - log(1 - F(t))</i> is </p> <pre>-pgamma(t, ..., lower = FALSE, log = TRUE) </pre> <p>Note that for smallish values of <code>shape</code> (and moderate <code>scale</code>) a large parts of the mass of the Gamma distribution is on values of <i>x</i> so near zero that they will be represented as zero in computer arithmetic. So <code>rgamma</code> may well return values which will be represented as zero. (This will also happen for very large values of <code>scale</code> since the actual generation is done for <code>scale = 1</code>.) </p> <h3>Value</h3> <p><code>dgamma</code> gives the density, <code>pgamma</code> gives the distribution function, <code>qgamma</code> gives the quantile function, and <code>rgamma</code> generates random deviates. </p> <p>Invalid arguments will result in return value <code>NaN</code>, with a warning. </p> <p>The length of the result is determined by <code>n</code> for <code>rgamma</code>, and is the maximum of the lengths of the numerical arguments for the other functions. </p> <p>The numerical arguments other than <code>n</code> are recycled to the length of the result. Only the first elements of the logical arguments are used. </p> <h3>Note</h3> <p>The S (Becker <em>et al</em>, 1988) parametrization was via <code>shape</code> and <code>rate</code>: S had no <code>scale</code> parameter. It is an error to supply and <code>scale</code> and <code>rate</code>. </p> <p><code>pgamma</code> is closely related to the incomplete gamma function. As defined by Abramowitz and Stegun 6.5.1 (and by ‘Numerical Recipes’) this is </p> <p style="text-align: center;"><i>P(a,x) = 1/Gamma(a) integral_0^x t^(a-1) exp(-t) dt</i></p> <p><i>P(a, x)</i> is <code>pgamma(x, a)</code>. Other authors (for example Karl Pearson in his 1922 tables) omit the normalizing factor, defining the incomplete gamma function <i>γ(a,x)</i> as <i>gamma(a,x) = integral_0^x t^(a-1) exp(-t) dt,</i> i.e., <code>pgamma(x, a) * gamma(a)</code>. Yet other use the ‘upper’ incomplete gamma function, </p> <p style="text-align: center;"><i>Gamma(a,x) = integral_x^Inf t^(a-1) exp(-t) dt,</i></p> <p>which can be computed by <code>pgamma(x, a, lower = FALSE) * gamma(a)</code>. </p> <p>Note however that <code>pgamma(x, a, ..)</code> currently requires <i>a > 0</i>, whereas the incomplete gamma function is also defined for negative <i>a</i>. In that case, you can use <code>gamma_inc(a,x)</code> (for <i>Γ(a,x)</i>) from package <a href="https://CRAN.R-project.org/package=gsl"><span class="pkg">gsl</span></a>. </p> <p>See also <a href="https://en.wikipedia.org/wiki/Incomplete_gamma_function">https://en.wikipedia.org/wiki/Incomplete_gamma_function</a>, or <a href="http://dlmf.nist.gov/8.2#i">http://dlmf.nist.gov/8.2#i</a>. </p> <h3>Source</h3> <p><code>dgamma</code> is computed via the Poisson density, using code contributed by Catherine Loader (see <code><a href="Binomial.html">dbinom</a></code>). </p> <p><code>pgamma</code> uses an unpublished (and not otherwise documented) algorithm ‘mainly by Morten Welinder’. </p> <p><code>qgamma</code> is based on a C translation of </p> <p>Best, D. J. and D. E. Roberts (1975). Algorithm AS91. Percentage points of the chi-squared distribution. <em>Applied Statistics</em>, <b>24</b>, 385–388. </p> <p>plus a final Newton step to improve the approximation. </p> <p><code>rgamma</code> for <code>shape >= 1</code> uses </p> <p>Ahrens, J. H. and Dieter, U. (1982). Generating gamma variates by a modified rejection technique. <em>Communications of the ACM</em>, <b>25</b>, 47–54, </p> <p>and for <code>0 < shape < 1</code> uses </p> <p>Ahrens, J. H. and Dieter, U. (1974). Computer methods for sampling from gamma, beta, Poisson and binomial distributions. <em>Computing</em>, <b>12</b>, 223–246. </p> <h3>References</h3> <p>Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988). <em>The New S Language</em>. Wadsworth & Brooks/Cole. </p> <p>Shea, B. L. (1988). Algorithm AS 239: Chi-squared and incomplete Gamma integral, <em>Applied Statistics (JRSS C)</em>, <b>37</b>, 466–473. doi: <a href="https://doi.org/10.2307/2347328">10.2307/2347328</a>. </p> <p>Abramowitz, M. and Stegun, I. A. (1972) <em>Handbook of Mathematical Functions.</em> New York: Dover. Chapter 6: Gamma and Related Functions. </p> <p>NIST Digital Library of Mathematical Functions. <a href="http://dlmf.nist.gov/">http://dlmf.nist.gov/</a>, section 8.2. </p> <h3>See Also</h3> <p><code><a href="../../base/html/Special.html">gamma</a></code> for the gamma function. </p> <p><a href="Distributions.html">Distributions</a> for other standard distributions, including <code><a href="Beta.html">dbeta</a></code> for the Beta distribution and <code><a href="Chisquare.html">dchisq</a></code> for the chi-squared distribution which is a special case of the Gamma distribution. </p> <h3>Examples</h3> <pre> -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 </pre> <hr /><div style="text-align: center;">[Package <em>stats</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>