cjekel / piecewise_linear_fit_py

fit piecewise linear data for a specified number of line segments
MIT License
300 stars 60 forks source link

Set Slope of Segment to 0 #107

Open markcdaley opened 1 year ago

markcdaley commented 1 year ago

Hello!

I was super excited to find this library as I have implemented the 'segmented' library in R, but was hoping to use it in Python. I have a set of data that I am trying to fit with two line segments. For the left segment, I want to force the slope to be zero so that the fit is horizontal in that one segment only. This is possible in R with the segmented library, but I can't figure out if this is possible with pwlf. Any chance this is easily implemented? I know this is similar to another question from August (#105), but I couldn't get that method to work. Thanks for any help you can provide!

For reference here is the R code and an attached image of the output. The slope of the right portion is not always similar under the slope constraints, just in this example.

image

Segmented_Example

cjekel commented 1 year ago

I never ended up merging this feature request for mixed degrees. Take a look at https://github.com/cjekel/piecewise_linear_fit_py/issues/41 using a degree_list=[0,1]. It was a rather small commit https://github.com/cjekel/piecewise_linear_fit_py/commit/ef58aaa93d1f07f643456bfdbc5f523ab751ee47 I just didn't get around to finishing the grunt work to make sure it didn't break any of the other behavior. Happy to accept a PR to get this in sooner.

markcdaley commented 1 year ago

Awesome, thanks, Charles, that seemed to work for my application! I'll see what comes up as I'm working with it!