Bernoulli Distribution in R - GeeksforGeeks

21 Apr, 2021
Bernoulli Distribution is a special case of Binomial distribution where only a single trial is performed. It is a discrete probability distribution for a Bernoulli trial (a trial that has only two outcomes i.e. either success or failure). For example, it can be represented as a coin toss where the probability of getting the head is 0.5 and getting a tail is 0.5. It is a probability distribution of a random variable that takes value 1 with probability p and the value 0 with probability q=1-p. The Bernoulli distribution is a special case of the binomial distribution with n=1.
The probability mass function f of this distribution, over possible outcomes k, is given by :
 
The above relation can also be expressed as:
In R Programming Language, there are 4 built-in functions to for Bernoulli distribution and all of them are discussed below.
dbern( ) function in R programming measures density function of Bernoulli distribution.  
Syntax: dbern(x, prob, log = FALSE)
Parameter:
x: vector of quantiles
prob: probability of success on each trial
log: logical; if TRUE, probabilities p are given as log(p)
In statistics, it is given by below formula:
Example: 
R
# import Rlab librarylibrary(Rlab)  # x values for the# dbern( ) functionx <- seq(0, 10, by = 1)  # using dbern( ) function# to x to obtain corresponding# Bernoulli  PDFy <- dbern(x, prob = 0.7)    # plot dbern valuesplot(y, type = "o")
Output:
pbern( ) function in R programming giver the distribution function for the Bernoulli distribution. The distribution function or cumulative distribution function (CDF) or cumulative frequency function, describes the probability that a variate X takes on a value less than or equal to a number x.
Syntax: pbern(q, prob, lower.tail = TRUE, log.p = FALSE)
Parameter:
q: vector of quantiles
prob: probability of success on each trial
lowe.tail: logical
log.p: logical; if TRUE, probabilities p are given as log(p).
Example:
R
# import Rlab librarylibrary(Rlab)  # x values for the# pbern( ) functionx <- seq(0, 10, by = 1)  # using pbern( ) function# to x to obtain corresponding# Bernoulli  CDFy <- pbern(x, prob = 0.7)    # plot pbern valuesplot(y, type = "o")      
Output:
The above plot represents the Cumulative Distribution Function of Bernoulli Distribution in R.
qbern( ) gives the quantile function for the Bernoulli distribution. A quantile function in statistical terms specifies the value of the random variable such that the probability of the variable being less than or equal to that value equals the given probability.
Syntax: qbern(p, prob, lower.tail = TRUE, log.p = FALSE)
Parameter:
p: vector of probabilities.
prob: probability of success on each trial.
lower.tail: logical
log.p: logical; if TRUE, probabilities p are given as log(p).
Example: 
R
# import Rlab librarylibrary(Rlab)    # x values for the# qbern( ) functionx <- seq(0, 1, by = 0.2)  # using qbern( ) function# to x to obtain corresponding# Bernoulli  QFy <- qbern(x, prob = 0.5,lower.tail = TRUE, log.p = FALSE)    # plot qbern valuesplot(y, type = "o")
Output:
rbern( ) function in R programming is used to generate a vector of random numbers which are Bernoulli distributed.
Syntax: rbern(n, prob)
Parameter:
n: number of observations.
prob: number of observations.
Example:
R
# import Rlab librarylibrary(Rlab)set.seed(98999)      # sample sizeN <- 1000  # generate random variables using# rbern( ) functionrandom_values <- rbern(N, prob = 0.5)    # print the valuesprint(random_values)  # plot of randomly# drawn densityhist(random_values,breaks = 10,main = "")
Output: 
   [1] 0 0 1 1 0 0 0 0 0 1 0 0 0 1 0 1 1 0 1 0 1 1 1 0 1 0 1 1 0 1 0 1 1 0 1 1 1 1 0 1 0 0 0 0 1 1 1 0 1 1 1 1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 1
  [68] 1 1 1 0 0 1 0 1 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 1 0 0 0 1 1 0 1 1 1 1 0 1 0 0 0 0 1 0 0 1 1 0 0 0 0 0 1 0 1 0 0 1 0 1 0 1 0 1 1 1 0 1
 [135] 0 0 0 0 0 1 1 0 1 1 1 1 0 0 0 0 0 1 1 0 0 0 1 1 1 1 1 0 1 1 0 0 1 0 0 1 0 1 1 0 1 1 0 1 1 1 1 1 0 0 1 0 0 0 0 1 1 0 0 0 0 0 1 1 0 0 0
 [202] 1 1 1 1 1 1 1 0 0 0 0 1 1 1 0 0 0 0 0 1 1 0 1 1 0 0 1 0 0 1 1 1 0 1 1 0 1 1 0 0 0 0 0 0 0 1 1 0 0 1 1 0 0 1 1 1 1 1 0 1 1 1 1 1 0 1 0
 [269] 0 0 1 1 0 1 0 1 0 1 0 0 0 0 1 1 0 0 1 1 1 1 0 0 1 0 0 1 0 0 0 0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1 1 0 1 0 1 1 0 1 1
 [805] 0 1 0 1 0 1 1 0 0 1 1 1 1 0 1 1 1 0 0 0 0 1 0 0 1 1 0 0 1 1 0 0 0 1 1 1 0 0 1 0 1 0 0 0 0 1 0 1 1 0 0 0 0 1 1 0 1 0 1 1 0 1 0 1 1 1 1
 [872] 1 1 0 1 0 0 1 0 0 1 1 0 1 0 0 1 0 0 0 0 0 1 1 1 0 1 1 0 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1 0 0 0 1 1 0 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 0 0
 [939] 1 1 1 1 0 1 0 0 1 1 1 0 1 0 0 1 1 0 1 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 1 0 1 0 0 0 0 0 1 1 0 0 1 0 0 1 1 0 0 0 1 1 1 0 0 0 0 1
The above plot represents Randomly Drawn Numbers of Bernoulli Distribution in R.
Picked
R-Mathematics
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Change Color of Bars in Barchart using ggplot2 in R
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to import an Excel File into R ?
How to filter R DataFrame by values in a column?
How to filter R dataframe by multiple conditions?
How to change the order of bars in bar chart in R ?
R - if statement
Replace Specific Characters in String in R