How to make segmented regression line and determine the breakpoints?
Show older comments
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.

Accepted Answer
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);
1 Comment
Harshita Garg
on 12 Jun 2021
Categories
Find more on Get Started with Curve Fitting Toolbox 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!