Skip to contents

The Gamma distribution is an absolute continuous probability distribution with two parameters: shape \(\alpha > 0\) and scale \(\beta > 0\).

Usage

Gam(shape = 1, scale = 1)

# S4 method for class 'Gam,numeric'
d(distr, x, log = FALSE)

# S4 method for class 'Gam,numeric'
p(distr, q, lower.tail = TRUE, log.p = FALSE)

# S4 method for class 'Gam,numeric'
qn(distr, p, lower.tail = TRUE, log.p = FALSE)

# S4 method for class 'Gam,numeric'
r(distr, n)

# S4 method for class 'Gam'
mean(x)

# S4 method for class 'Gam'
median(x)

# S4 method for class 'Gam'
mode(x)

# S4 method for class 'Gam'
var(x)

# S4 method for class 'Gam'
sd(x)

# S4 method for class 'Gam'
skew(x)

# S4 method for class 'Gam'
kurt(x)

# S4 method for class 'Gam'
entro(x)

# S4 method for class 'Gam'
finf(x)

llgamma(x, shape, scale)

# S4 method for class 'Gam,numeric'
ll(distr, x)

egamma(x, type = "mle", ...)

# S4 method for class 'Gam,numeric'
mle(
  distr,
  x,
  par0 = "same",
  method = "L-BFGS-B",
  lower = 1e-05,
  upper = Inf,
  na.rm = FALSE
)

# S4 method for class 'Gam,numeric'
me(distr, x, na.rm = FALSE)

# S4 method for class 'Gam,numeric'
same(distr, x, na.rm = FALSE)

vgamma(shape, scale, type = "mle")

# S4 method for class 'Gam'
avar_mle(distr)

# S4 method for class 'Gam'
avar_me(distr)

# S4 method for class 'Gam'
avar_same(distr)

Arguments

shape, scale

numeric. The non-negative distribution parameters.

distr

an object of class Gam.

x

For the density function, x is a numeric vector of quantiles. For the moments functions, x is an object of class Gam. For the log-likelihood and the estimation functions, x is the sample of observations.

log, log.p

logical. Should the logarithm of the probability be returned?

q

numeric. Vector of quantiles.

lower.tail

logical. If TRUE (default), probabilities are \(P(X \leq x)\), otherwise \(P(X > x)\).

p

numeric. Vector of probabilities.

n

number of observations. If length(n) > 1, the length is taken to be the number required.

type

character, case ignored. The estimator type (mle, me, or same).

...

extra arguments.

par0, method, lower, upper

arguments passed to optim for the mle optimization. See Details.

na.rm

logical. Should the NA values be removed?

Value

Each type of function returns a different type of object:

  • Distribution Functions: When supplied with one argument (distr), the d(), p(), q(), r(), ll() functions return the density, cumulative probability, quantile, random sample generator, and log-likelihood functions, respectively. When supplied with both arguments (distr and x), they evaluate the aforementioned functions directly.

  • Moments: Returns a numeric, either vector or matrix depending on the moment and the distribution. The moments() function returns a list with all the available methods.

  • Estimation: Returns a list, the estimators of the unknown parameters. Note that in distribution families like the binomial, multinomial, and negative binomial, the size is not returned, since it is considered known.

  • Variance: Returns a named matrix. The asymptotic covariance matrix of the estimator.

Details

The probability density function (PDF) of the Gamma distribution is given by: $$ f(x; \alpha, \beta) = \frac{\beta^{-\alpha} x^{\alpha-1} e^{-x/\beta}}{\Gamma(\alpha)}, \quad x > 0. $$

The MLE of the gamma distribution parameters is not available in closed form and has to be approximated numerically. This is done with optim(). The optimization can be performed on the shape parameter \(\alpha\in(0,+\infty)\). The default method used is the L-BFGS-B method with lower bound 1e-5 and upper bound Inf. The par0 argument can either be a numeric (satisfying lower <= par0 <= upper) or a character specifying the closed-form estimator to be used as initialization for the algorithm ("me" or "same" - the default value).

References

  • Wiens, D. P., Cheng, J., & Beaulieu, N. C. (2003). A class of method of moments estimators for the two-parameter gamma family. Pakistan Journal of Statistics, 19(1), 129-141.

  • Ye, Z. S., & Chen, N. (2017). Closed-form estimators for the gamma distribution derived from likelihood equations. The American Statistician, 71(2), 177-181.

  • Tamae, H., Irie, K. & Kubokawa, T. (2020), A score-adjusted approach to closed-form estimators for the gamma and beta distributions, Japanese Journal of Statistics and Data Science 3, 543–561.

  • Papadatos, N. (2022), On point estimators for gamma and beta distributions, arXiv preprint arXiv:2205.10799.

See also

Functions from the stats package: dgamma(), pgamma(), qgamma(), rgamma()

Examples

# -----------------------------------------------------
# Gamma Distribution Example
# -----------------------------------------------------

# Create the distribution
a <- 3 ; b <- 5
D <- Gam(a, b)

# ------------------
# dpqr Functions
# ------------------

d(D, c(0.3, 2, 10)) # density function
#> [1] 0.0003390352 0.0107251207 0.0541341133
p(D, c(0.3, 2, 10)) # distribution function
#> [1] 3.441824e-05 7.926332e-03 3.233236e-01
qn(D, c(0.4, 0.8)) # inverse distribution function
#> [1] 11.42538 21.39515
x <- r(D, 100) # random generator function

# alternative way to use the function
df <- d(D) ; df(x) # df is a function itself
#>   [1] 0.035895119 0.053743332 0.039414356 0.014069829 0.019256917 0.046398545
#>   [7] 0.054101078 0.054131386 0.007247814 0.007983775 0.029427730 0.038404705
#>  [13] 0.051540371 0.054080143 0.001593671 0.050292018 0.049361212 0.042934341
#>  [19] 0.029186593 0.007249341 0.042111499 0.053804345 0.035352916 0.020472016
#>  [25] 0.017284996 0.053192521 0.048084602 0.020601132 0.051059421 0.050945937
#>  [31] 0.047564047 0.050177120 0.028617257 0.054035666 0.047765225 0.049933452
#>  [37] 0.037612929 0.053398545 0.048667246 0.044698009 0.038693718 0.039031014
#>  [43] 0.041049829 0.052849212 0.028021300 0.025704862 0.046565777 0.052298014
#>  [49] 0.034296820 0.046681135 0.052304951 0.048938425 0.046322748 0.026345973
#>  [55] 0.051484673 0.029348053 0.052094877 0.030077529 0.003493075 0.051987391
#>  [61] 0.022624384 0.050663301 0.019786741 0.043953325 0.046104069 0.044957171
#>  [67] 0.053287657 0.028183125 0.053756380 0.028061800 0.047933534 0.049107138
#>  [73] 0.043852622 0.052547558 0.019640593 0.035400255 0.038365867 0.047724512
#>  [79] 0.032135621 0.053975057 0.054004620 0.036656794 0.041618440 0.009294019
#>  [85] 0.003494763 0.050843067 0.046632274 0.052332642 0.049976857 0.050270826
#>  [91] 0.028996493 0.001805018 0.049599480 0.053183519 0.052404912 0.007006395
#>  [97] 0.052995803 0.038592712 0.053301933 0.037254951

# ------------------
# Moments
# ------------------

mean(D) # Expectation
#> [1] 15
median(D) # Median
#> [1] 13.3703
mode(D) # Mode
#> [1] 0.4
var(D) # Variance
#> [1] 75
sd(D) # Standard Deviation
#> [1] 8.660254
skew(D) # Skewness
#> [1] 1.154701
kurt(D) # Excess Kurtosis
#> [1] 2
entro(D) # Entropy
#> [1] 3.457016
finf(D) # Fisher Information Matrix
#>           shape scale
#> shape 0.3949341  0.20
#> scale 0.2000000  0.12

# List of all available moments
mom <- moments(D)
mom$mean # expectation
#> [1] 15

# ------------------
# Point Estimation
# ------------------

ll(D, x)
#> [1] -339.6007
llgamma(x, a, b)
#> [1] -339.6007

egamma(x, type = "mle")
#> $shape
#> [1] 3.914281
#> 
#> $scale
#> [1] 3.923344
#> 
egamma(x, type = "me")
#> $shape
#> [1] 3.646524
#> 
#> $scale
#> [1] 4.211428
#> 
egamma(x, type = "same")
#> $shape
#> [1] 3.846913
#> 
#> $scale
#> [1] 3.992051
#> 

mle(D, x)
#> $shape
#> [1] 3.914281
#> 
#> $scale
#> [1] 3.923344
#> 
me(D, x)
#> $shape
#> [1] 3.646524
#> 
#> $scale
#> [1] 4.211428
#> 
same(D, x)
#> $shape
#> [1] 3.846913
#> 
#> $scale
#> [1] 3.992051
#> 
e(D, x, type = "mle")
#> $shape
#> [1] 3.914281
#> 
#> $scale
#> [1] 3.923344
#> 

mle("gam", x) # the distr argument can be a character
#> $shape
#> [1] 3.914281
#> 
#> $scale
#> [1] 3.923344
#> 

# ------------------
# Estimator Variance
# ------------------

vgamma(a, b, type = "mle")
#>           shape     scale
#> shape  16.23357 -27.05595
#> scale -27.05595  53.42659
vgamma(a, b, type = "me")
#>       shape scale
#> shape    24   -40
#> scale   -40    75
vgamma(a, b, type = "same")
#>           shape     scale
#> shape  16.66322 -27.77203
#> scale -27.77203  54.62006

avar_mle(D)
#>           shape     scale
#> shape  16.23357 -27.05595
#> scale -27.05595  53.42659
avar_me(D)
#>       shape scale
#> shape    24   -40
#> scale   -40    75
avar_same(D)
#>           shape     scale
#> shape  16.66322 -27.77203
#> scale -27.77203  54.62006

v(D, type = "mle")
#>           shape     scale
#> shape  16.23357 -27.05595
#> scale -27.05595  53.42659