2026-10-17

The Question

Cars in our dataset get different gas mileage (mpg).

Question: What is the true average mpg across all cars like these, and how confident can we be in our estimate from just a sample?

  • We only have 32 cars, not every car ever made
  • A single number (like “20.1 mpg”) hides our uncertainty
  • A confidence interval gives us a range we trust

What Is a Confidence Interval? (No Stats Background Needed)

A confidence interval (CI) is a range of values that likely contains the true population average.

  • We don’t know the real average mpg for all cars
  • We only have a sample (our 32 cars)
  • A 95% CI means: if we repeated this sampling many times, about 95% of the intervals we’d build would contain the true average

Think of it like a “net” we cast around our best guess — wide enough to likely catch the truth, but not so wide it’s useless.

The Math Behind It

For a sample mean, the confidence interval formula is:

\[\bar{x} \pm t^{*} \cdot \frac{s}{\sqrt{n}}\]

Where:

  • \(\bar{x}\) = sample mean (our best single estimate)
  • \(t^{*}\) = critical value from the t-distribution (depends on confidence level)
  • \(s\) = sample standard deviation (how spread out the data is)
  • \(n\) = sample size (number of cars)

Understanding the Formula

The piece \(\dfrac{s}{\sqrt{n}}\) is called the standard error — it shrinks as \(n\) grows, meaning more data gives a narrower, more confident interval.

\[\text{Margin of Error} = t^{*} \cdot \frac{s}{\sqrt{n}}\]

  • Bigger sample (\(n\)) → smaller margin of error → tighter interval
  • Higher confidence (e.g. 99% vs 95%) → wider interval

R Code: Calculating the Interval

# 95% confidence interval for mean mpg
ci_result <- t.test(mtcars$mpg, conf.level = 0.95)
ci_result$estimate   # sample mean
## mean of x 
##  20.09062
ci_result$conf.int   # lower and upper bounds
## [1] 17.91768 22.26357
## attr(,"conf.level")
## [1] 0.95

This single function computes the sample mean and the 95% CI bounds using the formula from the previous slide.

Visualizing the Data

The dashed line marks our sample mean — the center of our estimate.

Visualizing the Confidence Interval

Interactive Visualization

Hover over points to see individual cars — heavier cars tend to get lower mpg, adding context to our average.

What Did We Learn?

  • The sample mean mpg is about 20.1
  • We’re 95% confident the true average mpg falls between 17.9 and 22.3
  • A confidence interval communicates uncertainty, not just a single number
  • More data (bigger \(n\)) would narrow this interval further

Thank You

Questions?