File Exchange

image thumbnail

Two-phase linear regression model

version (2.56 KB) by Dimitar Atanasov
calculates the parameters of unidimensional two-phase linear regression model


Updated 25 Feb 2017

View License

x - vector row with 'x' values
y - vector row with 'y' values
r - expected 'x'-coordinate of break point
if r is empty it is calculated during
the optimisation
p - if p is equal to 1 the fit is plotted
th - estimated paremeters of the regression
y_1 = th(1) + th(2) * x
y_2 = th(3) + th(4) * x
r - the estimated break point
SSR - Sum of Squares of the Residuals
SSM - Sum of Squares due to the Model
R^2 can be calculated as RRM/(SSM + SSR)
res - vector of residuals

Comments and Ratings (5)

This is exactly what I wanted. Can you please let me know how to cite it in my journal paper?

Hi Dimitar Atanasov,
Thank you for your prompt reply.
I really appreciate it!
This function is very good!

Hi Dimitar Atanasov,

Thanks for your sharing the code, but I still have a couple of questions:

1)When I run function two_phase_lin I get an Error Message:

Undefined function or variable 'fmins'.
Error in two_phase_lin (line 61)
th = fmins(@aim,th0,[],[],x,y);
Could you solve this problem?

2)The second question is about using the function two_phase_lin when I want to find the intersection in a two-phase regression (the mathematical model of bisegmental linear regression using Hinkley’s algorithm). Specifically, I would like to find the point at which the trend of parameter y in terms of x changes. In another word, the trend of the parameter has a first linear phase and then a second linear phase, I want to find the point at which it passes from the first to the second phase. Is it possible to use this function for my problem?

Thanks for your great contribution.



Fixed to use fminsearch instead fmins

MATLAB Release Compatibility
Created with R14
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.

Learn About Live Editor