Skip to content

Help for numericDeriv function

3 messages · Cengiz Zopluoğlu, William Dunlap

#
You could use parse(text=a)[[1]]:
   > numericDeriv(parse(text=a)[[1]], c(t(l),recursive=TRUE))
  [1] 3
  attr(,"gradient")
       [,1] [,2] [,3] [,4] [,5] [,6]
  [1,]    1    0    3    0    0    0

or construct your expression directly:
  > lnames <- array(lapply(l, as.name), dim=dim(l))
  > aa <- call("+", call("*", lnames[[1,1]], lnames[[2,1]]), call("*", lnames[[1,2]], lnames[[1,2]]))
  > numericDeriv(aa, c(t(l),recursive=TRUE))
  [1] 3
  attr(,"gradient")
       [,1] [,2] [,3] [,4] [,5] [,6]
  [1,]    1    0    3    0    0    0

The results are identical.  The latter method is a bit more work to set up but
works no matter what the names in the expression look like.  The former
fails if the names contain spaces or other things that are not allowed in
R names.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com