deriv
Symbolic Partial Derivatives of Expressions

Description

Takes the partial derivative of an expression with respect to given variables. The deriv3 methods are similar to the deriv methods, except that deriv3 returns the "hessian" attribute by default.

Usage

deriv(expr, ...)
## Default S3 method:
deriv(expr, namevec, function.arg = NULL, tag = ".expr",
    hessian = FALSE, ...)
## S3 method for class 'formula':
deriv(expr, namevec, function.arg = NULL, tag = ".expr",
    hessian = FALSE, ...)

deriv3(expr, ...) ## Default S3 method: deriv3(expr, namevec, function.arg = NULL, tag = ".expr", hessian = TRUE, ...) ## S3 method for class 'formula': deriv3(expr, namevec, function.arg = NULL, tag = ".expr", hessian = TRUE, ...)

Arguments

expr the expression to be differentiated. Typically a formula, in which case the expression returned computes the right side of the ~ and its derivatives.
namevec a character vector of the names of parameters. (Derivatives are computed for these variables only.)
function.arg a character vector, a function prototype, or the logical value TRUE. Optional. If this argument is present, then the return value is in the form of a function instead of a multiple-statement expression. See Details for more information.
tag the base of the names to give to intermediate results. The default is ".expr".
hessian a logical value. If TRUE, then the second derivatives are calculated and included in the return value. The default is FALSE.
... other arguments to pass to or from the function.

Details

deriv is a generic function with a default method and a method for formulas.
While generating this sequence of expressions, the function attempts to eliminate repeated calculation of common subexpressions. Sometimes user assistance is needed, as in the example below.
Expressions that are used only once are folded back into the expression where they are used. This design improves readability; however, because parentheses are always added when such expressions are folded in, redundant parentheses can appear in the final expressions.
deriv3 and its methods are almost identical to deriv and its methods, except that the default value of argument hessian is TRUE for deriv3.
Value
returns a multiple-statement expression. When evaluated, these statements return the value of the original expression, along with an attribute called "gradient", which is the gradient matrix of the expression value with respect to the named parameters.
References
Bates, D. M. and Chambers, J. M. 1992. Statistical Models in S. Pacific Grove, CA.: Wadsworth & Brooks/Cole.
Griewank, A. and Corliss, G. F. 1991. Automatic Differentiation of Algorithms: Theory, Implementation, and Application. Philadelphia, PA: SIAM proceedings.
See Also
nls
Examples
# Value and gradient of the Michaelis-Menten model
deriv(~ Vm*conc/(K+conc), c("Vm","K"))
## expression({
##     .expr1 <- Vm * conc
##     .expr2 <- K + conc
##     .value <- .expr1/.expr2
##     .grad <- array(0, c(length(.value), 2),
##          list(NULL, c("Vm", "K")))
##     .grad[, "Vm"] <- conc/.expr2
##     .grad[, "K"] <-  - (.expr1/(.expr2^2))
##     attr(.value, "gradient") <- .grad
##     .value
## }
## )

# Return a function as the result deriv(~ Vm*conc/(K+conc), c("Vm","K"), function(Vm, K, conc = 1:10) NULL) ## function(Vm, K, conc = 1:10) ## { ## .expr1 <- Vm * conc ## .expr2 <- K + conc ## .value <- .expr1/.expr2 ## .grad <- array(0, c(length(.value), 2), ## list(NULL, c("Vm", "K"))) ## .grad[, "Vm"] <- conc/.expr2 ## .grad[, "K"] <- - (.expr1/(.expr2^2)) ## attr(.value, "gradient") <- .grad ## .value ## }

# Return a function when "function.arg" is specified as "TRUE" deriv(~ Vm*conc/(K+conc), c("Vm","K"), TRUE) ## function (Vm, K) ## { ## .expr1 <- Vm * conc ## .expr2 <- K + conc ## .value <- .expr1/.expr2 ## .grad <- array(0, c(length(.value), 2L), list(NULL, c("Vm", ## "K"))) ## .grad[, "Vm"] <- conc/.expr2 ## .grad[, "K"] <- -(.expr1/.expr2^2) ## attr(.value, "gradient") <- .grad ## .value ## }

# Investigate the effect of the argument "hessian": deriv vs. deriv3 x <- 1:3; y <- 2:4 f <- deriv(~a^2, "a", TRUE) f(x) ## [1] 1 4 9 ## attr(,"gradient") ## a ## [1,] 2 ## [2,] 4 ## [3,] 6 f3 <- deriv3(~a^2, "a", TRUE) f3(x) ## [1] 1 4 9 ## attr(,"gradient") ## a ## [1,] 2 ## [2,] 4 ## [3,] 6 ## attr(,"hessian") ## , , a ## ## a ## [1,] 2 ## [2,] 2 ## [3,] 2

# Investigate "hessian" with two variables: x <- 1:3; y <- 2:4 g <- deriv(~a*b^2, c("a","b"), TRUE) g3 <- deriv3(~a*b^2, c("a","b"), TRUE) g3(x, y) ## attr(,"gradient") ## a b ## [1,] 4 4 ## [2,] 9 12 ## [3,] 16 24 ## attr(,"hessian") ## , , a ## ## a b ## [1,] 0 4 ## [2,] 0 6 ## [3,] 0 8 ## ## , , b ## ## a b ## [1,] 4 2 ## [2,] 6 4 ## [3,] 8 6

Package stats version 6.1.1-7
Package Index