Load Libraries

The primary R function for fitting smoothing splines is smooth.spline available in the splines library.

library(splines)
help(smooth.spline)
options(digits = 4)

A Simulated Example

This is a simulated example from ESL Chapter 5.5.2. The true function is

\[ f(x) = \frac{\sin (12(x + 0.2))}{x+0.2}, \quad x \in [0, 1]. \] A graphical representation shows the true function as a grey curve, with observed data points shown as red dots.

set.seed(1234)
n = 30 
err = 1
x = sort(runif(n))
y = sin(12*(x+0.2))/(x+0.2) + rnorm(n, 0, err);
plot(x, y, col="red");

fx = 1:50/50;
fy = sin(12*(fx+0.2))/(fx+0.2)
lines(fx, fy, col=8, lwd=2);