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
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:
Why did those dots lie in a bell around 50?
How wide is that bell? Can you know that without a hundred researchers?
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:
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 and standard deviation , then for the sampling distribution of :
the middle is : the cards lie around the true mean, ;
the width is divided by , : that width is called the standard error;
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 = 1.22 for individual people to = 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:
Read it like this. is how far apart individual people lie. , 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 instead of , 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.
NoteIf you want to go further — where that square root comes from
Take = 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 .
The mean is that total divided by 4, so its standard deviation is 20 divided by 4: 5. And that is exactly divided by . With n instead of 4 it goes the same way: the total has standard deviation times , and dividing by n leaves divided by .
Back to week 1. The worry questionnaire has = 10, and each researcher drew 25 people. Then the standard error is
That is the width of the bell under the dots. Lay that bell over the dots of week 1, and it fits:
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 , 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?
Above the fraction bar is how far your mean lies from , the value from the null hypothesis: the 50 from : . 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 , 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 () 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:
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 : 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, 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 is true. In the bell, that is the area beyond on the right, plus beyond on the left.
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-tailedp-value. Two-tailed, you want both tails, and you do one tail times two:
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 positivez, 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:
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 and you reject . 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.
NoteCheck in R — the worry example
# the standard errorse <-10/sqrt(25)# z and the two-tailed p at 53, 54 and 55z <- (c(53, 54, 55) -50) / sez
[1] 1.5 2.0 2.5
2*pnorm(-abs(z))
[1] 0.13361440 0.04550026 0.01241933
# the critical values50-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 , the standard deviation in the population you are drawing from. In the worry example you assumed that = 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 . What you do have is the standard deviation of your own sample: . As a reminder, this is how you work it out:
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 in place of . The standard error, SE for short, then becomes
and the test statistic is no longer called z but t:
The same formula as for z, with where 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
is one fixed number. is not: every sample gives a different . 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 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: . With few people, is a wobbly estimate, and the tails are fat. With very many people you almost know , and the world behaves like z again.
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.
NoteIf you want to go further — why n − 1?
To work out , you first use . 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: , and . The six steps:
Hypotheses. : = 50; : ≠ 50; α = .05.
Sampling distribution. If is true, t follows a t-distribution with .
Test statistic. First the standard error, then t:
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 . That is ≤ .05.
Statistical conclusion. Reject the null hypothesis that first-year students who sleep badly score 50 on average on the worry questionnaire.
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 and , 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 of the general population after all, then and : almost the same. With 30 people, 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.
NoteCheck in R — the t-test by hand
R as a calculator, with the numbers of the sample:
x <- sleepstudy$worry_beforese <-sd(x) /sqrt(length(x))t <- (mean(x) -50) / set
[1] 2.451045
# two-tailed: the left tail of -t, times two2*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
you don’t know
standard error
distribution under
standard normal
t with
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 thicknessabline(v =50, col ="red", lwd =2)
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-axisabline(h =50, col ="red", lwd =2)
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.
NoteIf you want to go further — the central limit theorem, done yourself
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 eachmeans <-replicate(10000, mean(sample(awake, size =25)))# one bin per possible mean: they lie in steps of 1/25hist(means, breaks =seq(0.02, 4.02, by =0.04),main ="", xlab ="mean number of times awake", ylab ="count")
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
What is the standard error of the mean if you know ? Give the formula.
And if you don’t know ?
When do you do a z-test, and when a t-test?
Give the formula for z.
Give the formula for t, and say how many degrees of freedom go with it.
CautionAnswer 2.1
: the standard deviation of the sampling distribution of .
Then you estimate with : .
A z-test if you know , a t-test if you don’t know and estimate it with . In practice, the second is almost always the case.
, with .
NoteExercise 2.2 — Six t-values
Six researchers each did a t-test for one mean, two-tailed.
n = 6, t = 2.40
n = 16, t = 3.10
n = 12, t = −1.80
n = 40, t = −0.85
n = 25, t = 2.40
n = 61, t = 3.10
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 (α = .05)?
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?
In b, t stayed the same. In real life, t also changes when you take a bigger sample, even if the difference between and stays the same size. How can you see that in the formula for t?
CautionAnswer 2.2
# two-tailed: one tail, times two2*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
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.
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.
The denominator is . If n gets bigger, the standard error gets smaller, and the same distance 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
Write down and .
Which test fits here, and why?
Calculate and . Do it by hand; R may serve as a check.
Calculate the standard error.
Calculate t.
Look up the p-value with pt().
What is your statistical conclusion (α = .05)?
What is your conclusion in context?
CautionShort answer 2.3
: = 20; : ≠ 20.
A t-test for one mean (a one-sample t-test): you don’t know .
, .
.
, with .
.
Reject the null hypothesis that = 20.
On average, first-year students who sleep badly do not take 20 minutes to fall asleep; in this sample they take longer.
CautionFull worked answer 2.3 — step by step
The hypotheses:
: = 20 — first-year students who sleep badly take 20 minutes on average
: ≠ 20 — on average they take less time or more
You compare one mean with a fixed value, so a test for one mean. The spread in the population () is not given anywhere: you estimate it with . So a t-test.
The mean: add up and divide by 6.
For : 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
The standard error:
The test statistic:
with .
One tail times two: 2 * pt(-3.10, df = 5) gives .
.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.
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 Rx <-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.
Write down and .
Assume = 15, and do a z-test:
Calculate the standard error and z.
What is the p-value? Look up the tail in table A and multiply it by two. Then check it with pnorm().
Do you reject (α = .05)?
Now suppose you do not know . To keep it simple, assume that the sample has exactly the same spread: = 15. Do a t-test:
What is the test statistic now, and how many degrees of freedom go with it?
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().
Do you reject ?
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 row, below row 1000?
CautionAnswer 2.4
: = 100; : ≠ 100.
, and .
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 : that, too, is already the tail. To three decimals, the same number.
Yes: .036 ≤ .05. You reject the null hypothesis that students who slept less than four hours score 100 on average on the attention test.
With = 15 the standard error is 5 again, so , with .
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 : neatly inside the bracket.
No: .069 > .05. This time you do not reject the null hypothesis that students who slept less than four hours score 100 on average.
In the t-test, 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 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.
Pick out the rows of the control group. How many people are there?
What are the mean and the standard deviation of their worry score before?
Make a histogram of those scores, with a line at 50.
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.
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?
CautionAnswer 2.5
## 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)
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
15 people.
, .
, , . With these 15 people you do not reject the null hypothesis that first-year students who sleep badly score 50 on average.
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?
The standard error is larger for B, because more people give more spread.
The standard error is the same for A and B, because they come from the same population.
The standard error for B is a quarter of that for A.
The standard error for B is half that for A.
CautionAnswer question 1
D. The standard error is divided by . B has four times as many people, and : half the size. C is the trap of “just divide by n”. A mixes up the spread of the people (, 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?
4.00
0.33
2.00
24.00
CautionAnswer question 2
C. First from variance to standard deviation: = = 12. Then . B divides by n instead of ; 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?
−2.00
2.00
−0.33
−0.11
CautionAnswer question 3
A. . B flips the sign. C divides by 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)?
CautionAnswer question 4
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 (). Do babies prefer looking at one toy over the other? What is the test statistic?
t = 0.67
t = 2.67
z = 2.67
t = 16.00
CautionAnswer question 5
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 : = 20.
Then , the standard error is , and .
A divides by 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 .
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.
CautionAnswer question 6
.021.pnorm() gives one tail, the left one. Two-tailed: times two, , 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?
a z-test
a t-test with df = 20
a t-test with df = 19
a t-test with df = 18
CautionAnswer question 7
C. unknown, so t, and for one mean .
NoteQuestion 8 — multiple choice
Which statement about the t-distribution is correct?
It has thinner tails than the standard normal distribution.
With 10 degrees of freedom, the critical value is smaller than 1.96.
The more degrees of freedom, the more it looks like the standard normal distribution.
It is not symmetrical, and so you may not do one tail times two.
CautionAnswer question 8
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?
Their p-values are equal, because their t is equal.
The p-value is larger with 40 people.
The p-value is smaller with 40 people.
That depends on the value in the null hypothesis.
CautionAnswer question 9
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 (two-tailed, α = .05)?
CautionAnswer question 10
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.