Tutorials › Regression Analysis (Undergraduate) › Plotting Predicted Means With Confidence Bands

Predictions for Conditional Means · Tutorial 714 of 991

Plotting Predicted Means With Confidence Bands

Build clear plots of fitted conditional means and their uncertainty, with careful attention to factor groups and the predictor values the data support.

Beginner 9 min read

What You'll Learn

  • Distinguish a fitted conditional mean from an individual outcome prediction.
  • Calculate pointwise confidence limits for selected predictor values.
  • Plot a fitted curve and its confidence band over a suitable grid.
  • Display group-specific fitted means and uncertainty without obscuring factor-level comparisons.
  • Limit each plotted curve to predictor values supported by its group’s data.
  • Explain what a pointwise confidence band does and does not guarantee.

Why Plot Fitted Means?

A table of fitted means gives exact estimates at selected predictor values, but a plot makes the pattern across those values easier to see. It can show whether the estimated conditional mean rises, falls, or curves; how its uncertainty changes along the predictor; and whether estimated patterns differ across factor levels. A useful plot makes these features visible without suggesting that the data support predictions at every possible predictor value.

The curve represents an estimated conditional mean: the model’s estimate of the average outcome for cases with specified predictor values. It is not a curve of individual outcomes, and a confidence band for the mean does not show the range where individual outcomes are expected to fall. The distinction matters because individual outcomes vary around their conditional mean.

Definition: A pointwise confidence interval for a fitted conditional mean at a specified predictor profile gives an interval for that mean at that profile. A pointwise confidence band displays such intervals across a set of predictor values. At each fixed value, the interval has its stated confidence level under the model assumptions; that statement does not guarantee that the entire true mean curve lies inside the displayed band with that confidence level.

From Fitted Means to a Confidence Band

Suppose a regression model has conditional mean \(E(Y\mid \mathbf{x})=\mathbf{x}^{T}\boldsymbol{\beta}\), where \(\mathbf{x}\) is the vector describing a predictor profile and \(\boldsymbol{\beta}\) is the vector of model coefficients. The fitted conditional mean is \(\hat{\mu}(\mathbf{x})=\mathbf{x}^{T}\hat{\boldsymbol{\beta}}\). Its estimated standard error is \(SE\{\hat{\mu}(\mathbf{x})\}\), calculated using the coefficient uncertainty and the profile’s design vector.

For a linear model with independent errors, constant error variance, and normally distributed errors, a two-sided pointwise confidence interval can be written as follows. The \(t\) critical value uses the model’s residual degrees of freedom, \(df\), and the interval’s confidence level. These assumptions support the usual \(t\)-based interval; if they are not reasonable, the plotted limits may not have their intended coverage.

$$ \hat{\mu}(\mathbf{x}) \ \pm\ t_{1-\alpha/2,\,df}\,SE\{\hat{\mu}(\mathbf{x})\} $$

A brief justification explains why the band depends on the profile. The fitted mean is a linear combination of estimated coefficients. If the estimated coefficient covariance matrix is \(\widehat{\operatorname{Var}}(\hat{\boldsymbol{\beta}})\), the variance estimate of that linear combination is \(\mathbf{x}^{T}\widehat{\operatorname{Var}}(\hat{\boldsymbol{\beta}})\mathbf{x}\). Taking its square root gives the standard error; dividing the estimation error by that standard error gives the \(t\)-based statistic used for the interval.

$$ \widehat{\operatorname{Var}}\{\hat{\mu}(\mathbf{x})\} =\widehat{\operatorname{Var}}(\mathbf{x}^{T}\hat{\boldsymbol{\beta}}) =\mathbf{x}^{T}\widehat{\operatorname{Var}}(\hat{\boldsymbol{\beta}})\mathbf{x}, \qquad SE\{\hat{\mu}(\mathbf{x})\} =\sqrt{\mathbf{x}^{T}\widehat{\operatorname{Var}}(\hat{\boldsymbol{\beta}})\mathbf{x}} $$

To draw a plot, calculate fitted means and intervals at a grid of predictor values, then display the means as a curve and the lower and upper limits as the band boundaries. A denser grid makes the plotted curve look smoother, but it does not add information to the data or turn pointwise intervals into a simultaneous guarantee. The grid should stay within predictor values supported by the data, especially when the model includes factor levels with different observed ranges.

Worked Example: A Pointwise Band for a Straight-Line Fit

Suppose a hypothetical study fits a simple linear regression of an outcome measured in points on a quantitative predictor \(x\), measured in hours. The fitted mean is \(\hat{\mu}(x)=20+2x\). Assume the model’s residual standard deviation is \(s=3\) points, its sample size is \(n=10\), the observed predictor mean is \(\bar{x}=5\) hours, and \(S_{xx}=\sum (x_i-\bar{x})^2=30\) hours squared. The model has \(df=8\). For this example, assume independent, normally distributed errors with constant variance and a linear conditional mean.

For simple linear regression, the standard error of the fitted mean at \(x\) is \(s\sqrt{1/n+(x-\bar{x})^2/S_{xx}}\). Using \(t_{0.975,8}=2.306\) gives 95% pointwise intervals.

$$ SE\{\hat{\mu}(x)\}=s\sqrt{\frac{1}{n}+\frac{(x-\bar{x})^2}{S_{xx}}} $$

At \(x=2\) hours, the fitted mean is \(20+2(2)=24\) points. Its standard error and interval are

$$ SE=3\sqrt{\frac{1}{10}+\frac{(2-5)^2}{30}} =3\sqrt{0.4}=1.897, \qquad 24\pm 2.306(1.897)=24\pm4.374=(19.63,\,28.37) $$

At \(x=5\), the fitted mean is \(20+2(5)=30\), and \(SE=3\sqrt{0.1}=0.949\). Its interval is \(30\pm2.306(0.949)=(27.81,\,32.19)\) points. At \(x=8\), the fitted mean is \(36\), and \(SE=3\sqrt{0.4}=1.897\); the interval is \(36\pm2.306(1.897)=(31.63,\,40.37)\) points.

A plot would place the fitted means 24, 30, and 36 at 2, 5, and 8 hours, with the corresponding intervals shown as the band at those locations. The wider intervals at 2 and 8 hours than at 5 hours reflect greater uncertainty in the fitted mean farther from the observed predictor average. All limits describe uncertainty in the conditional mean outcome, in points—not a range of individual outcomes.

Comparing Factor Levels

When a model includes a factor, a plot can show a separate fitted curve for each factor level. With a quantitative predictor \(x\) and a binary factor \(G\), one common model is \(\hat{\mu}(x,G)=\hat{\beta}_0+\hat{\beta}_1x+\hat{\beta}_2G+\hat{\beta}_3xG\), where \(G=0\) denotes one level and \(G=1\) the other. The interaction term allows the fitted slopes to differ. The curve for each level is obtained by substituting that level’s value of \(G\) into the fitted model.

Before plotting, check the observed range of \(x\) within each factor level. If the ranges differ, do not extend a group’s curve across values that were not observed for that group merely to make the plot look complete. A shared horizontal axis can make the ranges clear; separate panels can help when overlapping curves are difficult to distinguish. When using panels, keep axis scales comparable if visual comparisons are intended, and label each panel and its units.

Worked Example: Two Group-Specific Curves and Bands

Consider a hypothetical model for a score measured in points. The quantitative predictor \(x\) is measured in hours, and the factor has levels A and B. Suppose the fitted group-specific means are \(\hat{\mu}_A(x)=50-x\) and \(\hat{\mu}_B(x)=55-0.5x\), so the fitted slopes differ. Assume the estimated coefficient covariance matrix is diagonal, with variances \(0.64\), \(0.01\), \(0.36\), and \(0.0025\) for the intercept, \(x\), group indicator, and interaction coefficients, respectively. Assume independent, normally distributed errors with constant variance and a correctly specified conditional mean; the residual degrees of freedom are 36.

For this illustrative covariance matrix, the standard error for level A is \(\sqrt{0.64+0.01x^2}\). For level B, the group indicator and interaction coefficients also contribute, giving \(\sqrt{0.64+0.36+0.01x^2+0.0025x^2}\). Use \(t_{0.975,36}=2.028\) for the 95% pointwise intervals.

At \(x=4\) hours, level A has fitted mean \(50-4=46\) points and standard error \(\sqrt{0.64+0.01(16)}=\sqrt{0.80}=0.894\). Its interval is \(46\pm2.028(0.894)=(44.19,\,47.81)\). At the same \(x\), level B has fitted mean \(55-0.5(4)=53\) points and standard error \(\sqrt{0.64+0.36+0.16+0.04}=\sqrt{1.20}=1.095\). Its interval is \(53\pm2.028(1.095)=(50.78,\,55.22)\).

At \(x=6\), level A has fitted mean 44 and standard error \(\sqrt{0.64+0.36}=1.000\), giving \(44\pm2.028=(41.97,\,46.03)\). Level B has fitted mean 52 and standard error \(\sqrt{0.64+0.36+0.36+0.09}=\sqrt{1.45}=1.204\), giving \(52\pm2.028(1.204)=(49.56,\,54.44)\). These computations provide four fitted means and their pointwise limits for plotting. They do not by themselves establish that the group means differ significantly; inference about a difference requires uncertainty for that difference.

Suppose the observed \(x\) ranges for both groups include 4 to 6 hours. The plotted curves and bands at those two locations are supported for both levels, and connecting estimates across that shared range gives a direct visual comparison. If one group had no observations near 4 hours, its fitted value there would instead rely on model extrapolation and should not be presented as equally data-supported.

Choosing What the Plot Shows

A clear plot usually includes a labeled horizontal axis with predictor units, a labeled vertical axis with outcome units, and a legend or panel labels for factor levels. It should distinguish the fitted mean curve from its confidence limits. Shading between the limits is common, but two boundary lines can be clearer when several curves or bands overlap. If observed data are added for context, identify them as observations; they are not the fitted means or the confidence limits.

The choice of grid should follow the purpose of the plot. A small set of values can emphasize specific, interpretable profiles; a denser grid can display the shape of a fitted curve. In either case, calculate the confidence limits at each plotted value from the fitted model and its uncertainty. Do not draw limits from a single standard error applied everywhere: the standard error can change with predictor location, as the first example illustrates. For curved models, calculate fitted values from the full model at each grid point rather than connecting only the endpoints with a straight line.

The plotted band is a visual summary of pointwise uncertainty, not a substitute for stating the model and the interpretation. In a caption or accompanying text, identify the outcome and its units, the predictor and its units, the factor levels, and the confidence level. State that the band concerns the fitted conditional mean. A reader should not have to infer whether a displayed band represents uncertainty about the mean or variation among individual outcomes.

Common mistake: Extending every group’s fitted line across the full horizontal axis can imply that fitted means are supported where a group has no observations. Instead, inspect the predictor range separately for each factor level and restrict that group’s curve to a defensible range. Also, do not interpret a 95% pointwise band as a promise that the entire true curve lies inside it with 95% confidence; the 95% statement applies at each fixed predictor profile.

Putting the Plot Together

1
Specify the fitted mean.
State the model form, outcome units, predictor values, and factor levels represented by each curve.
2
Choose a supported grid.
Check the observed predictor range overall and within each factor level. Avoid displaying unsupported group-specific values as though they were observed.
3
Calculate each interval.
At every grid value, compute the fitted mean, its standard error, and the pointwise \(t\)-based confidence limits under the stated model assumptions.
4
Label the visual summary.
Show units, group labels, and the confidence level, and identify the band as uncertainty for the conditional mean rather than individual outcomes.

Check Your Understanding

Use these questions to check your understanding of fitted-mean plots and their confidence bands.

  1. What does a pointwise 95% confidence interval at one predictor value describe, and what does it not describe?
  2. In the simple-regression example, why is the fitted-mean standard error smaller at \(x=5\) than at \(x=2\)?
  3. A model has separate curves for two factor levels, but one level has no observations near the upper end of the other level’s predictor range. What should you consider before extending its curve?
  4. Why can a 95% pointwise confidence band not automatically be interpreted as having 95% confidence that the whole mean curve lies inside it?
  5. Name two pieces of information that should be included in a plot or its caption so the fitted means and confidence band can be interpreted in context.