Generates the B-spline basis matrix representing the family of piecewise
polynomials with the specified interior knots, degree, and boundary knots,
evaluated at the values of x
.
Usage
bSpline(
x,
df = NULL,
knots = NULL,
degree = 3L,
intercept = FALSE,
Boundary.knots = NULL,
derivs = 0L,
integral = FALSE,
...
)
Arguments
- x
The predictor variable. Missing values are allowed and will be returned as they are.
- df
Degree of freedom that equals to the column number of the returned matrix. One can specify
df
rather thanknots
, then the function choosesdf - degree - as.integer(intercept)
internal knots at suitable quantiles ofx
ignoring missing values and thosex
outside of the boundary. If internal knots are specified viaknots
, the specifieddf
will be ignored.- knots
The internal breakpoints that define the splines. The default is
NULL
, which results in a basis for ordinary polynomial regression. Typical values are the mean or median for one knot, quantiles for more knots.- degree
A nonnegative integer specifying the degree of the piecewise polynomial. The default value is
3
for cubic splines. Zero degree is allowed for piecewise constant basis functions.- intercept
If
TRUE
, the complete basis matrix will be returned. Otherwise, the first basis will be excluded from the output.- Boundary.knots
Boundary points at which to anchor the splines. By default, they are the range of
x
excludingNA
. If bothknots
andBoundary.knots
are supplied, the basis parameters do not depend onx
. Data can extend beyondBoundary.knots
.- derivs
A nonnegative integer specifying the order of derivatives of B-splines. The default value is
0L
for B-spline basis functions.- integral
A logical value. If
TRUE
, the corresponding integrals of spline basis functions will be returned. The default value isFALSE
.- ...
Optional arguments that are not used.
Value
A numeric matrix of length(x)
rows and df
columns if
df
is specified or length(knots) + degree +
as.integer(intercept)
columns if knots
are specified instead.
Attributes that correspond to the arguments specified are returned
mainly for other functions in this package.
Details
This function extends the bs()
function in the splines
package
for B-spline basis by allowing piecewise constant (left-closed and
right-open except on the right boundary) spline basis of degree zero.
Examples
library(splines2)
x <- seq.int(0, 1, 0.01)
knots <- c(0.3, 0.5, 0.6)
## cubic B-splines
bsMat <- bSpline(x, knots = knots, degree = 3, intercept = TRUE)
op <- par(mar = c(2.5, 2.5, 0.2, 0.1), mgp = c(1.5, 0.5, 0))
matplot(x, bsMat, type = "l", ylab = "Cubic B-splines")
abline(v = knots, lty = 2, col = "gray")
## reset to previous plotting settings
par(op)
## the first derivaitves
d1Mat <- deriv(bsMat)
## the second derivaitves
d2Mat <- deriv(bsMat, 2)
## evaluate at new values
predict(bsMat, c(0.125, 0.801))
#> 1 2 3 4 5 6 7
#> [1,] 0.1984954 0.5584491 0.2213542 0.02170139 0.0000000 0.0000000 0.0000000
#> [2,] 0.0000000 0.0000000 0.0000000 0.05628999 0.3604115 0.4564141 0.1268844