Simulation &
Variability in R
Learn to generate simulated data, summarize results, and interpret variability using R programming: from random sampling to understanding the spread of outcomes.
What Is Simulation?
A simulation uses a computer to mimic a random process many times. Instead of collecting real-world data (which can be expensive or slow), we generate synthetic outcomes governed by known probability rules and study the patterns that emerge.
Why Simulate in R?
R is built for statistical computing. Its random-number functions let you model real-world uncertainty in a single line of code. Simulation helps you:
Core R Functions for Simulation
R provides a family of functions for generating random data. The most important ones:
What Is Variability?
Variability measures how spread out a set of values is. Two simulations can have the same average but very different spreads. Understanding variability tells us how reliable our estimate is.
Measuring Variability in R
R gives us several functions to quantify spread. The three most common measures:
The Law of Large Numbers
As the number of simulation replicates \(n\) increases, the sample mean \(\bar{x}\) converges to the true population mean \(\mu\). This is why more replicates = more reliable estimates.
Sample vs Population
Simulation helps us understand the distinction between a single sample and the population it came from. Each simulation run is like drawing one sample: the results will vary.
| Sample | Population | |
|---|---|---|
| Mean symbol | \( \bar{x} \) | \( \mu \) |
| SD symbol | \( s \) | \( \sigma \) |
| Size | Finite (e.g., \( n = 30 \)) | Often infinite or very large |
| Variability | Changes each draw | Fixed (unknown) |
| R function | sample(), rnorm() | The model parameters you set |
How to Run a Simulation
Follow these five steps every time you build a simulation in R:
Example 1: Coin Flip Simulation
Question: If you flip a fair coin 10 times, what proportion of heads do you get? Repeat 10,000 times and study the distribution.
Coin Flip: Distribution of Results
Example 2: Normal Sampling
Question: A machine fills bottles with a mean of \(\mu = 500\) mL and \(\sigma = 5\) mL. Simulate 10,000 samples of size \(n = 25\) and examine the distribution of sample means.
Example 3: Effect of Sample Size
How does increasing \(n\) change the spread of sample means? Let us simulate with \(n = 5\), \(n = 30\), and \(n = 100\) from the same population (\(\mu = 50\), \(\sigma = 10\)).
The Standard Error Formula
The variability of the sample mean is measured by the standard error:
Common Mistakes
Summary of Key Concepts
| Concept | R Function | Interpretation |
|---|---|---|
| Generate random data | sample(), rnorm() | Creates one trial of the simulation |
| Repeat many trials | replicate(n, expr) | Builds a distribution of outcomes |
| Center of distribution | mean() | Estimate of the distribution's center (e.g., \(\mu\)) |
| Spread of distribution | sd(), var() | Measures variability of outcomes |
| Standard error | \(\sigma / \sqrt{n}\) | Variability of the sample mean |
| Visualize | hist() | Shows shape, center, spread |
Question 1 of 5
You run replicate(10000, mean(rnorm(50, 100, 15))). What does the sd() of the resulting vector estimate?
Question 2 of 5
Which R function would you use to simulate rolling a six-sided die 20 times?
Question 3 of 5
A simulation of 10,000 sample means has \(\text{SD} = 3.2\). If you quadruple the sample size \(n\) used in each trial, the new SD of sample means would be approximately:
Question 4 of 5
Why is calling set.seed() important before running a simulation in R?
Question 5 of 5
You simulate 10,000 proportions of heads from flipping a fair coin \(n = 100\) times. The resulting histogram will be approximately: