How to make segmented regression line and determine the breakpoints?
63 views (last 30 days)
Show older comments
Harshita Garg
on 9 Jun 2021
Answered: Image Analyst
on 12 Jun 2021
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.
0 Comments
Accepted Answer
Image Analyst
on 12 Jun 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.
0 Comments
More Answers (1)
dpb
on 9 Jun 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);
See Also
Categories
Find more on Linear and Nonlinear Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!