First of all, apologies but my stats knowledge is limited and based on very isolated topics! From what I understand, regression splines are used when the covariates "aren't linear". What does that mean? Interaction of the covariate with what needs to be linear (if you could provide an example based on survival models in R it would be great)? I understand that the "regression splines" smooth the interaction of the covariate with something over knots and that they're smooth at the joints- but when to use them, how to pick the number of knots (because the position doesn't matter in cubic splines?) and what's the overall impact on a model?

I feel like I'm missing something really important so apologies if i completely misinterpreted their use!


1 Answer 1


Frank Harrell explains the principles in Chapter 2 of Regression Modeling Strategies. The "nonlinearity" in question is the association between outcome and the continuous predictor in ordinary least squares, between a function of outcome and the predictor in a generalized linear model, and between the log-hazard of an event and the predictor in Cox models.

For the number of knots, decide beforehand how many degrees of freedom that you want to devote to the predictor, and choose the number of knots accordingly. After that choice, you typically place them at evenly spaced quantiles of the distribution of the predictor unless you suspect there is some range of the predictor where the association changes rapidly. I prefer having the outermost knots somewhat within the extreme values, which is the default in the rcs() function in the rms package but not in the basic R splines::ns() function.

Chapter 21 illustrates the application of restricted cubic splines to a Cox survival model.

There are other ways to model a flexible association between (a function of) outcome and continuous predictors, summarized and compared on this page.

  • $\begingroup$ great thank you! Could you also explain how to predict using a model that was fitted with these splines? do i have to transform the test set somehow? $\endgroup$
    – Wojty
    May 2, 2023 at 17:14
  • $\begingroup$ @Wojty you should be able to get predictions directly from the model, as the software should set up a set of coefficients related to the spline fit of the continuous predictor. This is done very simply if you work within the rms package, as it includes both an rcs() function for setting up the spline and prediction functions designed to work with it. $\endgroup$
    – EdM
    May 2, 2023 at 17:32
  • $\begingroup$ thanks @EdM! unfortunately rms's rcs() didn't work for me when predicting (error message error in subtractoffset(new_x, covarOffset) : ncol(new_x) != ncol(offset)), so I used splines::bs() instead $\endgroup$
    – Wojty
    May 3, 2023 at 13:53
  • $\begingroup$ @Wojty the rcs() function works most reliably in modeling within the rms package. There can be problems when you try to use it with other packages. $\endgroup$
    – EdM
    May 5, 2023 at 12:34

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.