Skip to content
AgileLogics
6 min read

Probability, Sampling, and Simulation

Prerequisites: Descriptive statistics

R can generate random numbers and work with probability distributions — the foundation of statistical inference, and a powerful way to answer questions by simulation.

Random sampling and reproducibility

set.seed(42)
sample(1:6, 5, replace = TRUE)
sample(c("heads", "tails"), 3, replace = TRUE)
round(runif(3), 3)
[1] 1 5 1 1 2
[1] "tails" "tails" "tails"
[1] 0.657 0.705 0.458

Random numbers in R come from a generator that starts from a seed. set.seed(42) fixes the starting point, so you (and anyone rerunning your script) get exactly the same "random" results. Always set a seed in analyses that use randomness.

Distributions

R has four functions for each distribution, with prefixes:

Prefix Gives Example
r random values rnorm(5)
d the probability (or density) of a value dbinom(3, 10, 0.5)
p the probability of a value or less pnorm(1.96)
q the value for a given probability qnorm(0.975)
dbinom(3, size = 10, prob = 0.5)
pbinom(3, size = 10, prob = 0.5)
pnorm(1.96)
qnorm(0.975)
[1] 0.1171875
[1] 0.171875
[1] 0.9750021
[1] 1.959964
  • The chance of exactly 3 heads in 10 fair coin flips is about 11.7%; 3 or fewer, about 17.2%.
  • In a normal distribution, 97.5% of values fall below 1.96 standard deviations above the mean — the source of the familiar "±1.96" in 95% confidence intervals.

Answering questions by simulation

What's the chance that at least two people in a group of 23 share a birthday? Simulate it:

set.seed(1)
shared <- replicate(10000, {
  birthdays <- sample(1:365, 23, replace = TRUE)
  any(duplicated(birthdays))
})
mean(shared)
[1] 0.5026

About 50% (the exact answer is 50.7%) — the famous "birthday paradox". replicate runs the experiment 10,000 times; the mean of the TRUE/FALSE results is the proportion of times it happened. Simulation is a practical way to check your reasoning about probability.

Common mistakes

  • Forgetting set.seed, so results change every run and can't be reproduced.
  • Forgetting replace = TRUE in sample when values can repeat, such as dice rolls.