On Wed, 23 Feb 2000, Timothy R. Johnson wrote:
>
> Hello all.
>
> I am confused about how deriv() works in comparison to deriv3() (from
> the V&R MASS library). I'm using S-PLUS version 4.5, release 2, for MS
> Windows.
>
> I want to use deriv() to return a function that will give me the
> gradient of a specified function. I try deriv(),
>
> > foo1 <- deriv(z ~ 2*x + 3*y, c("x","y"), function(x,y) {})
> > foo1
> function(x, y)
> {
> .value <- (2 * x) + (3 * y)
> .actualArgs <- match.call()[c("x", "y")]
> if(all(unlist(lapply(as.list(.actualArgs), is.name)))) {
> .grad <- array(0, c(length(.value), 2), list(NULL, c("x", "y")))
> .grad[, "x"] <- 2
> .grad[, "y"] <- 3
> dimnames(.grad) <- list(NULL, .actualArgs)
> attr(.value, "gradient") <- .grad
> }
> .value
> }
> > foo1(1,1)
> [1] 5
> > attr(foo1(1,1), "gradient")
> NULL
>
> and no gradient! Where did it go?
Someone `improved' deriv for S-PLUS 4.x. (Also 5.x. To do with changes in
nls.) I have sent several bug reports about this change. What you have to
do is
x <- 1; y <- 1; foo1(x,y)
[1] 5
attr(, "gradient"):
x y
[1,] 2 3
in other words, you only get a gradient if you supply objects, not
values. All so you can name columns of the gradient by the actual
arguments. Wierd!
> I would use deriv3() but I do not need the hessian and therefore do
> not want to use up CPU time doing so. Of course, I could edit the
> function to remove the hessian computations but it seems like I should
> be able to get deriv() to do what I want.
It's easier to remove the added code in deriv.default, or edit the result.
> Am I confused about how deriv() does/should work. Any feedback would
So I think are MathSoft DAPD. It certainly is not behaving as they
have documented it (in S-PLUS 5.1, to be precise).
--
Brian D. Ripley, ripley@stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news
|