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.
 A: 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')


