<blockquote>
<p>I wanted to do a &quot;Two-phase linear regression&quot; to check my results.</p>
</blockquote>
<p>You aren't doing that. By specifying <code>model=&quot;l1&quot;</code>, you are asking the package to use a piecewise constant model (rather than a linear regression) for each segment. This models each segment as the mean of all the elements in that segment. The error is calculated as the sum of the absolute values of the difference between each element and the mean (the <span class="math-container">$L^1$</span> norm). Apparently asking for two breakpoints means one internal breakpoint, or two segments that collectively span the dataset.</p>
<p>That is not a good model of your dataset, and it is not a linear regression.</p>

CC BY-SA 4.0
2022-10-26T09:16:33.923Z
Thank you David for your reply and nicely explaining why "That is not a good model of your dataset". I am aware that what I am doing is not linear-regression.

What I meant to say is, I want to do Two-phase linear regression because I am not happy with the result of what I am doing and I am looking for a python package that can do a Two-phase linear regression. (probably it was not clear in my question).
Bidyut Bikash Goswami
CC BY-SA 4.0
2022-10-26T11:57:05.020Z
The package in question (ruptures) could probably do what you want as well. It has a custom model option.
David Hammen
CC BY-SA 4.0
2022-10-26T06:19:38.973Z
2022-10-26T06:32:01.197Z
2022-10-26T06:32:01.197Z
David Hammen
<p>This is my first post. I am trying my best to follow all the &quot;how to ask a question&quot; guidelines and hopefully my question will be clear.</p>
<p>I am trying to find the change of slope in a time series. I used the <a href="https://pypi.org/project/ruptures/" rel="nofollow noreferrer">ruptures</a> (for 2 break points using <code>model = rpt.Dynp(model=&quot;l1&quot;</code>) and this is what I got (please see the attached image). <a href="https://i.sstatic.net/7IIrB.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/7IIrB.jpg" alt="enter image description here" /></a></p>
<p>I am not happy with the break point location. Based on my visual inspection of the data, I feel the break-point is earlier than what I am getting. I wanted to do a &quot;Two-phase linear regression&quot; to check my results. I found that there is a function available in <a href="https://www.mathworks.com/matlabcentral/fileexchange/26804-two-phase-linear-regression-model" rel="nofollow noreferrer">Matlab</a>. I am wondering if there is any python package available to do the same thing what this Matlab function does, because I know nothing about Matlab.</p>
<p>Thank you in advance for your inputs.</p>
<p>[If anyone is wondering what happens if I try to find 3 or 4 break-points in my data, instead of looking for 2 break-points, then please see the images below. I am still not happy with the first change point location.]
<a href="https://i.sstatic.net/3hvsH.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/3hvsH.jpg" alt="enter image description here" /></a>
<a href="https://i.sstatic.net/wK4Te.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/wK4Te.jpg" alt="enter image description here" /></a></p>

<python><climate-data>
Two-phase linear regression model in Python
Bidyut Bikash Goswami
# Two-phase linear regression model in Python

(Asked by: username_0 on 2022-10-25)

This is my first post. I am trying my best to follow all the "how to ask a question" guidelines and hopefully my question will be clear.

I am trying to find the change of slope in a time series. I used the [ruptures](https://pypi.org/project/ruptures/) (for 2 break points using `model = rpt.Dynp(model="l1"`) and this is what I got (please see the attached image). [![enter image description here](https://i.sstatic.net/7IIrB.jpg)](https://i.sstatic.net/7IIrB.jpg)

I am not happy with the break point location. Based on my visual inspection of the data, I feel the break-point is earlier than what I am getting. I wanted to do a "Two-phase linear regression" to check my results. I found that there is a function available in [Matlab](https://www.mathworks.com/matlabcentral/fileexchange/26804-two-phase-linear-regression-model). I am wondering if there is any python package available to do the same thing what this Matlab function does, because I know nothing about Matlab.

Thank you in advance for your inputs.

[If anyone is wondering what happens if I try to find 3 or 4 break-points in my data, instead of looking for 2 break-points, then please see the images below. I am still not happy with the first change point location.]
[![enter image description here](https://i.sstatic.net/3hvsH.jpg)](https://i.sstatic.net/3hvsH.jpg)
[![enter image description here](https://i.sstatic.net/wK4Te.jpg)](https://i.sstatic.net/wK4Te.jpg)

**Tags:** python, climate-data


---

## 1 answers

### Answer by username_1 on 2022-10-26. Score: 1

> I wanted to do a "Two-phase linear regression" to check my results.

You aren't doing that. By specifying `model="l1"`, you are asking the package to use a piecewise constant model (rather than a linear regression) for each segment. This models each segment as the mean of all the elements in that segment. The error is calculated as the sum of the absolute values of the difference between each element and the mean (the $L^1$ norm). Apparently asking for two breakpoints means one internal breakpoint, or two segments that collectively span the dataset.

That is not a good model of your dataset, and it is not a linear regression.

> **Comments:**
> **username_0**: Thank you username_1 for your reply and nicely explaining why "That is not a good model of your dataset". I am aware that what I am doing is not linear-regression.  What I meant to say is, I want to do Two-phase linear regression because I am not happy with the result of what I am doing and I am looking for a python package that can do a Two-phase linear regression. (probably it was not clear in my question).  
> **username_1**: The package in question (ruptures) could probably do what you want as well. It has a custom model option.  

---

