Re: [Rd] incorrect result of deriv (PR#9449)

From: Thomas Lumley <tlumley_at_u.washington.edu>
Date: Tue 16 Jan 2007 - 19:02:01 GMT

This is as documented in help(deriv)

      The internal code knows about the arithmetic operators '+', '-',
      '*', '/' and '^', and the single-variable functions 'exp', 'log',
      'sin', 'cos', 'tan', 'sinh', 'cosh', 'sqrt', 'pnorm', 'dnorm',
      'asin', 'acos', 'atan', 'gamma' and 'lgamma'. (Note that only the
      standard normal distribution is considered.)



 	-thomas




On Fri, 12 Jan 2007, polzehl@pubhealth.ku.dk wrote:

> Full_Name: Joerg Polzehl
> Version: 2.3.1
> OS: x86_64, linux-gnu
> Submission from: (NULL) (62.141.176.22)
>
>
> I observed an incorrect behavior of function deriv when evaluating arguments of
> dnorm
>
> deriv(~dnorm(z,0,s),"z")
> expression({
> .value <- dnorm(z, 0, s)
> .grad <- array(0, c(length(.value), 1), list(NULL, c("z")))
> .grad[, "z"] <- -(z * dnorm(z))
> attr(.value, "gradient") <- .grad
> .value
> })
>
> deriv(~exp(-z^2/(2*s^2))/s/sqrt(2*pi),"z")
> expression({
> .expr4 <- 2 * s^2
> .expr6 <- exp(-z^2/.expr4)
> .expr9 <- sqrt(2 * pi)
> .value <- .expr6/s/.expr9
> .grad <- array(0, c(length(.value), 1), list(NULL, c("z")))
> .grad[, "z"] <- -(.expr6 * (2 * z/.expr4)/s/.expr9)
> attr(.value, "gradient") <- .grad
> .value
> })
>
> should provide the same expression but
>
>> eval(deriv(~exp(-z^2/(2*s^2))/s/sqrt(2*pi),"z"),list(z=1,s=2))
> [1] 0.1760327
> attr(,"gradient")
> z
> [1,] -0.04400817
>
>> eval(deriv(~dnorm(z,0,s),"z"),list(z=1,s=2))
> [1] 0.1760327
> attr(,"gradient")
> z
> [1,] -0.2419707
>
> provide different gradients ...
>
> ______________________________________________
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley@u.washington.edu	University of Washington, Seattle

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Wed Jan 17 06:05:45 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 16 Jan 2007 - 19:32:52 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.