[ANN] LinearSegmentation.jl: segmented linear regression

I have made a small package that does segmented linear regression, i.e. it fits piecewise linear functions to data and automatically identifies the optimal break points, see the readme for more details: LinearSegmentation.jl.

In short, suppose you have some data and you would like to fit linear models to it. Currently GLM.jl is your best bet, but it does not fit piecewise linear models to data. For the example below, you would need to manually segment the data and then call GLM.jl on each segment.

A better approach is to automatically find these segments based on some user criterion. LinearSegmentation exposes 3 methods for doing so: a sliding window, recursive, or graph-based approach. For example, using the graph-based approach, four segments are identified that optimally segment the data:

The application I use this package for is to automatically segment biological growth curves into phases of different growth rates, i.e. a more principled way to calculate growth rates from plate reader data. The methods are relatively efficient/fast and easily handle 1000s of datapoints.


This is very interesting! We have been using something similar in our FractalDimensions.jl package: FractalDimensions.jl · FractalDimensions.jl

like in your case, we want to decompose a curve y(x) into segments with different slopes and we want to keep the segment that covers most of the data. Just recently I’ve developed an extendable API that allows for different ways of fitting the slope (FractalDimensions.jl · FractalDimensions.jl) and Ill try to add some methods from your package in the future!

thanks for sharing!

1 Like

Cool package @Elmo , does it work in 2D as well?

Hi there! By 2D, I am assuming you mean something like 2 dependent variables and 1 independent variable? If so, then no, it only handles data like (x1, y1), (x2, y2),.... Extending it to other dimensions should be possible though…

1 Like

Ah, I did not know about your application! Sounds really interesting. Let me know if you need help understanding my code :slight_smile:

You should add an option for R2 score.
Since it is normalized it will be much easier and robust to tweak with.

Interesting package, it got me thinking. I do something slightly different using a smoothed cubic spline (or B-spline), where automatic knot placement is of interest.

Think for example of wanting a smoothed spline of the rotational speed of a racing engine. The change in engine speed is a function of the inertial effects and engine power output; but then there can occur sudden changes as when a gearshift occurs, or perhaps loss of traction of a tire. Then it would be of interest to have an automatic knot placement algorithm to place more knots at this location. It may work to use your technique to inform where more knots should be placed.

1 Like

Sounds like you would be interested in Dierckx.jl which can generate smoothed splines with automatically chosen knot locations.

1 Like