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>
... first of all you must to do it by iterations, and under some informative criterion, like AIC AICc BIC Cp; because you can get an "ideal" fit, if number of knots K = number od data points N, ok. 
... first put K = 0; estimate L = K + 1 regressions, calculate AICc, for instance; 
then assume minimal number of data points at a separate segment, say L = 3 or L = 4, ok
... put K = 1; start from L-th data as the first knot, calculate SS or MLE, ... and step by step the next data point as a knot, SS or MLE, up to the last knot at the N - L data; choose the arrangement with the best fit (SS or MLE) calculate AICc ... 
... put K = 2; ... use all previous regressions (that is their SS or MLE), but step by step divide a single segment into all possible parts ... choose the arrangement with the best fit (SS or MLE) calculate AICc ... if the last AICc occurs greater then the previous one: stop the iterations ! This is an optimal solution under AICc criterion, ok