2026-10-18

What is a Confidence Interval?

  • Typically, we cannot usually measure a whole population
  • To combat this issue, we take a sample and the compute the sample mean, so we can draw conclusions to form generalizations
  • A confidence interval gives us a range of likely values for the true mean \(\mu\)

Sample Mean and Standard Deviation

Mean: \[ \bar{x} = \frac{1}{n}\sum_{i=1}^{n} x_i \] Standard Deviation \[ s^2 = \frac{\sum_{i = 1}^{n} (x_i - \bar{x})^2}{n-1}, \qquad s = \sqrt(s^2) \] (I had to write the formula this way because the square root symbol was not fully showing.)

Confidence Interval and its Formula

A 95% confidence interval for \(\mu\) is:

\[ \bar{x} \pm t_{0.025,\, n-1} \cdot \frac{s}{\sqrt{n}} \] - \(t_{0.025,\, n-1}\) comes from the t-distribution -\(\frac{s}{\sqrt{n}}\) is defined as the standard error

Example using mtcars

We will be using the mpg (miles per gallon) or 32 cars.

##                    mpg    wt  hp
## Mazda RX4         21.0 2.620 110
## Mazda RX4 Wag     21.0 2.875 110
## Datsun 710        22.8 2.320  93
## Hornet 4 Drive    21.4 3.215 110
## Hornet Sportabout 18.7 3.440 175
## Valiant           18.1 3.460 105

Histogram of MPG

R code for the Previous Histogram

ggplot(mtcars, aes(x = mpg)) +
  geom_histogram(bins = 10, fill = "purple", color = "white") +
  geom_vline(xintercept = mean(mtcars$mpg), 
             color = "red", linetype = "dashed") +
  labs(x = "MPG (miles per gallon)", y = "Count")

Results

  • Sample mean: 20.09
  • Standard Deviation: 6.03
  • Sample size: 32
  • 95% Confidence Interval: (17.92, 22.26)

The interval as a plot

3D Plot: MPG, Weight, and Horsepower

Conclusion

  • Based on the calculated sample mean, 20.09, and the 95% confidence interval being 17.9 - 22.3, we can be 95% confident that the true average MPG falls within that range.
  • The histogram shows that the data is centered around 20 MPG and this is also reflected with the calculated mean being at around 20 as well.
  • Furthermore, the histogram appears roughly bimodal It would be risky to assume true bimodality though, as the dataset has s relatively small sample, and changing the number of bins changes the way the graph looks.
  • Based on the 3D plot created, it is visible that heavier cars, as well as cars with more horsepower, typically have lower MPG.