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/r four 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



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

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

100%
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

R
# 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?

100%
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

TEXT 📖 Display only
μ ± 1σ → 68.27% Data
μ ± 2σ → 95.45% Data
μ ± 3σ → 99.73% Data

(3) The 4 Standard Normal Functions

R
# 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

R
# 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.

TEXT 📖 Display only
Example: Toss 10 coins, number of times facing up X ~ Binomial(10, 0.5)

(2) The Four Major Binary Functions

R
# 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

R
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:

Parameter λ = average number of events per unit time.

(2) The Four Poisson Functions

R
# 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(λ, √λ):

R
# λ = 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

R
# 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
💡 Tip: When n > 30, the t-distribution is almost identical to the normal distribution, so you can use the normal approximation. When n < 30, strictly use the t-distribution.

(3) Chi-Square Distribution

R
# 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

R
# 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:

R
# 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
💡 Tip: Be sure to include set.seed() in your research/report—to ensure the results are reproducible.

(2) Practical Applications

R
# 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

R
# 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

R
# 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

R 📖 Display only
# ============================================
# 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")
56 logic lines (exceeds 40-line limit, display only)

Expected Output (Excerpt):

TEXT 📖 Display only
=== 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

Q How do you distinguish between d, p, q, and r?
A d stands for density, p for cumulative, q for quantile, and r for random.
Q When should you use the t-distribution versus the normal distribution?
A Use the normal distribution (CLT) when n ≥ 30, and the t-distribution (small sample) when n < 30. When the variance is unknown, prefer the t-distribution.
Q What is the difference between the Poisson distribution and the binomial distribution?
A The binomial distribution counts the number of successes in n trials (where n is fixed), while the Poisson distribution counts the number of events per unit time (where n is not fixed). The Poisson distribution is an approximation of the binomial distribution for cases where n is large and p is small.
Q How do I choose a random seed?
A Any integer will do—set.seed(42) is a common "magic number." The key is that the code must be reproducible after the seed is set.
Q How do you interpret a QQ-norm plot?
A Points close to the red line indicate a normal distribution. An S-shaped curve indicates a skewed distribution, and a U-shaped curve indicates heavy-tailed or light-tailed distribution.
Q What is the chi-square distribution used for?
A ① Goodness-of-fit test ② Test of independence (contingency table) ③ Confidence intervals for estimated variances. We won’t go into detail in this lesson, but it will be used in the next lesson on hypothesis testing.

📖 Summary


📝 Exercises

  1. Basic Problem: Use pnorm to 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.

  2. 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).

  3. 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.

  4. 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).

  5. 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.

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏