Open In App

A Guide to dgeom, pgeom, qgeom, and rgeom in R

Last Updated : 21 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will be looking at a guide to the dgeom, pgeom, qgeom, and rgeom methods of the geometric distribution in the R programming language.

dgeom function

The dgeom function finds the probability of experiencing a certain amount of failures before experiencing the first success in a series of Bernoulli trials. This returns the value of the geometric probability density function. In other words, it returns the probability of x failures prior o the first success (note the difference) when the probability of success is prob.

Syntax: dgeom(x, prob) 

Parameter:

  •    x: number of failures before the first success
  •   prob: the probability of success on a given trial

Example 1:

In this example, we are calculating the probability of a man asking for help, and the probability of getting the help is 0.5(p=0.5) so with the use of the dgeom() function we are calculating the probability that the sixth person giving help will talk to the first person given help.

R




dgeom(x=5, prob=.5)


Output:

[1] 0.015625

The probability that the person experiences 5 “failures” before the first success is 0.015625.

pgeom  function

This function returns the value of the geometric cumulative density function. Also, this function is used to find the probability of experiencing a certain amount of failures or fewer before experiencing the first success in a series of Bernoulli.

Syntax: pgeom(q, prob) 

Parameters: 

  • q: number of failures before the first success  
  • prob: probability of success on a given trial

Example:

In this example, we are calculating the probability of a man asking for help, and the probability of getting the help is 0.6(p=0.6) so with the use of the pgeom() function we are calculating the probability that the person will have to talk to 8 or less people to find someone who helps.

R




pgeom(q=8, prob=.6)


Output:

[1] 0.9997379

The probability that the person will have to talk to 8 or less people to find someone who supports the law is 0.9997379

qgeom function

 This function returns the value of the inverse geometric cumulative density function. Also, this function finds the number of failures that corresponds to a certain percentile.

Syntax: qgeom(p, prob) 

Parameters: 

  •  p: percentile
  •  prob: probability of success on a given trial

Example:

In this example, we are calculating the probability of a man asking for help, and the probability of getting the help is 0.4(p=0.4) so with the use of the qgeom() function, we are calculating the probability that how many “failures” would the person need to experience to be at the 70th percentile for a number of failures before the first success.

R




qgeom(p=.70, prob=0.4)


Output:

[1] 2

The person would need to experience 10 “failures” to be at the 70th percentile for a number of failures before the first success.

rgeom function

The rgeom function generates a vector of geometric distributed random variables. In other words, we can say that this function generates a list of random values that represent the number of failures before the first success, 

Syntax:  rgeom(n, prob) 

Parameters: 

  •    n: number of values to generate
  •    prob: probability of success on a given trial

Example:

In this example, we are calculating the probability of a man asking for help, and the probability of getting the help is 0.5(p=0.5) so with the use of the rgeom() function we are simulating 15 scenarios for how many “failures” the person will experience until she finds someone who helps.

R




rgeom(n=15, prob=.5)


Output:

 [1] 0 0 0 3 1 0 1 1 0 3 0 6 1 0 1


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads