📊 R – Normal & Binomial Distribution Explained with Code Examples
🧲 Introduction – Statistical Distributions in R
Understanding probability distributions is essential in statistics and data science. R provides built-in functions to simulate, analyze, and visualize popular distributions.
This guide focuses on:
- 📈 Normal Distribution (continuous)
- 🧮 Binomial Distribution (discrete)
🎯 In this guide, you’ll learn:
- How to generate random numbers from distributions in R
- Plot Probability Density Functions (PDFs) and Cumulative Distributions (CDFs)
- Understand syntax of
dnorm()
,rbinom()
, etc. - Apply these distributions to real-world examples
🔹 1. Normal Distribution in R
A normal distribution (bell curve) is defined by:
mean
: center of the distributionsd
: spread (standard deviation)
✅ Generate Normally Distributed Data
x <- rnorm(1000, mean = 50, sd = 10)
hist(x, breaks = 20, col = "lightblue", main = "Histogram of Normal Distribution")
🔍 Explanation:
rnorm(n, mean, sd)
generatesn
values from a normal distribution
✅ Plot Normal Curve
x <- seq(-3, 3, by = 0.1)
y <- dnorm(x, mean = 0, sd = 1)
plot(x, y, type = "l", col = "blue", main = "Standard Normal Distribution")
🔍 Explanation:
dnorm(x, mean, sd)
gives density valuestype = "l"
draws a line
✅ Cumulative Probability
pnorm(1.96, mean = 0, sd = 1) # Output: ~0.975
🧠 Meaning: Probability that a standard normal value is ≤ 1.96 is ~97.5%
✅ Inverse Lookup (Quantile Function)
qnorm(0.975, mean = 0, sd = 1) # Output: ~1.96
🧠 Meaning: The 97.5th percentile in standard normal distribution is 1.96
🔹 2. Binomial Distribution in R
A binomial distribution models:
- n independent trials
- each trial has success probability (p)
- output = number of successes
✅ Simulate Binomial Outcomes
rbinom(10, size = 5, prob = 0.5)
🔍 Explanation:
rbinom(n, size, prob)
generatesn
samples where each sample is the number of successes insize
trials with probabilityprob
✅ Probability Mass Function (PMF)
x <- 0:10
y <- dbinom(x, size = 10, prob = 0.5)
barplot(y, names.arg = x, col = "orange", main = "Binomial PMF (n=10, p=0.5)")
🔍 Explanation:
dbinom(x, size, prob)
gives the probability of x successes
✅ Cumulative Probability
pbinom(6, size = 10, prob = 0.5) # P(X ≤ 6)
✅ Quantile (Inverse)
qbinom(0.8, size = 10, prob = 0.5)
📊 Distribution Function Overview Table
Function | Purpose | Normal Example | Binomial Example |
---|---|---|---|
r*() | Generate random values | rnorm() | rbinom() |
d*() | PDF / PMF | dnorm() | dbinom() |
p*() | Cumulative probability (CDF) | pnorm() | pbinom() |
q*() | Quantile function (inverse CDF) | qnorm() | qbinom() |
📌 Summary – Recap & Next Steps
R provides intuitive functions to work with distributions both analytically and visually.
Understanding these is vital for hypothesis testing, simulation, and data modeling.
🔍 Key Takeaways:
- Use
r*
,d*
,p*
, andq*
functions for distributions - Normal: continuous, symmetric, bell curve
- Binomial: discrete, success/failure outcomes
- Visualize distributions with
hist()
,barplot()
,plot()
⚙️ Real-World Relevance:
Used in probability models, quality control, A/B testing, risk analysis, genetics, and machine learning.
❓ FAQs – Distributions in R
❓ What is the difference between dnorm()
and pnorm()
?
✅ dnorm()
gives density (height of the curve); pnorm()
gives cumulative probability.
❓ How to simulate 100 coin tosses in R?
✅ Use:
rbinom(100, size = 1, prob = 0.5)
❓ What does qnorm(0.975)
return?
✅ The value of Z such that 97.5% of values lie below it (≈ 1.96).
❓ When to use binomial vs normal distribution?
✅ Binomial is for discrete successes in finite trials; Normal is for continuous, bell-shaped data.
❓ Can I plot both distributions on the same graph?
✅ Yes, but normalize the binomial data for proper comparison.
Share Now :