Skip to main content

Probability distributions in R

A probability distribution describes the possible values of a random variable and the probabilities (or probability densities) associated with those values. Each distribution is characterized by one or more parameters. Table 20.1 summarizes the distribution prefixes available in R, their corresponding distributions, and their parameter (see also).

Table 20.1: Distribution prefixes in R, the corresponding probability distributions, and their parameters.
Prefix Distribution Parameters
beta beta shape1, shape2
binom binomial sample size, probability
cauchy Cauchy location, scale
exp exponential rate (optional)
chisq chi-squared degrees of freedom
f Fisher’s F df1, df2
gamma gamma shape
geom geometric probability
hyper hypergeometric m, n, k
lnorm lognormal mean, standard deviation
logis logistic location, scale
nbinom negative binomial size, probability
norm normal mean, standard deviation
pois Poisson mean
signrank Wilcoxon signed rank statistic sample size n
t Student’s t degrees of freedom
unif uniform minimum, maximum (opt.)
weibull Weibull shape
wilcox Wilcoxon rank sum m, n

R identifies each distribution by a distribution prefix, which is used to construct four related functions: d*, p*, q*, and r*. These functions provide four standard ways of working with a probability distribution:

For example, the prefix norm identifies the normal distribution and gives rise to the functions dnorm(), pnorm(), qnorm(), and rnorm(). The following examples illustrate their graphical interpretation.

Probability density function

The dnorm() function calculates the probability density at a given value of a normally distributed variable. By changing the mean and standard deviation, we can examine how the location and spread of the distribution affect its shape. This is useful for understanding how different parameter values influence a probability distribution.

x <- seq(-4, 4, length = 100)

normal_dist <- data.frame(x = x,
                          d1 = dnorm(x = x, mean = 0, sd = 1),
                          d2 = dnorm(x = x, mean = 0.5, sd = 2),
                          d3 = dnorm(x = x, mean = -1, sd = 0.5))

ggplot(normal_dist) +
  geom_line(aes(x = x, y = d1, colour = "N(0, 1)")) +
  geom_line(aes(x = x, y = d2, colour = "N(0.5, 2)")) +
  geom_line(aes(x = x, y = d3, colour = "N(-1, 0.5)")) +
  labs(x = "x", y = "Probability density", colour = "Distribution")

Cumulative distribution function

The pnorm() function calculates the probability that a normally distributed variable is less than or equal to a given value. For example, pnorm(1) gives the probability of observing a value of 1 or less from a standard normal distribution. This is useful for calculating probabilities associated with specific ranges of values and comparing distributions with different parameter values.

normal_dist <- normal_dist |>
  mutate(p1 = pnorm(q = x, mean = 0, sd = 1),
         p2 = pnorm(q = x, mean = 0.5, sd = 2),
         p3 = pnorm(q = x, mean = -1, sd = 0.5))

ggplot(normal_dist) +
  geom_line(aes(x = x, y = p1, colour = "N(0, 1)")) +
  geom_line(aes(x = x, y = p2, colour = "N(0.5, 2)")) +
  geom_line(aes(x = x, y = p3, colour = "N(-1, 0.5)")) +
  labs(x = "x", y = "Cumulative probability", colour = "Distribution")

Quantiles: the inverse cumulative distribution function

The qnorm() function answers the question: What value of the variable corresponds to a given cumulative probability? For example, qnorm(0.95) returns approximately 1.645, meaning that 95% of observations from a standard normal distribution fall below this value. This is useful for finding critical values, such as those used to construct confidence intervals or perform hypothesis tests.

z_95 <- qnorm(p = 0.95, mean = 0, sd = 1)

ggplot(normal_dist) +
  geom_line(aes(x = x, y = p1, colour = "N(0, 1)")) +
  geom_vline(xintercept = z_95) +
  labs(x = "x", y = "Cumulative probability")

Random draws from a distribution

random_normal <- data.frame(r1 = rnorm(n = 100, mean = 0, sd = 1),
                            r2 = rnorm(n = 100, mean = 0, sd = 1))

ggplot(random_normal, aes(x = r1, y = r2)) +
  geom_point() +
  labs(x = "random draws", y = "random draws")