Beta Distribution in R

What is Beta Distribution?

The beta distribution is a continuous probability distribution with two positive shape parameters, often denoted by α and β. It is used to model random variables that take on values between 0 and 1, such as proportions or probabilities.

In R, the beta distribution can be generated using the rbeta() function, which takes the shape parameters alpha and beta as arguments. The function dbeta() can be used to evaluate the probability density function (PDF) of the beta distribution, and the function pbeta() can be used to evaluate the cumulative distribution function (CDF) of the beta distribution.

 

How to Use the Beta Distribution in R?

Here’s an example code that generates a random sample of size 100 from a beta distribution with shape parameters alpha = 2 and beta = 5, and then plots the corresponding probability density function:

 

 

# set the shape parameters
alpha <- 2
beta <- 5

# generate a random sample from the beta distribution
sample <- rbeta(n = 100, shape1 = alpha, shape2 = beta)

# plot the probability density function
curve(dbeta(x, shape1 = alpha, shape2 = beta), from = 0, to = 1, 
main = "Beta Distribution", xlab = "x", ylab = "Density")

You can also use the qbeta() function to find quantiles of the beta distribution. For example, to find the 90th percentile of a beta distribution with alpha = 2 and beta = 5, you can use the following code:

qbeta(0.9, shape1 = 2, shape2 = 5)

This will return the value 0.6840934.

Example – 2

You can also plot the probability density function (PDF) or cumulative distribution function (CDF) of a beta distribution using the dbeta() and pbeta() functions, respectively. The syntax for these functions is similar to rbeta().

For example, to plot the PDF and CDF of a beta distribution with shape1 = 2 and shape2 = 5, you can use the following code:

# generate data
x <- seq(0, 1, length.out = 1000)
y_pdf <- dbeta(x, 2, 5)
y_cdf <- pbeta(x, 2, 5)

# plot PDF and CDF
par(mfrow = c(1, 2))
plot(
x,
y_pdf,
type = "l",
xlab = "x",
ylab = "f(x)",
main = "Beta PDF"
)
plot(
x,
y_cdf,
type = "l",
xlab = "x",
ylab = "F(x)",
main = "Beta CDF"
)

Example – 3

1. Generating random values from a beta distribution:

# generate 1000 random values from a beta distribution
# with shape1 = 2 and shape2 = 5
set.seed(123) # for reproducibility
x <- rbeta(1000, 2, 5)

# plot a histogram of the generated values
hist(x,
freq = FALSE,
main = "Beta Distribution",
xlab = "x")

This will generate a histogram of the 1000 random values from the specified beta distribution.

 

2. Calculating the probability density function (PDF) of a beta distribution:

# calculate the PDF of a beta distribution with 
# shape1 = 2 and shape2 = 5
x <- seq(0, 1, length.out = 1000)
y <- dbeta(x, 2, 5)

# plot the PDF
plot(
x,
y,
type = "l",
main = "Beta PDF",
xlab = "x",
ylab = "f(x)"
)

 

This will generate a plot of the PDF of the specified beta distribution.

3. Calculating the cumulative distribution function (CDF) of a beta distribution:

# calculate the CDF of a beta distribution with 
# shape1 = 2 and shape2 = 5
x <- seq(0, 1, length.out = 1000)
y <- pbeta(x, 2, 5)

# plot the CDF
plot(
x,
y,
type = "l",
main = "Beta CDF",
xlab = "x",
ylab = "F(x)"
)

 

How to Use the Multinomial Distribution in R

Chi-Square Distribution in R