I have body mass and age data for a population of individuals. I want to fit a cubic smoothing spline curve to the data. I'm using smooth.spline in R, which warns against using cross-validation to select a smoothing parameter when there are duplicate points in x, which I have. I have seen the suggestion to set cv=FALSE in this case in order to trigger "generalized" cross validation. However, when I do that, it yields very 'bumpy' looking curves. When I set the smoothing parameter to close to 1, I end up with the type of smooth growth curve that I would expect, but I'm wondering if there is another procedure that I can use to estimate the smoothing parameter.
I would use the mgcv package for this, which fits the spline as a penalised regression problem. It can choose smoothness via GCV or, potentially more reliably, using ML or REML smoothness selection.
For a cubic spline, you need to select that type of basis when fitting the model:
mod <- gam(bodyMass ~ s(Age, bs = "cr"), data = mydata, method = "REML")
bs = "cr" selects a cubic regression spline basis. Use
method = "GCV.Cp" (or leave out the argument entirely as GCV smoothness selection is the default) to get GCV selection. The above code assumes
bodyMass is the variable on the y-axis, that the variables are named
Age and are located in data frame
To draw the fitted spline, the best option is to predict for a set of evenly spaced values over the interval of
pred <- with(mydata, data.frame(Age = seq(min(Age), max(Age), length = 100))) pred <- transform(pred, bodyMass = predict(mod, newdata = pred)) plot(bodyMass ~ Age, data = mydata) lines(bodyMass ~ Age, data = pred, col = "red", lwd = 2)