How to make segmented regression line and determine the breakpoints?

61 vues (au cours des 30 derniers jours)
Hi,
I have a data set which shows multiple distinct linear lines as shown in the figure. I have to plot equations mx+y in such a way that the slope is always positive and where there is change in gradient, breakpoint is determined. I tried to do it but I am sure how it is done in Matlab.
Any suggestions would be helpful. I have attached the dataset.
Thank you in advance.

Réponse acceptée

Image Analyst
Image Analyst le 12 Juin 2021
The way I do it is to pick a dividing point and then slide that along fitting a line to each side. The dividing point that has the biggest difference in the slopes of the two lines is the dividing point to use. Then just fit the two lines on either side of that. Full demo is attached.

Plus de réponses (1)

dpb
dpb le 9 Juin 2021
I've answered this several time in the past, but never think to keep a link...
Ah! There's the one...
Following is most of text from above...
Piecewise linear regression is fairly easy to code anyway...the algebra to add the condition to match the two at the breakpoint is
y = a1 + b1 x, x<=c,
y = a2 + b2 x, x>c.
Match breakpoint, or
a1 + b1 c = a2 + b2 c.
Rearrange this to isolate (say) a2 as
a2 = a1 + b1 c - b2 c --> a1 + c(b1-b2) = aprime
Now have
y = a1 + b1 x, x<=c,
y = aprime + b2 x, x>c.
This is easy-peasy to code in Matlab and use as target for nlinfit --
function y=piecewise(coef,x)
% return piecewise linear fit given a1,b1,c,b2 as coefficient array and vector x
% c is breakpoint, a1 is intercept of first section, b1,b2 are two segment slopes
a1=coef(1); b1=coef(2); % reduce parentheses clutter....
c=coef(3); b2=coef(4);
ix=x>c; % location > breakpoint
y(ix)=[a1+c*(b1-b2)+b2*x(ix)];
y(~ix)=[a1+b1*x(~ix)];
y=y(:);
Use this as
coeff=nlinfit(X,Y,@piecewise,coeff0);

Catégories

En savoir plus sur Linear and Nonlinear Regression dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by