Gamma Distribution in R Programming - dgamma(), pgamma(), qgamma(), and rgamma() Functions
Last Updated :
17 May, 2025
The Gamma distribution in R Programming Language is defined as a two-parameter family of continuous probability distributions which is used in exponential distribution, Erlang distribution and chi-squared distribution.
Mathematical Implementation of the Gamma Distribution
A random variable X is said to follow a Gamma distribution with shape parameter \alpha (alpha) and rate parameter\beta (beta) if its probability density function (PDF) is defined as:
f(x; \alpha, \beta) = \frac{\beta^\alpha}{\Gamma(\alpha)} x^{\alpha - 1} e^{-\beta x}, \quad \text{for } x > 0, \alpha, \beta > 0
where:
- \alpha > 0is the shape parameter.
- \beta > 0 is the rate parameter (sometimes the scale parameter\theta = 1/\beta is used).
- \Gamma(\alpha) is the gamma function, defined as\Gamma(\alpha) = \int_0^\infty t^{\alpha - 1} e^{-t} dt
Key properties:
- Mean:\mu = \frac{\alpha}{\beta}
- Variance:\sigma^2 = \frac{\alpha}{\beta^2}
Implementation of Gamma Distribution Functions in R
We will implement various gamma distribution functions available in the R programming language and understand their interpretation through visualizations.
1. dgamma() Function
The dgamma() function in R calculates the probability density function (PDF) of the Gamma distribution at specified values.
Syntax:
dgamma(x_dgamma, shape)
Parameters:
x_dgamma
: A numeric vector of values at which to evaluate the gamma density.shape
: The shape parameter (α\alphaα) of the Gamma distribution (must be positive).
Example :
We generate a sequence of values from 0 to 2 and compute the gamma probability density function (PDF) with a shape parameter of 6 at those points. Finally, we plot the computed gamma density values to visualize the distribution.
R
x_dgamma <- seq(0, 2, by = 0.04)
y_dgamma <- dgamma(x_dgamma, shape = 6)
plot(y_dgamma)
Output :
Gamma Distribution in R2. pgamma() Function
The pgamma()
function computes the cumulative distribution function (CDF) of the Gamma distribution at specified values. It returns the probability that a Gamma distributed random variable is less than or equal to each value in the input.
Syntax:
pgamma(x_pgamma, shape)
Parameters:
- x_pgamma: defines gamma function
- shape: gamma density of input values
Example:
We create a sequence of values from 0 to 2 and calculate the gamma cumulative distribution function (CDF) with shape parameter 6 for those values. Then, we plot the CDF values to visualize the accumulation of probability.
R
x_pgamma <- seq(0, 2, by = 0.04)
y_pgamma <- pgamma(x_pgamma, shape = 6)
plot(y_pgamma)
Output:
Cumulative Gamma Distribution Function3. qgamma() Function
The qgamma()
function computes the quantile function (inverse cumulative distribution function) of the Gamma distribution. It returns the value x such that the cumulative probability of the Gamma distribution up to x equals a given probability.
Syntax:
qgamma(x_qgamma, shape)
Parameters:
- x_qgamma: defines gamma function
- shape: gamma density of input values
Example:
We generate a sequence of probabilities from 0 to 1 and compute the gamma quantile function (inverse CDF) with a shape parameter of 6 at those probabilities. The resulting quantiles are then plotted to visualize the relationship between cumulative probabilities and their corresponding gamma distribution values.
R
x_qgamma <- seq(0, 1, by = 0.03)
y_qgamma <- qgamma(x_qgamma, shape = 6)
plot(y_qgamma)
Output:
Quantile Function of Gamma Distribution4. rgamma() Function
The rgamma()
function generates random numbers drawn from a Gamma distribution. It is commonly used for simulations and modeling when random samples from a Gamma distribution are required.
Syntax:
rgamma(N, shape)
Parameters:
- N: gamma distributed values
- shape: gamma density of input values
Example:
We set a seed for reproducibility and generate 800 random values from a gamma distribution with shape parameter 5. A histogram with 500 bins is plotted to visualize the distribution of these random gamma values.
R
set.seed(1200)
N <- 800
y_rgamma <- rgamma(N, shape = 5)
hist(y_rgamma, breaks = 500, main = "")
Output:
Histogram of 100 Gamma Distributed NumbersIn this article, we explored the key functions of the Gamma distribution in R such as dgamma(), pgamma(), qgamma() and rgamma(). We demonstrated how to use these functions for density calculation, cumulative probabilities, quantile estimation and random number generation.
Similar Reads
Compute Beta Distribution in R Programming - dbeta(), pbeta(), qbeta(), and rbeta() Functions
Beta Distribution in R Language is defined as property which represents the possible values of probability. This article is an illustration of dbeta, pbeta, qbeta, and rbeta functions of Beta Distribution. dbeta() Function It is defined as Beta Density function and is used to create beta density val
2 min read
Exponential Distribution in R Programming - dexp(), pexp(), qexp(), and rexp() Functions
The Exponential Distribution is a continuous probability distribution that models the time between independent events occurring at a constant average rate. It is widely used in fields like reliability analysis, queuing theory, and survival analysis. The exponential distribution is a special case of
5 min read
Compute the Logarithmic Derivative of the gamma Function in R Programming - digamma() Function
digamma() function in R Language is used to calculate the logarithmic derivative of the gamma value calculated using the gamma function. digamma Function is basically, digamma(x) = d(ln(factorial(n-1)))/dx Syntax: digamma(x) Parameters: x: Numeric vector Example 1: Python3 1== # R program to find lo
1 min read
Formatting Numbers and Strings in R Programming - format() Function
In R programming, the format() function formats numbers, strings and dates to meet presentation needs. It gives formatting features to modify the display of numeric values, strings and date/time information. This function is applied to regulate the number of decimal places, alignment, scientific not
3 min read
Compute the Second Derivative of the Logarithmic value of the gamma Function in R Programming - trigamma() Function
trigamma() function in R Language is used to calculate the second derivative of the natural logarithm of the gamma value calculated using the gamma function. trigamma Function is basically, trigamma(x) = d2(ln(factorial(n-1)))/dx2 Syntax: trigamma(x) Parameters: x: Numeric vector Example 1: Python3
1 min read
Binomial Distribution in R Programming
Binomial distribution in R is a probability distribution used in statistics. The binomial distribution is a discrete distribution and has only two outcomes i.e. success or failure. All its trials are independent, the probability of success remains the same and the previous outcome does not affect th
3 min read
Perform the Inverse Probability Cumulative Density Analysis on t-Distribution in R Programming - qt() Function
qt() function in R Language is used to return the inverse probability cumulative density of the Student t-distribution.  Syntax: qt(x, df)Parameters: x: Random variable df: Degree of Freedom  Example 1:  Python3 # R Program to perform Inverse # Cumulative Density Analysis # Calling qt() Function
1 min read
Compute the value of CDF over Wilcoxon Signedrank Distribution in R Programming - psignrank() Function
psignrank() function in R Language is used to compute the value of Cumulative Density Function(CDF) over Wilcoxon Signedrank Statistic Distribution for a sequence of Numeric values. Syntax: psignrank(x, n) Parameters: x: Numeric Vector n: Sample Size Example 1: Python3 1== # R Program to compute the
1 min read
Compute the Value of Geometric Quantile Function in R Programming - qgeom() Function
qgeom() Function in R Language is used to plot a graph for quantile function of Geometric Distribution. Syntax: qgeom(x, prob) Parameters: x: x-values of qgeom() function prob: prob of plot Example 1: Python3 # R program to illustrate # qgeom() function in r # Specify x-values for qgeom function x_q
1 min read
Conversion Functions in R Programming
Sometimes to analyze data using R, we need to convert data into another data type. As we know R has the following data types Numeric, Integer, Logical, Character, etc. similarly R has various conversion functions that are used to convert the data type. In R, Conversion Function are of two types: Con
4 min read