How to find probabilities, cumulative probabilities and quantiles using R

Probability Distributions using R

In this article, you will learn about how to calculate probabilities, cumulative probabilities, quantiles and random numbers for various probability distributions using R.

Probability distributions are the foundation of inferential statistics. In statistics there are many probability distributions, some are discrete probability distributions and some are continuous probability distributions.

R has many built-in functions to calculate probabilities for discrete as well as continuous probability distributions.

Functions for Probability Distribution in R

Four types of functions related to probability distributions are available. The name of the R function for probability distributions comprise two part: first part (the first letter) indicates the function group, and the second part indicates the name of the distribution.

The four functions are as follows:

  • d : for density function or probability at a point
  • p : for cumulative probability distribution (distribution function)
  • q : for quantiles of the probability distribution
  • r : for generating Pseudo-random numbers from the probability distribution.

For example for binomial distribution the four functions are :

  • dbinom() for probability at a point,
  • pbinom() for cumulative probability (distribution function),
  • qbinom() for quantiles,
  • rbinom() for pseudo-random numbers

Discrete Probability Distributions

Some of the commonly used discrete probability distributions and their related abbreviations in R are as follows:

Distribution Abbreviation
Binomial binom
Poisson pois
Geometric geom
Negative Binomial nbinom
Hypergeometric hyper
Multinomial multinom

We will discuss how to calculate probabilities, cumulative probabilities, quantiles for all the above probability distributions in subsequent tutorials.

Continuous Probability Distributions

Some of the commonly used continuous probability distributions and their related abbrevations in R are as follows:

Distribution Abbreviation
Uniform unif
Normal norm
Exponential exp
Gamma gammma
Weibull weibull
Log-normal lnorm
Logistic logis
Cauchy cauchy
Laplace laplace
Beta beta
Chi-square chi
t t
F f
Wilcoxon Rank Sum wilcox
Wilcoxon Singed Rank signrank

We will discuss about how to calculate probabilities, cumulative probabilities, quantiles for all the above continuous probability distributions in subsequent tutorials.

Density Function using R

For discrete probability distribution, density is the probability of getting exactly the value $x$ (i.e., $P(X=x)$), while for continuous distribution density is the relative probability of getting a value close to $x$ (i.e., $f(x)$).

e.g., the function dbinom(x,size,prob) calculate the binomial probability at x for a binomial distribution with given number of trials (size) and probability of success (prob).

px<-dbinom(4,size=5,p=0.56)
px
[1] 0.2163589

$$ \begin{aligned} P(X=4)& =\binom{5}{4}(0.56)^4(1-0.56)^{5-4}\\ & =0.2163589 \end{aligned} $$

Cumulative Distribution Function Using R

The cumulative distribution function gives the probability of getting the value $x$ or less than $x$. That is $F(x)= P(X\leq x)$.

e.g., the function pbinom(q, size, prob) calculate the cumulative probability of binomial distribution below q with given number of trials (size) and probability of success (prob).

Fx <- pbinom(3, size = 5, p = 0.56)
Fx
[1] 0.7285679

$$ \begin{aligned} P(X\leq 3)& =P(X=0) + P(X=1) +P(X=2) + P(X=3)\\ &=\sum_{x=0}^3\binom{8}{x}(0.56)^x(1-0.56)^{8-x}\\ & =0.7285679 \end{aligned} $$

Quantile Function using R

Quantile is the inverse of the cumulative distribution function. That is this function returns the smallest value of $x$ given the quantile $p$ such that $P(X\leq x) > p$.

e.g., the function qbinom(p, size, prob) calculate the p$^{th}$ quantile of binomial distribution with given number of trials (size) and probability of success (prob).

## The value of random variable x
x <- 0:5
# calculate probabilities
px <- dbinom(x, size = 5, prob = 0.56)
# calculate cumulative probabilities
Fx <- pbinom(x, size = 5, prob = 0.56)
# use column bind function
bino.dist <- cbind(x, px, Fx)
colnames(bino.dist) <- c("x", "P(X=x)", "P(X<=x)")
bino.dist
     x     P(X=x)    P(X<=x)
[1,] 0 0.01649162 0.01649162
[2,] 1 0.10494669 0.12143831
[3,] 2 0.26713702 0.38857533
[4,] 3 0.33999258 0.72856791
[5,] 4 0.21635891 0.94492682
[6,] 5 0.05507318 1.00000000
library(knitr)
kable(bino.dist,align="c")
x P(X=x) P(X<=x)
0 0.0164916 0.0164916
1 0.1049467 0.1214383
2 0.2671370 0.3885753
3 0.3399926 0.7285679
4 0.2163589 0.9449268
5 0.0550732 1.0000000
qbinom(0.238, size = 5, prob = 0.56)
[1] 2

$P(X\leq x) > 0.238\Rightarrow x= 2$.

Pseudo-random Numbers using R

This function generates pseudo-random numbers of given size from a particular distribution based on the specified parameters.

The first argument of specifies the number of random numbers to compute and the other arguments are related to the distribution.

e.g., the function rbinom(n,size,prob) generate n random observations from binomial distribution with given number of trials (size) and the probability of success (prob).

rbinom(15, size = 5, p = 0.56)
 [1] 2 3 4 1 2 3 2 3 1 2 4 3 4 1 3
rbinom(15, size = 5, p = 0.56)
 [1] 4 3 3 1 2 2 3 2 0 4 2 3 3 2 3
set.seed(135)
rbinom(15, size = 5, p = 0.56)
 [1] 3 5 3 3 3 3 1 3 4 2 3 4 1 4 3
set.seed(135)
rbinom(15, size = 5, p = 0.56)
 [1] 3 5 3 3 3 3 1 3 4 2 3 4 1 4 3

To learn more about other discrete and continuous probability distributions using R, go through the following tutorials:

Discrete Distributions Using R

Binomial distribution in R
Poisson distribution in R
Geometric distribution in R
Negative Binomial distribution in R
Hypergeometric distribution in R

Continuous Distributions Using R

Uniform distribution in R
Exponential distribution in R
Normal distribution in R
Log-Normal distribution in R
Beta distribution in R
Gamma distribution in R
Cauchy distribution in R
Laplace distribution in R
Logistic distribution in R
Weibull distribution in R

Endnote

In this tutorial, you learn about which functions available in R to calculate probabilities, cumulative probabilities, quantiles for various discrete and continuous distributions. Also the functions to generate a random numbers from various discrete and continuous distributions.

Probability Distributions using R

Hopefully you enjoyed learning probability distributions using R. Hope the content is more than sufficient to understand probabilities, cumulative probabilities, quantiles and random numbers for various discrete and continuous probability distributions using R.

VRCBuzz co-founder and passionate about making every day the greatest day of life. Raju is nerd at heart with a background in Statistics. Raju looks after overseeing day to day operations as well as focusing on strategic planning and growth of VRCBuzz products and services. Raju has more than 25 years of experience in Teaching fields. He gain energy by helping people to reach their goal and motivate to align to their passion. Raju holds a Ph.D. degree in Statistics. Raju loves to spend his leisure time on reading and implementing AI and machine learning concepts using statistical models.

Leave a Comment