Solved – Calculation of natural cubic splines in R

rsplines

I am new to the use of cubic splines for regression purposes and wanted to find out

1) What is a good source (besides ESL which I read but am still uncertain) to learn about splines for regression?
2) How would you calculate the basis of a given natural cubic spline solution on new data? Specifically if one were to do the following:

data(iris)
colnames(iris)
Sepal.Length.ns<-ns(iris$Sepal.Length,df=5)
Sepal.Length.ns

How would you take the information in Sepal.Length.ns (knots, boundaries) and compute the values for a new observation? The reason is to code this process outside of R, once fit in R initially (i.e. to put a regression model using cubic splines into a production system).

For example I can do this in R, but want to understand the calculation:

#three new observations to predict
newVector<-c(4.45,3.35,2.2)
pred.new<-predict(Sepal.Length.ns,newVector)

Thanks!

Best Answer

Wikipedia has a nice explanation of spline interpolation

I posted the code to create cubic Bezier splines on Rosettacode a while ago.

Also, you can have a look at this discussion on SO about spline extrapolation.