如何从R中拟合线性b样条回归中提取基础系数?

时间:2015-04-07 19:28:14

标签: r regression bspline

例如以下单结,第一级,样条:

library(splines)
library(ISLR)

age.grid    = seq(range(Wage$age)[1], range(Wage$age)[2])
fit.spline  = lm(wage~bs(age, knots=c(30), degree=1), data=Wage)
pred.spline = predict(fit.spline, newdata=list(age=age.grid), se=T)

plot(Wage$age, Wage$wage, col="gray")
lines(age.grid, pred.spline$fit, col="red")

# NOTE: This is **NOT** the same as fitting two piece-wise linear models becase
# the spline will add the contraint that the function is continuous at age=30
# fit.1  = lm(wage~age, data=subset(Wage,age<30))
# fit.2  = lm(wage~age, data=subset(Wage,age>=30))

Spline Plot

有没有办法在结之前和之后提取线性模型(及其系数)?也就是说,如何在age=30

的切割点之前和之后提取两个线性模型

使用summary(fit.spline)会产生系数,但(根据我的理解)它们对解释没有意义。

2 个答案:

答案 0 :(得分:1)

您可以从fit.spline手动提取系数

summary(fit.spline)

Call:
lm(formula = wage ~ bs(age, knots = 30, degree = 1), data = Wage)
Coefficients:
                                 Estimate Std. Error t value Pr(>|t|)    
(Intercept)                         54.19       4.05    13.4   <2e-16 ***
bs(age, knots = 30, degree = 1)1    58.43       4.61    12.7   <2e-16 ***
bs(age, knots = 30, degree = 1)2    68.73       4.54    15.1   <2e-16 ***
---

range(Wage$age)
## [1] 18 80
## coefficients of the first model
a1 <- seq(18, 30, length.out = 10)
b1 <- seq(54.19, 58.43+54.19, length.out = 10)
## coefficients of the second model
a2 <- seq(30, 80, length.out = 10)
b2 <- seq(54.19 + 58.43, 54.19 + 68.73, length.out = 10)
plot(Wage$age, Wage$wage, col="gray", xlim = c(0, 90))
lines(x = a1, y = b1, col = "blue" )
lines(x = a2, y = b2, col = "red")

如果你想要线性模型中的斜率系数,那么你可以简单地使用

b1 <- (58.43)/(30 - 18)
b2 <- (68.73 - 58.43)/(80 - 30)

请注意,在fit.spline中,截距表示wageage = 18的值,而在线性模型中,截距表示wage时的值age = 0

答案 1 :(得分:0)

当您在bspline回归中预先指定自由度时,主要完成提取结。例如:

  

fit.spline = lm(工资~bs(年龄,df = 5),数据=工资)

     

ATTR(BS(年龄,DF = 5)&#34;节&#34)

     
    

33.33333%66.66667%

  37        48
  

可以在第293页的ISLR书(您似乎正在使用)中找到一个示例。