将拟合回归样条(由 'bs' 或 'ns' 构造)导出为分段多项式
Export fitted regression splines (constructed by 'bs' or 'ns') as piecewise polynomials
以下面的一结二阶样条为例:
library(splines)
library(ISLR)
fit.spline <- lm(wage~bs(age, knots=c(42), degree=2), data=Wage)
summary(fit.spline)
我看到了出乎我意料的估计值。
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 57.349 3.950 14.518 < 2e-16 ***
bs(age, knots = c(42), degree = 2)1 59.511 5.786 10.285 < 2e-16 ***
bs(age, knots = c(42), degree = 2)2 65.722 4.076 16.122 < 2e-16 ***
bs(age, knots = c(42), degree = 2)3 37.170 9.722 3.823 0.000134 ***
有没有办法提取结前后的二次模型(及其系数)?即如何提取age = 42
切点前后的两个二次模型?
使用 summary(fit.spline)
产生系数,但(据我所知)它们对解释没有意义。
我经常被要求将原始答案中的想法包装成一个用户友好的函数,能够用 bs
或 ns
项重新参数化拟合线性或广义线性模型。最终我在 https://github.com/ZheyuanLi/SplinesUtils 推出了一个小的 R 包 SplinesUtils
(带有 PDF 版本的包手册)。您可以通过
安装它
## make sure you have the `devtools` package avaiable
devtools::install_github("ZheyuanLi/SplinesUtils")
这里要用到的函数是RegBsplineAsPiecePoly
.
library(SplinesUtils)
library(splines)
library(ISLR)
fit.spline <- lm(wage ~ bs(age, knots=c(42), degree=2), data = Wage)
ans1 <- RegBsplineAsPiecePoly(fit.spline, "bs(age, knots = c(42), degree = 2)")
ans1
#2 piecewise polynomials of degree 2 are constructed!
#Use 'summary' to export all of them.
#The first 2 are printed below.
#8.2e-15 + 4.96 * (x - 18) + 0.0991 * (x - 18) ^ 2
#61.9 + 0.2 * (x - 42) + 0.0224 * (x - 42) ^ 2
## coefficients as a matrix
ans1$PiecePoly$coef
# [,1] [,2]
#[1,] 8.204641e-15 61.91542748
#[2,] 4.959286e+00 0.20033307
#[3,] -9.914485e-02 -0.02240887
## knots
ans1$knots
#[1] 18 42 80
该函数默认以移位形式参数化分段多项式(参见 ?PiecePoly
)。您可以为非移动版本设置 shift = FALSE
。
ans2 <- RegBsplineAsPiecePoly(fit.spline, "bs(age, knots = c(42), degree = 2)",
shift = FALSE)
ans2
#2 piecewise polynomials of degree 2 are constructed!
#Use 'summary' to export all of them.
#The first 2 are printed below.
#-121 + 8.53 * x + 0.0991 * x ^ 2
#14 + 2.08 * x + 0.0224 * x ^ 2
## coefficients as a matrix
ans2$PiecePoly$coef
# [,1] [,2]
#[1,] -121.39007747 13.97219046
#[2,] 8.52850050 2.08267822
#[3,] -0.09914485 -0.02240887
您可以使用 predict
.
预测样条曲线
xg <- 18:80
yg1 <- predict(ans1, xg) ## use shifted form
yg2 <- predict(ans2, xg) ## use non-shifted form
all.equal(yg1, yg2)
#[1] TRUE
但是由于模型中存在截距,因此预测值与截距的模型预测值会有所不同。
yh <- predict(fit.spline, data.frame(age = xg))
intercept <- coef(fit.spline)[[1]]
all.equal(yh, yg1 + intercept, check.attributes = FALSE)
#[1] TRUE
该包具有“PiecePoly”class 的 summary
、print
、plot
、predict
和 solve
方法。浏览套餐了解更多。
以下面的一结二阶样条为例:
library(splines)
library(ISLR)
fit.spline <- lm(wage~bs(age, knots=c(42), degree=2), data=Wage)
summary(fit.spline)
我看到了出乎我意料的估计值。
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 57.349 3.950 14.518 < 2e-16 ***
bs(age, knots = c(42), degree = 2)1 59.511 5.786 10.285 < 2e-16 ***
bs(age, knots = c(42), degree = 2)2 65.722 4.076 16.122 < 2e-16 ***
bs(age, knots = c(42), degree = 2)3 37.170 9.722 3.823 0.000134 ***
有没有办法提取结前后的二次模型(及其系数)?即如何提取age = 42
切点前后的两个二次模型?
使用 summary(fit.spline)
产生系数,但(据我所知)它们对解释没有意义。
我经常被要求将原始答案中的想法包装成一个用户友好的函数,能够用 bs
或 ns
项重新参数化拟合线性或广义线性模型。最终我在 https://github.com/ZheyuanLi/SplinesUtils 推出了一个小的 R 包 SplinesUtils
(带有 PDF 版本的包手册)。您可以通过
## make sure you have the `devtools` package avaiable
devtools::install_github("ZheyuanLi/SplinesUtils")
这里要用到的函数是RegBsplineAsPiecePoly
.
library(SplinesUtils)
library(splines)
library(ISLR)
fit.spline <- lm(wage ~ bs(age, knots=c(42), degree=2), data = Wage)
ans1 <- RegBsplineAsPiecePoly(fit.spline, "bs(age, knots = c(42), degree = 2)")
ans1
#2 piecewise polynomials of degree 2 are constructed!
#Use 'summary' to export all of them.
#The first 2 are printed below.
#8.2e-15 + 4.96 * (x - 18) + 0.0991 * (x - 18) ^ 2
#61.9 + 0.2 * (x - 42) + 0.0224 * (x - 42) ^ 2
## coefficients as a matrix
ans1$PiecePoly$coef
# [,1] [,2]
#[1,] 8.204641e-15 61.91542748
#[2,] 4.959286e+00 0.20033307
#[3,] -9.914485e-02 -0.02240887
## knots
ans1$knots
#[1] 18 42 80
该函数默认以移位形式参数化分段多项式(参见 ?PiecePoly
)。您可以为非移动版本设置 shift = FALSE
。
ans2 <- RegBsplineAsPiecePoly(fit.spline, "bs(age, knots = c(42), degree = 2)",
shift = FALSE)
ans2
#2 piecewise polynomials of degree 2 are constructed!
#Use 'summary' to export all of them.
#The first 2 are printed below.
#-121 + 8.53 * x + 0.0991 * x ^ 2
#14 + 2.08 * x + 0.0224 * x ^ 2
## coefficients as a matrix
ans2$PiecePoly$coef
# [,1] [,2]
#[1,] -121.39007747 13.97219046
#[2,] 8.52850050 2.08267822
#[3,] -0.09914485 -0.02240887
您可以使用 predict
.
xg <- 18:80
yg1 <- predict(ans1, xg) ## use shifted form
yg2 <- predict(ans2, xg) ## use non-shifted form
all.equal(yg1, yg2)
#[1] TRUE
但是由于模型中存在截距,因此预测值与截距的模型预测值会有所不同。
yh <- predict(fit.spline, data.frame(age = xg))
intercept <- coef(fit.spline)[[1]]
all.equal(yh, yg1 + intercept, check.attributes = FALSE)
#[1] TRUE
该包具有“PiecePoly”class 的 summary
、print
、plot
、predict
和 solve
方法。浏览套餐了解更多。