R: Probability Distributions
Last updated: 2026-08-26
In the previous lesson, we learned about "examining data"—descriptive statistics. In this lesson, we’ll learn about "examining randomness"—probability distributions. All statistical inference in R (t-tests, regression, hypothesis testing) is based on probability distributions. In this lesson, we’ll cover R’s
d/p/q/rfour major function families + four commonly used distributions.
After completing this lesson, you’ll be able to use R to simulate any random phenomenon, perform probability calculations, and lay the groundwork for future hypothesis testing.
1. What You'll Learn
- The Four Major Families of Probability Distribution Functions (d/p/q/r)
- Normal distribution (most commonly used)
- Binomial distribution (success/failure)
- Poisson distribution (number of events)
- t-distribution / F-distribution / chi-square distribution
- set.seed: random seed
- Hands-On: Quality Inspection Simulation
2. A Story About Quality Inspection
(1) Pain Point: What is a normal product pass rate?
Alice is a quality control inspector who found that the mean weight of products on a certain production line is 100g, with a standard deviation of 2g. Her manager asked, "What is the proportion of products weighing less than 95g?"
Look up the normal distribution table in a book? Use an Excel formula? Or just one line of R code—
(2) Solution using R
# Weight < 95g the probability
pnorm(95, mean = 100, sd = 2)
# [1] 0.00621 ← 0.6%
# Extract 1000 Product Simulation Testing
set.seed(42)
samples <- rnorm(1000, mean = 100, sd = 2)
mean(samples < 95) # 0.6% ← Verification
2 lines of code → Answer + Simulation Verification.
3. The Four Major Families of Probability Distribution Functions in R
(1) Comparison of the Four Major Functions
| Function Prefix | Meaning | Purpose |
|---|---|---|
d |
density | probability density function (PDF) |
p |
probability | Cumulative Distribution Function (CDF) |
q |
quantile | inverse function (finding a quantile given a probability) |
r |
random | Generate a random number |
All distributions have these four functions: dnorm pnorm qnorm rnorm (normal); dbinom pbinom... (binomial), and so on.
(2) Mnemonic Phrases
graph LR
A[d Density] -->|Find the probability| B[p Cumulative]
B -->|Inverse Quantile| C[q quantile]
A -->|Simulation| D[r Random]
style A fill:#cce5ff
style B fill:#d4edda
style C fill:#f8d7da
style D fill:#fff3cd
(3) A Detailed Explanation of the Four Major Functions
# Take the normal distribution as an example (Mean 0, Standard Deviation 1)
dnorm(0) # 0.3989 ← Probability Density
pnorm(0) # 0.5 ← Cumulative Probability
qnorm(0.975) # 1.96 ← Given that 97.5% Find the percentile
rnorm(5) # 5 A random number
# Custom Parameters (Mean 100, Standard Deviation 2)
dnorm(95, 100, 2) # 95 Density at that point
pnorm(95, 100, 2) # 95 Left-sided cumulative probability
qnorm(0.975, 100, 2) # Given that 97.5% Find the percentile
rnorm(5, 100, 2) # 5 Per capita average 100 Random Number
4. Normal Distribution (Most Commonly Used)
(1) What is a normal distribution?
graph TB
A[Normal Distribution N mu sigma] --> B[Bell-shaped symmetry]
A --> C[68-95-99.7 Principles]
A --> D[Central Limit Theorem]
A --> E[Widely found in nature/Social Phenomena]
style A fill:#fff3cd
style B fill:#cce5ff
style C fill:#d4edda
style D fill:#f8d7da
style E fill:#e1d4ff
Normal Distribution = Bell-shaped, symmetrical, and the most common distribution found in nature.
(2) The 68-95-99.7 Rule
μ ± 1σ → 68.27% Data
μ ± 2σ → 95.45% Data
μ ± 3σ → 99.73% Data
(3) The 4 Standard Normal Functions
# Standard Normal N(0, 1)
dnorm(0) # 0.3989
pnorm(1.96) # 0.975 ← 1.96 On the left 97.5%
qnorm(0.975) # 1.96 ← 97.5% quantile = 1.96
rnorm(5) # 5 A standard normal random number
# General Normal N(μ, σ)
mu <- 100
sigma <- 2
# P(X < 95) = ?
pnorm(95, mean = mu, sd = sigma) # 0.00621
# P(95 < X < 105) = ?
pnorm(105, mu, sigma) - pnorm(95, mu, sigma) # 0.9876
# 99% Data Range
qnorm(0.005, mu, sigma) # The Lower World
qnorm(0.995, mu, sigma) # Upper Realm
(4) Test for Normality
# 1. Visual: QQ plot
qqnorm(x)
qqline(x, col = "red")
# 2. Statistical Tests
shapiro.test(x) # Shapiro-Wilk (n < 5000)
# p < 0.05 Indicates non-normal distribution
5. Binomial Distribution (Success/Failure)
(1) What is a binomial distribution?
The distribution of the number of successes in n independent Bernoulli trials. The probability of success in each trial is p, and the probability of failure is 1-p.
Example: Toss 10 coins, number of times facing up X ~ Binomial(10, 0.5)
(2) The Four Major Binary Functions
# Toss 10 coins, number of heads
dbinom(5, size = 10, prob = 0.5) # 0.246 <- P(5 heads)
pbinom(5, size = 10, prob = 0.5) # 0.623 <- P(<= 5 heads)
qbinom(0.5, size = 10, prob = 0.5) # 5 ← Median
rbinom(1000, size = 10, prob = 0.5) # 1000 sets of 10 trials, number of successes each
# Example: 100 shots, hit rate 80%, probability of at least 90 hits
1 - pbinom(89, size = 100, prob = 0.8)
# [1] 0.0227 ← 2.3%
(3) Binomial Visualization
n <- 10
p <- 0.5
x <- 0:n
# Probability Mass Function
plot(x, dbinom(x, n, p), type = "h",
main = paste0("Binomial(", n, ",", p, ")"),
xlab = "Number of Successes", ylab = "Probability",
col = "blue", lwd = 2)
points(x, dbinom(x, n, p), pch = 19, col = "red")
6. Poisson Distribution (Number of Events)
(1) What is the Poisson distribution?
The distribution of the number of events occurring within a fixed time or space. Commonly used for:
- Number of customers arriving at the store within 1 hour
- Number of traffic accidents per kilometer of road
- Number of calls received in 1 day
Parameter λ = average number of events per unit time.
(2) The Four Poisson Functions
# Example: Average 10 customers per hour
dpois(8, lambda = 10) # P(X = 8) = 0.1126
ppois(8, lambda = 10) # P(X ≤ 8) = 0.3328
qpois(0.5, lambda = 10) # Median = 10
rpois(100, lambda = 10) # 100 Number of customers visiting the store per hour
# Example: 5 complaints per day, probability of more than 10
1 - ppois(10, lambda = 5) # 0.0137 ← 1.4%
(3) The Relationship Between the Poisson and Normal Distributions
When λ is large, the Poisson distribution approximates the normal distribution N(λ, √λ):
# λ = 100
lambda <- 100
# Poisson P(90 ≤ X ≤ 110)
ppois(110, lambda) - ppois(89, lambda) # ≈ 0.728
# Equivalent Normal Approximation
pnorm(110, 100, 10) - pnorm(90, 100, 10) # ≈ 0.683
# Approximate but not exactly equal
7. t-Distribution / F-Distribution / Chi-Square Distribution
(1) Quick Reference for the Three Major Sampling Distributions
| Distribution | Purpose | R Function |
|---|---|---|
| t-distribution | Small-sample mean inference | dt/pt/qt/rt |
| F Distribution | Analysis of Variance (ANOVA) | df/pf/qf/rf |
| Chi-Square Distribution | Categorical variables, goodness of fit | dchisq/pchisq/qchisq/rchisq |
(2) A Detailed Explanation of the t-Distribution
# Degrees of freedom df = 10
qt(0.975, df = 10) # 2.228 <- t threshold (larger than the normal distribution's 1.96)
pt(2.228, df = 10) # 0.975
# As df -> inf, t distribution -> standard normal
qt(0.975, df = 1000) # 1.962 ← Approach 1.96
qt(0.975, df = 10000) # 1.960
(3) Chi-Square Distribution
# Degrees of freedom df = 5
dchisq(3, df = 5) # Density
pchisq(11.07, df = 5) # 0.95 <- Chi-square critical value (95%)
qchisq(0.95, df = 5) # 11.07
# Example: Observed 8 events, expected 5, p-value
1 - pchisq(8, df = 5) # 0.846
(4) F Distribution
# Degrees of freedom df1 = 5, df2 = 10
qf(0.95, df1 = 5, df2 = 10) # 3.326 ← F Threshold
pf(3.326, df1 = 5, df2 = 10) # 0.95
8. set.seed: Random Seed
(1) Why are seeds needed?
R's "random numbers" are actually pseudo-random—given a seed, the results are reproducible:
# No seeded players
rnorm(3) # The results are different every time
# Let $s$ be a seed
set.seed(42)
rnorm(3) # The First Time
set.seed(42)
rnorm(3) # Exactly the same
set.seed() in your research/report—to ensure the results are reproducible.
(2) Practical Applications
# 1. Simulate Preset Seeds
set.seed(42)
sim_data <- rnorm(1000, 100, 15)
# 2. Set a seed before training the model
set.seed(123)
model <- lm(y ~ x, data)
# 3. Set a seed before cross-validation
set.seed(2024)
folds <- createFolds(data$y, k = 5)
9. Sampling and Simulation
(1) sample() Sampling
# 1. Simple Random Sampling
sample(1:100, 10) # Draw 10 from 1-100 (no duplicates)
sample(1:100, 10, replace = TRUE) # Sampling with replacement
# 2. Data Frame Sampling
sample_n(df, 10) # Draw 10 rows (old)
slice_sample(df, n = 10) # Draw 10 rows (dplyr 1.0+)
# 3. Stratified Sampling
df |>
group_by(class) |>
slice_sample(n = 5) # Draw one from each group 5 row
# 4. Set Up Probability Sampling
sample(c("A", "B", "C"), 100, replace = TRUE,
prob = c(0.5, 0.3, 0.2))
(2) Monte Carlo Simulation
# Example: Estimate pi
set.seed(42)
n_sim <- 100000
x <- runif(n_sim, -1, 1)
y <- runif(n_sim, -1, 1)
inside <- sqrt(x^2 + y^2) <= 1
pi_est <- 4 * mean(inside)
# [1] 3.14116 ← Approach π = 3.14159
10. Complete Example: Quality Inspection Simulation
Below is an example of a complete workflow that ties together all the probability distribution concepts covered in this lesson.
▶ Example: Simulation of Quality Inspection on a Production Line
# ============================================
# Production Line Quality Inspection Simulation
# Features: Comprehensive Application of 4 Distributions
# ============================================
set.seed(42)
# 1. Product Weight Inspection (Normal Distribution)
# Specifications: Mean 100g, Standard Deviation 2g
n_products <- 10000
weights <- rnorm(n_products, mean = 100, sd = 2)
cat("=== Weight Inspection (Normal Distribution N(100, 2)) ===\n")
cat("Sample size:", length(weights), "\n")
cat("Actual Mean:", round(mean(weights), 4), "\n")
cat("Actual standard deviation:", round(sd(weights), 4), "\n")
cat("Theory < 95g Ratio:", round(pnorm(95, 100, 2), 4), "\n")
cat("Actual < 95g Ratio:", round(mean(weights < 95), 4), "\n")
cat("Theory < 105g Ratio:", round(pnorm(105, 100, 2), 4), "\n")
cat("Actual < 105g Ratio:", round(mean(weights < 105), 4), "\n\n")
# 2. Inspection of Nonconforming Products (Binomial Distribution)
# Sample 100 items, probability of at least 95 passing
n_sample <- 100
p_pass <- 0.95
prob_at_least_95 <- 1 - pbinom(94, n_sample, p_pass)
cat("=== Random Sampling Inspection (Binomial(100, 0.95)) ===\n")
cat("P(95 Qualified) =", round(prob_at_least_95, 4), "\n\n")
# 3. Customers Visit the Store (Poisson Distribution)
# Average 10 customers per hour
n_hours <- 1000
customers <- rpois(n_hours, lambda = 10)
cat("=== Customers Visit the Store (Poisson(10)) ===\n")
cat("Theoretical mean: 10\n")
cat("Actual Mean:", round(mean(customers), 2), "\n")
cat("Theory P(>15):", round(1 - ppois(15, 10), 4), "\n")
cat("Actual P(>15):", round(mean(customers > 15), 4), "\n\n")
# 4. Sample Survey (t Distribution)
# Sample 30 products, estimate mean
sample_size <- 30
sample_data <- sample(weights, sample_size)
cat("=== Sample Survey (t Distribution) ===\n")
cat("Sample size:", sample_size, "\n")
cat("Sample mean:", round(mean(sample_data), 2), "\n")
cat("95% Confidence Interval: [\n")
ci <- t.test(sample_data)$conf.int
cat(" ", round(ci[1], 2), ",", round(ci[2], 2), "]\n\n")
# 5. Hypothesis Testing (use qnorm)
# I want to know if the weight deviates significantly 100g
z_score <- (mean(sample_data) - 100) / (sd(sample_data) / sqrt(sample_size))
p_value <- 2 * (1 - pnorm(abs(z_score)))
cat("=== Hypothesis Testing (Z Test) ===\n")
cat("Z score:", round(z_score, 4), "\n")
cat("P value:", round(p_value, 4), "\n")
cat("Conclusion:", ifelse(p_value < 0.05, "Significant deviation from 100g", "No significant difference"), "\n\n")
# 6. Outlier Detection (The Concept of the Chi-Square Distribution)
# Use 3-sigma principle
outliers <- weights[abs(weights - 100) > 3 * 2]
cat("=== Outlier Detection (3-sigma Principle) ===\n")
cat("Theoretical Anomaly Ratio:", round(2 * pnorm(-3), 6), "(i.e. 0.27%)\n")
cat("Actual Number of Exceptions:", length(outliers), "/", n_products, "\n")
cat("Actual Exception Rate:", round(length(outliers) / n_products, 6), "\n")
# 7. Sampling Design (Stratified Sampling)
cat("\n=== Stratified Sampling ===\n")
# Simulate 3 production lines (Different Pass Rates)
production <- tibble::tibble(
line = rep(c("Line A", "Line B", "Line C"), each = 1000),
weight = c(rnorm(1000, 100, 2),
rnorm(1000, 101, 2),
rnorm(1000, 99, 2))
)
# Draw 50 from each line
library(dplyr)
sampled <- production |>
group_by(line) |>
slice_sample(n = 50)
cat("Draw 50 per line, Estimated Overall Mean:\n")
cat("Overall Sample Mean:", round(mean(sampled$weight), 2), "\n")
cat("Total True Mean:", round(mean(production$weight), 2), "\n")
Expected Output (Excerpt):
=== Weight Inspection (Normal Distribution N(100, 2)) ===
Sample size: 10000
Actual Mean: 99.9898
Actual standard deviation: 1.9973
Theory < 95g Ratio: 0.0062
Actual < 95g Ratio: 0.0063
=== Random Sampling Inspection (Binomial(100, 0.95)) ===
P(95 Qualified) = 0.5647
❓ FAQ
set.seed(42) is a common "magic number." The key is that the code must be reproducible after the seed is set.📖 Summary
- R Probability Distributions: 4 Major Families of Functions:
dDensity /pCumulative /qQuantile /rRandom - Normal Distribution (most commonly used): bell-shaped and symmetrical, 68-95-99.7 rule,
pnorm/qnorm/rnorm - Binomial Distribution: The number of successes in n independent trials,
dbinom/pbinom/qbinom/rbinom - Poisson distribution: Number of events per unit time,
dpois/ppois/qpois/rpois - t-distribution: Small-sample mean estimation (n < 30); F-distribution: Ratio of variances; chi-square distribution: Categorical data
set.seed(42)Making Randomness Reproducible—A Must-Include in Research Reports- Sampling:
sample(x, n)Without replacement /replace = TRUEWith replacement - Monte Carlo simulation = Generating large numbers of samples using R + statistics to estimate complex probabilities
📝 Exercises
-
Basic Problem: Use
pnormto calculate the following for an N(100, 2) distribution: ① P(X < 95), ② P(X > 105), ③ P(95 < X < 105), and ④ the 99% confidence interval. Verify the results. -
Basic Problem: Use
rbinom(1000, 10, 0.5)to simulate 1,000 trials of "flipping a coin 10 times" and calculate the mean, variance, and standard deviation of the sample. Compare these values with the theoretical values (5, 2.5, 1.58). -
Basic Problem: Use
rpois(1000, 5)to simulate 1,000 instances of "the number of calls received within 5 minutes," plot a histogram and the theoretical Poisson PDF, and compare the distributions. -
Advanced Problem: Simulate 10,000 product weights (normally distributed N(100, 2)), use the 3σ rule to identify the number of outliers, and compare it with the theoretical value of 0.27% to verify the Central Limit Theorem (CLT).
-
Challenge: Estimate π using Monte Carlo simulation: ① Roll a die 10,000 times; ② Calculate the proportion of rolls that land within a quarter circle; ③ Estimate 4 × this proportion = π; ④ Vary the number of rolls (100, 1,000, 10,000, 100,000) to observe changes in accuracy. Take screenshots of the results for the four levels of accuracy.