简体   繁体   中英

How to determine the location of knots of the restricted cubic spline produced by rcs() of the RMS package?

I am developing a prediction model in R. It uses the restricted cubic spline of an important continuous predictor that is a priori likely to have a nonlinear relationship to the outcome. To do this I used rms::rcs() and specified the number of knots, but allowed rcs() to 'decide' the location.

I want to extract the coefficients for all the predictors to use in an external application, the purpose of which is to predict Y given new input data. However, to do this I need to be able to find the location of the knots that were used by rcs() .

The relevant code within rcs() is

   if (!length(knots)) {
    xd <- rcspline.eval(x, nk = nknots, inclx = TRUE, pc = pc, 
        fractied = fractied)
    knots <- attr(xd, "knots")
}

In my case, pc == 0 and fractied == 0.05

How can I find the location of the knots?

As hinted in the question, the one possible way to accomplish this is to run rcspline.evel() with the predictor and relevant parameters set to those that were used in the model fitting procedure. An example would be

rcspline.eval(
     data.frame$predictor,
     nk = number_of_knots_used,
     inclx = TRUE,
     pc = 0, # what ever was used
     fractied = 0.05 # what ever was used
 )

The output of this contains attr(, "knots") which gives the location of the knots.

一种不同且更好的方法是内置的Function()函数,它提供拟合模型的方程以及所有节点位置。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM