Load Libraries

Let’s delve into fitting a regression spline model in R. The foundational package we’ll use is splines, with two pivotal commands: bs and ns. These commands allow us to generate the design matrix for cubic splines and natural cubic splines respectively.

library(splines);
library(ggplot2)
help(bs)
help(ns)

Spline Basis Functions

To get a grasp on what the spline basis functions resemble, consider the scenario with five knots. This gives us a degree of freedom of nine, implying we should expect nine basis functions. These knots are equally spaced, and their locations are provided.

x = (1:199)/100;
n = length(x)
m = 5;
myknots = 2*(1:m)/(m+1)
myknots
## [1] 0.3333333 0.6666667 1.0000000 1.3333333 1.6666667

Among the nine basis functions referenced in the notes, the initial four represent the basis functions for global cubic polynomials: the intercept, x, x^2, and x^3. The latter five basis functions are truncated functions situated at each of the five knots. To the left of a given knot, the function remains at zero; to its right, it takes on a cubic form.

Visualizing these, the five knots appear as dashed lines representing the truncated cubic functions, while the solid lines denote the global cubic polynomial basis functions.

X = cbind(1, x, x^2, x^3);
for(i in 1:m){
    tmp = (x-myknots[i])^3;
    tmp[tmp<0] = 0;
    X = cbind(X, tmp);
    }
plot(c(0,2), range(X), type="n", xlab="", ylab="")
title("Truncated Power Basis")

for(i in 1:(m+4)){
    tmp = X[,i];
    if (i<=4) mylty=1 else mylty=2;
    lines(x[tmp!=0], tmp[tmp!=0], col=i, lty=mylty, lwd=2)
    }
for(i in 1:m){
    points(myknots[i], 0, pty="m", pch=19, cex=2)
  }