Generate a Basis Matrix for Natural Cubic Splines

DESCRIPTION:

Generates a basis matrix for representing the family of piecewise-cubic splines with the specified sequence of interior knots, and the boundary conditions.

USAGE:

ns(x, df, knots, intercept=F, Boundary.knots, derivs)

ARGUMENTS:

x
the predictor variable.

OPTIONAL ARGUMENTS:

df
degrees of freedom. One can supply df rather than knots; ns then chooses df-1-intercept knots (not including boundary knots) at suitably chosen quantiles of x. This argument is ignored if knots is supplied.
knots
breakpoints that define the spline. The default is no knots; together with the natural boundary conditions this results in a basis for linear regression on x. Typical values are the mean or median for one knot, quantiles for more knots. See also Boundary.knots.
intercept
a logical value; if TRUE, an intercept is included in the basis. The default is FALSE.
Boundary.knots
vector of length two, boundary points at which to impose the natural boundary conditions and anchor the B-spline basis (default is the range of the data). If both knots and Boundary.knots are supplied, the basis parameters do not depend on x. Alternately, if Boundary.knots is TRUE, then knots includes both the internal knots and boundary points. Data can extend beyond Boundary.knots
derivs
positive integer, or vector the same length as x; if supplied then derivatives of the basis functions are returned.

VALUE:

a matrix of dimension length(x)*df, where df=length(knots)+1+intercept . (Here length(knots) does not count boundary knots.) Attributes are returned that correspond to the arguments to ns , and explicitly give the knots, Boundary.knots , etc., for use by predict.ns.

DETAILS:

The ns function is based on the function spline.des written by Douglas Bates. It generates a basis matrix for representing the family of piecewise-cubic splines with the specified sequence of interior knots, boundary knots, and the natural boundary conditions. These enforce the constraint that the function is linear beyond the boundary knots. A primary use is in modeling formula to directly specify a natural spline term in a model.

Beware of making predictions with new x values. Results will differ unless the knots and Boundary.knots are the same. predict(object, newx) uses the same values when object is the direct output of bs, but not when object is the output of a modeling function like lm. It is best to explicitly specify knots and Boundary.knots when using modeling functions for prediction.

For arithmetic involving intercept, FALSE==0 and TRUE==1 .

REFERENCES:

de Boor, C. (1978). A Practical Guide to Splines. Berlin: Springer Verlag.
Cheney, W., Kincaid, D. (1985). Numerical Mathematics and Computing, Second edition. New York: Brooks/Cole Publishing Co.

SEE ALSO:

, , , , , , .

EXAMPLES:

N <- 150
x <- runif(N, 0, 10)
y <- 2*(2 + sin(x)) + rnorm(N)
knots <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
plot(x, y, xlim=c(-5, 15), ylim=c(-5, 15))
boundary <- range(x)
lmObj <- lm(y ~ ns(x, knots=knots, Boundary.knots=boundary))
x.new <- seq(-1, 12, by=.2)
y.new <- predict(lmObj, data.frame(x=x.new))
points(x.new, y.new, pch=16)