Interactive demo · Applied Statistical Learning in R and Python · Chapter 3

Least squares with confidence and prediction bands on the Advertising data

Two hundred markets, each with its advertising budget (thousands of dollars) and its sales (thousands of units). Pick one medium and the page fits the least-squares line of sales on that budget, draws the residuals, and surrounds the line with two bands: the narrow confidence band for the average sales at a given budget, and the wide prediction band for the sales of one new market. Move the budget x0 to read both intervals there, and watch the bands widen away from the mean budget. The last preset draws 20 new data sets from the fitted line and counts how many of their confidence intervals catch the line: about 95% of them, on average.

Predictor

Confidence level

Budget x0

Bands

Sampling

Try these

The intervals use the t distribution with n − 2 = 198 degrees of freedom. At x0 the standard error of the fitted line is RSE·√(1/n + (x0 − x̄)²/Σ(xi − x̄)²); for a new market a 1 is added under the root, because its own error ε must be predicted too. New data sets keep the 200 budgets and draw sales from the fitted line plus Gaussian noise with standard deviation RSE (fixed random seed, so each press continues the same sequence).

In the lecture notes