# load the dataset — 100 births recorded in North Carolina in 2004
load('data/ncbirths.RData')Lab 3: Confidence intervals
With solutions
Fill in the author and calpoly-id fields at the very top of this file. Replace "Your Name Here" with your full name and "yourpolynetid" with your Cal Poly username (the part of your email before @calpoly.edu, e.g. jdoe01). Your submission cannot be matched to your record without this.
Where a “Your turn” asks you to save a result by a specific name (shown in bold), use the exact name given — your work is checked automatically.
A confidence interval gives a plausible range of values for a population parameter. The general formula is:
\[\bar{x} \pm c \times SE(\bar{x})\]
where \(c\) is a critical value that controls coverage: how often the interval captures the population mean. The empirical rule gives us whole-number critical values — in particular, \(c = 2\) yields approximately 95% coverage.
Computing a confidence interval by hand
How old are mothers at the time of birth in NC?
From Lab 2, we know how to get the point estimate and standard error from t.test(). We can then plug those directly into the interval formula.
# extract mother's age
mage <- ncbirths$mother.age
# run t.test
mage.tt <- t.test(mage)
# retrieve sample mean and standard error
mage.tt$estimatemean of x
26.9
mage.tt$stderr[1] 0.6342895
Calculating by hand:
\[ 26.9 \pm 2\times0.63 = (26.3, 28.8) \]
With 95% confidence, the mean age of mothers at birth in NC in 2004 is estimated to be between 26.3 and 28.8 years.
What is a plausible range for the mean birth weight of NC babies?
Extract birth.weight from ncbirths, run t.test(), and store the result as bweight.tt.
Then follow the example above to construct a 95% confidence interval using the empirical rule (\(c = 2\)).
Write a one-sentence interpretation.
# extract birth weight
bweight <- ncbirths$birth.weight
# run t.test
bweight.tt <- t.test(bweight)
# retrieve sample mean and standard error
bweight.tt$estimatemean of x
7.1795
bweight.tt$stderr[1] 0.1434152
With 95% confidence, the mean birth weight of NC babies in 2004 is estimated to be between 6.89 and 7.47 lbs.
Confidence intervals from t.test()
Is there a faster way?
t.test() computes the confidence interval for you. The $conf.int component contains the lower and upper bounds directly.
# t.test computes the interval automatically — no arithmetic needed
mage.ci <- mage.tt$conf.int
mage.ci[1] 25.64143 28.15857
attr(,"conf.level")
[1] 0.95
This matches what we calculated by hand above (with a slight difference because t.test() uses an exact \(t\) critical value rather than the approximation \(c = 2\)).
What is a plausible range for the mean birth weight of NC babies?
Repeat your calculation from Your turn 1, but this time use t.test()$conf.int instead of the manual formula. Store the result as bweight.ci.
# same question as YT1, but letting t.test() do the arithmetic
bweight.ci <- bweight.tt$conf.int
bweight.ci[1] 6.894933 7.464067
attr(,"conf.level")
[1] 0.95
The interval is nearly identical to the one from Your turn 1 — the small difference comes from t.test() using an exact \(t\) critical value rather than the \(c = 2\) approximation.
Adjusting coverage
What if we want to be more — or less — confident that the interval captures the true mean?
The conf.level argument controls coverage. The default is 0.95.
# 90% confidence interval for mother's age
t.test(mage, conf.level = 0.90)$conf.int[1] 25.84683 27.95317
attr(,"conf.level")
[1] 0.9
# 99% confidence interval for mother's age
t.test(mage, conf.level = 0.99)$conf.int[1] 25.2341 28.5659
attr(,"conf.level")
[1] 0.99
Higher coverage means a wider interval: you’re more confident it contains the true mean, but it pins down the mean less precisely.
How long is a typical pregnancy in NC?
Extract weeks from ncbirths, run t.test(), and store the result as bweeks.tt. Then adjust conf.level to compare 90% and 99% intervals — no need to store these.
Which interval is narrowest?
# extract gestational age and run t.test (default 95% coverage)
bweeks <- ncbirths$weeks
bweeks.tt <- t.test(bweeks)
# adjust conf.level to compare widths — no need to store these
t.test(bweeks, conf.level = 0.90)$conf.int[1] 38.07251 39.02749
attr(,"conf.level")
[1] 0.9
t.test(bweeks, conf.level = 0.99)$conf.int[1] 37.7947 39.3053
attr(,"conf.level")
[1] 0.99
The 90% interval is narrowest.
Why not just use 100% confidence?
If higher coverage is better, why not always set
conf.level = 1?
Construct a 100% confidence interval for the population mean gestational age (number of weeks at birth).
a. Does the result make sense conceptually?
b. Is the result useful?
# use conf.level = 1
t.test(bweeks, conf.level = 1)$conf.int[1] -Inf Inf
attr(,"conf.level")
[1] 1
- Yes. The interval says we are certain that the mean is somewhere between \(-\infty\) and \(+\infty\).
- An infinitely wide interval provides no useful information about where the true mean lies. In practice, we accept a small chance of being wrong in exchange for a meaningful, finite range.
- Save the file (Ctrl+S / Cmd+S).
- Render to PDF: click Render or press Ctrl+Shift+K / Cmd+Shift+K.
- Download both files: in the Files panel, check the
.qmdand PDF, then click More ▾ → Export…. - Upload the PDF to the Gradescope assignment.
- Upload the
.qmdfile to the course submission link.