Professional Documents
Culture Documents
Data R
Introduction
Introduction
Gamma Function
Beta Function in R
The beta function in R can be implemented using
the beta(a,b) function, where a and b are non-
negative numeric vectors. Similarly, the function
lbeta(a,b) returns the natural logarithm of the
beta function. The lines of code below provide an
illustration.
{r}
beta(2,3)
1
2 lbeta(1,3)
3
4 x1 <- c(2,4, 6)
5 y1 <- c(7,5,9)
6 beta(x1,y1)
7 lbeta(x1,y1)
8
Output:
1 [1] 0.08333333
2
3 [1] -1.098612
4
5 [1] 1.785714e-02 3.571429e-03 5.550006e-05
6
7 [1] -4.025352 -5.634790 -9.799127
{r}
1 beta(-1,2)
Output:
x: vector of quantiles
shape1, shape2: non-negative parameters of the
Beta distribution
ncp: non-centrality parameter
log: logical; if TRUE
Output:
{r}
1 by2 <- dbeta(b1, shape1 = 20, shape2 = 5)
2
3 plot(by2)
Output:
2. pbeta : This function returns the cumulative
distribution function of the beta distribution.
The syntax is pbeta(q, shape1, shape2, ncp
= 0, lower.tail = TRUE, log.p = FALSE) ,
which takes the following arguments.
q: vector of quantiles
shape1, shape2: non-negative parameters of the
Beta distribution
ncp: non-centrality parameter
lower.tail: logical; if TRUE (default),
probabilities are PX???x, otherwise, PX>x
log, log.p: logical; if TRUE, probabilities p are
given as log(p)
{r}
1 b2 <- seq(0, 1, by = 0.02)
2
3 by2 <- pbeta(b2, shape1 = 4, shape2 = 6)
4
5 plot(by2)
Output:
p: vector of probabilities
shape1, shape2: non-negative parameters of the
Beta distribution
ncp: non-centrality parameter.
lower.tail: logical; if TRUE (default),
probabilities are PX<=x, otherwise, PX>x
log, log.p: logical; if TRUE, probabilities p are
given as log(p)
{r}
1 b3 <- seq(0, 1, by = 0.01)
2
3 by3 <- qbeta(b3, shape1 = 4, shape2 = 6)
4
5 plot(by3)
Output:
n: number of observations
shape1, shape2: non-negative parameters of the
Beta distribution
ncp: non-centrality parameter
{r}
1 set.seed(100)
2 n = 1000
3 b4 <- rbeta(n, shape1 = 2, shape2 = 8)
4 plot(density(b4), main = "Plot")
5
Output:
Gamma Function in R
The gamma function in R can be implemented
using the gamma(x) function, where the argument
x represents a non-negative numeric vector. It is
to be noted that any negative argument will not
produce a result, as shown below. The code below
provides an illustration.
{r}
1 gamma(10)
2
3 gamma(-1)
Output:
1 [1] 362880
2
3 NaNs produced[1] NaN
The gamma function is also used in Gamma
Distribution, which is a continuous probability
distribution that has only one mode and is always
positive.
x: vector of quantiles
alpha, beta: parameters of the gamma
distribution
rate: an alternative way to specify the scale
scale: the scale parameter, must be positive
{r}
1 alpha = 2
2 beta = 10
3 mean = alpha * beta
4 std = sqrt(alpha*beta^2)
5 print(mean); print(std)
Output:
1 [1] 20
2
3 [1] 14.14214
{r}
1 range = seq(0, mean + 4*std, 0.02)
2 gy1 = dgamma(range, alpha, rate = 1/beta)
3 plot(range, gy1, type ="l")
Output:
2. pgamma : This function returns the cumulative
probability of the gamma distribution. Suppose
the distribution we generated in the previous
step represents the salary of the sample taken
from a population, and we want to find out the
probability that the salary is below 20. This can
be achieved with the pgamma function.
{r}
1 pgamma(20, alpha, rate = 1/beta)
2
3 1 - pgamma(20, alpha, rate = 1/beta)
Output:
1 [1] 0.5939942
2
3 [1] 0.4060058
3. qgamma : This function returns the quantile of
the distribution. If we want to find the median
(50th percentile) of the above distribution, we
can use the following command. The median
comes out to be 16.8, which is smaller than the
mean.
{r}
1 qgamma(0.5, alpha, rate = 1/beta)
Output:
1 [1] 16.78347
{r}
1 set.seed(100)
2 n = 1000
3 g4 <- rgamma(n, alpha, rate = 1/beta)
4 plot(density(g4), main = "Plot")
5
Output:
Conclusion
In this guide, you learned the concepts and
implementation of beta and gamma function in R.
You also learned the properties and usage of these
functions in beta and gamma distribution, which is
used in many statistical, mathematical, and
machine learning applications.