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