numericDeriv
Evaluate derivatives numerically
Description
Evaluates an expression and the gradient of the expression numerically.
Usage
numericDeriv(expr, theta, rho = parent.frame(), dir = 1)
Arguments
expr |
an expression to be differentiated. The value of the expression should be a numeric vector.
|
theta |
a vector of variable names. The derivative of expr with respect to each
element of each variable is approximated. These variables must be accessible
in the environment rho, and they must be numeric, not integer or
complex.
|
rho |
the environment in which the expression is evaluated. All variables in expr
must be accessible in this environment. The value and derivatives of expr
are evaluated at the values of the variables in this environment.
|
dir |
a numeric vector of directions. Typically -1 or 1: the length of theta (if
shorter, it is repeated to the length of theta).
These are used to determine the change in the dependent variable when computing finite
difference approximations to the derivatives.
This is useful when you need to estimate the derivative at the
edge of the domain of a function.
|
Details
numericDeriv uses the one-sided secant method to compute a finite difference
approximation to the derivative.
The dependent variable is perturbed by a factor of 1.0 + dir * sqrt(.Machine$double.eps), unless the
variable is 0, in which case dir * sqrt(.Machine$double.eps) is added to it.
Value
returns the value of the expression with the attribute of "gradient".
The columns of "gradient" are the derivatives of the value corresponding
to the elements of the variables named in theta.
See Also
Examples
x <- c(1,2,3,4)
y <- c(4, 15, 32, 54)
p0 <- 3
p1 <- 2
numericDeriv(quote( p0 * x^p1 ), c("p0", "p1"))
numericDeriv(quote( sum((y - p0 * x^p1) ^ 2)), c("p0", "p1"))
numericDeriv(quote(det(m)), "m", list2env(list(m=matrix(c(1,2,3,5),2,2))))