Here, we discuss gamma distribution functions in R, plots, parameter setting, random sampling, density, cumulative distribution and quantiles.
The gamma distribution with parameters \(\tt{shape}=\alpha\), and \(\tt{rate}=\lambda\) (or \(\tt{scale}=\theta=1/\lambda\)) has probability density function (pdf) formula as:
\[\begin{align} f(x) & = \frac{\lambda^\alpha}{\Gamma(\alpha)} x^{\alpha - 1} e^{-\lambda x } \\ & = \frac{1}{\Gamma(\alpha) \theta^\alpha} x^{\alpha - 1} e^{-\frac{x}{\theta}},\end{align}\]
for \(x \geq 0\), where \(\alpha > 0\), \(\lambda > 0\) (or \(\theta > 0\)),
and \(\Gamma\) is the \(\tt{gamma\;function}\).
The mean is \(\frac{\alpha}{\lambda}\) (or \(\alpha\theta\)), and the variance is \(\frac{\alpha}{\lambda^2}\) (or \(\alpha\theta^2\)).
See also probability distributions and plots and charts.
The table below shows the functions for gamma distributions in R.
Function | Usage |
rgamma(n, shape, rate=1, scale=1/rate) | Simulate a random sample with \(n\) observations |
dgamma(x, shape, rate=1, scale=1/rate) | Calculate the probability density at the point \(x\) |
pgamma(q, shape, rate=1, scale=1/rate) | Calculate the cumulative distribution at the point \(q\) |
qgamma(p, shape, rate=1, scale=1/rate) | Calculate the quantile value associated with \(p\) |
The examples here are based on the \(\tt{shape}\) and \(\tt{rate}\) parameters in the pdf above, as only one of the "rate" and "scale" arguments can be specified at a time. Hence, the "scale" argument (for \(\tt{scale}\)) is excluded in the examples below.
However, to use the \(\tt{scale}\) parameter, you can set the argument to the scale value of the distribution. For example:
[1] 0.1492214
Which (since \(\tt{rate = 1/scale}\)) is equivalent to:
[1] 0.1492214
Below is a plot of the gamma distribution function with \(\tt{shape}=3\) and \(\tt{rate}=1/2\).
x = seq(0, 24, 1/1000); y = dgamma(x, 3, 1/2)
plot(x, y, type = "l",
xlim = c(0, 24), ylim = c(0, max(y)),
main = "Probability Density Function of Gamma Distribution (3, 1/2)",
xlab = "x", ylab = "Density",
lwd = 2, col = "blue")
# Add legend
legend("topright", "Shape = 3 Rate = 1/2",
lwd = 2,
col = "blue",
bty = "n")
Below is a plot of the multiple gamma distribution functions in one graph.
x1 = seq(0, 25, 1/1000); y1 = dgamma(x1, 2, 1/3)
x2 = seq(0, 25, 1/1000); y2 = dgamma(x2, 2, 1/2)
x3 = seq(0, 25, 1/1000); y3 = dgamma(x3, 4, 1/2)
plot(x1, y1, type = "l",
xlim = c(0, 25), ylim = range(c(y1, y2, y3)),
main = "Probability Density Functions of Gamma Distributions",
xlab = "x", ylab = "Density",
lwd = 2, col = "blue")
points(x2, y2, type = "l", lwd = 2, col = "red")
points(x3, y3, type = "l", lwd = 2, col = "green")
# Add legend
legend("topright", c("shape = 2, rate = 1/3",
"shape = 2, rate = 1/2",
"shape = 4, rate = 1/2"),
lwd = c(2, 2, 2),
col = c("blue", "red", "green"),
bty = "n")
To set the parameters for the gamma distribution function, with \(\tt{shape}=4.5\) and \(\tt{rate}=1/3\).
For example, for dgamma()
, the following are the
same:
# The order of 4.5 and 1/3 matters here as the parameter names are not used.
# The first number 4.5 is shape, and 1/3 is rate.
dgamma(5.5, 4.5, 1/3)
[1] 0.03822724
[1] 0.03822724
[1] 0.03822724
Sample 1200 observations from the gamma distribution with \(\tt{shape}=6\) and \(\tt{rate}=1/2.5\):
set.seed(123) # Line allows replication (use any number).
sample = rgamma(1200, 6, 1/2.5)
hist(sample,
main = "Histogram of 1200 Observations from Gamma Distribution
with Shape = 6 and Rate = 1/2.5",
xlab = "x",
col = "deepskyblue", border = "white")
Calculate the density at \(x = 4.6\), in the gamma distribution with \(\tt{shape}=5.5\) and \(\tt{rate}=0.6\):
[1] 0.06994112
x = seq(0, 25, 1/1000); y = dgamma(x, 5.5, 0.6)
plot(x, y, type = "l",
xlim = c(0, 25), ylim = c(0, max(y)),
main = "Probability Density Function of Gamma Distribution
with Shape = 5.5 and Rate = 0.6",
xlab = "x", ylab = "Density",
lwd = 2, col = "blue")
# Add lines
segments(4.6, -1, 4.6, 0.06994112)
segments(-1, 0.06994112, 4.6, 0.06994112)
Calculate the cumulative distribution at \(x = 8.5\), in the gamma distribution with \(\tt{shape}=4\) and \(\tt{rate}=1/2\). That is, \(P(X \le 8.5)\):
[1] 0.6137884
x = seq(0, 25, 1/1000); y = pgamma(x, 4, 1/2)
plot(x, y, type = "l",
xlim = c(0, 25), ylim = c(0,1),
main = "Cumulative Distribution Function of Gamma Distribution
Shape = 4 and Rate = 1/2",
xlab = "x", ylab = "Cumulative Distribution",
lwd = 2, col = "blue")
# Add lines
segments(8.5, -1, 8.5, 0.6137884)
segments(-1, 0.6137884, 8.5, 0.6137884)
x = seq(0, 25, 1/1000); y = dgamma(x, 4, 1/2)
plot(x, y, type = "l",
xlim = c(0, 25), ylim = c(0, max(y)),
main = "Probability Density Function of Gamma Distribution
Shape = 4 and Rate = 1/2",
xlab = "x", ylab = "Density",
lwd = 2, col = "blue")
# Add shaded region and legend
point = 8.5
polygon(x = c(x[x <= point], point),
y = c(y[x <= point], 0),
col = "limegreen")
legend("topright", c("Area = 0.6137884"),
fill = c("limegreen"),
inset = 0.01)
For upper tail, at \(x = 8.5\), that is, \(P(X \ge 8.5) = 1 - P(X \le 8.5)\), set the "lower.tail" argument:
[1] 0.3862116
x = seq(0, 25, 1/1000); y = dgamma(x, 4, 1/2)
plot(x, y, type = "l",
xlim = c(0, 25), ylim = c(0, max(y)),
main = "Shaded Upper Region: Probability Density Function of
Gamma Distribution with Shape = 4 and Rate = 1/2",
xlab = "x", ylab = "Density",
lwd = 2, col = "blue")
# Add shaded region and legend
point = 8.5
polygon(x = c(point, x[x >= point]),
y = c(0, y[x >= point]),
col = "limegreen")
legend("topright", c("Area = 0.3862116"),
fill = c("limegreen"),
inset = 0.01)
Derive the quantile for \(p = 0.95\), in the gamma distribution with \(\tt{shape}=6.5\) and \(\tt{rate}=0.3\). That is, \(x\) such that, \(P(X \le x)=0.95\):
[1] 37.27005
x = seq(0, 60, 1/1000); y = pgamma(x, 6.5, 0.3)
plot(x, y, type = "l",
xlim = c(0, 60), ylim = c(0,1),
main = "Cumulative Distribution Function of Gamma Distribution
with Shape = 6.5 and Rate = 0.3",
xlab = "x", ylab = "Cumulative Distribution",
lwd = 2, col = "blue")
# Add lines
segments(37.27005, -1, 37.27005, 0.95)
segments(-3, 0.95, 37.27005, 0.95)
x = seq(0, 60, 1/1000); y = dgamma(x, 6.5, 0.3)
plot(x, y, type = "l",
xlim = c(0, 60), ylim = c(0, max(y)),
main = "Probability Density Function of Gamma Distribution
with Shape = 6.5 and Rate = 0.3",
xlab = "x", ylab = "Density",
lwd = 2, col = "blue")
# Add shaded region and legend
point = 37.27005
polygon(x = c(x[x <= point], point),
y = c(y[x <= point], 0),
col = "limegreen")
legend("topright", c("Area = 0.95"),
fill = c("limegreen"),
inset = 0.01)
For upper tail, for \(p = 0.05\), that is, \(x\) such that, \(P(X \ge x)=0.05\):
[1] 37.27005
x = seq(0, 60, 1/1000); y = dgamma(x, 6.5, 0.3)
plot(x, y, type = "l",
xlim = c(0, 60), ylim = c(0, max(y)),
main = "Shaded Upper Region: Probability Density Function of
Gamma Distribution with Shape = 6.5 and Rate = 0.3",
xlab = "x", ylab = "Density",
lwd = 2, col = "blue")
# Add shaded region and legend
point = 37.27005
polygon(x = c(point, x[x >= point]),
y = c(0, y[x >= point]),
col = "limegreen")
legend("topright", c("Area = 0.05"),
fill = c("limegreen"),
inset = 0.01)
The feedback form is a Google form but it does not collect any personal information.
Please click on the link below to go to the Google form.
Thank You!
Go to Feedback Form
Copyright © 2020 - 2024. All Rights Reserved by Stats Codes