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

Thomas Lumley tlumley at u.washington.edu
Tue Jan 16 20:02:01 CET 2007


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 at 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 at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

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



More information about the R-devel mailing list