MATH U113 · Probability & Statistics · R lab support
R lab 3 · Sampling distributions and the CLT
The lab sheet "Sampling Distributions: Sample Mean and Sample Sum". It covers the distribution of X̄ §5.3 and the Central Limit Theorem §5.4, taught from zero: what a sampling distribution is, the two formulas the whole lab checks, a simulator you can drive yourself, the sheet's script line by line, and its observation table filled in from a real run.
What this is for: Question 3 of the Lab Assignment (10 of its 20 marks, due 4 Nov) is this lab again, with two new populations. Read this page first, then go to the assignment page.
Where you actually stand: sampling distributions and the CLT are §5.3–5.4, Module 5. No school syllabus, JEE included, teaches them, so this is new to everyone in the hall. The R on the sheet is Lab 2's rexp plus three new functions: matrix, rowMeans and sapply. The only earlier facts you need are the mean and variance of the exponential (Module 3) and the rule V(aX) = a2V(X) (Module 2).
1 · A statistic is a random variable
Pick 5 light bulbs at random from a factory and average their lifetimes. You get one number, say 0.91 years. Pick another 5 and you get a different number, 1.34. A third batch gives 0.62. The sample mean X̄ changes from sample to sample, so it is itself a random variable, with its own distribution. That distribution is called the sampling distribution of X̄.
The lab asks one question: what does that distribution look like? R answers it by brute force. It takes B = 5000 samples, computes 5000 sample means, and draws a histogram of them. The same goes for the sample sum Sn = X1 + ⋯ + Xn.
n is the size of one sample (5 bulbs). B is how many samples you take (5000 batches). The theory depends only on n. B only controls how smooth the histogram looks. The assignment calls its B "10,000 independent random samples" and its n "sample sizes", so keep them apart.
2 · The two facts the lab checks
Let X1, …, Xn be independent draws from a population with mean μ and standard deviation σ. Then, for every n:
| Sample mean X̄ | Sample sum Sn | |
|---|---|---|
| Mean (centre) | μ | nμ |
| Standard deviation (spread) | σ/√n | σ√n |
The centres make sense straight away. If each bulb lasts 1 year on average, five bulbs total 5 years on average, and their average is still 1 year.
The spreads need one idea: for independent variables, variances add, not standard deviations. Each Xi has variance σ2, so
The sum gets wider as n grows, but only like √n, not like n, because some draws come out high and some low, and they partly cancel. The mean is the sum divided by n, X̄ = Sn/n:
Unpack this step
The first equals sign is the Module 2 rule V(aX) = a2V(X) with a = 1/n. Constants come out of a variance squared, so dividing by n divides the variance by n2. (That variances of independent variables add is proved in §5.5; take it as given for now.)
So the average of more draws is more tightly packed around μ. To halve the spread you need four times the sample size.
Check yourself: the lab's population is Exponential(rate 1), so μ = σ = 1. Predict the SD of X̄ and of Sn at n = 30 before reading on.
SD(X̄) = 1/√30 = 0.1826 and SD(S30) = √30 = 5.4772. The real run in §5 gives 0.1811 and 5.4332, within 1%.
3 · The Central Limit Theorem: the shape
Section 2 fixed the centre and spread for every n. It said nothing about shape. The CLT does:
Whatever the population's shape (as long as σ is finite), for large n
Devore's rule of thumb is n > 30. The more skewed the population, the larger n has to be.
Why would averaging produce a bell? A very large average needs many draws to be large at the same time, which is rare. A middling average can happen in a huge number of ways. Averaging piles probability up in the middle and thins the tails, and the bell is the shape that pile settles into. The population's skew gets diluted, by a factor of about √n.
That is the whole lab, and it's about motion, so drive it yourself. Pick a population, then slide n. The bars are 5000 simulated values of the statistic, and the red curve is the CLT's normal.
What you should see. For the exponential at n = 1, the histogram is the population: a ski slope. By n = 5 it has a hump but a long right tail. By 30 it's nearly a bell, and only a slight right lean remains. The uniform is symmetric to begin with, so it looks bell-shaped by n = 3. That's why the assignment's sizes (30 and up) all look normal for it, and why the change you'll describe there is mainly the width. The binomial is mildly skewed and lumpy (it only takes whole-number values), and both features fade as n grows. The mean and the sum always have the same shape, because Sn = nX̄ is just a rescaling. Only the axis labels differ.
Unpack: measuring "how skewed" with one number
The readout's skewness is the average of ((x − x̄)/s)3: 0 for a symmetric shape, positive for a long right tail (it's the Lab 1 measure). An exponential has skewness 2, and the skewness of a mean of n draws is 2/√n. At n = 30 that's 0.37, small but not zero.
4 · Using the CLT to get a number
The CLT turns any "probability about an average" question into a normal-table question. You standardise with the sampling SD, not the population's σ:
Worked example 1The lab's population, n = 30
Bulb lifetimes are Exponential(1) (mean 1 year). For a random sample of 30 bulbs, find P(X̄ > 1.2).
- Centre and spread of X̄: mean μ = 1, SD σ/√n = 1/√30 = 0.1826.
- Standardise: z = (1.2 − 1)/0.1826 = 1.10.
- Normal table: P(Z > 1.10) = 1 − Φ(1.10) = 1 − 0.8643 = 0.1357. In R it's
1 - pnorm(1.2, mean = 1, sd = 1/sqrt(30))→ 0.1367 (R uses the unrounded z = 1.095). - How good is it? For this population the exact answer exists:
1 - pgamma(36, shape = 30)→ 0.1379. The CLT is off by about 0.001. The lab's own 5000 sample means givemean(means[["30"]] > 1.2)→ 0.1294, which differs by simulation noise (about ±0.005 for 5000 samples).
Unpack: why pgamma gives the exact answer
A sum of n independent Exponential(1) variables has the gamma distribution with α = n, β = 1 (the gamma family from Module 3). X̄ > 1.2 means S30 > 36. This is only a cross-check: no question will ask for it.
Worked example 2The same question at n = 5: when the CLT is too early
- CLT: SD = 1/√5 = 0.4472, so z = 0.2/0.4472 = 0.447 and P ≈ 1 − Φ(0.447) = 0.327.
- Exact:
1 - pgamma(6, shape = 5)→ 0.285. The approximation is off by 0.04, about 15%. - Why: at n = 5 the histogram (set the widget to it) still has a long right tail, and the normal curve puts too much area just above the centre. The formulas for the centre and spread are still exactly right at n = 5. It's the normal shape that isn't there yet.
Check yourself: for the same bulbs (n = 30), what is P(S30 > 36) by the CLT, using the sum's own formulas?
S30 ≈ N(30, 30), so SD = √30 = 5.477 and z = (36 − 30)/5.477 = 1.095. That's the same z as worked example 1, so the answer is the same, 0.1367. It has to be: S30 > 36 and X̄ > 1.2 are the same event.
5 · The sheet's script, line by line
The script has six numbered steps. Here's what each new piece does.
| Line(s) | Code | What it does |
|---|---|---|
| 7 | set.seed(123) | Fixes the random numbers, so every run (yours, ours, your lab partner's) prints the same output. Same as Lab 2. |
| 13–14 | B <- 5000 n_vals <- c(1, 5, 10, 30) | B samples of each size in n_vals. See the two-n trap in §1. |
| 17–18 | means <- list() | An empty list: a box with labelled compartments. It will hold one vector of 5000 means for each n. |
| 20 | matrix(rexp(B * n, rate = lambda), nrow = B, ncol = n) | The key trick. Draw all B × n values at once and fold them into a table with B rows and n columns. Each row is one sample. |
| 21–22 | rowMeans(x), rowSums(x) | The mean and the sum of each row, so 5000 sample means and 5000 sample sums. |
| 21 | means[[as.character(n)]] | Stores them under the label "5", "10" and so on. Labels must be text, hence as.character. Later, means[["30"]] fetches the n = 30 vector. |
| 26–30 | sapply(means, sd) | Applies sd to every compartment of the list and returns the four answers as one vector. That's one table column in one line. |
| 45, 53 | pdf("figures/means.pdf", …) … dev.off() | Sends the plots to a PDF file, not to the screen. dev.off() closes the file. See the trap below. |
| 46 | par(mfrow = c(2, 2)) | Splits the page into a 2 × 2 grid, so four histograms land on one figure. |
| 48–51 | hist(…, freq = FALSE) curve(dnorm(x, mu, sigma/sqrt(n)), add = TRUE) | Density-scale histogram (Lab 2's probability = TRUE: same thing, different name) with the CLT curve laid over it. Note the sd passed is σ/√n. |
1) "I ran it and no plots appeared." Correct: pdf(…) sends them to means.pdf, sums.pdf and population.pdf inside a figures folder in R's working directory. Type getwd() to see where that is. To see them in RStudio's Plots pane instead, skip the pdf(…) and dev.off() lines. 2) Copying from the PDF also copies the line numbers (8 lambda <- 1 is a syntax error) and can turn quotes into curly ones. Type it, or delete the numbers. 3) Running step 5 before step 3 fails with "object 'means' not found". The steps depend on each other, so run them in order. 4) Passing the variance as sd in dnorm: sigma^2/n instead of sigma/sqrt(n). For the exponential, σ = 1 hides this mistake. In the assignment it won't stay hidden.
The sheet's observation table, filled in from a real run
These are the actual outputs of the sheet's script, run unchanged in R 4.6.2. Because of set.seed(123), yours should match. The theory columns are just 1/√n and √n.
| n | SD of X̄ | σ/√n | SD of Sn | σ√n | Shape |
|---|---|---|---|---|---|
| 1 | 1.0076 | 1.0000 | 1.0076 | 1.0000 | Strongly right-skewed: the population itself (skewness 1.94) |
| 5 | 0.4458 | 0.4472 | 2.2288 | 2.2361 | Clearly right-skewed, with a hump (0.87) |
| 10 | 0.3109 | 0.3162 | 3.1092 | 3.1623 | Mildly right-skewed (0.53) |
| 30 | 0.1811 | 0.1826 | 5.4332 | 5.4772 | Nearly bell-shaped, slight right tail (0.33) |
The script's other output, the simulated means: X̄ averages 1.0087, 1.0014, 0.9965, 0.9977 (theory 1), and Sn averages 1.0087, 5.0070, 9.9649, 29.9297 (theory 1, 5, 10, 30). The skewness figures in brackets aren't printed by the sheet's script. They're there to back up the shape words. If you want them, sapply(means, function(v) mean((v - mean(v))^3) / sd(v)^3) prints them.
One overstatement on the sheet
The sheet's conclusion says that by n = 30 both distributions are "very close to the normal curves". That's close, not exact. The exponential is about as skewed as common populations get, and at n = 30 the means still have skewness 0.33 (theory 2/√30 = 0.37). That's visible as a slightly heavier right side in the n = 30 panel. Worked example 1 shows the practical effect is tiny (0.001 in a probability). If your report says "nearly normal, with a slight right skew remaining", that's the accurate version. Everything else on the sheet, code and formulas included, is correct.
Your minimal prerequisite kit for this lab
- Exponential with rate λ: mean 1/λ, SD 1/λ (Module 3). Uniform(0, 1): mean ½, variance 1/12. Binomial(n, p): mean np, variance np(1 − p).
- V(aX) = a2V(X), and SD = √variance (Module 2).
- Standardising and reading Φ, and its R form
pnorm(x, mean = …, sd = …)(Lab 2 page). - Density-scale histograms:
freq = FALSE/probability = TRUE, so a pdf can be drawn on top (Lab 2 page).
What to practise
| Skill | Where | How many |
|---|---|---|
| Run the sheet's script unchanged and check your table against §5 | Lab 3 sheet | 1 run |
Change the population to runif (μ = 0.5, σ = √(1/12)) and re-run. This is half of assignment Q3 | Lab 3 sheet, lines 8–10 and 20 | 1 run |
CLT probabilities for X̄ and for a total, by hand, then check with pnorm | Devore §5.4 odd exercises (answers in the back) | 4–6 |
| Mean and SD of X̄ from a population mean and SD | Devore §5.4 odd exercises | 2–3 |
Budget: about an hour, including the widget, before starting the assignment's Question 3.
"I'm in a first probability course (Devore 9th ed., §5.3–5.4). Using only the facts E(X̄) = μ, SD(X̄) = σ/√n and the CLT, give me 5 short problems mixing sample means and totals, including one where n is too small for the CLT to be trusted. Don't show answers until I've tried them."
"Here is my base-R loop that simulates sample means with matrix() and rowMeans(). Explain what each line does without rewriting it in tidyverse or adding packages: [paste code]."
Check any number it gives with one pnorm line yourself.