ns(x, df, knots, intercept=F, Boundary.knots, derivs)
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.
x.
Typical values are the mean or median for one knot, quantiles for
more knots. See also
Boundary.knots.
TRUE,
an intercept is included in the basis.
The default is
FALSE.
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
x;
if supplied then derivatives of the basis functions are returned.
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.
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
.
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.
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)