Beta Distribution probabilities using R

Beta Distribution probabilities using R

In this tutorial, you will learn about how to use dbeta(), pbeta(), qbeta() and rbeta() functions in R programming language to compute the individual probabilities, cumulative probabilities, quantiles and to generate random sample for Beta Type I distribution.

Before we discuss R functions for Beta Type I distribution, let us see what is Beta Type I distribution.

Beta Type I Distribution

Beta Type I distribution distribution is a continuous type probability distribution.

Let $X\sim \beta_1(\alpha,\beta)$. Then the probability distribution of $X$ is

$$ \begin{aligned} f(x)&= \begin{cases} \frac{1}{B(\alpha,\beta)}x^{\alpha-1}(1-x)^{\beta-1}, & 0\leq x\leq 1;\alpha,\beta>0 \\ 0, & Otherwise. \end{cases} \end{aligned} $$

where $\alpha$ is the shape parameter 1 and $\beta$ is the shape parameter 2 of Beta Type I distribution.

Read more about the theory and results of Beta Type I distribution here.

Beta Type I probabilities using dbeta() function in R

For continuous probability distribution, density is the value of the probability density function at $x$ (i.e., $f(x)$).

The syntax to compute the probability density function for Beta Type I distribution using R is

dbeta(x,shape1, shape2)

where

  • x : the value(s) of the variable and,
  • shape1 : first parameter of beta distribution,
  • shape2 : second parameter of beta distribution.

The dbeta() function gives the density for given value(s) x, shape1 and shape2.

Numerical Problem for Beta Type I Distribution

To understand the four functions dbeta(), pbeta(), qbeta() and rbeta(), let us take the following numerical problem.

Beta Type I Distribution Example

The daily proportion of major automobile accidents across the United States can be treated as a random variable having a beta distribution with $\alpha = 6$ and $\beta = 4$.

(a) Find the value of the density function at $x=0.35$.
(b) Plot the graph of Beta Type I probability distribution.
(c) Find the probability that, on a certain day, the percentage of major accidents is less than 80%.
(d) Find the probability that, on a certain day, the percentage of major accidents is more than 60%.
(e) Find the probability that, on a certain day, the percentage of major accidents is less than 80% but greater than 60%.
(f) Plot the graph of cumulative Beta Type I probabilities.
(g) What is the value of $c$, if $P(X\leq c) \geq 0.85$?
(h) Simulate 1000 Beta Type I distributed random variables with $\alpha= 6$ and $\beta = 4$.

Let $X$ denote the daily proportion of major automobile accidents across the United States. Given that $X\sim \beta_1(\alpha=6, \beta=4)$.

Example 1: How to use dbeta() function in R?

To find the value of the density function at $x=0.35$ we need to use dbeta() function.

Let $X$ denote the daily proportion of major automobile accidents across the United States. Here $X\sim \beta_1(6,4)$.

First let us define the given parameters as

# parameter 1 shape1
alpha <- 6
# parameter 2 shape2
beta <- 4

The probability density function of $X$ is

$$ \begin{aligned} f(x)&= \frac{1}{B(6,4)}x^{6-1}(1-x)^{4-1},\\ &\quad\text{for } 0 \leq x \leq 1. \end{aligned} $$

For part (a), we need to find the density function at $x=0.35$. That is $f(0.35)$.

First I will show you how to calculate the value of the density function for given value of $x$. Then I will show you how to compute the same using dbeta() function in R.

(a) The value of the density function at $x=0.35$ is

$$ \begin{aligned} f(0.35)&= \frac{1}{B(6, 4)}*(0.35)^{6-1} (1-0.35)^{4-1}\\ &= \frac{\Gamma(10)}{\Gamma(6)\Gamma(4)}*(0.35)^{5} (0.65)^{3}\\ &=504*0.0052522 * 0.274625\\ &= 0.7269605 \end{aligned} $$

The above probability can be calculated using dbeta(0.35,6,4) function in R.

# Compute Beta Type I  probability
result1 <- dbeta(0.35,alpha,beta)
result1
[1] 0.7269605

Example 2 Visualize Beta Type I probability distribution

Using dbeta() function we can compute Beta Type I distribution probabilities for given x, shape1 and shape2. To plot the probability density function of Beta Type I distribution, we need to create a sequence of x values and compute the corresponding probabilities.

# create a sequence of x values
x <- seq(0,1, by=0.02)
## Compute the Beta Type I pdf for each x
px<-dbeta(x,alpha,beta)

(b) Visualizing Beta Type I Distribution with dbeta() function and plot() function in R:

The probability density function of Beta Type I distribution with given 6 and 4 can be visualized using plot() function as follows:

## Plot the Beta Type I  probability dist
plot(x,px,type="l",xlim=c(0,1),ylim=c(0,max(px)),
     lwd=3, col="darkred",ylab="f(x)")
title("PDF of Beta Type I  (alpha = 6, beta= 4)")
PDF of Beta Distribution
PDF of Beta Distribution

Beta Type I cumulative probability using pbeta() function in R

The syntax to compute the cumulative probability distribution function (CDF) for Beta Type I distribution using R is

pbeta(q,shape1, shape2)

where

  • q : the value(s) of the variable,
  • shape1 : first parameter of beta distribution,
  • shape2 : second parameter of beta distribution.

Using this function one can calculate the cumulative distribution function of Beta Type I distribution for given value(s) of q (value of the variable x), shape1 and shape2.

Example 3: How to use pbeta() function in R?

In the above example, for part (c), we need to find the probability $P(X\leq 0.80)$.

(c) The probability that, on a certain day, the percentage of major accidents is less than 80% is

$$ \begin{aligned} P(X\leq 0.80) &=\int_0^{0.80} f(x)\; dx. \end{aligned} $$

## Compute cumulative Beta Type I  probability
result2 <- pbeta(0.80,alpha,beta)
result2
[1] 0.9143583

Example 4: How to use pbeta() function in R?

In the above example, for part (d), we need to find the probability $P(X \geq 0.60)$.

To calculate the probability that a random variable $X$ is greater than a given number one can use the option lower.tail=FALSE in pbeta() function.

Above probability can be calculated easily using pbeta() function with argument lower.tail=FALSE as

$P(X \geq 0.60) =\int_{0.60}^1 f(x)\; dx$ = pbeta(0.60,alpha,beta,lower.tail=FALSE)

or by using complementary event as

$P(X \geq 0.60) = 1- P(X\leq 0.60)$= 1- pbeta(1,alpha,beta)

# compute cumulative Beta Type I  probabilities
# with lower.tail False
pbeta(0.60,alpha,beta,lower.tail=FALSE)
[1] 0.5173903

(d) The probability that, on a certain day, the percentage of major accidents is more than 60% is

$$ \begin{aligned} P(X\geq 0.60) &=\int_{0.60}^1 f(x)\; dx\\ &=0.5173903. \end{aligned} $$

# Using complementary event
1-pbeta(0.60,alpha,beta)
[1] 0.5173903

Example 5: How to use pbeta() function in R?

One can also use pbeta() function to calculate the probability that the random variable $X$ is between two values.

(e) The probability that, on a certain day, the percentage of major accidents is less than 80% but greater than 60% can be written as $P(0.60 < X < 0.80)$.

$$ \begin{aligned} P(0.60 < X < 0.80) &= P(X< 0.80) -P(X < 0.60)\\ &= 0.9143583 - 0.4826097\\ &= 0.4317486 \end{aligned} $$

The above probability can be calculated using pbeta() function as follows:

result3 <- pbeta(0.80,alpha,beta)-pbeta(0.60,alpha,beta)
result3
[1] 0.4317486

Example 6: Visualize the cumulative Beta Type I probability distribution

Using pbeta() function we can compute Beta Type I cumulative probabilities (CDF) for given x, shape1 and shape2. To plot the CDF of Beta Type I distribution, we need to create a sequence of x values and compute the corresponding cumulative probabilities.

# create a sequence of x values
x <- seq(0,1, by=0.02)
## Compute the Beta Type I pdf for each x
Fx <- pbeta(x,alpha,beta)

(f) Visualizing Beta Type I Distribution with pbeta() function and plot() function in R:

The cumulative probability distribution of Beta Type I distribution with given x, shape1 and shape2 can be visualized using plot() function as follows:

## Plot the Beta Type I  probability dist
plot(x,Fx,type="l",xlim=c(0,1),ylim=c(0,1),
     lwd=3, col="darkred",ylab="f(x)")
title("Distribution Function of Beta Type I  (alpha = 6, beta= 4)")
CDF of Beta Distribution
CDF of Beta Distribution

Beta Type I Distribution Quantiles using qbeta() in R

The syntax to compute the quantiles of Beta Type I distribution using R is

qbeta(p,shape1,shape2)

where

  • p : the value(s) of the probabilities,
  • shape1 : first parameter of beta distribution,
  • shape2 : second parameter of beta distribution.

The function qbeta(p,shape1,shape2) gives $100*p^{th}$ quantile of Beta Type I distribution for given value of p, shape1 and shape2.

The $p^{th}$ quantile is the smallest value of Beta Type I random variable $X$ such that $P(X\leq x) \geq p$.

It is the inverse of pbeta() function. That is, inverse cumulative probability distribution function for Beta Type I distribution.

Example 7: How to use qbeta() function in R?

In part (g), we need to find the value of $c$ such a that $P(X\leq c) \geq 0.85$. That is we need to find the $85^{th}$ quantile of given Beta Type I distribution.

alpha <- 6
beta <- 4
prob <- 0.85
# compute the quantile for Beta Type I  dist
qbeta(0.85,alpha, beta)
[1] 0.7586159

The $85^{th}$ percentile of given Beta Type I distribution is 0.7586159.

Visualize the quantiles of Beta Distribution

The quantiles of Beta distribution with given p, shape1 and shape2 can be visualized using plot() function as follows:

p <- seq(0,1,by=0.02)
qx <- qbeta(p,alpha,beta)
# Plot the quantiles of Beta Type I  dist
plot(p,qx,type="l",lwd=2,col="darkred",
     ylab="quantiles",
main="Quantiles of Beta(alpha= 6,beta = 4)")
Quantiles of Beta Distribution
Quantiles of Beta Distribution

Simulating Beta Type I random variable using rbeta() function in R

The general R function to generate random numbers from Beta Type I distribution is

rbeta(n,shape1,shape2)

where,

  • n : the sample observations,
  • shape1 : first parameter of beta distribution,
  • shape2 : second parameter of beta distribution.

The function rbeta(n,shape1,shape2) generates n random numbers from Beta Type I distribution with given shape1 and shape2.

Example 8: How to use rbeta() function in R?

In part (h), we need to generate 1000 random numbers from Beta Type I distribution with given $shape1 = 6$ and $shape2=4$.

(h) We can use rbeta(1000,alpha,beta) function to generate random numbers from Beta Type I distribution.

## initialize sample size to generate
n <- 1000
# Simulate 1000 values From Beta Type I  dist
x_sim <- rbeta(n,alpha,beta)

The below graphs shows the density of the simulated random variables from Beta Type I Distribution.

## Plot the simulated data
plot(density(x_sim),xlab="Simulated x",ylab="density",
     lwd=5,col="darkred",
     main="Simulated data from Beta(6,4) dist")
Random Numbers from Beta Distribution
Random Numbers from Beta Distribution

If you use same function again, R will generate another set of random numbers from $\beta_1(6,4)$.

# Simulate 1000 values From Beta Type I  dist
x_sim_2 <- rbeta(n,alpha,beta)
## Plot the simulated data
plot(density(x_sim_2),xlab="Simulated x",ylab="density",
     lwd=5,col="blue",
     main="Simulated data from Beta(6,4) dist")
Random Numbers from Beta Distribution 2
Random Numbers from Beta Distribution 2

For the simulation purpose to reproduce same set of random numbers, one can use set.seed() function.

# set seed for reproducibility
set.seed(1457)
# Simulate 1000 values From Beta Type I  dist
x_sim_3 <- rbeta(n,alpha,beta)
## Plot the simulated data
plot(density(x_sim_3),xlab="Simulated x",ylab="density",
     lwd=5,col="darkred",
     main="Simulated data from Beta(6,4) dist")
Random Numbers from Beta Distribution 3
Random Numbers from Beta Distribution 3
set.seed(1457)
# Simulate 1000 values From Beta Type I  dist
x_sim_4 <- rbeta(n,alpha,beta)
## Plot the simulated data
plot(density(x_sim_4),xlab="Simulated x",ylab="density",
     lwd=5,col="darkred",
     main="Simulated data from Beta(6,4) dist")
Random Numbers from Beta Distribution 3
Random Numbers from Beta Distribution 4

Since we have used set.seed(1457) function, R will generate the same set of Beta Type I distributed random numbers.

Histogram Beta Distribution
Histogram Beta Distribution

To learn more about other discrete and continuous probability distributions using R, go through the following tutorials:

Discrete Distributions Using R

Binomial distribution in R
Poisson distribution in R
Geometric distribution in R
Negative Binomial distribution in R
Hypergeometric distribution in R

Continuous Distributions Using R

Uniform distribution in R
Exponential distribution in R
Log-Normal distribution in R
Normal distribution in R
Gamma distribution in R
Cauchy distribution in R
Laplace distribution in R
Logistic distribution in R
Weibull distribution in R

Endnote

In this tutorial, you learned about how to compute the probabilities, cumulative probabilities and quantiles of Beta Type I distribution in R programming. You also learned about how to simulate a Beta Type I distribution using R programming.

To learn more about R code for discrete and continuous probability distributions, please refer to the following tutorials:

Probability Distributions using R

Let me know in the comments below, if you have any questions on Beta Type I Distribution using R and your thought on this article.

VRCBuzz co-founder and passionate about making every day the greatest day of life. Raju is nerd at heart with a background in Statistics. Raju looks after overseeing day to day operations as well as focusing on strategic planning and growth of VRCBuzz products and services. Raju has more than 25 years of experience in Teaching fields. He gain energy by helping people to reach their goal and motivate to align to their passion. Raju holds a Ph.D. degree in Statistics. Raju loves to spend his leisure time on reading and implementing AI and machine learning concepts using statistical models.

Leave a Comment