How Surprising Is 496 Grams?
A machine should fill cereal boxes with 500 g on average. A sample of 25 boxes averages 496 g. Is that evidence the machine short-fills, or just chance? You'll find out in R.
Assumes: the sampling distribution of the sample mean, the standard error SE = σ ÷ √n, z-scores and pnorm(), and pressing Run on R code. Each R task is one short edit.
The facts
- The machine is set to fill boxes with 500 g on average.
- Single boxes vary: from years of records, their SD is σ = 10 g.
- A consumer group weighs a random sample of n = 25 boxes. Their average: x̄ = 496 g.
Two explanations
- Null hypothesis, H₀: μ = 500 g. The machine is fine; this sample came out low by chance.
- Alternative hypothesis, HA: μ < 500 g. The machine short-fills.
μ is the machine's true average fill, which nobody knows. HA says "less than" because the group suspected short-filling before weighing anything.
1. Is the machine short-filling?
A hypothesis test starts by assuming H₀ is true and asks: would a sample like ours be surprising then?
If the machine really averages 500 g, how often would a random sample of 25 boxes average 496 g or less?
Locked in. In steps 2 and 3 you'll simulate a machine that really averages 500 g, and count how often its samples average 496 g or less.
2. What would samples look like if H₀ were true?
Build the world of H₀ in R: a machine that averages exactly the H₀ value. Take 10,000 samples of 25 boxes from it and keep each sample's average.
| Code | What it does |
|---|---|
| rnorm(n, mean = mu0, sd = sigma) | 25 random box weights from a machine that averages mu0 grams. |
| mean( … ) | Their average: one sample mean. |
| replicate(10000, … ) | Do that 10,000 times and keep all 10,000 averages, in null_means. |
Your task: replace NA in the mu0 line with the machine's average fill if H₀ is true. Then Run.
Hint 1
H₀ is "μ = …". Look at the facts in step 1.Hint 2
The line should readmu0 <- 500.The red dashed line is our sample, 496 g. Step 3 counts how many of the 10,000 averages fall at or to the left of it.
3. How often is a fine machine this low?
null_means <= 496 gives TRUE for each simulated average at or below 496 g and FALSE for the rest. mean() of TRUEs and FALSEs is the share that are TRUE: mean(c(TRUE, FALSE, FALSE, TRUE)) is 0.5.
Your task: replace NA with our sample's average, xbar. Then Run.
Hint 1
"At or below our sample" means<= our sample's average.Hint 2
The line should readp_sim <- mean(null_means <= xbar).Your prediction
You predicted "often". Single boxes vary by about 10 g, so being 4 g short looks ordinary. But the count says only about 2% of samples from a fine machine average 496 g or less.
Some people predict "rarely" instead: "an average of 25 boxes can't drift far from 500 g, so 4 g short is a lot for an average."
Why might they be right? Use step 2's histogram: how far from 500 g do averages of 25 boxes usually land?
Averages vary much less than single boxes. σ = 10 g describes single boxes. Averages of 25 have SE = σ ÷ √n = 10 ÷ 5 = 2 g, and step 2's histogram is that narrow: most averages land within 2 g of 500. So 496 g is 2 SEs below 500, out in the tail.
Try it on a new case: a sample of 25 averaging 499 g is only half an SE below 500. About 31% of samples from a fine machine do that or worse: not surprising at all.
You predicted "rarely", and the count agrees: only about 2% of samples from a fine machine average 496 g or less.
Some people predict "often": "single boxes vary by about 10 g, so an average only 4 g short is nothing unusual."
How would you explain to them why 496 g is surprising anyway?
Averages vary much less than single boxes. σ = 10 g describes single boxes. Averages of 25 have SE = σ ÷ √n = 10 ÷ 5 = 2 g, and step 2's histogram is that narrow: most averages land within 2 g of 500. So 496 g is 2 SEs below 500, out in the tail.
Try it on a new case: a sample of 25 averaging 499 g is only half an SE below 500. About 31% of samples from a fine machine do that or worse: not surprising at all.
The share you counted estimates the p-value: the probability, if H₀ is true, of getting a sample mean at least as extreme as ours (here: 496 g or less, the direction HA points).
One-sided or two-sided? HA decides which samples count as "at least as extreme".
- One-sided, HA: μ < 500. Only short fills count as evidence, so you count one tail: averages of 496 g or less. That is what you did: about 0.022.
- Two-sided, HA: μ ≠ 500 ("the machine is off, in either direction"). Overfilling would count too, so you also count averages as far above 500 as ours is below: 504 g or more. Two tails of about equal size: p ≈ 0.044, about double.
Choose the side from the question, before seeing the data. Picking it afterwards, to make p smaller, is cheating.
A regulator wants to know whether the machine's average is off target in either direction, so HA: μ ≠ 500. Which simulated averages count toward the p-value for our 496 g sample?
4. Can a formula skip the simulation?
Under H₀ the sample means are close to normal (the CLT), centred at the H₀ value, μ₀ = 500 g, with SE = σ ÷ √n = 2 g. So the p-value is a normal-curve area, which pnorm() gives once you have the test statistic:
z = (x̄ − μ₀) ÷ SE: how many SEs the sample mean sits from the H₀ value. Negative means below.
Your task: replace NA with the formula for z, using xbar, mu0 and se. Then Run, and compare p with your p_sim from step 3.
Hint 1
Sample mean minus H₀ value, in brackets, divided by the SE.Hint 2
The line should readz <- (xbar - mu0) / se.The simulation (about 0.022) and the formula (about 0.023) agree up to simulation noise. Two-sided (step 3): with HA: μ ≠ 500, count both tails: 2 * pnorm(-abs(z)) ≈ 0.046. If σ were unknown and you used the sample's SD, you'd use the t-distribution, pt(t, df = n - 1), as with t intervals.
5. What does p ≈ 0.023 tell the consumer group?
In one or two sentences of your own: what does p ≈ 0.023 say about their sample of 25 boxes?
One way to say it. If the machine really averaged 500 g (H₀ true), only about 2.3% of random samples of 25 boxes would average 496 g or less. A sample this low would be surprising under H₀, so it is evidence against H₀.
Compare with yours: does it start from "if H₀ were true"? A p-value is worked out assuming H₀, so it cannot be the chance that H₀ is true. That mix-up is the most common one.
6. When is a sample surprising enough?
The cut-off for "surprising" is set before the data are seen: the significance level α (alpha), usually 0.05. Then:
- p < α: reject H₀. The result is statistically significant: evidence against H₀, in the direction HA points.
- p ≥ α: fail to reject H₀. Not enough evidence for HA. This does not prove H₀.
Click a sample mean. R draws the distribution of a fine machine's sample means (H₀ true), shades the p-value, and gives the decision at α = 0.05.
Another batch: 25 boxes average 498 g. Click 498 above. What should the consumer group conclude?
7. Check yourself
A study tests H₀: students sleep 7 hours a night on average, against HA: less than 7. A random sample of 40 students gives p = 0.30. Which conclusion is right?
How sure were you?
You should now be able to:
- state H₀ and HA for a claim about a mean (step 1);
- find a p-value by simulating the world of H₀, or with z and
pnorm(), and say what it means, starting from "if H₀ were true" (steps 2–5); - say from HA whether a p-value counts one tail or both (steps 3–4);
- decide at α = 0.05: reject H₀ or fail to reject it, and why failing to reject is not proof (step 6).
If one of these isn't clear yet, go back to the step in brackets and rerun its code.
Welcome back: two quick questions
A test of H₀: μ = 330 ml against HA: μ ≠ 330 ml gives z = −1.8. Which R line gives the p-value?
2 * pnorm(-1.8)pnorm(-1.8)1 - pnorm(-1.8)A bottling machine is tested: H₀: it fills 330 ml on average, against HA: less. The test gives p = 0.12, with α = 0.05. What follows?
Assumptions
- dataThe cereal machine, its 500 g setting, σ = 10 g and the sample mean of 496 g are made up for this page.
- distributionSingle box weights are simulated as normal with SD σ = 10 g; σ is treated as known, so the test uses z and pnorm() rather than t.
- parameterStep 2: 10,000 samples of 25 boxes (set.seed(21)). α = 0.05 throughout.
- simplificationOne-sided test (HA: μ < 500), because the suspicion was short-filling. Step 3 also shows the two-sided version (HA: μ ≠ 500): both tails count, p ≈ 0.044 simulated, 2 × pnorm(−2) ≈ 0.046.
Left out: t-tests with an unknown σ (mentioned in step 4), tests for proportions, effect size and practical significance, and how tests relate to confidence intervals.