deriv methods that obtain derivative of given order of B-splines, M-spline, I-splines, and C-splines, etc. At knots, the derivative is defined to be the right derivative. By default, the function returns the first derivative. 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.

# 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, ...)

## Arguments

expr Objects of class bSpline2, ibs, dbs, mSpline, iSpline, or cSpline, etc. A positive integer specifying the order of derivatives. By default, it is 1L for the first derivative. Other arguments for further usage.

## Value

A matrix of dimension length(x) by df = degree + length(knots) (plus one if intercept is included). Attributes that correspond to the arguments specified are returned for usage for other function in this package.

## Details

The function is designed for most of the objects generated from this package. It internally extracts necessary information about the input spline basis matrix from its attributes. So the function will not work if some attribute is not available.

De Boor, Carl. (1978). A practical guide to splines. Vol. 27. New York: Springer-Verlag.

bSpline for B-splines; ibs for integral of B-splines; mSpline for M-splines; iSpline for I-splines; cSpline for C-splines.

## Examples

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, d2Mat3, check.attributes = FALSE))

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

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

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