Week 2 · Hypothesis tests for one mean

from counting dots to calculating

That night the bat kept track of how often everyone woke up, and he scratched it into the bark: most of them once or twice, the moth six times, a messy, lopsided list.

Then he put the animals in groups of four and scratched only the mean under each group.

Those means lay a good deal closer together, with a small hump in the middle.

“So there is a secret order in it after all,” he whispered.

“That comes of a bat taking means,” said the dormouse, “and that’s not the same thing as a secret.”

The bat looked from the messy list to the neat hump and back again, and it was still the same night.

What you learn this week

  • why the dots of week 1 form a bell, and how wide that bell is
  • the standard error, and why there is a square root in it
  • the z-test: calculating the p-value instead of counting it
  • looking up one tail and doubling it, and why you may do that with a bell
  • the t-test, for when you don’t know σx\sigma_x
  • in R: pnorm(), pt() and t.test()

From counting to calculating

In week 1 you counted dots. A hundred researchers each drew 25 poor sleepers, wrote their sample mean on a card, and you looked at how strange your mean was among all those cards. That works, and it shows exactly what a p-value is. But it left three things open:

  1. Why did those dots lie in a bell around 50?
  2. How wide is that bell? Can you know that without a hundred researchers?
  3. The dots were rounded, so the p-value was rough. With a mean of exactly 54, counting even gave a different decision from calculating.

This week you solve all three. It is the same question as in week 1, with the same six-step plan. Only now you don’t count: you calculate.

Why the dots form a bell

Start with one person. How often do you wake up in the night? Most people once or twice, some not at all, and a few as many as five or six times. That is a skewed distribution: a hill on the left, and a long tail to the right. No bell.

Now draw four people and take their mean. That someone wakes up five times happens. That all four do, almost never: usually there is someone among them who sleeps right through, and that person pulls the mean back. A mean evens things out. The extremes damp each other down, and what is left lies closer to the middle.

With twenty-five people it goes further still. This is what that looks like, for all possible samples at once:

Three bar charts, one above the other, each with its own axis from 0 to 7 and its own axis 'proportion'. Top, for individual people: the tallest bars at 1 and 2, a long tail to the right, and the orange bell fits it badly. In the middle, for means of 4 people: narrower and less skewed. Bottom, for means of 25 people: narrow around 1.5, and the orange bell lies almost exactly on top of it. How to read it: at the top, the bar at 0 is the proportion of people who wake up zero times a night (.22). The bar at 1 is taller (.33): more people wake up once than zero times. In the middle, a bar is the proportion of all samples of 4 with that mean. The bar at 1.5 (.16) is almost four times as tall as the one at 2.5 (.04): far more samples of 4 have a mean around 1.5 than around 2.5. At the bottom, the same for 25 people: the tallest bars stand next to 1.5, at 1.48 and 1.52 (.065 and .064), and at 2.5 there is no bar left to see: a mean of 2.5 almost never happens with 25 people.
Figure 1: How often someone wakes up in the night. Top: individual people, a skewed distribution with mean 1.5. Below: the means of all possible samples of 4 and of 25 people. The orange line is the bell that the central limit theorem predicts. The height is scaled separately in each panel: compare the width, not the height.

How to read it: at the top, the bar at 0 is the proportion of people who wake up zero times a night (.22). The bar at 1 is taller (.33): more people wake up once than zero times. In the middle, a bar is the proportion of all samples of 4 with that mean. The bar at 1.5 (.16) is almost four times as tall as the one at 2.5 (.04): far more samples of 4 have a mean around 1.5 than around 2.5. At the bottom, the same for 25 people: the tallest bars stand next to 1.5, at 1.48 and 1.52 (.065 and .064), and at 2.5 there is no bar left to see: a mean of 2.5 almost never happens with 25 people.

Three things happen at once, and together they have a name.

NoteThe central limit theorem

If you keep drawing a random sample of n people from a population with mean μx\mu_x and standard deviation σx\sigma_x, then for the sampling distribution of x‾\bar{x}:

  1. the middle is μx\mu_x: the cards lie around the true mean, μx‾=μx\mu_{\bar{x}} = \mu_x;
  2. the width is σx\sigma_x divided by n\sqrt{n}, σx‾=σx/n\sigma_{\bar{x}} = \sigma_x / \sqrt{n}: that width is called the standard error;
  3. the shape is roughly a bell (a normal distribution), even if the population itself is not a bell. The larger n, the more the shape of the sampling distribution looks like a bell.

In Figure 1 you can see all three. The numbers that go with it:

  • the middle stays 1.5, for one person and for twenty-five (the middle is the mean, not the tallest bar: the tail to the right pulls the mean past the peak at 1);
  • the width shrinks: from σx\sigma_x = 1.22 for individual people to σx‾\sigma_{\bar{x}} = 0.61 for means of 4, and to 0.24 for means of 25;
  • the shape becomes a bell: for individual people the orange line fits badly (it even carries on to the left of 0, as if you could wake up a negative number of times), for 25 people almost exactly.

That third point is the lifeline. Never mind how ugly your population is: with enough people, the mean behaves like a bell. If the population is a bell already, then so is the mean, for every n. How many people is “enough” for which population is a question for week 6 (Assumptions and alternative tests).

The standard error

The second point of the central limit theorem is the most important one this week. The more people, the steadier the card. Two Einsteins in a sample of five can happen, and then the mean shoots up. Forty Einsteins in a sample of a hundred would be strange, even though it is the same share. That steadiness gets a formula of its own:

σx‾=σxn \sigma_{\bar{x}} = \dfrac{\sigma_x}{\sqrt{n}}

Read it like this. σx\sigma_x is how far apart individual people lie. σx‾\sigma_{\bar{x}}, the standard error, is how far apart cards lie: the standard deviation of the sampling distribution. The standard deviation says how stable your data are, the standard error how stable your statistic is. Cards lie closer together than people, because a mean evens things out. How much closer? Divide by the square root of n. (If you are given the variance σx2\sigma_x^2 instead of σx\sigma_x, take the square root first.)

That it is a square root and not just n means: take four times as many people, and the bell gets not a quarter as wide but half as wide. Every extra person helps, but a little less each time. You can see it in Figure 1: from one to 4 people the width halves, from 1.22 to 0.61.

TipFour times as many, half as wide

If you want the bell half as wide, you need four times as many people. A bell a tenth as wide costs a hundred times as many people. That is why precise studies are expensive.

Take σx\sigma_x = 10 and four people. If you add up their four scores, the spread of that total is not 4 × 10 = 40. High and low scores partly cancel each other out, and what adds up are the variances: 4 × 10² = 400. So the standard deviation of the total is 400=20\sqrt{400} = 20.

The mean is that total divided by 4, so its standard deviation is 20 divided by 4: 5. And that is exactly σx\sigma_x divided by 4\sqrt{4}. With n instead of 4 it goes the same way: the total has standard deviation σx\sigma_x times n\sqrt{n}, and dividing by n leaves σx\sigma_x divided by n\sqrt{n}.

Back to week 1. The worry questionnaire has σx\sigma_x = 10, and each researcher drew 25 people. Then the standard error is

σx‾=1025=105=2 \sigma_{\bar{x}} = \dfrac{10}{\sqrt{25}} = \dfrac{10}{5} = 2

That is the width of the bell under the dots. Lay that bell over the dots of week 1, and it fits:

The dot plot from week 1, with open squares in stacks from 45 to 55 and the tallest stack (20 squares) above 50. An orange bell-shaped line runs over the tops of the stacks. How to read it: above 50 stands the tallest stack, 20 squares, and above 53 there are 7: a mean of about 53 came up almost three times as rarely as one of about 50. The orange bell runs over the tops of both (at 50 it reaches 19.9 squares, at 53 it reaches 6.5), and at no stack is it a whole square off.
Figure 2: The hundred means of the worry example from week 1, with the bell that the central limit theorem predicts: middle 50, standard error 2. Each square is the mean of one sample of 25.

How to read it: above 50 stands the tallest stack, 20 squares, and above 53 there are 7: a mean of about 53 came up almost three times as rarely as one of about 50. The orange bell runs over the tops of both (at 50 it reaches 19.9 squares, at 53 it reaches 6.5), and at no stack is it a whole square off.

That it fits so precisely is no coincidence: the dots of week 1 were the counts this bell predicts. A hundred real researchers give a heap that lies just off it, with the same shape (try it in R under If you want to go further, in week 1). So you didn’t need a hundred researchers. With μx\mu_x, σx\sigma_x and n you already know how the cards lie, without drawing a single one.

The z-test: calculating instead of counting

In week 1, with a mean of 55, you asked: how many dots lie at least as far from 50? Now that you know the bell, you ask it differently.

5 points above 50: is that a lot? Raw, it tells you nothing. Raw is rubbish. It depends on how wide the bell is, and that width is the standard error. Use it as a ruler: how many times does the standard error fit into the distance between your mean and 50?

z=x‾−μ0σx‾=x‾−μ0σxn z = \dfrac{\bar{x} - \mu_0}{\sigma_{\bar{x}}} = \dfrac{\bar{x} - \mu_0}{\dfrac{\sigma_x}{\sqrt{n}}}

Above the fraction bar is how far your mean lies from μ0\mu_0, the value from the null hypothesis: the 50 from H0H_0: μx=50\mu_x = 50. Here the little 0 does not say of what, but from which hypothesis. Below it is the standard error: the unit you express that distance in. So this is a different z from week 0: now zx‾z_{\bar{x}}, the z of a mean. Same question, smaller ruler. That z is your test statistic, the number you hold up against the sampling distribution. The test itself is called, in full, the z-test for one mean, also known as the one-sample z-test.

Here you also see the three things from exercise 1.4 again, which together decide whether a difference is significant: the difference is above the fraction bar, and the spread (σx\sigma_x) and the sample size (n) sit together in the standard error below it. A bigger difference makes z bigger, more spread makes it smaller, and more people make it bigger again.

For the worry example:

z=55−502=2.50 z = \dfrac{55 - 50}{2} = 2.50

The ruler of 2 fits 2.50 times into the distance of 5: the mean lies 2.50 standard errors above 50. Had the mean been 45, then z=−2.50z = −2.50: just as far, on the other side.

Why the detour through z? Because after this division every bell becomes the same bell: middle 0, width 1. That is called the standard normal distribution, N(0,1)N(0, 1) for short. Whether you measure worry scores, reaction times or IQ: after dividing by the standard error you are always looking at that same bell. That dividing is called standardising. Want to brush up on the standard normal distribution and z-scores first? That is what week 0 is for.

One tail times two

Now the p-value: the probability of a mean at least as far from 50 as 55, if H0H_0 is true. In the bell, that is the area beyond z=2.50z = 2.50 on the right, plus beyond z=−2.50z = −2.50 on the left.

A bell-shaped curve with its middle above 50. Under the axis are the means 44, 46, 48, 50, 52, 54 and 56, and below them the matching z, from −3 to 3. At 45 and at 55 there is an orange dashed line; the thin tips of the bell beyond those lines are coloured orange. How to read it: under the axis there are two rows, the mean and below it z: every step of 2 worry points is one step in z, because the standard error is 2. The orange lines are at 45 and 55, in z at −2.50 and 2.50. The orange piece to the right of 55 is just as big as the one to the left of 45, each .0062 of the bell; together they are .012, the p-value for a mean of 55.
Figure 3: The sampling distribution of the worry example as a bell (middle 50, standard error 2). Orange: everything at least as far from 50 as 55 is, on either side. Each tail is .0062; together .012.

How to read it: under the axis there are two rows, the mean and below it z: every step of 2 worry points is one step in z, because the standard error is 2. The orange lines are at 45 and 55, in z at −2.50 and 2.50. The orange piece to the right of 55 is just as big as the one to the left of 45, each .0062 of the bell; together they are .012, the p-value for a mean of 55.

You don’t work out that orange area by hand; R does that. The function pnorm() gives the area to the left of a z. With a negative z you therefore get exactly the left tail:

pnorm(-2.50)
[1] 0.006209665

That is one tail: .0062, the one-tailed p-value. Two-tailed, you want both tails, and you do one tail times two:

p=2×.0062=.012 p = 2 \times .0062 = .012

Can you just do that, times two? With a bell, yes. Two-tailed is Pippi Longstocking: two pigtails. And on a bell those two tails are exactly the same size, because they lie equally far from the middle and the bell is the same on the left as on the right. So there, times two gives the same as adding up both sides, and it is quicker.

With dots you can’t count on that. The worry example of week 1 was symmetrical, because it showed the counts you can expect from a hundred researchers. The hundred drawn dots of exercise 1.5 were not, and that is why you really counted both sides there.

WarningA p above 1 does not exist

If you type pnorm(2.50) with a positive z, you get the area to the left of 2.50: almost the whole bell (.9938). Times two that is 1.9876, and a probability above 1 does not exist. You have taken the big side instead of the tail.

Two ways to get it right with a positive z: put a minus sign in front, pnorm(-2.50), or ask for the right-hand side, pnorm(2.50, lower.tail = FALSE). Both give .0062.

If you don’t want to think about the side, write 2 * pnorm(-abs(z)). abs() first turns z into a positive number, and the minus sign in front makes it negative: that way you always take a tail, whichever side z is on.

Without R: the holy z-value

In an exam you usually don’t have R. Then you are given the p-value, or you decide with the critical value: the route from week 1, now with z. (The probability for every z is also at the back, in table A.)

From now on, one number has to live in your head: 1.96. It is the only z-value you really have to know by heart, because it goes with .95 in the middle and .05 in the two tails together:

A bell around 0 with dashed lines at −1.96 and 1.96. In the middle it says 'p = .95'; beyond each line a narrow tail is coloured orange, with 'p = .025' above it. How to read it: in the middle piece it says p = .95: that is the area between −1.96 and 1.96, the probability that z falls there, not the height of the bell. In each orange tail it says p = .025, together .05: the α you agree on beforehand. The further out, the smaller the tails: beyond −3 and 3 there is only .003 left together.
Figure 4: The standard normal distribution. Between −1.96 and 1.96 lies .95 of the bell; in each tail .025, together .05. Each p is the probability of that piece of the bell.

How to read it: in the middle piece it says p = .95: that is the area between −1.96 and 1.96, the probability that z falls there, not the height of the bell. In each orange tail it says p = .025, together .05: the α you agree on beforehand. The further out, the smaller the tails: beyond −3 and 3 there is only .003 left together.

If your z lies on or beyond 1.96 (or −1.96), then p≤.05p \leq .05 and you reject H0H_0. For the worry example: 2.50 ≥ 1.96, so you reject the null hypothesis that poor sleepers score 50 on average.

You will also come across two neighbours of 1.96, and recognising them is enough: 1.645 goes with a two-tailed p of .10, and 2.576 with .01.

You can also turn the boundaries back into worry scores: 1.96 standard errors of 2 is 3.92. The critical values are then 50 − 3.92 = 46.08 and 50 + 3.92 = 53.92. A mean on or beyond those boundaries lies in the rejection region.

Back to the dots

Week 1 promised that this week you would work out the worry example exactly. Here it is, next to what you counted in week 1:

The worry example: counting dots against calculating
mean p counted p calculated decision counted decision calculated
53 .22 .13 do not reject do not reject
54 .08 .046 do not reject reject
55 .02 .012 reject reject
critical values 45 and 55 46.08 and 53.92

The calculated p is smaller everywhere. That comes from the rounding. A dot above 54 stands for every mean from 53.5 to 54.5. So if you count “54 or higher”, you count everything from 53.5 upwards: a piece that lies closer to 50 than 54 itself. The bell counts from exactly 54.

At 54 that makes the difference. Counting said .08, just above .05; calculating says .046, just below. That is not a mistake of the dots. The dots show the shape, the formula gives the number.

# the standard error
se <- 10 / sqrt(25)

# z and the two-tailed p at 53, 54 and 55
z <- (c(53, 54, 55) - 50) / se
z
[1] 1.5 2.0 2.5
2 * pnorm(-abs(z))
[1] 0.13361440 0.04550026 0.01241933
# the critical values
50 - 1.96 * se
[1] 46.08
50 + 1.96 * se
[1] 53.92

abs() turns every z into a positive number, and the minus sign in front makes it negative again. That way you always take the tail, whichever side your z is on.

When you don’t know σx: the t-test

The z-test has one weak spot: you have to know σx\sigma_x, the standard deviation in the population you are drawing from. In the worry example you assumed that σx\sigma_x = 10, just as in week 1. But that 10 is the spread in the general population. You are studying poor sleepers. Do they differ from each other just as much as everyone does? You don’t know.

In practice you almost never know σx\sigma_x. What you do have is the standard deviation of your own sample: sxs_x. As a reminder, this is how you work it out:

sx=∑(x−x‾)2n−1 s_x = \sqrt{\dfrac{\sum (x - \bar{x})^2}{n - 1}}

Subtract the mean from each score, square, add up, divide by n − 1, and take the square root. That n − 1 will come back in a moment.

You use sxs_x in place of σx\sigma_x. The standard error, SE for short, then becomes

𝑆𝐸x‾=sxn \mathit{SE}_{\bar{x}} = \dfrac{s_x}{\sqrt{n}}

and the test statistic is no longer called z but t:

t=x‾−μ0𝑆𝐸x‾=x‾−μ0sxn t = \dfrac{\bar{x} - \mu_0}{\mathit{SE}_{\bar{x}}} = \dfrac{\bar{x} - \mu_0}{\dfrac{s_x}{\sqrt{n}}}

The same formula as for z, with sxs_x where σx\sigma_x was: the t-test for one mean, also known as the one-sample t-test. They feel the same, too: z and t both count how many standard errors your mean lies off to the side. The difference is in the distribution you use to find the p-value.

Fatter tails

σx\sigma_x is one fixed number. sxs_x is not: every sample gives a different sxs_x. Sometimes it happens to be a bit small, and then your t gets bigger than it deserves. Sometimes it is a bit large, and then t gets smaller. That does not cancel out, because a test looks only at the tails, and that is exactly where the samples with too small an sxs_x end up. That extra uncertainty makes large t-values more common than large z-values. So the distribution of t has fatter tails than the standard normal distribution: the t-distribution.

How fat depends on the degrees of freedom, df for short: 𝑑𝑓=n−1\mathit{df} = n - 1. With few people, sxs_x is a wobbly estimate, and the tails are fat. With very many people you almost know σx\sigma_x, and the world behaves like z again.

Three bell-shaped curves around 0. The dark z curve and the blue dashed t with df = 29 almost coincide. The orange t with df = 5 has a lower peak. Beyond about 1.6 and −1.6 it lies above the other two, a thin strip all the way to the edge. How to read it: the orange curve (t with df = 5) has a lower peak than the dark one (z). What comes off the top comes back in the tails, because the area under each curve adds up to exactly 1: out to about 1.6 on either side the orange curve lies below the other two, and beyond that above them. Beyond 1.96 and −1.96 the area under z comes to .05, under the blue t (df = 29) to .06 and under the orange one to .11: more than two times as much as under z.
Figure 5: The standard normal distribution (z) and two t-distributions. The fewer degrees of freedom, the lower the peak and the fatter the tails.

How to read it: the orange curve (t with df = 5) has a lower peak than the dark one (z). What comes off the top comes back in the tails, because the area under each curve adds up to exactly 1: out to about 1.6 on either side the orange curve lies below the other two, and beyond that above them. Beyond 1.96 and −1.96 the area under z comes to .05, under the blue t (df = 29) to .06 and under the orange one to .11: more than two times as much as under z.

More uncertainty means: you have to allow more room. Your mean has to lie further from the middle for the same p, and the critical value goes up.

The fewer degrees of freedom, the further out the boundary
degrees of freedom critical value (two-tailed, α = .05)
5 2.57
8 2.31
29 2.05
infinitely many (that is z) 1.96

The same boundaries, for every df and to three decimals, are at the back in table D, in the .025 column.

To work out sxs_x, you first use x‾\bar{x}. And once the mean is fixed, not all n scores are free any more. If you know n − 1 of them, the last one follows from the mean: it has to make the total come out right.

Three scores with mean 10, so 30 together. The first can be anything, say 8. The second too, say 11. The third has no choice left: 30 − 8 − 11 = 11. The last person to come strolling in has no freedom; they are stuck. Of the three scores, two could move freely: n − 1 degrees of freedom.

The example: the sleep study

The practice dataset sleepstudy holds 30 first-year students who sleep badly, with their worry score before the training. Do they worry differently from the general population, where the mean is 50? You don’t know their spread, so: a t-test.

From the sample you have: x‾=54.17\bar{x} = 54.17, sx=9.31s_x = 9.31 and n=30n = 30. The six steps:

  1. Hypotheses. H0H_0: μx\mu_x = 50; HaH_\text{a}: μx\mu_x ≠ 50; α = .05.

  2. Sampling distribution. If H0H_0 is true, t follows a t-distribution with 𝑑𝑓=30−1=29\mathit{df} = 30 - 1 = 29.

  3. Test statistic. First the standard error, then t:

    𝑆𝐸x‾=9.3130=9.315.48=1.70 \mathit{SE}_{\bar{x}} = \dfrac{9.31}{\sqrt{30}} = \dfrac{9.31}{5.48} = 1.70

    t=54.17−501.70=2.45 t = \dfrac{54.17 - 50}{1.70} = 2.45

  4. p-value. One tail times two. The left tail comes from pt(), written out in full here:

    pt(q = -2.45, df = 29, lower.tail = TRUE)
    [1] 0.01027934

    q is the place on the axis, df the number of degrees of freedom. lower.tail = TRUE is the default; leave it out and R picks it, and you get the area to the left of q. Short, and times two straight away: 2 * pt(-2.45, df = 29) gives p=.021p = .021. That is ≤ .05.

  5. Statistical conclusion. Reject the null hypothesis that first-year students who sleep badly score 50 on average on the worry questionnaire.

  6. Conclusion in context. First-year students who sleep badly worry differently, on average, from the general population; in this sample, more.

Via the critical value you arrive at the same place: 2.45 ≥ 2.05, the boundary at 29 degrees of freedom.

In a report you write the mean as M and the standard deviation as SD: the same x‾\bar{x} and sxs_x, with a different letter.

How to report it

The first-year students who sleep badly had a mean worry score that differed from the mean of 50 in the general population (M = 54.17, SD = 9.31), t(29) = 2.45, p = .021.

And with σx = 10? If you had used the σx\sigma_x of the general population after all, then z=2.28z = 2.28 and p=.022p = .022: almost the same. With 30 people, sxs_x is already a good estimate, and t looks like z. With a small group it makes much more difference; you will see that in exercise 2.4.

R as a calculator, with the numbers of the sample:

x <- sleepstudy$worry_before
se <- sd(x) / sqrt(length(x))
t <- (mean(x) - 50) / se
t
[1] 2.451045
# two-tailed: the left tail of -t, times two
2 * pt(-abs(t), df = 29)
[1] 0.02050953

pt() does for the t-distribution what pnorm() does for the standard normal, except that it also wants to know the degrees of freedom.

z or t?

Two tests for one mean
z-test t-test
when you know σx\sigma_x you don’t know σx\sigma_x
standard error σx‾=σxn\sigma_{\bar{x}} = \dfrac{\sigma_x}{\sqrt{n}} 𝑆𝐸x‾=sxn\mathit{SE}_{\bar{x}} = \dfrac{s_x}{\sqrt{n}}
distribution under H0H_0 standard normal t with 𝑑𝑓=n−1\mathit{df} = n - 1
p-value in R 2 * pnorm(-abs(z)) 2 * pt(-abs(t), df)
critical value, α = .05 1.96 larger, and the smaller n, the larger
TipWhere does μ0 come from?

Usually it is given: a norm, the mean in the population, the result of earlier research. Sometimes you have to come up with it yourself. If you give people 30 seconds to choose between two pictures, then “no preference” means: half the time on each, so μ0 = 15 seconds.

In R: testing one mean

By hand, you work through the test to understand what it does. For real data you use t.test(). Load sleepstudy as in week 1:

## Load the data ----
load("sleepstudy.RData")

First, look. How are the worry scores spread, and where does 50 lie? A histogram with a vertical line, or a boxplot with a horizontal one:

## Describe ----
mean(sleepstudy$worry_before)
[1] 54.16667
sd(sleepstudy$worry_before)
[1] 9.311037
hist(sleepstudy$worry_before, main = "", xlab = "worry score before",
     ylab = "count")
# v = a vertical line at 50; col = the colour; lwd = the thickness
abline(v = 50, col = "red", lwd = 2)
A histogram of the worry scores of the 30 first-year students, with a red vertical line at 50. Most of the bars lie to the right of the line. How to read it: each bar counts how many first-year students have a score in that 5-point bin. The tallest bar, 7 people between 45 and 50, stands to the left of the line, but to the right of the line there are more of them together: 18 of the 30, against 12 on the left. So don't look only at the tallest bar: the mean, 54.17, lies to the right of the line.
Figure 6: Worry scores before the training, with the line at 50.

How to read it: each bar counts how many first-year students have a score in that 5-point bin. The tallest bar, 7 people between 45 and 50, stands to the left of the line, but to the right of the line there are more of them together: 18 of the 30, against 12 on the left. So don’t look only at the tallest bar: the mean, 54.17, lies to the right of the line.

boxplot(sleepstudy$worry_before, range = 1.5, ylab = "worry score before")
# range = 1.5 is the default; leave it out and R picks it. The lines above and
# below the box run to the furthest score within one and a half times the
# length of the box; anything further out, R draws as a separate circle.
# h = a horizontal line, because in a boxplot the scores are on the y-axis
abline(h = 50, col = "red", lwd = 2)
An upright boxplot of the worry scores, with a red horizontal line at 50. The thick bar in the box, the median, lies above the line. How to read it: the box runs from 48 to 61, the middle half of the scores, and the thick bar in it is the median, 54.5. The red line at 50 cuts through the box low down, below that bar: 12 of the 30 first-year students score below 50, more than a quarter but less than half. The lines below and above the box run to the lowest and the highest score, 36 and 72: nobody lies more than one and a half boxes from the box, so R draws no separate circle.
Figure 7: The same scores as a boxplot, with the line at 50.

How to read it: the box runs from 48 to 61, the middle half of the scores, and the thick bar in it is the median, 54.5. The red line at 50 cuts through the box low down, below that bar: 12 of the 30 first-year students score below 50, more than a quarter but less than half. The lines below and above the box run to the lowest and the highest score, 36 and 72: nobody lies more than one and a half boxes from the box, so R draws no separate circle.

Then, test. With mu = you give the value from the null hypothesis. t.test() appears here for the first time, so in full, with the defaults written out:

## Test ----
t.test(x = sleepstudy$worry_before, alternative = "two.sided", mu = 50,
       conf.level = 0.95)

    One Sample t-test

data:  sleepstudy$worry_before
t = 2.451, df = 29, p-value = 0.02051
alternative hypothesis: true mean is not equal to 50
95 percent confidence interval:
 50.68987 57.64346
sample estimates:
mean of x 
 54.16667 

alternative = "two.sided" and conf.level = 0.95 are the defaults; leave them out and R picks these. For mu the default is 0, and that is not what you want here, so you always give it. The short form is t.test(sleepstudy$worry_before, mu = 50), with exactly the same output. (var.equal and paired are about two groups; with one group they don’t come into it.)

What you read there:

  • t = 2.451, df = 29: the same t as by hand, and the same degrees of freedom;
  • p-value = 0.02051: rounded .021, as with pt();
  • alternative hypothesis: true mean is not equal to 50: two-tailed, as alternative = "two.sided" asks;
  • the 95 percent confidence interval (that is conf.level = 0.95) you leave alone for now. What that is comes in week 3 (Hypothesis tests for two means and confidence intervals).

t.test() is the quick route, but it shows no intermediate steps. If you understand what happens when you work it through by hand, you understand its output too.

In Figure 1 the distributions were calculated. You can also simply draw them. Make a large population of people who wake up in the night, draw 25 people ten thousand times, and look at how the means lie.

set.seed(2)

# a population: how often does someone wake up in the night?
awake <- rpois(100000, lambda = 1.5)

# ten thousand samples of 25, and the mean of each
means <- replicate(10000, mean(sample(awake, size = 25)))
# one bin per possible mean: they lie in steps of 1/25
hist(means, breaks = seq(0.02, 4.02, by = 0.04),
     main = "", xlab = "mean number of times awake", ylab = "count")
A histogram of ten thousand means, bell-shaped around 1.5, with one bar per possible mean, in steps of 0.04, and a y-axis 'count'. The reading is in the text below the figure.
Figure 8: Ten thousand means of 25 people, drawn in R.

How to read it: each bar counts how many of the ten thousand samples got exactly that mean. The bar at 1.44, just left of 1.5, counts 671; the one at 1.52, just right of it, 608. Calculated, they should be the same height (the proportion at the bottom of Figure 1 times ten thousand: 643 and 644), so that difference of 63 is the luck of the draw; the shape, a bell around 1.5, is the same.

# how widely are they spread, and what does the formula say?
sd(means)
[1] 0.2458957
sd(awake) / sqrt(25)
[1] 0.2462979

The last two numbers lie close together: the width of the drawn means, and the standard error from the formula. Change the 25 to 100 both times, and the bell becomes half as wide.

Practice

NoteExercise 2.1 — Concepts
  1. What is the standard error of the mean if you know σx\sigma_x? Give the formula.
  2. And if you don’t know σx\sigma_x?
  3. When do you do a z-test, and when a t-test?
  4. Give the formula for z.
  5. Give the formula for t, and say how many degrees of freedom go with it.
  1. σx‾=σxn\sigma_{\bar{x}} = \dfrac{\sigma_x}{\sqrt{n}}: the standard deviation of the sampling distribution of x‾\bar{x}.

  2. Then you estimate σx\sigma_x with sxs_x: 𝑆𝐸x‾=sxn\mathit{SE}_{\bar{x}} = \dfrac{s_x}{\sqrt{n}}.

  3. A z-test if you know σx\sigma_x, a t-test if you don’t know σx\sigma_x and estimate it with sxs_x. In practice, the second is almost always the case.

  4. z=x‾−μ0σxnz = \dfrac{\bar{x} - \mu_0}{\dfrac{\sigma_x}{\sqrt{n}}}

  5. t=x‾−μ0sxnt = \dfrac{\bar{x} - \mu_0}{\dfrac{s_x}{\sqrt{n}}}, with 𝑑𝑓=n−1\mathit{df} = n - 1.

NoteExercise 2.2 — Six t-values

Six researchers each did a t-test for one mean, two-tailed.

  1. n = 6, t = 2.40
  2. n = 16, t = 3.10
  3. n = 12, t = −1.80
  4. n = 40, t = −0.85
  5. n = 25, t = 2.40
  6. n = 61, t = 3.10
  1. What is the p-value for each of them? Do it in two ways. First with table D: find the row for your df, see which two columns |t| falls between, and multiply those two probabilities by two. That is your bracket: p lies between them. If your df is not in the table, take the next smaller df that is in the table. Then calculate the exact p with pt(). Does it lie in your bracket? For which do you reject H0H_0 (α = .05)?
  2. Researchers 1 and 5 find the same t. What does the sample size do to the p-value? What do you change in pt() for that?
  3. In b, t stayed the same. In real life, t also changes when you take a bigger sample, even if the difference between x‾\bar{x} and μ0\mu_0 stays the same size. How can you see that in the formula for t?
# two-tailed: one tail, times two
2 * pt(-2.40, df = 5)
[1] 0.06162079
2 * pt(-3.10, df = 15)
[1] 0.007317231
2 * pt(-1.80, df = 11)
[1] 0.09931589
2 * pt(-0.85, df = 39)
[1] 0.4005133
2 * pt(-2.40, df = 24)
[1] 0.02450989
2 * pt(-3.10, df = 60)
[1] 0.002945749
  1. The p-values and the decisions, first from table D and then from pt():

    Six t-tests for one mean, two-tailed, done two ways
    researcher t df table D pt() reject?
    1 2.40 5 .05 < p < .10 .062 no
    2 3.10 15 .005 < p < .01 .007 yes
    3 −1.80 11 .05 < p < .10 .099 no
    4 −0.85 39 (row 30) .40 < p < .50 .40 no
    5 2.40 24 .02 < p < .04 .025 yes
    6 3.10 60 .002 < p < .005 .003 yes

    Every p from pt() lies in its bracket, and the decision is the same both ways. The table gives one tail, so you multiply the column headings by two. You look up a negative t as a positive one: the bell is symmetrical. And researcher 4 has df = 39, which is not in the table: then you take the next smaller df that is in the table, row 30.

    Twice, |t| lies close to a number in the table. For researcher 3, 1.80 lies just beyond 1.796; for researcher 4, 0.85 lies just short of 0.854. That is where your bracket easily ends up one column off, so compare to three decimals. For researcher 4 the p from pt() is on the edge too: .4005, just inside the bracket, even though the pt() column above shows it rounded, as .40. Here it makes no difference to the decision: even one column off, the answer stays no. Then only your bracket is wrong.

  2. With the same t, p is smaller when the sample is bigger: at n = 6, p = .062; at n = 25, p = .025. More people means more degrees of freedom, and so thinner tails. In pt() you change only df.

  3. The denominator is sxn\dfrac{s_x}{\sqrt{n}}. If n gets bigger, the standard error gets smaller, and the same distance x‾−μ0\bar{x} - \mu_0 then counts for more standard errors. So a bigger n gives a bigger t, for the same difference.

NoteExercise 2.3 — A t-test by hand

Suppose adults take 20 minutes on average to fall asleep. A researcher wonders whether that is different for first-year students who sleep badly. For six of them, they measure how many minutes it took:

24, 31, 18, 35, 27 and 33

  1. Write down H0H_0 and HaH_\text{a}.
  2. Which test fits here, and why?
  3. Calculate x‾\bar{x} and sxs_x. Do it by hand; R may serve as a check.
  4. Calculate the standard error.
  5. Calculate t.
  6. Look up the p-value with pt().
  7. What is your statistical conclusion (α = .05)?
  8. What is your conclusion in context?
  1. H0H_0: μx\mu_x = 20; HaH_\text{a}: μx\mu_x ≠ 20.
  2. A t-test for one mean (a one-sample t-test): you don’t know σx\sigma_x.
  3. x‾=28\bar{x} = 28, sx=6.32s_x = 6.32.
  4. 𝑆𝐸x‾=2.58\mathit{SE}_{\bar{x}} = 2.58.
  5. t=3.10t = 3.10, with 𝑑𝑓=5\mathit{df} = 5.
  6. p=.027p = .027.
  7. Reject the null hypothesis that μx\mu_x = 20.
  8. On average, first-year students who sleep badly do not take 20 minutes to fall asleep; in this sample they take longer.
  1. The hypotheses:

    H0H_0: μx\mu_x = 20 — first-year students who sleep badly take 20 minutes on average
    HaH_\text{a}: μx\mu_x ≠ 20 — on average they take less time or more

  2. You compare one mean with a fixed value, so a test for one mean. The spread in the population (σx\sigma_x) is not given anywhere: you estimate it with sxs_x. So a t-test.

  3. The mean: add up and divide by 6.

    x‾=24+31+18+35+27+336=1686=28 \bar{x} = \dfrac{24 + 31 + 18 + 35 + 27 + 33}{6} = \dfrac{168}{6} = 28

    For sxs_x: subtract the mean from each score, and square.

    The mean subtracted from each score, and that squared
    score minus 28 squared
    24 −4 16
    31 3 9
    18 −10 100
    35 7 49
    27 −1 1
    33 5 25
    total 200

    sx2=2006−1=40sx=40=6.32 s_x^2 = \dfrac{200}{6 - 1} = 40 \qquad s_x = \sqrt{40} = 6.32

  4. The standard error:

    𝑆𝐸x‾=6.326=6.322.45=2.58 \mathit{SE}_{\bar{x}} = \dfrac{6.32}{\sqrt{6}} = \dfrac{6.32}{2.45} = 2.58

  5. The test statistic:

    t=28−202.58=3.10 t = \dfrac{28 - 20}{2.58} = 3.10

    with 𝑑𝑓=6−1=5\mathit{df} = 6 - 1 = 5.

  6. One tail times two: 2 * pt(-3.10, df = 5) gives p=.027p = .027.

  7. .027 ≤ .05, so you reject the null hypothesis that first-year students who sleep badly take 20 minutes on average to fall asleep. You get there via the critical value too: 3.10 ≥ 2.57, the boundary at 5 degrees of freedom.

  8. On average, first-year students who sleep badly do not take 20 minutes to fall asleep; in this sample they take longer (28 minutes). With six people, that is a small sample. The t-test already takes that into account, but only if the times it takes to fall asleep roughly form a bell. If the population is skewed, then six may not be enough for the central limit theorem. How you check that is in week 6.

# check in R
x <- c(24, 31, 18, 35, 27, 33)
mean(x)
[1] 28
sd(x)
[1] 6.324555
t.test(x, mu = 20)

    One Sample t-test

data:  x
t = 3.0984, df = 5, p-value = 0.0269
alternative hypothesis: true mean is not equal to 20
95 percent confidence interval:
 21.36279 34.63721
sample estimates:
mean of x 
       28 

t.test() gives t = 3.0984: rounded 3.10, the same as by hand.

NoteExercise 2.4 — z or t?

An attention test is designed so that adults score 100 on average, with a standard deviation of 15. A researcher gives the test to nine students who slept less than four hours that night. Their mean is 89.5.

  1. Write down H0H_0 and HaH_\text{a}.

Assume σx\sigma_x = 15, and do a z-test:

  1. Calculate the standard error and z.
  2. What is the p-value? Look up the tail in table A and multiply it by two. Then check it with pnorm().
  3. Do you reject H0H_0 (α = .05)?

Now suppose you do not know σx\sigma_x. To keep it simple, assume that the sample has exactly the same spread: sxs_x = 15. Do a t-test:

  1. What is the test statistic now, and how many degrees of freedom go with it?
  2. What is the p-value? First look in table D, in the row for your df: which two columns does |t| fall between? Then calculate it exactly with pt().
  3. Do you reject H0H_0?
  4. The numbers are the same, the conclusion is not. How can that be? Look in table D, in the .025 column: which boundary is in your row, and which in the z*z^* row, below row 1000?
  1. H0H_0: μx\mu_x = 100; HaH_\text{a}: μx\mu_x ≠ 100.

  2. σx‾=159=5\sigma_{\bar{x}} = \dfrac{15}{\sqrt{9}} = 5, and z=89.5−1005=−2.10z = \dfrac{89.5 - 100}{5} = -2.10.

  3. In table A, in the part for negative z, row −2.1 and column .00 give the probability .0179. A negative z lies on the left, so that is already the small tail. Two-tailed: 2 × .0179 = .0358. In R, 2 * pnorm(-2.10) gives p=.036p = .036: that, too, is already the tail. To three decimals, the same number.

  4. Yes: .036 ≤ .05. You reject the null hypothesis that students who slept less than four hours score 100 on average on the attention test.

  5. With sxs_x = 15 the standard error is 5 again, so t=−2.10t = -2.10, with 𝑑𝑓=9−1=8\mathit{df} = 9 - 1 = 8.

  6. In row 8 of table D, 2.10 lies between 1.860 (column .05) and 2.306 (column .025). So one tail lies between .025 and .05; two-tailed, .05 < p < .10. In R, 2 * pt(-2.10, df = 8) gives p=.069p = .069: neatly inside the bracket.

  7. No: .069 > .05. This time you do not reject the null hypothesis that students who slept less than four hours score 100 on average.

  8. In the t-test, σx\sigma_x is estimated, and with only nine people that estimate is uncertain. That uncertainty sits in the fatter tails of the t-distribution with 8 degrees of freedom. The same −2.10 lies less far into the tail there. In table D you see it in one column. In row 8, under .025, the boundary is 2.306, and in the z*z^* row, below row 1000, it is 1.960. With only 8 degrees of freedom, the boundary lies further out. Your distance 2.10 lies in between: beyond the boundary for z, not beyond the one for t.

NoteExercise 2.5 — In R

Use sleepstudy, but now only the control group.

  1. Pick out the rows of the control group. How many people are there?
  2. What are the mean and the standard deviation of their worry score before?
  3. Make a histogram of those scores, with a line at 50.
  4. Using only the people in the control group, test with t.test() whether first-year students who sleep badly have a mean worry score different from 50. Write down t, df and p.
  5. The mean of the control group lies above 50, just like that of the whole group. Yet the outcome is different from the example. Why is that?
## a. the control group, as in week 1: first choose, then select ----
keep <- sleepstudy$group == "control"
control <- sleepstudy[keep, ]
nrow(control)
[1] 15
## b. describe ----
mean(control$worry_before)
[1] 53.6
sd(control$worry_before)
[1] 9.217995
## c. histogram ----
hist(control$worry_before, main = "", xlab = "worry score before",
     ylab = "count")
abline(v = 50, col = "red", lwd = 2)
A histogram of the worry scores of the 15 people in the control group, with a red vertical line at 50. How to read it: to the right of the line there are 9 of the 15 people, to the left 6. That is exactly the same proportion on the right as in the whole group (.60), but from half as many people; what that does to the test, you can read under e.
Figure 9: Worry scores before, the control group only (answer 2.5c).

How to read it: to the right of the line there are 9 of the 15 people, to the left 6. That is exactly the same proportion on the right as in the whole group (.60), but from half as many people; what that does to the test, you can read under e.

## d. test ----
t.test(control$worry_before, mu = 50)

    One Sample t-test

data:  control$worry_before
t = 1.5126, df = 14, p-value = 0.1526
alternative hypothesis: true mean is not equal to 50
95 percent confidence interval:
 48.49524 58.70476
sample estimates:
mean of x 
     53.6 
  1. 15 people.

  2. x‾=53.60\bar{x} = 53.60, sx=9.22s_x = 9.22.

  3. t=1.51t = 1.51, 𝑑𝑓=14\mathit{df} = 14, p=.15p = .15. With these 15 people you do not reject the null hypothesis that first-year students who sleep badly score 50 on average.

  4. The control group is half of the group: 15 people instead of 30. With fewer people the standard error is larger, and a mean of 53.60 then lies fewer standard errors away from 50. Both groups come from the same population, because the scores are from before the training. With all 30 people you did reject the null hypothesis. So here, not significant does not mean that first-year students who sleep badly score 50 on average: with 15 people the evidence just isn’t strong enough. How likely you are to miss a real difference between the population mean and 50 this way, you read in week 5 (Effect sizes and power).

Exam block

This is how you might meet it in the exam: as a multiple-choice question with four options. The exam has 30 of them. Below there are also a few questions you fill in yourself or answer with yes or no; the exam does not have those, they are extra practice. Assume you don’t have R: only your calculator and your cheat sheet. Where you need a p-value, it is given, or you compare your z with 1.96. The exam does not ask you to look anything up in a table; you did that in exercises 2.2 and 2.4, to check yourself.

NoteQuestion 1 — multiple choice

From the same population, someone draws two samples: A with n = 25 and B with n = 100. For each, the mean is calculated. Which statement is correct?

  1. The standard error is larger for B, because more people give more spread.
  2. The standard error is the same for A and B, because they come from the same population.
  3. The standard error for B is a quarter of that for A.
  4. The standard error for B is half that for A.

D. The standard error is σx\sigma_x divided by n\sqrt{n}. B has four times as many people, and 4=2\sqrt{4} = 2: half the size. C is the trap of “just divide by n”. A mixes up the spread of the people (σx\sigma_x, which stays the same) and that of the means. B forgets that the standard error also depends on n.

NoteQuestion 2 — multiple choice

A memory test has a variance of 144 in the general population. You give the test to 36 people who worked a week of night shifts. How big is the standard error of the mean, if you use the variance of the general population?

  1. 4.00
  2. 0.33
  3. 2.00
  4. 24.00

C. First from variance to standard deviation: σx\sigma_x = 144\sqrt{144} = 12. Then σx‾=1236=126=2.00\sigma_{\bar{x}} = \dfrac{12}{\sqrt{36}} = \dfrac{12}{6} = 2.00. B divides by n instead of n\sqrt{n}; A and D forget the square root of the variance.

NoteQuestion 3 — multiple choice

On the same memory test, the general population scores 50 on average. The 36 night workers score 46 on average. What is z?

  1. −2.00
  2. 2.00
  3. −0.33
  4. −0.11

A. z=46−502=−2.00z = \dfrac{46 - 50}{2} = −2.00. B flips the sign. C divides by σx\sigma_x instead of by the standard error. D divides by n.

NoteQuestion 4 — yes or no

With that z of −2.00, do you reject the null hypothesis that people who worked a week of night shifts score 50 on average (two-tailed, α = .05)?

Yes. −2.00 lies beyond −1.96: further than 1.96 standard errors from the middle. The two-tailed p is then smaller than .05 (it is .046).

NoteQuestion 5 — multiple choice

A researcher shows each of 16 babies two toys, a rattle and a block, for 40 seconds. The babies look at the rattle for 24 seconds on average, with a variance of 36 (sx2=36s_x^2 = 36). Do babies prefer looking at one toy over the other? What is the test statistic?

  1. t = 0.67
  2. t = 2.67
  3. z = 2.67
  4. t = 16.00

B. The null hypothesis is not given; you have to come up with it yourself. No preference means: half the time on each toy, so H0H_0: μx\mu_x = 20.

Then sx=36=6s_x = \sqrt{36} = 6, the standard error is 616=1.5\dfrac{6}{\sqrt{16}} = 1.5, and t=24−201.5=2.67t = \dfrac{24 - 20}{1.5} = 2.67.

A divides by sxs_x instead of by the standard error. C is the wrong test: you only know the spread in the sample, so t. D forgets to subtract μ0\mu_0.

NoteQuestion 6 — fill in

In a z-test you find z = −2.30. You type pnorm(-2.30), and R gives [1] 0.01072411. What is the two-tailed p-value? Give three decimals.

.021. pnorm() gives one tail, the left one. Two-tailed: times two, 2×0.01072411=0.021448222 \times 0.01072411 = 0.02144822, rounded .021. With a bell you may do that, because the right tail is the same size.

NoteQuestion 7 — multiple choice

A researcher has 20 people and does not know the spread in the population. They use the standard deviation of their sample. Which test do they do?

  1. a z-test
  2. a t-test with df = 20
  3. a t-test with df = 19
  4. a t-test with df = 18

C. σx\sigma_x unknown, so t, and for one mean 𝑑𝑓=n−1=19\mathit{df} = n - 1 = 19.

NoteQuestion 8 — multiple choice

Which statement about the t-distribution is correct?

  1. It has thinner tails than the standard normal distribution.
  2. With 10 degrees of freedom, the critical value is smaller than 1.96.
  3. The more degrees of freedom, the more it looks like the standard normal distribution.
  4. It is not symmetrical, and so you may not do one tail times two.

C. The tails are in fact fatter (A is wrong), so the critical value is larger than 1.96: with 10 degrees of freedom it is 2.23 (B is wrong). The t-distribution is symmetrical around 0, so times two is allowed (D is wrong).

NoteQuestion 9 — multiple choice

Two researchers both find t = 2.20. One had 8 people, the other 40. What is correct?

  1. Their p-values are equal, because their t is equal.
  2. The p-value is larger with 40 people.
  3. The p-value is smaller with 40 people.
  4. That depends on the value in the null hypothesis.

C. More people means more degrees of freedom and thinner tails. Here it even changes the decision: with 8 people p = .064, with 40 p = .034.

NoteQuestion 10 — yes or no

You find t(8) = −2.10. The critical value at 8 degrees of freedom is 2.31. Do you reject H0H_0 (two-tailed, α = .05)?

No. −2.10 lies 2.10 from 0, and that is not on or beyond 2.31. With the z-boundary of 1.96 you would have rejected. In a t-test, the boundary of the t-distribution applies.

Next week

This week you compared one mean with a fixed value. But often you have no fixed value: you have two groups, or the same people before and after. How you compare those is in week 3. The t-test you know now simply comes back there.

Back to top