Confidence band around linear least-squares line

I am using lsline in Matlab 2016b to add a linear least-squares line to a scatter plot. I would like to add a 95% confidence band around that fit line, such that it looks like this (plot is made with the python function seaborn):
However, lsline returns no fit parameters from which to construct the 95% confidence band, and the only Matlab function I could find that does return these, is nlpredci, but that function is used for something else (nonlinear regression prediction)

 Réponse acceptée

Star Strider
Star Strider le 4 Avr 2018

1 vote

One option is the Statistics and Machine Learning Toolbox fitlm (link) function. Then use the predict function (linked at the end of that page).
Another option is to sue polyfit and polyval with the File Exchange polypredci function, or the Statistics and Machine Learning Toolbox polyconf (link) function.

7 commentaires

As always, my pleasure!
z8080
z8080 le 4 Avr 2018
Modifié(e) : z8080 le 4 Avr 2018
Thanks! Any suggestion how I can then shade the inside of the confidence band, to make it look more like the picture in my OP?
Also, I wonder if doing it this way is really equivalent to what lsline is doing: with this data,
x = [62 97 85 78 79 68 91 98 89 54 61 93 76 51 105 24 62 94 92 91 94 81 73 101 63 95 79 81 81 68 ]
y = [1.53 1.35 1.20 1.06 1.23 0.55 0.58 1.38 1.63 0.98 1.63 1.21 1.10 1.03 0.85 0.47 0.85 1.78 2.23 1.55 0.80 1.35 1.25 1.45 1.10 1.38 1.00 1.00 0.79 1.63 ]
and these commands,
[p,S] = polyfit(x,y,1);
[y_ext,delta] = polyconf(p,x,S);
my confidence band (y_ext - delta, to y_ext + delta) is almost perfectly symmetrical about the fit line, which seems odd..
First, this will plot your data and produce a plot similar to the one you posted:
x = [62 97 85 78 79 68 91 98 89 54 61 93 76 51 105 24 62 94 92 91 94 81 73 101 63 95 79 81 81 68 ];
y = [1.53 1.35 1.20 1.06 1.23 0.55 0.58 1.38 1.63 0.98 1.63 1.21 1.10 1.03 0.85 0.47 0.85 1.78 2.23 1.55 0.80 1.35 1.25 1.45 1.10 1.38 1.00 1.00 0.79 1.63 ];
[p,S] = polyfit(x,y,1);
xv = linspace(min(x), max(x), 150);
[y_ext,delta] = polyconf(p,xv,S);
figure
plot(x, y, 'pg', 'MarkerFaceColor','g')
hold on
plot(xv, y_ext, '-g')
patch([xv fliplr(xv)], [(y_ext+delta) fliplr((y_ext-delta))], 'g', 'FaceAlpha',0.1, 'EdgeColor','none')
hold off
Experiment with the colours and other arguments to get the result you want.
Second, it should be symmetrical, since that is the way the confidence interval with symmetric distributions (such as the normal and t-distributions) are defined. Also, with respect to symmetry, it will be symmetrical about the (mean(x),mean(y)) point. It diverges as the distance from the means increases.
z8080
z8080 le 5 Avr 2018
Many thanks once again, this is indeed what I wanted.
I had made a mistake earlier, what I meant to say was that I found it surprising that the confidence band is of constant width. Especially around that outlier at x=24, I'd have expected the confidence band to be much wider than towards the centre of the distribution.
But it could well be this just shows my inexperience with confidence bands and curve fits...
As always my pleasure.
The width isn’t constant. It may look that way if you use only the original ‘x’ values. However if you use my code (with the ‘xv’ vector) you can easily see that it is not constant, and diverges appropriately from the mean values to the extremes.
It’s not your inexperience. When I plot a nonlinear regression, confidence intervals, or anything else that needs nigher-than normal resolution to depict correctly, I use something like ‘xv’ instead of only using the original *‘x’*values, unless they are sufficiently high resolution. The image you posted appears to have used the same sort of approach.
z8080
z8080 le 5 Avr 2018
Makes perfect sense now.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with Curve Fitting Toolbox dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by