Gamma Distribution in R Programming

The Gamma distribution is a continuous probability distribution that is widely used in various fields such as finance, engineering, and natural sciences. In R programming, you can generate random numbers from a Gamma distribution, calculate the density, distribution function, and quantile function using the built-in functions.

Here is a brief overview of these functions:

  1. rgamma(n, shape, scale = 1): Generates ‘n’ random numbers from a Gamma distribution with the specified ‘shape’ and ‘scale’ parameters.
  2. dgamma(x, shape, scale = 1, log = FALSE): Computes the density function of the Gamma distribution for a given vector ‘x’ with the specified ‘shape’ and ‘scale’ parameters. If ‘log = TRUE’, the logarithm of the density is returned.
  3. pgamma(q, shape, scale = 1, lower.tail = TRUE, log.p = FALSE): Calculates the cumulative distribution function of the Gamma distribution for a given vector ‘q’ with the specified ‘shape’ and ‘scale’ parameters. If ‘lower.tail = FALSE’, the upper tail is considered. If ‘log.p = TRUE’, the logarithm of the probability is returned.
  4. qgamma(p, shape, scale = 1, lower.tail = TRUE, log.p = FALSE): Computes the quantile function (inverse of the cumulative distribution function) of the Gamma distribution for a given vector ‘p’ with the specified ‘shape’ and ‘scale’ parameters. If ‘lower.tail = FALSE’, the upper tail is considered. If ‘log.p = TRUE’, the logarithm of the probability is returned.

Here’s an example demonstrating the use of these functions:

 

 

# Set seed for reproducibility
set.seed(123)

# Parameters for the Gamma distribution
shape <- 2
scale <- 3

# Generate random numbers from a Gamma distribution
n <- 10
random_numbers <- rgamma(n, shape, scale)
print(random_numbers)

# Compute the density function
x <- seq(0, 20, by = 0.1)
density <- dgamma(x, shape, scale)
plot(
x,
density,
type = "l",
main = "Gamma Distribution Density",
xlab = "x",
ylab = "Density"
)

 

# Calculate the cumulative distribution function
cdf <- pgamma(x, shape, scale)
plot(
x,
cdf,
type = "l",
main = "Gamma Distribution CDF",
xlab = "x",
ylab = "CDF"
)

 

# Compute the quantile function
p <- seq(0, 1, by = 0.01)
quantiles <- qgamma(p, shape, scale)
plot(
p,
quantiles,
type = "l",
main = "Gamma Distribution Quantile Function",
xlab = "Probability",
ylab = "Quantiles"
)

 

Understanding the t-distribution in R

How to Calculate Conditional Probability in R?