Here, we discuss probability distributions functions in R, setting parameters, getting random samples, density or mass, cumulative probability and quantile.

1 Table of Distribution Functions in R

The table below contains 16 probability distributions from the "stats" package in R. It contains information on how to sample from each distribution with different parameter values, derive probability density or mass for any \(x\) value, cumulative probability for any quantile (\(q\)) value, and deriving quantile values for any probability (\(p\)).

Table of Some Distribution Functions in R
Distribution Random Sample of Length \(n\) Probability Density or Mass at \(x\) Cumulative Probability at \(q\) Quantile for \(p\)
Normal Distribution rnorm(n, mean=0, sd=1) dnorm(x, mean=0, sd=1) pnorm(q, mean=0, sd=1) qnorm(p, mean=0, sd=1)
Student’s t Distribution rt(n, df, ncp) dt(x, df, ncp) pt(q, df, ncp) qt(p, df, ncp)
Binomial Distribution rbinom(n, size, prob) dbinom(x, size, prob) pbinom(q, size, prob) qbinom(p, size, prob)
Poisson Distribution rpois(n, lambda) dpois(x, lambda) ppois(q, lambda) qpois(p, lambda)
Exponential Distribution rexp(n, rate=1) dexp(x, rate=1) pexp(q, rate=1) qexp(p, rate=1)
Uniform Distribution runif(n, min=0, max=1) dunif(x, min=0, max=1) punif(q, min=0, max=1) qunif(p, min=0, max=1)
Chi-squared Distribution rchisq(n, df, ncp=0) dchisq(x, df, ncp=0) pchisq(q, df, ncp=0) qchisq(p, df, ncp=0)
F Distribution rf(n, df1, df2, ncp) df(x, df1, df2, ncp) pf(q, df1, df2, ncp) qf(p, df1, df2, ncp)
Lognormal Distribution rlnorm(n, meanlog=0, sdlog=1) dlnorm(x, meanlog=0, sdlog=1) plnorm(q, meanlog=0, sdlog=1) qlnorm(p, meanlog=0, sdlog=1)
Negative Binomial Distribution rnbinom(n, size, prob, mu) dnbinom(x, size, prob, mu) pnbinom(q, size, prob, mu) qnbinom(p, size, prob, mu)
Geometric Distribution rgeom(n, prob) dgeom(x, prob) pgeom(q, prob) qgeom(p, prob)
Hypergeometric Distribution rhyper(nn, m, n, k) dhyper(x, m, n, k) phyper(q, m, n, k) qhyper(p, m, n, k)
Beta Distribution rbeta(n, shape1, shape2, ncp=0) dbeta(x, shape1, shape2, ncp=0) pbeta(q, shape1, shape2, ncp=0) qbeta(p, shape1, shape2, ncp=0)
Gamma Distribution rgamma(n, shape, rate=1, scale=1/rate) dgamma(x, shape, rate=1, scale=1/rate) pgamma(q, shape, rate=1, scale=1/rate) qgamma(p, shape, rate=1, scale=1/rate)
Cauchy Distribution rcauchy(n, location=0, scale=1) dcauchy(x, location=0, scale=1) pcauchy(q, location=0, scale=1) qcauchy(p, location=0, scale=1)
Weibull Distribution rweibull(n, shape, scale=1) dweibull(x, shape, scale=1) pweibull(q, shape, scale=1) qweibull(p, shape, scale=1)

2 Examples for Setting Parameters

When a parameter’s value in a function is pre-specified to a default value, it does not need to be specified, unless it is to be set to a different parameter value.

For example, for pnorm(1.96, mean=0, sd=1), the following are the same:

pnorm(1.96)
[1] 0.9750021

or:

pnorm(1.96, 0)
[1] 0.9750021

and:

pnorm(1.96, 0, 1)
[1] 0.9750021

Note that the order of 0 and 1 matters as pnorm(1.96, 1) and pnorm(1.96, 1, 0) will give different results.

Similarly for dexp(x, rate=1), the following are the same:

dexp(5)
[1] 0.006737947

and:

dexp(5, 1)
[1] 0.006737947

3 Examples for Random Sampling, Density or Mass, Cumulative Probability and Quantiles

Random Sampling:

Sample 10 observations from the binomial distribution with \(\tt{size} = 12\) and \(p = 0.7\):

rbinom(n = 10, size = 12, prob = 0.7)

# Same as:
rbinom(10, 12, 0.7) 
 [1] 10  8  8  8  7  8 12 10  8  8

Density or Mass:

Calculate the density at \(x=0.7\), in the uniform distribution with \(\min = 0.5\) and \(\max = 1\):

dunif(0.7, min = 0.5, max = 1)

# Same as:
dunif(0.7, 0.5, 1)
[1] 2

Cumulative Probability:

Calculate the cumulative density at \(x=2.2\), in the Student’s t distribution with degrees of freedom \(\tt{df} = 12\):

pt(2.2, df = 12)

# Same as:
pt(2.2, 12)
[1] 0.9759316

Quantile:

Derive the quantile for \(p = 0.95\), in the F distribution with degrees of freedom, \(\tt{df1} = 1\) and \(\tt{df2} = 25\):

qf (0.95, df1 = 1, df2 = 25)

# Same as:
qf (0.95, 1, 25)
[1] 4.241699

Copyright © 2020 - 2024. All Rights Reserved by Stats Codes