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.


1 Answer 1


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")

The 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 bodyMass and Age and are located in data frame mydata.

To draw the fitted spline, the best option is to predict for a set of evenly spaced values over the interval of Age.

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)
  • $\begingroup$ Thank you very much Gavin, your code worked perfectly. The resulting curve was smoother than the one I made using generalized cross-validation. Still slightly bumpy, but I'm guessing that just reflects my data, rather than any issue with the method. $\endgroup$
    – Erica
    Commented Sep 22, 2014 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.