以下面的一结一阶样条为例:
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))
有没有办法提取结前后的线性模型(及其系数)?也就是说,如何提取切点前后的两个线性模型age=30
?
使用summary(fit.spline)
收益率系数,但(据我所知)它们对解释没有意义。