Question: Are there any packages to do piecewise linear regression, which can detect the multiple knots automatically? Thanks.
When I use the strucchange package. I could not detect the change points. I have no idea how it detects the change points. From the plots, I could see there are several points I want it could help me to pick them out. Could anyone give an example here?

Answer: 
There is a pretty nice algorithm described in [Tomé and Miranda (1984)](http://idl.ul.pt/sites/idl.ul.pt/files/docs/Tome%26Miranda2004GRL.pdf). 

> 
> The proposed methodology uses a least-squares approach to compute the best continuous set of straight lines that fit a given time series, subject to a number of constraints on the minimum distance between breakpoints and on the minimum trend change at each breakpoint.
> 
> 
> 

The code and a GUI are available in both Fortran and IDL from their website: 
<http://www.dfisica.ubi.pt/~artome/linearstep.html>
In order to fit to data a piecewise function :

[![enter image description here](https://i.stack.imgur.com/iwv3f.gif)](https://i.stack.imgur.com/iwv3f.gif)

where $a\_1 , a\_2 , p\_1 , q\_1, p\_2 , q\_2 , p\_3 , q\_3$ are unknown parameters to be approximately computed, there is a very simple method (not iterative, no initial guess, easy to code in any math computer language). The theory given page 29 in paper : <https://fr.scribd.com/document/380941024/Regression-par-morceaux-Piecewise-Regression-pdf> and from page 30 :

[![enter image description here](https://i.stack.imgur.com/PDBCT.gif)](https://i.stack.imgur.com/PDBCT.gif)

For example, with the exact data provided by Mats Granvik the result is :

[![enter image description here](https://i.stack.imgur.com/u3vCk.gif)](https://i.stack.imgur.com/u3vCk.gif)

Without scattered data, this example is not very signifiant. Other examples with scattered data are shown in the referenced paper.