Answer

Confidence vs prediction interval: mean response or a new observation?

A frequently asked statistics question ·

confidence intervalsregression

The short answer

A confidence interval is a range for the average outcome at a given x: the height of the true line. A prediction interval is a range for one new case at that x. The second adds the scatter of individual cases around the line, so it is always wider. With more data the first shrinks toward zero, while the second levels off at about plus or minus two residual SDs, because no sample size makes individuals less variable.

Two different targets: an average or one new case

Suppose you have regressed exam score on hours of study, and you ask about students who study 5 hours. There are two quite different things you might want to know:

Both intervals are centered on the same fitted value. They differ only in how much uncertainty they include, and that difference is large. Using a confidence interval when you mean to predict an individual case is a common mistake, and it makes predictions look far more precise than they are.

The formulas side by side

For a simple linear regression fitted to n points, with residual standard deviation s, the standard error of the fitted mean at a value x₀ is:

se_fit = s * sqrt(1/n + (x0 - mean(x))^2 / sum((x - mean(x))^2))

Two consequences follow. First, se_fit contains 1/n and a sum of squares that grows with n, so it falls toward zero as the sample grows; the confidence interval collapses onto the true line. The s² term does not depend on n at all, so the prediction interval never gets narrower than about ±1.96σ, where σ is the true residual SD. More data pins down the line, but it cannot make individuals less variable. Second, both intervals widen as x₀ moves away from the mean of x, because errors in the slope matter more there.

Worked example in R and Python: widths, sample size and coverage

The code simulates data from a known line (intercept 2, slope 0.5, residual SD 1.5) and uses R's predict() with interval = "confidence" and interval = "prediction". Part 2 refits the model with ever larger samples, and part 3 repeats a 30-point study 10,000 times to check what each interval actually captures. The Python version computes the same intervals from the formulas above.

R

set.seed(415)
sigma <- 1.5   # true residual SD
make_data <- function(n) {
  x <- runif(n, 0, 10)
  data.frame(x, y = 2 + 0.5 * x + rnorm(n, sd = sigma))
}

# 1. One study with n = 30: both intervals at x = 5 and x = 9.5
d   <- make_data(30)
fit <- lm(y ~ x, data = d)
new <- data.frame(x = c(5, 9.5))
ci  <- predict(fit, new, interval = "confidence")   # for the mean of y at x
pi  <- predict(fit, new, interval = "prediction")   # for one new y at x
round(data.frame(x = new$x, fit = ci[, 1], ci_lwr = ci[, 2], ci_upr = ci[, 3],
                 pi_lwr = pi[, 2], pi_upr = pi[, 3]), 2)

# 2. How the half-widths at x = 5 change as the sample grows
half_width <- function(n) {
  f  <- lm(y ~ x, data = make_data(n))
  ci <- predict(f, data.frame(x = 5), interval = "confidence")
  pi <- predict(f, data.frame(x = 5), interval = "prediction")
  c(n = n, CI = (ci[3] - ci[2]) / 2, PI = (pi[3] - pi[2]) / 2)
}
round(t(sapply(c(30, 300, 3000, 30000), half_width)), 3)

# 3. What each interval captures, over 10,000 repeated studies with n = 30
hits <- replicate(10000, {
  f  <- lm(y ~ x, data = make_data(30))
  ci <- predict(f, data.frame(x = 5), interval = "confidence")
  pi <- predict(f, data.frame(x = 5), interval = "prediction")
  true_mean <- 2 + 0.5 * 5
  y_new <- true_mean + rnorm(1, sd = sigma)                 # one new observation at x = 5
  c(CI_has_true_mean = ci[2] < true_mean & true_mean < ci[3],
    CI_has_new_y     = ci[2] < y_new & y_new < ci[3],
    PI_has_new_y     = pi[2] < y_new & y_new < pi[3])
})
round(rowMeans(hits), 3)

Python

import numpy as np
from scipy import stats

rng = np.random.default_rng(415)
sigma = 1.5   # true residual SD

def make_data(n):
    x = rng.uniform(0, 10, n)
    return x, 2 + 0.5 * x + rng.normal(0, sigma, n)

def intervals(x, y, x0, level=0.95):
    # OLS fit, then the confidence interval (mean of y at x0) and prediction interval (one new y)
    X = np.column_stack([np.ones_like(x), x])
    beta, *_ = np.linalg.lstsq(X, y, rcond=None)
    df = len(y) - 2
    s = np.sqrt(np.sum((y - X @ beta) ** 2) / df)          # residual SD
    X0 = np.column_stack([np.ones_like(x0), x0])
    fit = X0 @ beta
    se_fit = s * np.sqrt(np.sum(X0 @ np.linalg.inv(X.T @ X) * X0, axis=1))
    t = stats.t.ppf(1 - (1 - level) / 2, df)
    ci = t * se_fit                                           # half-width for the mean
    pi = t * np.sqrt(s**2 + se_fit**2)                        # half-width for a new case
    return fit, ci, pi

# 1. One study with n = 30: both intervals at x = 5 and x = 9.5
x, y = make_data(30)
x0 = np.array([5, 9.5])
fit, ci, pi = intervals(x, y, x0)
for i in range(2):
    print(f"x={x0[i]}: fit={fit[i]:.2f} CI=[{fit[i]-ci[i]:.2f}, {fit[i]+ci[i]:.2f}] "
          f"PI=[{fit[i]-pi[i]:.2f}, {fit[i]+pi[i]:.2f}]")

# 2. How the half-widths at x = 5 change as the sample grows
for n in (30, 300, 3000, 30000):
    _, ci_n, pi_n = intervals(*make_data(n), np.array([5.0]))
    print(f"n={n}: CI={ci_n[0]:.3f} PI={pi_n[0]:.3f}")

# 3. What each interval captures, over 10,000 repeated studies with n = 30
true_mean = 2 + 0.5 * 5
hits = []
for _ in range(10000):
    fit5, ci5, pi5 = (v[0] for v in intervals(*make_data(30), np.array([5.0])))
    y_new = true_mean + rng.normal(0, sigma)                  # one new observation at x = 5
    hits.append([abs(true_mean - fit5) < ci5, abs(y_new - fit5) < ci5, abs(y_new - fit5) < pi5])
names = ["CI_has_true_mean", "CI_has_new_y", "PI_has_new_y"]
print({k: round(float(v), 3) for k, v in zip(names, np.mean(hits, axis=0))})
    x  fit ci_lwr ci_upr pi_lwr pi_upr
1 5.0 4.18   3.62   4.73   1.09   7.26
2 9.5 5.88   4.81   6.95   2.67   9.10
         n    CI    PI
[1,]    30 0.601 3.073
[2,]   300 0.188 3.267
[3,]  3000 0.055 2.993
[4,] 30000 0.017 2.948
CI_has_true_mean     CI_has_new_y     PI_has_new_y 
           0.947            0.299            0.952

These figures come from one seeded run of the R code. Python's random numbers differ from R's, so its figures differ slightly, but they show the same pattern. (On the same data, the Python formulas reproduce predict() exactly.)

Which one should you use?

In both cases the 95% describes the method's long-run performance, not a probability for one particular interval; see what 95% confidence means. And neither interval is trustworthy far outside the range of x you observed, since the straight line itself is an assumption there.

How to report both intervals in APA style (7th edition)

Say which interval you are giving and what it is for, because readers often assume a confidence interval. Write out "prediction interval" the first time, as APA has no standard abbreviation for it. Using the x = 5 results above:

"At 5 hours of study, the predicted mean score was 4.18, 95% CI [3.62, 4.73]. For an individual student, the 95% prediction interval (PI) was [1.09, 7.26]."

In a table of predictions, give the fitted value, the CI and the PI in separate columns, and state in a note the model and the predictor values used.

Related tools and guides

More answered questions

Working with your own data?

General answers only go so far. Send us your situation and we'll reply by email within two business days.

Ask your question

Need more than an answer?

Written by AskStats with AI assistance. This is general information, not advice for your specific data or study. When the results matter, check your approach with a qualified statistician.