I am trying to fit some time series data to a smoothing spline in R. However, it seems like the spline is fitting the data too perfectly, meaning overfitting. I was trying to figure out what settings to change to try and adjust the level of smoothing. I don't want to manually set the $\lambda$ parameter, because it seems like that should automatically be set according to some metric. I believe the default is generalized cross-validation, so that should work okay.
Here is some data and code. Can anyone tell me the correct way to apply the splines to the dataset.
I can manually change the number of Knots, but that seems a bit manual. I have a number of datasets to fit, so I don't want to manually fit the number of knots each time. Is there a better way to determine the penalty. I suppose having a twice differentiable function is optimal, so no sharp edges.
library(npreg)
y <- c(23.0, 27.0, 25.0, 25.0, 25.0, 22.0, 22.0, 21.0, 18.0, 16.0, 17.0, 17.0, 19.0, 19.0, 19.0, 20.0, 19.0, 18.0, 20.0, 19.0, 17.0, 21.0, 20.0, 16.0, 15.0, 16.0, 14.0, 14.0, 12.0, 14.0)
x <- c(1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008)
mod <- ss(x, y)
pred_y <-predict(mod, x)$y
plot(x,y)
lines(x, pred_y, lwd=2)
The corresponding picture is.