Bernoulli Distribution in R

Bernoulli Distribution in R, You will discover how to use the Bernoulli distribution functions in this R tutorial.

Bernoulli Distribution in R

Example 1: The dbern function, or Bernoulli probability density function

W’ll demonstrate how to visualize the probability density function (PDF) of the Bernoulli distribution in the first case.

No Bernoulli distribution functions are included in R’s default installation. Because of this, we must first install and load the Rlab add-on package:

install.packages("Rlab")                         # Install Rlab package
library("Rlab")                                  # Load Rlab package

The next step is to use R to build a vector of quantiles:

x_dbern <- seq(0, 10, by = 1)                    # Specify x-values for dbern function

Now that we have our vector of quantiles, we can use the dbern function from the Rlab R package to get the corresponding values of the Bernoulli PDF.

y_dbern <- dbern(x_dbern, prob = 0.7)            # Apply dbern function

We can use the plot function to visualize this distribution by doing as follows:

plot(y_dbern, type = "o")                        # Plot dbern values

R-based visualization of the Bernoulli probability density function

Example 2: The pbern function, or Bernoulli Cumulative Distribution Function

Similar to Example 1, the Bernoulli distribution’s cumulative distribution function in R uses a similar syntax. The first step is to build a vector of quantiles:

x_pbern <- seq(0, 10, by = 1)                    # Specify x-values for pbern function

The pbern function can then be applied to this vector as follows:

y_pbern <- pbern(x_pbern, prob = 0.7)            # Apply pbern function

Finally, using the plot function, we can plot the results of pbern:

plot(y_pbern, type = "o")                        # Plot pbern values

R plot of the Bernoulli cumulative distribution function

Machine Learning Archives – Data Science Tutorials

Example 3: Qbern Function, a Bernoulli quantile function

How to graphically represent the quantile function of the Bernoulli distribution is demonstrated in Example 3.

We must first build a series of probabilities (i.e., values ranging from 0 to 1):

x_qbern <- seq(0, 1, by = 0.1)                   # Specify x-values for qbern function

The quantile function values for our probabilities can now be obtained using the qbern function:

y_qbern <- qbern(x_qbern, prob = 0.7)            # Apply qbern function

The plot function can be used to create the corresponding plot:

plot(y_qbern, type = "o")                        # Plot qbern values

R visualization for the Bernoulli quantile function

Example 4: Using the rbern Function to Create Random Numbers

We must first provide a seed and sample size N in order to produce a set of random numbers with a Bernoulli distribution:

set.seed(123)                                  # Set seed for reproducibility
N <- 10000                                       # Specify sample size

Then, to generate N Bernoulli distributed random numbers, we can use the rbern function:

y_rbern <- rbern(N, prob = 0.7)                  # Draw N random values
y_rbern                                          # Print values to RStudio console

A histogram can be used to show the rbern function’s output:

Regression Analysis Example-Ultimate Guide »

hist(y_rbern,                                    
breaks = 5, main = "")

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

2 × four =