# The Geometric Distribution in R geometric distribution tells us how many trials (k) are required until we obtain the first “success.”

The probability that we will obtain the first success on the kth trial can be found using the formula:

P(obtain first success on kth trial) = (1-p)k-1 * p

where is the probability of success in a given trial.

The geometric distribution has the following properties:

• The mean of the distribution is μ = 1/p
• The variance of the distribution is σ2 = (1-p) / p2
• The standard deviation of the distribution is σ = √σ2

This tutorial explains how to solve problems using the geometric distribution in R.

You can find a complete introduction to the geometric distribution here.

## Geometric Distribution in R: Syntax

The two built-in functions in R we’ll use to answer questions using the geometric distribution are:

dgeom(x, prob) – calculates the probability mass function (pmf) for the geometric distribution where is the number of failures before the first success and prob is the probability of success on each trial.

pgeom(q, prob) – calculates the cumulative distribution function (cdf) for the geometric distribution where is the number of failures before the first success and prob is the probability of success on each trial.

Find the full R documentation for the geometric distribution here.

## Solving Problems Using the Geometric Distribution in R

Example 1: A restaurant gives a raffle ticket to each customer that walks in the door. If a customer receives a winning raffle ticket, they get a free dinner. The probability that a given raffle ticket is a winner is 5%. What is the probability that the 10th customer that walks in the door is the first winner?

Solution: We want to know the probability that there will be nine “failures” (people not receiving a winning ticket) before the first “success” (person receives a winning ticket). Since we know the probability of “success” for each person is 0.05, we will use the following syntax in R to find the solution:

`dgeom(9, 0.05)`
`##  0.0315124704862305`

The probability that the 10th customer that walks in the door is the first winner is 0.0315.

Example 2: Jessica makes 70% of her free-throw attempts. What is the probability that she makes her first free-throw within her first two attempts?

Solution: We want to know the probability that it will take two attempts at most for Jessica to make her first free-throw. Since we know the probability of “success” for each attempt is 0.7, we will use the following syntax in R to find the solution:

```#calculate the cumulative probability of experiencing either 0 or 1 failures
#before the first "success" occurs, given that probability of success for each
#attempt is 0.7.
pgeom(1, 0.7)```
`##  0.91`

Thus, the probability that Jessica makes her first free-throw within her first two attempts is 0.91

Example 3: Mike makes 80% of his field goal attempts. What is the probability that he needs more than two attempts to make his first field goal?

Solution:  To answer this question, we can use the formula 1 – (probability that he needs two or less attempts to make first field goal). This is given by:

`1-pgeom(1, .8)`
`##  0.04`

The probability that Mike needs more than two attempts to make his first field goal is 0.04.