1Lesson 9 — Confidence Intervals & Tests: Reading R’s Output¶
1.1Objectives¶
By the end of this lesson you will be able to:
Run a one-proportion confidence interval and test with
prop.test().Run a one-mean or one-proportion test from summary statistics alone (mean, SD, n — exactly what a textbook problem gives you) with
BSDA’szsum.test()andtsum.test().Locate the confidence interval, the test statistic, and the p-value inside R’s printed output.
Match R’s output back to the calculator’s
1-PropZInt/1-PropZTest,TInterval/T-Testmenus, so you can cross-check either tool against the other.
Every function below works the same way on your own laptop or on the CSUB
JupyterHub (https://
1.2Setup¶
library(mosaic)
library(BSDA)
data(KidsFeet)This lesson reuses KidsFeet from Lessons 6–7, so the numbers below connect
directly back to summaries you’ve already computed.
1.31. One-proportion inference: prop.test()¶
Recall from Lesson 7: of 39 kids, 17 have a bigger right foot.
tally(~ biggerfoot, data = KidsFeet)biggerfoot
L R
22 17 Is that 17-out-of-39 convincingly different from a 50/50 split, or could it easily
be chance? prop.test(successes, n) runs the test and builds the confidence
interval in one call — the R match to your calculator’s 1-PropZTest /
1-PropZInt:
prop.test(17, 39)
1-sample proportions test with continuity correction
data: 17 out of 39
X-squared = 0.41026, df = 1, p-value = 0.5218
alternative hypothesis: true p is not equal to 0.5
95 percent confidence interval:
0.2818740 0.6023198
sample estimates:
p
0.4358974 Read the pieces from top to bottom: the test statistic (X-squared = 0.41026
— prop.test() uses a chi-square statistic, which is mathematically equivalent to
a z-statistic here), the p-value (0.5218), the stated alternative
hypothesis (two-sided, “not equal to 0.5” — the default), the 95% confidence
interval for the true proportion (0.2818740 to 0.6023198), and the
sample proportion (p = 0.4358974, i.e., 17/39). With a p-value of 0.52 —
much bigger than a typical 0.05 cutoff — there’s no real evidence the true
proportion differs from 0.5, and the interval (0.28, 0.60) comfortably contains
0.5, telling the same story two ways.
Add alternative = for a one-sided test, matching your calculator’s directional
option:
prop.test(17, 39, p = 0.5, alternative = "less")
1-sample proportions test with continuity correction
data: 17 out of 39
X-squared = 0.41026, df = 1, p-value = 0.2609
alternative hypothesis: true p is less than 0.5
95 percent confidence interval:
0.0000000 0.5787672
sample estimates:
p
0.4358974 The test statistic doesn’t change, but the p-value is now half of the two-sided value (0.2609 ≈ 0.5218 / 2), and the confidence interval becomes one-sided — it runs all the way down to 0, because a one-sided “less than” interval only bounds the proportion from above.
1.42. One- and two-sample means: t.test()¶
Recall the foot-length summary from Lessons 5–7: mean 24.72 cm, SD 1.32 cm, n = 39.
Is that mean convincingly different from 25 cm? t.test(~x, data =, mu =) runs a
one-sample t-test directly on raw data — the match to TInterval/T-Test on your
calculator:
t.test(~ length, data = KidsFeet, mu = 25)
One Sample t-test
data: length
t = -1.3125, df = 38, p-value = 0.1972
alternative hypothesis: true mean is not equal to 25
95 percent confidence interval:
24.29597 25.15019
sample estimates:
mean of x
24.72308 Same reading strategy as prop.test(): t-statistic (-1.3125), degrees of
freedom (df = 38, one less than the sample size), p-value (0.1972), and
95% CI for the true mean (24.296 to 25.150). Since 25 falls inside that
interval, and the p-value is well above 0.05, there’s no strong evidence the true
mean foot length differs from 25 cm.
R’s test results aren’t just printed text — they’re stored in an object you can
pull individual pieces out of, using $:
tt1 <- t.test(~ length, data = KidsFeet, mu = 25)
tt1$statistic
tt1$p.value
tt1$conf.int[1] -1.31254
[1] 0.1972106
[1] 24.29597 25.15019This is useful whenever you need just one number — say, to report only the p-value — without retyping or re-reading the full printout.
For a two-sample comparison, put the grouping variable on the right of ~, the
same formula grammar from every earlier lesson. Is boys’ mean foot length
different from girls’?
t.test(length ~ sex, data = KidsFeet)
Welch Two Sample t-test
data: length by sex
t = 1.9174, df = 36.275, p-value = 0.06308
alternative hypothesis: true difference in means between group B and group G is not equal to 0
95 percent confidence interval:
-0.04502067 1.61291541
sample estimates:
mean in group B mean in group G
25.10500 24.32105 Notice the label: “Welch” Two Sample t-test — R’s default two-sample t.test()
does not assume the two groups have equal spread (var.equal = FALSE by
default), so the degrees of freedom (36.275) come out as a decimal instead of a
whole number. This is a small but genuine difference from the “pooled” two-sample
t-test some calculators default to; if an assignment specifically calls for the
pooled version, add var.equal = TRUE. Here, the p-value (0.063) is just above
0.05, and the confidence interval for the difference in means (-0.045 to 1.613)
just barely contains 0 — a border-line result worth discussing with your
instructor’s stated significance level.
1.53. Testing from summary statistics: zsum.test() and tsum.test()¶
Textbook and exam problems usually give you the mean, SD, and n directly,
instead of the raw data — exactly the situation BSDA’s zsum.test() (population
SD known, matching 1-PropZTest-style z inference) and tsum.test() (sample SD
only, matching T-Test) are built for.
As a check that these give the same answer as raw-data t.test(), feed
tsum.test() the exact summary numbers from the KidsFeet foot-length test above
(mean 24.72308, SD 1.317586, n = 39):
tsum.test(mean.x = 24.72308, s.x = 1.317586, n.x = 39, mu = 25)
One-sample t-Test
data: Summarized x
t = -1.3125, df = 38, p-value = 0.1972
alternative hypothesis: true mean is not equal to 25
95 percent confidence interval:
24.29597 25.15019
sample estimates:
mean of x
24.72308 Identical t-statistic, df, p-value, and confidence interval to the raw-data
t.test() in Section 2 — because they’re testing the exact same numbers, just
handed to R two different ways. This is exactly the move you’ll make on an exam:
compute (or read off) the mean, SD, and n, then run tsum.test() instead of typing
out a whole dataset.
zsum.test() has the identical shape, but is for the case where the population
standard deviation is genuinely known (not just estimated from your sample) — a
z-test, not a t-test. Here’s a typical textbook-style setup: a city’s DMV claims
its average wait time is 25 minutes; from years of records, the population SD is
known to be 6 minutes; a sample of 40 customers had a mean wait of 27.5 minutes.
zsum.test(mean.x = 27.5, sigma.x = 6, n.x = 40, mu = 25)
One-sample z-Test
data: Summarized x
z = 2.6352, p-value = 0.008408
alternative hypothesis: true mean is not equal to 25
95 percent confidence interval:
25.64061 29.35939
sample estimates:
mean of x
27.5 Note the z-statistic (not t) and the argument name sigma.x — the known
population SD — instead of s.x. Here the p-value (0.0084) is well below 0.05, and
25 sits outside the 95% CI (25.64 to 29.36): real evidence the true mean wait time
is longer than the DMV’s claimed 25 minutes.
1.6Summary¶
prop.test(successes, n)runs a one-proportion test and CI in one call; addalternative = "less"/"greater"for a one-sided test.prop.test()'s confidence interval is a Wilson score interval (with continuity correction), not the Wald interval from a typical textbook formula — expect small differences, not a bug.t.test(~x, data =, mu =)runs a one-sample t-test on raw data;t.test(y ~ g, data =)runs a two-sample test, defaulting to Welch’s (unequal-variance) version.Every test object stores its pieces —
$statistic,$p.value,$conf.int— for when you need just one number.tsum.test()andzsum.test()(BSDA) run the same tests from summary statistics alone; usezonly when the population SD is genuinely known, otherwise uset.
1.7Check your understanding¶
A sample of 50 CSUB students, 31 of whom carry a job while enrolled, is tested against the claim that the true proportion is 0.5. Write the
prop.test()call.Using the
KidsFeetwidthvariable (Lesson 7: mean 8.992308, SD 0.5095843, n = 39), write thetsum.test()call to test whether the true mean width differs from 9 cm.A
t.test()printout showsp-value = 0.031and a 95% CI for the difference in means of(0.12, 2.45). In one or two sentences, what do these two pieces of output tell you together, and do they agree with each other?In your own words, what’s the one question you ask yourself to decide between
zsum.test()andtsum.test()?