# Piecewise linear regression with automatic knot selection

**URL:** https://discourse.julialang.org/t/piecewise-linear-regression-with-automatic-knot-selection/93984
**Category:** Statistics
**Tags:** question, linear-regression, piecewise
**Created:** [February 3, 2023, 11:20am UTC](https://discourse.julialang.org/t/piecewise-linear-regression-with-automatic-knot-selection/93984 "2023-02-03T11:20:36Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![meghapatnaik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/meghapatnaik/32/45479_2.png) [@meghapatnaik](https://discourse.julialang.org/u/meghapatnaik)
#### Post date: [February 3, 2023, 11:20am UTC](https://discourse.julialang.org/t/piecewise-linear-regression-with-automatic-knot-selection/93984/1 "2023-02-03T11:20:36Z")

</div>

Hi everyone,

I am new to Julia, mostly work with R. I am looking for the equivalent of the “segmented” package in R, to fit a piecewise linear (constant in my special case) regression where the knot location is automated. Does this functionality exist in Julia?

Thanks in advance

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [February 3, 2023, 11:22am UTC](https://discourse.julialang.org/t/piecewise-linear-regression-with-automatic-knot-selection/93984/2 "2023-02-03T11:22:50Z")

</div>

Hello and welcome to the community 🙂

Would [EllZeroTrendFiltering.jl](https://github.com/mfalt/EllZeroTrendFiltering.jl) perhaps fit your bill? I do not think that this package fits the regression models for you, but it solves the most difficult problem, finding the optimal knot points by solving a dynamic-programming problem. With the knot points determined, it should be straightforward to use any of the standard packages for linear regression to fit the models.

---

<div class="post-metadata">

### Author: ![meghapatnaik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/meghapatnaik/32/45479_2.png) [@meghapatnaik](https://discourse.julialang.org/u/meghapatnaik)
#### Post date: [February 3, 2023, 3:05pm UTC](https://discourse.julialang.org/t/piecewise-linear-regression-with-automatic-knot-selection/93984/3 "2023-02-03T15:05:29Z")

</div>

Thank you @baggepinnen for a perfect warm welcome! That is super helpful, it got me thinking. I am concerned that the optimal knot locations that emerge for a piecewise constant function may be not be same as the knots for a piecewise linear function. I am wondering if the recursive problem in the piecewise constant case is identical to fitting a 1-D regression tree.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [February 3, 2023, 3:32pm UTC](https://discourse.julialang.org/t/piecewise-linear-regression-with-automatic-knot-selection/93984/4 "2023-02-03T15:32:41Z")

</div>

The package should find you a piecewise linear function, from the readme

> We want to find a piecewise linear, continuous function

---

<div class="post-metadata">

### Author: ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)
#### Post date: [February 3, 2023, 5:29pm UTC](https://discourse.julialang.org/t/piecewise-linear-regression-with-automatic-knot-selection/93984/5 "2023-02-03T17:29:29Z")

</div>

Related: If you first perform an ordinary regression which returns a function for evaluating your regression model, then the problem reduces to finding a piecewise linear approximation to that function. [This](https://discourse.julialang.org/t/hi-i-have-a-function-lets-say-f-sin-0-10-now-i-want-to-break/54826) thread contains several methods for performing adaptive piecewise linear interpolation of an arbitrary function to a user-specified error tolerance.

---

<div class="post-metadata">

### Author: ![ParadaCarleton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paradacarleton/32/20005_2.png) [@ParadaCarleton](https://discourse.julialang.org/u/ParadaCarleton)
#### Post date: [February 8, 2023, 11:16pm UTC](https://discourse.julialang.org/t/piecewise-linear-regression-with-automatic-knot-selection/93984/6 "2023-02-08T23:16:49Z")

</div>

Quick question–is there a reason your function has to be piecewise-linear? Smooth approaches like LOESS are almost always better.

---

<div class="post-metadata">

### Author: ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)
#### Post date: [February 9, 2023, 10:40am UTC](https://discourse.julialang.org/t/piecewise-linear-regression-with-automatic-knot-selection/93984/10 "2023-02-09T10:40:49Z")

</div>

[GitHub - iuliancioarca/AdaptiveSampling.jl: Adaptive sampling rate algorithms for waveform compression](https://github.com/iuliancioarca/AdaptiveSampling.jl) My simplistic approach.

---

<div class="post-metadata">

### Author: ![Elmo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmo/32/17979_2.png) [@Elmo](https://discourse.julialang.org/u/Elmo)
#### Post date: [July 6, 2023, 5:11pm UTC](https://discourse.julialang.org/t/piecewise-linear-regression-with-automatic-knot-selection/93984/11 "2023-07-06T17:11:21Z")

</div>

For what its worth, I made this package that explicitly does what you want: [GitHub - stelmo/LinearSegmentation.jl: Linear segmentation](https://github.com/stelmo/LinearSegmentation.jl)

---

<div class="post-metadata">

### Author: ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)
#### Post date: [January 2, 2024, 7:56pm UTC](https://discourse.julialang.org/t/piecewise-linear-regression-with-automatic-knot-selection/93984/12 "2024-01-02T19:56:04Z")

</div>

> [@Iulian.Cioarca](#):
>
> [GitHub - iuliancioarca/AdaptiveSampling.jl: Adaptive sampling rate algorithms for waveform compression](https://github.com/iuliancioarca/AdaptiveSampling.jl) My simplistic approach.

Could you explain the method and how to use it in the above context?

There is a simple method based on a proximal gradient method which is described by [Adaptive Piecewise Polynomial Estimation via Trend Filtering](https://arxiv.org/abs/1304.2986).

---

<div class="post-metadata">

### Author: ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)
#### Post date: [January 2, 2024, 8:42pm UTC](https://discourse.julialang.org/t/piecewise-linear-regression-with-automatic-knot-selection/93984/13 "2024-01-02T20:42:04Z")

</div>

I have some code for doing linear regressions where the knots are also estimated - all coded up as a NLS/GMM problem. See section 12.2 of these notes (I could probably share the code, if wanted.)

> **[EmpFinPhDAll.pdf](https://drive.google.com/file/d/1k8PmkdVXAA_NR3YD5sdpL4pbXH1-DhAh/view)**
>
> Google Drive file.
