# How to translate the output from an lm() fit with a cubic spline into a regression equation

I have some code and output, and I would like to construct a model. I don't know how to construct a model using this output:

 require("splines")
x   <- c(0.2,   0.23,   0.26,   0.29,   0.33,   0.46,    0.53 )
y   <- c(0.211, 0.2026, 0.2034, 0.2167, 0.2177, 0.19225, 0.182)
fit <- lm(y ~ ns(x,3))
summary(fit)


Note that ns() generates the B-spline basis matrix for a natural cubic spline. Thus this model regresses y against a B-spline for x using three degrees of freedom. What would the equation for such a model look like?

• Although this concern is of interest here, this question is stated too much in an R-centric way--and therefore belongs on SO--unless you explain what ns does. (It's not even part of R: what package does it come from?)
– whuber
Aug 16, 2013 at 20:21
• @whuber, see here: ?ns; ns() is part of the splines package. I recognize that this question is couched in R terms, but my opinion is that it's on-topic here. Aug 16, 2013 at 20:34
• @gung Yes, I was able to find the package too, but that's not the point: for this question to stay here it needs to be made intelligible even to non-R users.
– whuber
Aug 16, 2013 at 20:41
• @whuber I've added a minor bit of explanation. Are you looking for more that this? Aug 16, 2013 at 20:43
• @Gavin Thank you. I have taken the liberty of adding one more line so that non-R users can understand what is being asked (and perhaps, therefore, offer meaningful answers).
– whuber
Aug 16, 2013 at 20:47

require(rms)
f <- ols(y ~ rcs(x, 3))  # 2 d.f. for x
Function(f)  # represent fitted function in simplest R form
latex(f)     # typeset algebraic representation of fit


rcs "restricted cublic spline" is another representation of a natural spline.

• That is impressive. But I think the answer one would look for on this site (as opposed to SO) would explain how, in general, one determines the equation of a spline.
– whuber
Aug 16, 2013 at 20:44
• Thank you! Can you explain why the coefficients produced by f are different than the coefficients produced by fit? Aug 19, 2013 at 13:48
• There are different basis function representations for splines. ns generates orthogonal (uncorrelated) terms that are harder to interpret whereas rcs uses the truncated power basis which is easy to represent in an equation. Think about this example: you could have a model with $X$ and $X^{2}$ or you could fit $X-\bar{X}$ and $(X-\bar{X})^{2}$. The latter's terms would be orthogonal but harder to interpret in terms of raw variables. Aug 19, 2013 at 13:59