Plotting curved confidence intervals for linear regression

I am using coefCI function to estimate the confidence intervals of a linear model. I can plot this and get linear confidence bands for the regression line, but, I've seen plots of linear regression where the bands are curved. How can I get those? Thanks.

 Accepted Answer

I did not see your post before.
Use the appropriate version of the predict (link) function for a compact linear model, predict (link) for a generalised linear model, or others, depending on what you are doing.
Those will give the confidence intervals. If you want to plot smooth curves, use:
xv = linspace(min(x), max(x), 1000);
for the independent variable ‘Xnew’ to use with predict.

8 Comments

Excellent. Not sure how I missed the predict function. Thanks!
Well, the regression line does not need interpolation. For the confidence intervals, using linspace will give me straight lines rather than curved, which is what I want. I would rather use a cubic interpolation.
yci = spline(x, yci, x(1):0.01:x(end));
My pleasure.
The confidence intervals do not need interpolation. Plot them as returned by predict.
If the confidence limits lines appear straight, that means there is not much variation in the confidence limits over the interval. This occurs if there is very little error in the dependent variable, especially with respect to its magnitude. (I opted for the 1000 points because I do not know your data set. That number should give a smooth approximation in any event. Change it as necessary.)
Without your data and relevant code, I cannot suggest any other reason for the confidence limit lines appearing not to vary over the limits of your independent variable. They should have a minimum difference at the mean of your independent variable, and a maximum (and approximately equivalent) difference at the extremes of your independent variable. Use that as a test to see if they are actually straight. (I suspect they are not.)
Hi Star, I think you misunderstood my comment. The confidence intervals are and indeed appear curved. But using linspace as you suggest will make them straight and therefore wrong. Whereas, using spline interpolation will make them smooth and preserve their curved shape. Maybe I misunderstood your suggestion or linspace in my MATLAB works differently!
Thanks anyways!
My pleasure.
They aren’t straight when I plot them, using essentially the same code as in my original Answer:
x = sort(rand(1,100));
y = rand(1,100);
mdl = fitlm(x,y);
Xnew = linspace(min(x), max(x), 1000)';
[ypred,yci] = predict(mdl, Xnew);
figure
plot(x, y, 'p')
hold on
plot(Xnew, ypred, '--g')
plot(Xnew, yci, '--r')
hold off
grid
Note that ‘Xnew’ must be a column vector.
Aaah Ok, I see. Use linspace before predict... got it! I thought you meant to use linspace with the predicted min and max ypred values, which didn't make much sense. The two approaches give me the same smooth confidence intervals, but yours is definitely more appropriate.
Thank you.
If my Answer helped you solve your problem, please Accept it!
This answer appears to work great for a model with a single predictor.
However, when trying to use it with a model in which I have multiple predictors (e.g. 5), I get the following error:
Error using classreg.regr.CompactTermsRegression/designMatrix (line 166)
X must have 5 columns.
Error in LinearModel/predict (line 337)
design = designMatrix(model,Xpred);
Any idea as to how to plot confidence intervals from a fitlm structure based on a multiple linear regression model?
The short answer is that you get this error because you have multiple predictors, so the values in Xnew (or xv in the example above) should have a column for each predictor. (ie. you need to make an xv for every predictor in your multiple regression).
This makes visualizing the data tricky, because for every additional predictor, you add another dimension to your plot, I believe. The question here I think was focused mostly on linear regression with one predictor.

Sign in to comment.

More Answers (0)

Asked:

Xen
on 9 Jun 2018

Edited:

on 20 Jul 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!