# Gamma: The Gamma Distribution. In ExtDist: Extending the Range of Functions for Probability Distributions

## Description

Density, distribution, quantile, random number generation, and parameter estimation functions for the gamma distribution with parameters `shape` and `scale`. Parameter estimation can be based on a weighted or unweighted i.i.d sample and can be carried out numerically.

## Usage

 ``` 1 2 3 4 5 6 7 8 9 10 11 12``` ```dGamma(x, shape = 2, scale = 2, params = list(shape = 2, scale = 2), ...) pGamma(q, shape = 2, scale = 2, params = list(shape = 2, scale = 2), ...) qGamma(p, shape = 2, scale = 2, params = list(shape = 2, scale = 2), ...) rGamma(n, shape = 2, scale = 2, params = list(shape = 2, scale = 2), ...) eGamma(X, w, method = c("moments", "numerical.MLE"), ...) lGamma(X, w, shape = 2, scale = 2, params = list(shape = 2, scale = 2), logL = TRUE, ...) ```

## Arguments

 `x,q` A vector of quantiles. `shape` Shape parameter. `scale` Scale parameter. `params` A list that includes all named parameters `...` Additional parameters. `p` A vector of probabilities. `n` Number of observations. `X` Sample observations. `w` An optional vector of sample weights. `method` Parameter estimation method. `logL` logical; if TRUE, lBeta_ab gives the log-likelihood, otherwise the likelihood is given.

## Details

The `dGamma()`, `pGamma()`, `qGamma()`,and `rGamma()` functions serve as wrappers of the standard `dgamma`, `pgamma`, `qgamma`, and `rgamma` functions in the stats package. They allow for the parameters to be declared not only as individual numerical values, but also as a list so parameter estimation can be carried out.

The gamma distribution with parameter `shape`=α and `scale`=β has probability density function,

f(x)= (1/β^α Γ(α))x^{α-1}e^{-x/β}

where α > 0 and β > 0. Parameter estimation can be performed using the method of moments as given by Johnson et.al (pp.356-357).

The log-likelihood function of the gamma distribution is given by,

l(α, β |x) = (α -1) ∑_i ln(x_i) - ∑_i(x_i/β) -nα ln(β) + n ln Γ(α)

where Γ is the gamma function. The score function is provided by Rice (2007), p.270.

## Value

dGamma gives the density, pGamma the distribution function, qGamma the quantile function, rGamma generates random deviates, and eGamma estimates the distribution parameters.lgamma provides the log-likelihood function.

## Author(s)

Haizhen Wu and A. Jonathan R. Godfrey.
Updates and bug fixes by Sarah Pirikahu.

## References

Johnson, N. L., Kotz, S. and Balakrishnan, N. (1995) Continuous Univariate Distributions, volume 1, chapter 17, Wiley, New York.

Bury, K. (1999) Statistical Distributions in Engineering, Chapter 13, pp.225-226, Cambridge University Press.

Rice, J.A. (2007) Mathematical Statistics and Data Analysis, 3rd Ed, Brookes/Cole.

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36``` ```# Parameter estimation for a distribution with known shape parameters X <- rGamma(n=500, shape=1.5, scale=0.5) est.par <- eGamma(X); est.par plot(est.par) # Fitted density curve and histogram den.x <- seq(min(X),max(X),length=100) den.y <- dGamma(den.x,shape=est.par\$shape,scale=est.par\$scale) hist(X, breaks=10, probability=TRUE, ylim = c(0,1.1*max(den.y))) lines(den.x, den.y, col="blue") lines(density(X), lty=2) # Extracting shape or scale parameters est.par[attributes(est.par)\$par.type=="shape"] est.par[attributes(est.par)\$par.type=="scale"] # Parameter estimation for a distribution with unknown shape parameters # Example from: Bury(1999) pp.225-226, parameter estimates as given by Bury are # shape = 6.40 and scale=2.54. The log-likelihood for this data given # Bury's parameter estimates is -656.7921. data <- c(16, 11.6, 19.9, 18.6, 18, 13.1, 29.1, 10.3, 12.2, 15.6, 12.7, 13.1, 19.2, 19.5, 23, 6.7, 7.1, 14.3, 20.6, 25.6, 8.2, 34.4, 16.1, 10.2, 12.3) est.par <- eGamma(data, method="numerical.MLE"); est.par plot(est.par) # Estimates calculated by eGamma differ from those given by Bury(1999). # However, eGamma's parameter estimates appear to be an improvement, due to a larger # log-likelihood of -80.68186 (as given by lGamma below). # log-likelihood lGamma(data,param = est.par) # Evaluating the precision of the parameter estimates by the Hessian matrix H <- attributes(est.par)\$nll.hessian var <- solve(H) se <- sqrt(diag(var));se ```