bs(x, df = NULL, knots = NULL, degree = 3, intercept = FALSE, Boundary.knots = range(x))
df-degree(minus one if there is an intercept) knots at suitable quantiles of
x(which will ignore missing values). The default,
NULL, corresponds to no inner knots, i.e.,
degree - intercept.
NULL, which results in a basis for ordinary polynomial regression. Typical values are the mean or median for one knot, quantiles for more knots. See also
3for cubic splines.
TRUE, an intercept is included in the basis; default is
NAdata). If both
Boundary.knotsare supplied, the basis parameters do not depend on
x. Data can extend beyond
c(length(x), df), where either
dfwas supplied or if
df = length(knots) + degreeplus one if there is an intercept. Attributes are returned that correspond to the arguments to
bs, and explicitly give the
Boundary.knotsetc for use by
bsis based on the function
spline.des. 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. When
Boundary.knotsare set inside
bs()now uses a ‘pivot’ inside the respective boundary knot which is important for derivative evaluation. In R versions \(\le\) 3.2.2, the boundary knot itself had been used as pivot, which lead to somewhat wrong extrapolations.
require(stats); require(graphics) bs(women$height, df = 5) summary(fm1 <- lm(weight ~ bs(height, df = 5), data = women)) ## example of safe prediction plot(women, xlab = "Height (in)", ylab = "Weight (lb)") ht <- seq(57, 73, length.out = 200) lines(ht, predict(fm1, data.frame(height = ht)))
Run the code above in your browser using DataCamp Workspace