The primary R function for fitting smoothing splines is
smooth.spline
available in the splines
library.
library(splines)
help(smooth.spline)
options(digits = 4)
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)
= 30
n = 1
err = sort(runif(n))
x = sin(12*(x+0.2))/(x+0.2) + rnorm(n, 0, err);
y plot(x, y, col="red");
= 1:50/50;
fx = sin(12*(fx+0.2))/(fx+0.2)
fy lines(fx, fy, col=8, lwd=2);