7.5 Probability distributions in R

Appendix B covers a number of common probability distributions that are relevant for the purposes of this course. Appendix C furthermore provides additional theoretical background on the exponential family, an important class of probability distributions widely used in statistics.

R has built-in functions for most common probability distributions. Further distributions are covered in additional packages. If mydist is the name of a probability distribution, then R routinely offers four functions for mydist, distinguished by the first letter:

  1. dmydist(x, ...) the density function gives the probability (mass/density) \(f(x)\) for x
  2. pmydist(x, ...) the cumulative probability function gives the cumulative distribution function \(F(x)\) for x
  3. qmydist(p, ...) the quantile function gives the value x for which p = pmydist(x, ...)
  4. rmydist(n, ...) the random sample function returns n samples from the distribution

For example, the family of functions for the normal distribution has the following functions:

# density of standard normal at x = 1
dnorm(x = 1, mean = 0, sd = 1)
## [1] 0.2419707
# cumulative density of standard normal at q = 0
pnorm(q = 0, mean = 0, sd = 1)
## [1] 0.5
# point where the cumulative density of standard normal is p = 0.5
qnorm(p = 0.5, mean = 0, sd = 1)
## [1] 0
# n = 3 random samples from a standard normal
rnorm(n = 3, mean = 0, sd = 1)
## [1]  0.8625486 -2.6191819 -0.8560546

Exercise 7.6

  1. Use R to compute the median of the exponential distribution with rate \(\lambda = 1\). Remember that the median is the 50% quantile. The quantile function of the exponential distribution can be accessed with qexp in R.
qexp(0.5, rate = 1)
## [1] 0.6931472
  1. Use R’s function for the cumulative normal distribution (see above) to compute this integral, i.e., the area under the density function of a standard normal distribution ranging from -1 to 2:

\[ \int_{-1}^{2} \mathcal{N}(x, \mu = 0, \sigma = 1) \text{d}x \]

pnorm(2, mean = 0, sd = 1) - pnorm(-1, mean = 0, sd = 1)
## [1] 0.8185946