17
$\begingroup$

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?

$\endgroup$
8
  • 2
    $\begingroup$ 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?) $\endgroup$
    – whuber
    Commented Aug 16, 2013 at 20:21
  • 1
    $\begingroup$ @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. $\endgroup$ Commented Aug 16, 2013 at 20:34
  • $\begingroup$ @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. $\endgroup$
    – whuber
    Commented Aug 16, 2013 at 20:41
  • 1
    $\begingroup$ @whuber I've added a minor bit of explanation. Are you looking for more that this? $\endgroup$ Commented Aug 16, 2013 at 20:43
  • 2
    $\begingroup$ @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). $\endgroup$
    – whuber
    Commented Aug 16, 2013 at 20:47

1 Answer 1

26
$\begingroup$
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.

$\endgroup$
3
  • 7
    $\begingroup$ 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. $\endgroup$
    – whuber
    Commented Aug 16, 2013 at 20:44
  • $\begingroup$ Thank you! Can you explain why the coefficients produced by f are different than the coefficients produced by fit? $\endgroup$ Commented Aug 19, 2013 at 13:48
  • 2
    $\begingroup$ 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. $\endgroup$ Commented Aug 19, 2013 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.