R Probability Distributions

A probability distribution describes how likely different outcomes are. R supports all major statistical distributions with a consistent set of four functions per distribution. These functions let you calculate probabilities, find critical values, and generate random data for simulations.

The Four Function Prefixes

Prefix    Purpose                          Example (Normal)
─────────────────────────────────────────────────────────────────
d___()    Density / probability mass       dnorm(x)
p___()    Cumulative probability (CDF)     pnorm(q)
q___()    Quantile (inverse CDF)           qnorm(p)
r___()    Random number generation         rnorm(n)
Memory trick:
  d = density         (how likely is this exact value?)
  p = probability     (how likely is a value ≤ this?)
  q = quantile        (what value marks this probability?)
  r = random          (give me random draws)

Normal Distribution

# IQ scores: mean=100, sd=15
dnorm(100, mean=100, sd=15)   # density at IQ=100
pnorm(115, mean=100, sd=15)   # P(IQ ≤ 115) = 0.8413
pnorm(115, mean=100, sd=15) - pnorm(85, mean=100, sd=15)  # P(85 ≤ IQ ≤ 115)
qnorm(0.95, mean=100, sd=15)  # IQ score at 95th percentile = 124.7
rnorm(5,    mean=100, sd=15)  # 5 random IQ scores

# Standard normal (mean=0, sd=1)
pnorm(1.96)   # 0.975  — classic 95% CI boundary
qnorm(0.975)  # 1.96
Normal distribution diagram:
         68.3%
      ┌─────────┐
  95.4%         │
┌──────────────────┐
99.7%              │
┌──────────────────────┐
   ─3  ─2  ─1   0   1   2   3  (standard deviations)
              ↑ mean

Binomial Distribution

# Coin flips: n=10, P(heads)=0.5
# P(exactly 6 heads)
dbinom(6, size=10, prob=0.5)   # 0.2051

# P(6 or fewer heads)
pbinom(6, size=10, prob=0.5)   # 0.8281

# Expected number of successes
n <- 10; p <- 0.5
cat("Mean:", n*p, "SD:", sqrt(n*p*(1-p)), "\n")
# Mean: 5  SD: 1.58

# 20 random outcomes
rbinom(20, size=10, prob=0.5)

Poisson Distribution

# Customer arrivals: lambda=4 per hour
# P(exactly 3 arrivals)
dpois(3, lambda=4)    # 0.1954

# P(3 or fewer arrivals)
ppois(3, lambda=4)    # 0.4335

# P(more than 5 arrivals)
1 - ppois(5, lambda=4)  # 0.2149

# Simulate 1 hour
rpois(1, lambda=4)   # random count

Other Common Distributions

Distribution    R suffix   Key Parameters    Typical Use
──────────────────────────────────────────────────────────────────────
Uniform         unif       min, max          Equal probability events
Exponential     exp        rate              Time between events
t-distribution  t          df                Small-sample inference
Chi-square      chisq      df                Goodness-of-fit tests
F-distribution  f          df1, df2          ANOVA, regression F-test
Beta            beta       shape1, shape2    Proportions (0 to 1)
Gamma           gamma      shape, rate       Positive skewed data
# Examples
runif(5, min=0, max=100)           # 5 random numbers 0-100
pexp(2, rate=0.5)                  # P(wait ≤ 2 min) if avg wait=2
qt(0.975, df=29)                   # t critical value (95% CI, n=30)
pchisq(9.5, df=4)                  # chi-square CDF

Visualizing a Distribution

library(ggplot2)

x <- seq(-4, 4, length.out=200)
df <- data.frame(x=x, density=dnorm(x))

ggplot(df, aes(x=x, y=density)) +
  geom_line(color="steelblue", linewidth=1.5) +
  geom_area(data=subset(df, x >= -1 & x <= 1),
            fill="steelblue", alpha=0.3) +
  labs(title="Standard Normal Distribution",
       subtitle="Shaded area = P(-1 ≤ Z ≤ 1) = 68.3%",
       x="Z-score", y="Density") +
  theme_minimal()

Checking if Data Follows a Distribution

# Q-Q plot (normal distribution check)
qqnorm(scores)
qqline(scores, col="red")
# Points close to the red line → approximately normal

# Shapiro-Wilk normality test
shapiro.test(scores)
# p > 0.05 → not enough evidence to reject normality

Probability distributions are the mathematical foundation of statistical inference. Every hypothesis test, confidence interval, and regression model assumes a specific distribution for the data or errors. Knowing how to compute probabilities, quantiles, and random draws in R gives you the building blocks for all statistical analysis.

Leave a Comment

Your email address will not be published. Required fields are marked *