识别样条函数中节点的位置
Identify position of knots in a spline function
我在 x
上有一个 y
的线性回归模型,x
使用具有 6 个等间距节点的自然样条函数编码。如何找出结点位置对应的 x
的值?
library(splines)
data <- data.frame(y = rnorm(500,100:200), x = rnorm(500,5:40))
lm(y ~ ns(x, df = 7), data = data)
ns
函数本身计算节点位置并将其作为属性存储在 return 值中,因此如果您有:
library(splines)
set.seed(1)
data <- data.frame(y = rnorm(500, 100:200), x = rnorm(500, 5:40))
你可以得到这样的样条结:
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
数值告诉您节点位置,标签显示“七分位数”,因为您使用了 df = 7
如果你想要结(包括边界)的纯数字向量,你可以这样做:
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
我在 x
上有一个 y
的线性回归模型,x
使用具有 6 个等间距节点的自然样条函数编码。如何找出结点位置对应的 x
的值?
library(splines)
data <- data.frame(y = rnorm(500,100:200), x = rnorm(500,5:40))
lm(y ~ ns(x, df = 7), data = data)
ns
函数本身计算节点位置并将其作为属性存储在 return 值中,因此如果您有:
library(splines)
set.seed(1)
data <- data.frame(y = rnorm(500, 100:200), x = rnorm(500, 5:40))
你可以得到这样的样条结:
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
数值告诉您节点位置,标签显示“七分位数”,因为您使用了 df = 7
如果你想要结(包括边界)的纯数字向量,你可以这样做:
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