Plot Simultaneous and Pointwise Confidence Bands for Linear Regression
When performing linear regression analysis it can be useful to plot the confidence bands around the regression line to indicate the range of possible values for the response variable.
There are two types of confidence bands that can be plotted: pointwise and simultaneous.
- Pointwise confidence bands indicate the uncertainty in the predicted response variable at each value of the predictor variable.
- Simultaneous confidence bands indicate the range of the possible values for the entire regression line.
We will use the ggplot2 package in R Programming Language to plot both pointwise and simultaneous confidence bands for linear regression.
Simultaneous Confidence Bands
In linear regression, simultaneous confidence bands are used to provide a visual representation of uncertainty associated with the estimated regression line. These bands show the range within which the true regression line is likely to fall with a certain level of confidence.
R
library (ggplot2) data (mtcars) #regression model fit <- lm (mpg ~ wt, data = mtcars) ci <- predict (fit, interval = "confidence" , level = 0.95) ggplot (data = mtcars, aes (x = wt, y = mpg)) + geom_point () + geom_smooth (method = "lm" , se = FALSE ) + geom_ribbon ( aes (ymin = ci[, 2], ymax = ci[, 3]), alpha = 0.2) |
Output:
Pointwise Confidence Bands
The Pointwise confidence bands, on the other hand, provide a visual representation of uncertainty associated with a single regression coefficient. These bands show the range within which the true value of the coefficient is likely to fall with a certain level of confidence.
R
library (ggplot2) data (mtcars) #regression model fit <- lm (mpg ~ wt, data = mtcars) ggplot (data = mtcars, aes (x = wt, y = mpg)) + geom_point () + stat_smooth (method = "lm" , se = TRUE ) |
Output:
Now let’s plot the simultaneous as well as pointwise confidence bands on the same graph and compare the two visually.
R
library (dplyr) # sample datasets setosa <- iris %>% filter (Species == "setosa" ) %>% select (Sepal.Length, Sepal.Width, Species) virginica <- iris %>% filter (Species == "virginica" ) %>% select (Sepal.Length, Sepal.Width, Species) # compute simultaneous confidence bands setosa <- setosa %>% arrange (Sepal.Length) virginica <- virginica %>% arrange (Sepal.Length) compute linear models Model <- as.formula (Sepal.Width ~ poly (Sepal.Length,2)) fit1 <- lm (Model, data = setosa) fit2 <- lm (Model, data = virginica) # compute design matrices X1 <- model.matrix (fit1) X2 <- model.matrix (fit2) # general linear hypotheses cht1 <- multcomp:: glht (fit1, linfct = X1) cht2 <- multcomp:: glht (fit2, linfct = X2) # simultaneous confidence bands (finally!) cc1 <- confint (cht1); cc1 <- as.data.frame (cc1$confint) cc2 <- confint (cht2); cc2 <- as.data.frame (cc2$confint) setosa$LowerBound <- cc1$lwr setosa$UpperBound <- cc1$upr virginica$LowerBound <- cc2$lwr virginica$UpperBound <- cc1$upr # combine datasets mydata <- rbind (setosa, virginica) # plot both simultaneous confidence # bands and pointwise confidence # bands, to show the difference library (ggplot2) # prepare a plot using dataframe mydata, # mapping sepal Length to x, # sepal width to y, and grouping the data by species ggplot (data = mydata, aes (x = Sepal.Length, y = Sepal.Width, color = Species)) + # add data points geom_point () + # add quadratic regression with orthogonal polynomials and 95% pointwise # confidence intervals geom_smooth (method = "lm" , formula = y ~ poly (x,2)) + # # add 95% simultaneous confidence bands geom_ribbon ( aes (ymin = LowerBound, ymax = UpperBound), alpha = 0.5, fill = "grey70" ) |
Output:
Contact Us