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: 
In general, it's a bit odd to want to fit something as piece-wise linear. However, if you really wish to do so, then the MARS algorithm is the most direct. It will build up a function one knot at a time; and then usually prunes back the number of knots to combat over-fitting ala decision trees. You can access the MARS algotithm in R via `earth` or `mda`. In general, it's fit with GCV which is not so far removed from the other information criterion (AIC, BIC etc.)

MARS won't really give you an "optimal" fit since the knots are grown one at a time. It really would be rather difficult to fit a truly "optimal" number of knots since the possible permutations of knot placements would quickly explode.

Generally, this is why people turn towards smoothing splines. Most smoothing splines are cubic just so you can fool a human eye into missing the discontinuities. It would be quite possible to do a linear smoothing spline however. The big advantage of smoothing splines are their single parameter to optimize. That allows you to quickly reach a truly "optimal" solution without having to search through gobs of permutations. However, if you really want to seek inflection points, and you have enough data to do so, then something like MARS would probably be your best bet.

Here's some example code for penalized linear smoothing splines in R:

```
require(mgcv);data(iris);
gam.test <- gam(Sepal.Length ~ s(Petal.Width,k=6,bs='ps',m=0),data=iris)
summary(gam.test);plot(gam.test);

```

The actual knots chosen would not necessarily correlate with any true inflection points however.
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>