How to make segmented regression line and determine the breakpoints?

93 次查看(过去 30 天)
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.

采纳的回答

Image Analyst
Image Analyst 2021-6-12
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.

更多回答(1 个)

dpb
dpb 2021-6-9
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);

类别

Help CenterFile Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by