splines2 (version 0.3.1)

deriv: Derivatives of Spline Bases

Description

Returns derivatives of given order for the spline bases.

Usage

# S3 method for bSpline2
deriv(expr, derivs = 1L, ...)

# S3 method for dbs deriv(expr, derivs = 1L, ...)

# S3 method for ibs deriv(expr, derivs = 1L, ...)

# S3 method for mSpline deriv(expr, derivs = 1L, ...)

# S3 method for iSpline deriv(expr, derivs = 1L, ...)

# S3 method for cSpline deriv(expr, derivs = 1L, ...)

# S3 method for bernsteinPoly deriv(expr, derivs = 1L, ...)

Arguments

expr

Objects of class bSpline2, ibs, dbs, mSpline, iSpline, or cSpline, etc.

derivs

A positive integer specifying the order of derivatives. By default, it is 1L for the first derivative.

...

Other arguments that are not used.

Value

A numeric matrix of the same dimension with the input expr.

Details

At knots, the derivative is defined to be the right derivative. By default, the function returns the first derivatives. For derivatives of order greater than one, the nested call such as deriv(deriv(expr)) is supported but not recommended. For a better performance, argument derivs should be specified instead.

This function is designed for objects produced by this package. It internally extracts necessary specification about the spline/polynomial basis matrix from its attributes. Therefore, the function will not work if the key attributions are not available after some operations.

Examples

Run this code
# NOT RUN {
library(splines2)
x <- c(seq.int(0, 1, 0.1), NA)  # NA's will be kept.
knots <- c(0.3, 0.5, 0.6)

## integal of B-splines and the corresponding B-splines integrated
ibsMat <- ibs(x, knots = knots)
bsMat <- bSpline(x, knots = knots)

## the first derivative
d1Mat <- deriv(ibsMat)
stopifnot(all.equal(bsMat, d1Mat, check.attributes = FALSE))

## the second derivative
d2Mat1 <- deriv(bsMat)
d2Mat2 <- deriv(ibsMat, derivs = 2L)
## nested calls are supported but not recommended
d2Mat3 <- deriv(deriv(ibsMat))
stopifnot(all.equal(d2Mat1, d2Mat2, check.attributes = FALSE))
stopifnot(all.equal(d2Mat2, d2Mat3, check.attributes = FALSE))

## C-splines, I-splines, M-splines and the derivatives
csMat <- cSpline(x, knots = knots, intercept = TRUE, scale = FALSE)
isMat <- iSpline(x, knots = knots, intercept = TRUE)
stopifnot(all.equal(isMat, deriv(csMat), check.attributes = FALSE))

msMat <- mSpline(x, knots = knots, intercept = TRUE)
stopifnot(all.equal(msMat, deriv(isMat), check.attributes = FALSE))
stopifnot(all.equal(msMat, deriv(csMat, 2), check.attributes = FALSE))
stopifnot(all.equal(msMat, deriv(deriv(csMat)), check.attributes = FALSE))

dmsMat <- mSpline(x, knots = knots, intercept = TRUE, derivs = 1)
stopifnot(all.equal(dmsMat, deriv(msMat), check.attributes = FALSE))
stopifnot(all.equal(dmsMat, deriv(isMat, 2), check.attributes = FALSE))
stopifnot(all.equal(dmsMat, deriv(deriv(isMat)), check.attributes = FALSE))
stopifnot(all.equal(dmsMat, deriv(csMat, 3), check.attributes = FALSE))
stopifnot(all.equal(dmsMat, deriv(deriv(deriv(csMat))), check.attributes = FALSE))
# }

Run the code above in your browser using DataLab