Search code examples
rfunctiondplyrspline

Identify position of knots in a spline function


I have a linear regression model of y on x, with x coded using a natural spline function with 6 equally spaced knots. How can I find out the values of x corresponding to the knot positions?

library(splines)
data <- data.frame(y = rnorm(500,100:200), x = rnorm(500,5:40))
lm(y ~ ns(x, df = 7), data = data)

Solution

  • The ns function itself calculates the knot position and stores it as an attribute in the return value, so if you have:

    library(splines)
    set.seed(1)
    
    data  <- data.frame(y = rnorm(500, 100:200), x = rnorm(500, 5:40))
    

    You can get the spline knots like this:

    attr(ns(data$x, df = 7), "knots")
    #> 14.28571% 28.57143% 42.85714% 57.14286% 71.42857% 85.71429% 
    #>  9.572589 14.592410 19.936425 24.812394 29.970179 35.084943
    

    The numeric value tells you the knot positions, with the labels showing the "septiles" since you used df = 7

    If you want a plain numeric vector of the knots (including the boundaries) you can do:

    s <- ns(data$x, df = 7)
    as.numeric(sort(c(attr(s, "Boundary.knots"), attr(s, "knots"))))
    #> [1] 3.597769 9.572589 14.592410 19.936425 24.812394 29.970179 35.08494 40.91534