# Normal density at x = 0, mean = 0, sd = 1
dnorm(0, mean = 0, sd = 1)[1] 0.3989423
# Probability that a Poisson(λ=2) equals x = 3
dpois(3, lambda = 2)[1] 0.180447
R provides a systematic way to work with probability distributions.
For each distribution, there are four types of functions, following a naming convention:
dxxx: Density (for continuous) or Probability Mass (for discrete)pxxx: Cumulative distribution function (CDF)qxxx: Quantile function (inverse CDF)rxxx: Random sample generationdxxx)dnorm(0) gives the height of the normal curve at 0.dbinom(3, size=10, prob=0.5) = probability of exactly 3 successes.Think of dxxx as answering: “What is the likelihood at this exact point?”
# Normal density at x = 0, mean = 0, sd = 1
dnorm(0, mean = 0, sd = 1)[1] 0.3989423
# Probability that a Poisson(λ=2) equals x = 3
dpois(3, lambda = 2)[1] 0.180447
pxxx)This function gives the probability that the random variable is less than or equal to a value.
Example: pnorm(1.96) = probability that a standard normal random variable is ≤ 1.96 (about 0.975).
Example: pbinom(4, size=10, prob=0.5) = probability of getting up to 4 successes in 10 coin tosses
Think of pxxx as answering:
“What is the probability that I am at or below this value?”
# P(X ≤ 1.96) when X ~ N(0,1)
pnorm(1.96, mean = 0, sd = 1)[1] 0.9750021
# Cumulative probability for Binomial(n=10, p=0.3), X ≤ 4
pbinom(4, size = 10, prob = 0.3)[1] 0.8497317
qxxx)This is the inverse of the CDF.
You give it a probability p, and it tells you the corresponding value (quantile).
Example: qnorm(0.975) = the 97.5th percentile of the normal distribution (≈ 1.96).
Example: qbinom(0.5, size=10, prob=0.5) = the median number of successes in 10 coin tosses.
Think of qxxx as answering:
“At what value do I reach this probability?”
# 97.5th percentile of a N(0,1)
qnorm(0.975, mean = 0, sd = 1)[1] 1.959964
# Median of Chi-squared with 5 df
qchisq(0.5, df = 5)[1] 4.35146
rxxx)This generates random numbers following the distribution.
Example: rnorm(5, mean=0, sd=1) → five random draws from a standard normal distribution.
Example: rpois(10, lambda=3) → ten simulated counts from a Poisson distribution.
Think of rxxx as answering:
“Give me some random data from this distribution.”
# Generate 5 random normal values
rnorm(5, mean = 0, sd = 1)[1] -0.7918441 -0.5117870 1.1472192 -0.1115822 0.3613358
# Generate 10 random uniform values between 0 and 1
runif(10, min = 0, max = 1) [1] 0.1024625 0.2789253 0.6558259 0.1502892 0.7182222 0.8038826 0.2372838
[8] 0.5196909 0.5032154 0.7815392
| Distribution | Prefix | Example Functions |
|---|---|---|
| Normal | norm |
dnorm, pnorm, qnorm, rnorm |
| Binomial | binom |
dbinom, pbinom, qbinom, rbinom |
| Poisson | pois |
dpois, ppois, qpois, rpois |
| Exponential | exp |
dexp, pexp, qexp, rexp |
| Uniform | unif |
dunif, punif, qunif, runif |
| Chi-squared | chisq |
dchisq, pchisq, qchisq, rchisq |
| t-distribution | t |
dt, pt, qt, rt |
| F-distribution | f |
df, pf, qf, rf |
| Gamma | gamma |
dgamma, pgamma, qgamma, rgamma |
| Beta | beta |
dbeta, pbeta, qbeta, rbeta |
| Geometric | geom |
dgeom, pgeom, qgeom, rgeom |
| Hypergeometric | hyper |
dhyper, phyper, qhyper, rhyper |
Right now, these distributions may seem very theoretical. In chapter P-values and Confidence Intervals however, we’ll use these to get some actual inference from the STEpS data.