Learn Without Walls
← Back to Statistics with R
Module 4 of 8: Statistics with R

Confidence Intervals

How certain are we? Quantifying uncertainty

← Module 3: Sampling & CLT Module 4 of 8 Module 5: Hypothesis Testing →
Loading R... (first load takes ~15 seconds)

Before You Start

What you need: Module 3 (CLT and standard error) completed. Understanding of the normal distribution.

What you’ll learn: What a confidence interval actually means. How to calculate one manually and with t.test(). How confidence level affects interval width. The correct, and incorrect, ways to interpret a CI.

The Concept: Confidence Intervals

A confidence interval (CI) gives a range of plausible values for a population parameter (like μ). It pairs our best estimate with a measure of uncertainty.

A 95% confidence interval means: if we repeated this study many times and built a CI each time, about 95% of those intervals would contain the true population parameter.

The Formula

CI = x̄ ± t* × (s / √n)

x̄ = sample mean  |  t* = critical value  |  s = sample SD  |  n = sample size

t* = qt(1 - α/2, df = n - 1)

For 95% CI: α = 0.05, so qt(0.975, df=n-1)

In R: Worked Example (read only)

Two ways to get a confidence interval: manual calculation and the quick way with t.test(). Both give the same result.

# Confidence intervals in R set.seed(42) sample_data <- rnorm(25, mean=70, sd=12) # Method 1: Manual calculation n <- length(sample_data) x_bar <- mean(sample_data) s <- sd(sample_data) se <- s / sqrt(n) t_star <- qt(0.975, df=n-1) # 95% CI, two tailed lower <- x_bar - t_star * se upper <- x_bar + t_star * se cat(sprintf("95%% CI (manual): (%.2f, %.2f)\n", lower, upper)) # Method 2: Using t.test (easier!) result <- t.test(sample_data) cat("95% CI (t.test): (", round(result$conf.int[1],2), ",", round(result$conf.int[2],2), ")\n") cat("Same result both ways!\n")

Common Misconception

A 95% CI does NOT mean "there is a 95% probability the true mean is in this interval." The true mean is either in the interval or it isn’t: probability doesn’t apply to a fixed (unknown) constant. The correct interpretation: the method produces intervals that contain the true mean 95% of the time across many repetitions.

Your Turn

Exercise 1: Three Confidence Levels

A sample of 20 students has mean = 78 and SD = 9. Calculate 90%, 95%, and 99% confidence intervals. Notice how the interval widens as you demand more confidence.

Output will appear here...
What to notice: The 99% CI is wider than the 95% CI, which is wider than the 90% CI. You pay for higher confidence with a less precise interval.

Exercise 2: Simulate 50 Confidence Intervals

Generate 50 random samples of n = 30 from a Normal(70, 10) population. Build a 95% CI for each. Count how many intervals contain the true mean of 70. It should be close to 47 to 48 (95% of 50).

Output will appear here...
This is the real meaning of "95% CI." The red intervals in the plot are the ones that missed. With 50 simulations at 95% confidence, you expect about 2 to 3 misses.

Exercise 3: Interpret Three Real CIs

Plain language interpretation practice. Three studies report confidence intervals. For each, interpret what the CI tells us, and what it does NOT tell us.

Output will appear here...

Brain Break

A confidence interval is not about the probability that the parameter is inside, it’s about how often the method works.

Quick check: If you want a narrower CI without changing the confidence level, what would you do? (Collect a larger sample! SE = s/√n decreases as n increases.)

Key Takeaway

A 95% CI means the method captures the true parameter 95% of the time, not that there’s a 95% chance the parameter is in this specific interval. Use t.test() in R for confidence intervals. Wider CI = lower precision; narrower CI = higher precision.

Module 4 Complete!

You can now build and correctly interpret confidence intervals, one of the most commonly misunderstood concepts in all of statistics. Next up: the closely related idea of hypothesis testing.

Continue to Module 5: Hypothesis Testing →

← Module 3: Sampling & CLT Module 4 of 8 Module 5: Hypothesis Testing →