In order to fit a smoothing spline, we use the smooth.spline()
function.
This figure was produced with the following code:
plot(age, wage, xlim = agelims, cex = .5, col = "darkgrey") title("Smoothing Spline") fit <- smooth.spline(age, wage, df = 16) fit2 <- smooth.spline(age, wage, cv = TRUE) fit2$df [1] 6.794596 lines(fit, col = "red", lwd = 2) lines(fit2, col = "blue", lwd = 2) legend("topright", legend = c("16 DF", "6.8 DF"), col = c("red", "blue"), lty = 1, lwd = 2, cex = .8)
Notice that in the first call to smooth.spline()
, we specified df=16
. The
function then determines which value of smooth.spline()
, we select the smoothness level by cross-validation;
this results in a value of
medv
as dependent variable and rm
as independent variable.
Determine the optimal degrees of freedom with cross-validation.
Store the result in the variable fit
. (note: please ignore the warning about non-unique ‘x’ values)df.cv
and do not hardcode the value.Assume that: