Open In App

Simulate Binomial or Bernoulli Trials in R

Last Updated : 05 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will going to learn about the simulate binomial or Bernoulli trials in R programming language.

A binomial trial is a statistical experiment that has two possible outcomes, such as success or failure, and the outcome of each trial is independent of the others. A bernoulli trial is a particular case of a binomial trial where there is only one trial. We will use R’s built-in functions to simulate these trials and demonstrate their properties.

Simulating Binomial Trials

To simulate binomial trials in R, we will use the rbinom() function. This function generates random numbers from a binomial distribution.

Syntax of rbinom() function:

rbinom(n, size, prob)

Parameters:

  • n: the number of trials
  • prob: the probability of success for each trial
  • size: the number of In above simulated trials

Example 1:

In this example, the function rbinom(10, 1, 0.7) generates 10 random numbers, each representing the outcome of a Bernoulli trial with a probability of success of 0.7. The result is stored in the variable “result” and is printed using the print() function. The outcome of the trial will be either 0 or 1. A 0 represents a failure and a 1 represents a success.

R




result <- rbinom(10, 1, 0.7)
print(result)


Output:

[1] 1 1 1 1 1 1 1 1 0 1

Example 2:

In this example, we are going to simulate 1000 Bernoulli trials with a probability of success of 0.7 in R, using the rbinom() function and generate a summary of the results using the summary() function which returns the count of failures and successes in the 1000 trials.

R




result <- rbinom(1000, 1, 0.7)
summary(result)


Output:

   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.000   1.000   1.000   0.700   1.000   1.000 

The statistics shown in the output are as follows:

  • Min: Minimum value in the data set (0 in this case)
  • 1st Qu.: First Quartile (25th percentile) (1 in this case)
  • Median: The middle value of the data set (1 in this case)
  • Mean: The average value of the data set (0.7 in this case)
  • 3rd Qu.: Third Quartile (75th percentile) (1 in this case)
  • Max: Maximum value in the data set (1 in this case)

Example 3:

In this article, the code simulates 100 Bernoulli trials with a probability of success of 0.5 and creates a histogram of the results. The rbinom() function is used to generate 100 random numbers, each representing the outcome of a Bernoulli trial with a probability of success of 0.5. The generated numbers are stored in the variable result. The hist() function is then used to create a histogram of the values stored in the result variable. The histogram will display the frequency of 0’s and 1’s in the result variable, which represents the number of failures and successes in the 100 Bernoulli trials. 

The x-axis of the histogram will be labelled as “0” and “1”, representing failure and success respectively. The y-axis will represent the frequency of the outcomes. Since the probability of success is set to 0.5, it is expected that the histogram will have an approximately equal number of 0’s and 1’s. The outcome may vary as the simulation is random.

R




result <- rbinom(100, 1, 0.5)
hist(result)


Output:

Simulate Binomial or Bernoulli Trials in R

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads