Generate a Basis for Polynomial Splines

DESCRIPTION:

Generate a basis matrix for polynomial splines.

USAGE:

bs(x, df, knots, degree=3, intercept=FALSE, Boundary.knots, derivs)

REQUIRED ARGUMENTS:

x
the predictor variable.

OPTIONAL ARGUMENTS:

df
degrees of freedom; one can specify df rather than knots; bs then chooses df-degree-intercept knots (not including boundary knots) at suitable quantiles of x. This argument is ignored if knots is supplied.
knots
the internal breakpoints that define the spline. The default is NULL, which results in a basis for ordinary polynomial regression (if df is not given). Typical values are the mean or median for one knot, quantiles for more knots. See also Boundary.knots.
degree
degree of the piecewise polynomial---default is 3 for cubic splines.
intercept
if TRUE, an intercept is included in the basis; default is FALSE.
Boundary.knots
vector of length two, boundary points at which to 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) + degree + intercept. (Here length(knots) does not count boundary knots.) Attributes are returned that correspond to the arguments to bs and explicitly give the knots, Boundary.knots, etc., for use by predict.bs .

DETAILS:

The bs function is based on the function spline.des written by Douglas Bates. It generates a basis matrix for representing the family of piecewise polynomials with the specified interior knots and degree, evaluated at the values of x. A primary use is in modeling formulas to directly specify a piecewise polynomial 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:

lm(y ~ bs(age, 4) + bs(income, 4))  # an additive model
fit <- lm(y ~ bs(age, knots=c(20,30), B=c(0,100)))
predict(fit, new.age)  # safe prediction because all knots supplied

x <- -10:110
b <- bs(x, knots=c(20,30), B=c(0,100))
b <- bs(x, knots=c(0,20,30,100), Bound=T) # equivalent
matplot(x, b, type="l")
matplot(x, predict(b, x, derivs=1), type="l")