I was looking for the difference between spline regression and piecewise regression. Can someone please explain it to me? Also, if someone can provide me good reference on these 2 topics, with implementation in either R or Python, that would be very helpful.


1 Answer 1


Here are some of the differences

  • Piecewise regression yields continuous functions which are not, generally, differentiable and hence not smooth.
  • Regression with splines yields smooth continuous functions. The degree of smoothness will depend on what kind of spline you use, but cubic splines are differentiable at least twice.

Good references include Frank Harrell's Regression Modelling Strategies. Libraries in R include {splines} or the {rms} library. In particular, the {splines} library can expand predictors into a linear and cubic spline basis through use of the degree argument in the bs function. It is worthwhile to note that piecewise regression is just spline regression where the basis functions are linear polynomials as opposed to cubic or restricted cubic polynomials.

Here is an example of using the splines library.


x <- runif(100)
y <- sin(2*pi*x) + rnorm(100, 0, 0.3)

xx <- seq(0, 1, 0.01)

fit1 <- lm(y~bs(x, degree=1, df=4))
fit2 <- lm(y~bs(x, degree=3, df=4))

plot(x, y, pch=19)
lines(xx, predict(fit1, newdata=list(x=xx)), col='red')
lines(xx, predict(fit2, newdata=list(x=xx)), col='blue')

enter image description here

  • $\begingroup$ Thanks for your answer. Can you please confirm if using these 2 R packages can we do both piecewise and spline regression or does they support only spline regression? Thanks! $\endgroup$
    – ragas
    Commented Nov 10, 2022 at 14:57
  • 2
    $\begingroup$ @ragas both should be able to do spline and piecewise regression (since one is a special case of the other using a basis comprised of degree 1 polynomials). See my included example. $\endgroup$ Commented Nov 10, 2022 at 16:20
  • $\begingroup$ I think your answer should make this more explicit $\endgroup$
    – seanv507
    Commented Nov 10, 2022 at 16:23
  • $\begingroup$ Thanks Demetri! Your answer complete in every respect. $\endgroup$
    – ragas
    Commented Nov 10, 2022 at 17:53
  • $\begingroup$ @seanv507 I've added some clarity $\endgroup$ Commented Nov 10, 2022 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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