# Difference between Spline and Piecewise Regression

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.

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.

library(splines)

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')


• 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! Commented Nov 10, 2022 at 14:57
• @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. Commented Nov 10, 2022 at 16:20
• I think your answer should make this more explicit Commented Nov 10, 2022 at 16:23
• Thanks Demetri! Your answer complete in every respect. Commented Nov 10, 2022 at 17:53
• @seanv507 I've added some clarity Commented Nov 10, 2022 at 18:38