# How to Use the Bernoulli Distribution in R

A random variable follows a Bernoulli distribution if it only has two possible outcomes: 0 or 1.

For example, suppose we flip a coin one time. Let the probability that it lands on heads be p. This means the probability that it lands on tails is 1-p.

Thus, we could write:

In this case, random variable X follows a Bernoulli distribution.

In the real world there are many instances where random variables follow a Bernoulli distribution. Any scenario where a random variable can only take on one of two values follows a Bernoulli distribution.

There are two ways to simulate a Bernoulli distribution in R:

Method 1: Use the dbinom() Function in Base R

```#calculate Bernoulli probabilities
dbinom(c(0, 1), size = 1, p = 0.7)
```

This particular example will return the probability associated with an outcome of 0 and an outcome of 1 for a Bernoulli distribution that has a probability of success of p = 0.7.

Note that we use the dbinom() function to use the Binomial distribution function in R with a sample size of 1, which is simply the Bernoulli distribution.

Method 2: Use the dbern() Function from the Rlab package

```library(Rlab)

#calculate Bernoulli probabilities
dbern(c(0, 1), prob=0.7)
```

This particular example will return the probability associated with an outcome of 0 and an outcome of 1 for a Bernoulli distribution that has a probability of success of p = 0.7.

The following examples show how to use each of these methods in practice to simulate the Bernoulli distribution in R.

## Example 1: Use dbinom() to Simulate Bernoulli Distribution in R

Suppose that we would like to calculate the probability associated with an outcome of 0 and an outcome of 1 for a Bernoulli distribution that has a probability of success of p = 0.7.

We can use the following syntax with the dbinom() function from base R to do so:

```#calculate Bernoulli probabilities
dbinom(c(0, 1), size = 1, p = 0.7)

[1] 0.3 0.7
```

The output tells us:

• The probability of an outcome of 0 is 0.3.
• The probability of an outcome of 1 is 0.7.

Note that the sum of these probabilities is 1, which is true of any Bernoulli distribution.

Feel free to change the value of p in the dbinom() function to simulate a Bernoulli distribution that has a different probability of success on a given trial.

## Example 2: Use dbern() to Simulate Bernoulli Distribution in R

Suppose that we would like to calculate the probability associated with an outcome of 0 and an outcome of 1 for a Bernoulli distribution that has a probability of success of p = 0.2.

We can use the following syntax with the dbern() function from the Rlab package in R to do so:

```library(Rlab)

#calculate Bernoulli probabilities
dbern(c(0, 1), prob=0.2)

[1] 0.8 0.2
```

The output tells us:

• The probability of an outcome of 0 is 0.8.
• The probability of an outcome of 1 is 0.2.

Once again, the sum of these probabilities is 1.

Feel free to use either the dbinom() function with a size argument of 1 or the dbern() function from the Rlab package in R to simulate a Bernoulli distribution.