5

If the linear model is fitted as follows, what is the formula of the fitted model? I need to use the coefficients outside R.

fit <- lm( y ~ bs( x, degree=1, knots=c(6,12,18) ) );

Coefficients:
(Intercept)                    9.2679
ns(x, knots = c(6, 12, 18))1   5.3064
ns(x, knots = c(6, 12, 18))2   5.1313
ns(x, knots = c(6, 12, 18))3   6.1153
ns(x, knots = c(6, 12, 18))4   1.3724
Hong Ooi
  • 7,629
  • 3
  • 29
  • 52
A-L
  • 141
  • 2
  • 4

1 Answers1

5

The coefficients have the usual interpretation, but for the B-spline basis functions; which you can generate for new data easily enough in R :

bs(x, degree=1, knots=c(6,12,18)) -> x.bspline.bff
new.x <- c(10.2, 11.8, 13, 30)
predict(x.bspline.bff, new.x)

Most software will have functions to generate these (e.g. SAS, Stata); should you need to do it yourself, a recursive procedure is given in Hastie et.al (2009), The Elements of Statistical Learning, Ch.5, "Appendix: Computational considerations for splines".

You could also use an equivalent reëxpression with truncated power functions, but in general that's not a good idea—there's a danger of numerical instability with higher order splines & interactions. See here for an example of exporting a spline function to Excel.

Scortchi - Reinstate Monica
  • 27,560
  • 8
  • 81
  • 248