How to extract basic coefficients from a linear spline regression snap in R?

Take, for example, the following node, one, a spline:

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

Is there a way to extract a linear model (and its coefficients) before and after the node? That is, how can I extract two linear models before and after the cut point age=30?

Using summary(fit.spline)gives coefficients, but (as far as I know) they do not make sense for interpretation.

+4
source share
2 answers

You can extract the coefficients manually from fit.splineas shown

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")

If you want tilt coefficients, as in a linear model, you can simply use

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

, fit.spline wage, age = 18, wage, age = 0.

+1

, bspline. :

fit.spline = lm ( ~ bs (, df = 5), = )

( (, DF = 5), "" )

33.33333% 66.66667%

  37        48

ISLR ( , , ) . 293.

0

All Articles