Skip to content
Back to formatted view

Raw Message

Message-ID: <CA91B11D-E667-4E20-980E-04403F377A7D@me.com>
Date: 2013-03-12T21:15:17Z
From: Marc Schwartz
Subject: Extracting the knots of a natural cubic spline fit
In-Reply-To: <CAO-mjT_bfndYXEtPXvzOiemwfz11rfvTDHaHNRvsAgNWbOAiJQ@mail.gmail.com>

On Mar 12, 2013, at 2:59 PM, Rajat Tayal <rajat at igidr.ac.in> wrote:

> Dear list members,
> 
> I am trying to fit a natural cubic spline to my dataset using the ns
> function in the splines package.
> Specifically, I do:
> 
> library(splines)
> lm(y ~ ns(x, df=3), data =data)
> 
> How do I extract the values of the interior knots of the fitted spline ?
> 
> Thanks,
> 
> Rajat


Using the example from ?ns:

require(splines)
fm1 <- lm(weight ~ ns(height, df = 5), data = women)


> attr(terms(fm1), "predvars")
list(weight, ns(height, knots = c(60.8, 63.6, 66.4, 69.2), Boundary.knots = c(58, 
72), intercept = FALSE))


or directly on the data:

> attr(ns(women$height, df = 5), "knots")
 20%  40%  60%  80% 
60.8 63.6 66.4 69.2 

> attr(ns(women$height, df = 5), "Boundary.knots")
[1] 58 72



Regards,

Marc Schwartz