# Calculation of natural cubic splines in R

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!

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.

• Do these show how to calculate the new variables from new data (i.e what predict() is doing)? Apr 8, 2011 at 13:07
• @B_Miner: yes, the SO link does.
– nico
Apr 8, 2011 at 13:09
• Why the downvote?
– nico
Apr 8, 2011 at 14:25
• Sorry Nico, I went to click on the other button! It wont let me change it. I will try to edit your answer ever so slightly and then vote it up. I am still trying to figure out how exactly to use compute the variables, seems there must be a simple formula that I am just not seeing in the SO post. Apr 8, 2011 at 17:07
• @B_Miner: don't worry, no problem. PS: depending on the type/amount of data you have, you may want to have a look at LOESS regression.
– nico
Apr 9, 2011 at 6:20

I learnt about the use of splines in regression from the book "Regression Modeling Strategies" by Frank Harrell. Harrell's R package rms allows you to easily fit regression models in which some predictor variables are represented as splines.