# Simple demo of splines

First generate some pretend data (this will look familiar)

set.seed(3)
true.curve = function(x) {sin(x)*cos(5*x)}
ylim = c(-2,2)
n = 100
x = sort(rnorm(n,1.5,.5))
y = true.curve(x)+rnorm(length(x),0,0.15)
plot(x,y,xlab="x",ylab=expression(f(x)+epsilon),ylim=ylim)

data = data.frame(x=x,y=y)

We can fit splines with smooth.spline.

• We can manually specify lambda with spar (actually specifies a monotone function of it)
• We can feed the data in as separate x and y arguments, or as a data frame with x and y columns

We can predict new values with predict.

• To see help file, do ?predict.smooth.spline
• Takes new x values as x argument
• Returns data frame with x and y elements (sorted)

Very low $$\lambda$$:

fit_rough = smooth.spline(data,spar = 0)
eval.grid = seq(from=min(data$x),to=max(data$x),length.out=1000)
plot(data, ylim=ylim)
lines(predict(fit_rough, x=eval.grid))

Very high $$\lambda$$:

fit_verysmooth = smooth.spline(data,spar=2)
plot(data, ylim=ylim)
lines(predict(fit_verysmooth, x=eval.grid))

Slightly lower, but still very high $$\lambda$$:

fit_smooth = smooth.spline(data,spar=1)
plot(data, ylim=ylim)
lines(predict(fit_smooth, x=eval.grid))