Re: [S] extrapolating natural splines

Trevor Hastie (
Tue, 10 Feb 1998 07:35:46 -0800 (PST)

Here follows a message I posted in 1994 (with promises of followup
that never occurred!) that gets around the extrapolation problem.

> To: S-news
> Subject: modified bs() and ns()
> From: trevor
> Date: Fri Mar 4 15:31:34 1994
> I have posted version 1 of an improved and augmented set of functions
> for working with regression splines. The functions posted are revisions
> of bs() and ns(), and predict() methods for each. The entry is called
> "safe.predict" for reasons which will become apparant.
> Each of ns() and bs() have an additional argument: Boundary.knots.
> These give the "anchor points" for the B-splines constructed by
> spline.des(), a primative used by both ns() and bs(). Boundary.knots
> defaults to the range of the supplied x argument. If supplied, they
> do not need to cover the range of the data (i.e. data can lie outside
> the boundary knots). If data lie far outside the boundary knots,
> numerical problems typical of polynomials can occur.
> For ns() the Boundary.knots simultaneously anchor the B-splines, and also
> define the points at which the natural boundary conditions are imposed: the
> basis is linear beyond them.
> Why these added arguments? Now bs() and ns() can be used by lm(),
> glm() etc, and produce accurate predictions IF both the "knots" and
> "Boundary.knots" arguments are supplied in the original call.
> E.G.
> fit <- glm(Kyphosis ~ ns(Age, kn=c(5,12), B=c(0,50)) + Start, family=binomial)
> <- predict(fit,list(Age=seq(0,100,by=1)), type="response")
> >>note the arguments can be abbreviated <<
> and predict.ns() are simple functions that allow one to evaluate
> the basis returned by bs() or ns() at new locations. The revised bs() and
> ns() carry as attributes the explicit arguments needed to define the basis.
> E.G.
> basis <- ns(x, df=6) # create a basis, with data dependent parameters
> attached as attributes
> basis.aux < predict(basis, newx) # the same basis, evaluated elsewhere.
> Why version 1, and way in an entry called safe.predict? There will be
> a version 2 of experimental functions that allow the modelling functions
> to remember the "parameters" of special smart functions like bs, ns and poly.
> These parameters will be saved on the object, much like contrasts are now,
> and will be retrieved when predictions are made from the model. More to
> come a bit later.
> Trevor Hastie
> Phone: 908-582-5647 Fax: 908-582-3340
> 2C-261 AT&T Bell Labs, Murray Hill, NJ 07974
Trevor Hastie, Associate Professor
Phone: 650-725-2231 Fax: 650-725-8977
paper: Statistics Department, Stanford University, CA94305
office: Margaret Jacks Hall, rm 362
This message was distributed by To unsubscribe
send e-mail to with the BODY of the
message: unsubscribe s-news