I would like to fit a piecewise linear regression with knots as parameters. I would like to know what's the best solution.

  • Should I run a set of regressions with all the possible knots and choosing the knots which could minimize an information criterion such as AIC (Akaike Information Criterion)?
  • If that's the best solution, how can I compute standard errors for my estimates?
  • $\begingroup$ The following paper by Ruppert (2002) might give you some help. D. Ruppert. Selecting the number of knots for penalized splines. Journal of Computational and Graphical Statistics, 11:735–757, 2002. $\endgroup$ – semibruin Jul 22 '13 at 17:24
  • $\begingroup$ @semibruin : My question is not about selecting the number of knots but the knots themselves. I would like an estimation procedure which would give me the points in the data where the slope of the line is changing. $\endgroup$ – PAC Aug 1 '13 at 10:08

Making the knots free parameters in the model turns the problem into a complex one not amenable to using standard estimation software. Computation of standard errors becomes very complex. Linear splines are very sensitive to where the knots are placed, and model "elbows" that are unlikely to be real unless $X=$ calendar time. Cubic splines have the advantages of (1) not having elbows because they have 3 orders of continuity, and (2) giving similar fits even if you move the knots around. Thus you can usually set knots based on quantiles of $X$ and not make knot estimation part of the optimization problem. Restricting the cubic regression splines to be linear in the tails (beyond the outer knots), called natural splines or restricted cubic splines, reduces the number of parameters to estimate and makes for more realistic fits.

This approach allows you to use standard estimation and hypothesis testing tools and does not require any special regression fitting functions, once you create the design matrix. Much more information is at Handouts under http://biostat.mc.vanderbilt.edu/CourseBios330. Once you fit the restricted cubic spline you can plot it along with confidence bands (which are obtained using standard methods also) and see slope changes. If you have special knowledge of regions of volatility you can put two knots closer together in that pre-specified region of $X$.


Frank Harell suggested interesting alternatives. There are cases where however one might be interested in estimating a piecewise linear model:

  • Interest in the knot location per se: knot location can represent a tipping point, discontinuity point, that one wants to know.
  • Reduced number of parameters

I assume here that you are interested in finding the location of the knots. This is known as segmented regression and threshold regression in some literature, which are general cases of the changepoint, structural break regressions (the 𝑋= calendar time in Frank answer). Note that in these models, the lines are not necessarily restricted to pass by the knots (i.e. you fit intercept and slope separately in each regime).

This literature answers your two questions:

  1. Estimation: this is done usually with non-linear least square (NLS). A simple algorithm is over a grid searching for every knot, then picking the one with lowest LS error. With multiple knots, this algorithm would require a 2D grid, 3D grid etc... which becomes infeasible, but luckily much more efficient solutions have been suggested (see Killick et al 2012 as one example). Several R packages allow this, for example segmented, or seglm

  2. An alternative estimation is to use a LASSO-like estimator using one coefficient for each observation, and penalizing difference between coefficients (i.e. use penalty $|\beta_k - \beta_{k-1}|$). Knots are the location where $\beta_k \neq \beta_{k-1}$. Advantage is that there exist efficient estimators in this case, see for example Tibshirani and Taylor (2011). This is furthermore implemented in R package genlasso.

  3. Inference Bad news is that inference is very complicated in these models (assuming there are a few large break points). See for example Hansen (1996, 2000)


  • Hansen, B. E., March 1996. Inference when a nuisance parameter is not identified under the null hypothesis. Econometrica 64 (2), 413–30.

  • Hansen, B. E., May 2000. Sample splitting and threshold estimation. Econo- metrica 68 (3), 575–604.

  • Killick, R., Fearnhead, P. and Eckley, I. A. (2012), ‘Optimal Detection of Changepoints with a Linear Computational Cost’, Journal of the American Statistical Association 107(500), 1590–1598.

  • Tibshirani, R. J., and Taylor, J. (2011), “The Solution Path of the Generalized Lasso,” Annals of Statistics, 39, 1335–1371. [843]


MARS (Multivariate Adaptive Regression splines) is yet another approach which might be closer to what you're aiming for. Here's a link to the original paper and a python library implementing it: https://github.com/scikit-learn-contrib/py-earth.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.