# gamma distribution in R

Gamma distribution plays an important part in the statistical field. In this article, we will explain to you concepts and formulas about the gamma distribution and provide family gamma functions to visualize the distribution in R.

## What is the gamma distribution?

The gamma distribution is a continuous probability distribution defined by 2 parameters called shape (alpha) and inverse scale (beta). This article will sign alpha as \alpha and beta as \beta .

The gamma function

The gamma function is defined as \Gamma(n) \\

If n is an integer, the gamma function is: \Gamma(n)=(n-1)! \\

If n is a positive real number, the gamma function is: \Gamma(n)= \int\limits_{0}^{\infty} \dfrac{\mathrm{x}^{n-1}}{\mathrm{e}^{-x}}\;\mathrm{d}x \\

The gamma distribution function

The gamma distribution of a continuous random variable with parameters shape (\alpha>0) and scale (\beta>0) is shown as x \sim Gamma(\alpha, \beta)

The probability density function (PDF):

f(x)=\frac{\beta^{\alpha}x^{\alpha - 1}e^{- \beta x}}{{\Gamma(\alpha)}, } \text{ if }{x > 0}, otherwise f(x) = 0

The cumulative density function (CDF):

\text{f(x) = } \int\limits_{0}^{x} \frac{\beta^{\alpha}t^{\alpha - 1}e^{- \beta t}}{\Gamma(\alpha)}\;\mathrm{d}x \\

## The gamma family functions

The gamma family functions consist of 4 functions are:

• dgamma(x, shape, rate)
• pgamma(x, shape, rate)
• qgamma(x, shape, rate)
• rgamma(n, shape, rate)

The first, second, and third functions receive parameter x as a vector of random variables, while the last function receives parameter n as the number of random variables.

### The dgamma() function

The dgamma() function calculates the probability density function’s value of the distribution.

Code:

# Create a vector of variables
x <- seq(0, 5, by=0.001)

# Calculate the probability density
y <- dgamma(x, shape=5, rate = 2)

# Visualize the gamma density
plot(x, y)

Result:

### The pgamma() function

The pgamma() function calculates the cumulative density function’s value of the distribution.

Code:

# Create a vector of variables
x <- seq(0, 5, by=0.001)

# Calculate the cumulative density
y <- pgamma(x, shape=5, rate = 2)

# Visualize the cumulative gamma density
plot(x, y)

Result:

### The qgamma() function

The pgamma() function creates a quantile-quantile (q-q) plot of the distribution.

Code:

# Create a vector of variables
x <- seq(0, 1, by=0.001)

# Calculate the quantile-quantile (q-q) plot
y <- qgamma(x, shape=5)

# Visualize the quantile-quantile (q-q) plot
plot(x, y)

Result:

### The rgamma() function

The rgamma() functions create a set of random variables.

Code:

# Create reproducible results
set.seed(1)

# Create 3000 random variables
x <- rgamma(n=3000, shape=5, rate=3)

# Visualize the random variables
hist(x, main = "Random variables x")

Result:

## Summary

In summary, the gamma distribution of a random variable x is defined by two parameters: shape and scale. In R, the gamma functions provide useful methods to calculate the density functions, quantile-quantile (q-q) plot, and the random variables as well.

Maybe you are interested:

Posted in R