[R] numericDeriv

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Nov 16 14:05:55 CET 2005


On Wed, 16 Nov 2005, Florent Bresson wrote:

> I have to compute some standard errors using the delta
> method and so have to use the command "numericDeriv"
> to get the desired gradient. Befor using it on my
> complicated function, I've done a try with a simple
> exemple :
>
> x <- 1:5
> numericDeriv(quote(x^2),"x")
>
> and i get :
>
> [1]   1   8  27  64 125 216
> attr(,"gradient")
>     [,1] [,2] [,3] [,4] [,5] [,6]
> [1,]  Inf    0    0  NaN    0    0
> [2,]    0    0    0  NaN    0    0
> [3,]    0  Inf    0  NaN    0    0
> [4,]    0    0    0  NaN    0    0
> [5,]    0    0  Inf  NaN    0    0
> [6,]    0    0    0  NaN    0    0
>
> I don't understand the result. I thought I will get :
>
> [1]   1   8  27  64 125 216
> attr(,"gradient")
>     [,1]
> [1,]  1
> [2,]  4
> [3,]  6
> [4,]  8
> [5,]  10
> [6,]  12
>
> The derivative of x^2 is still 2x, isn't it ?

and (1:5)^2 is still

[1]  1  4  9 16 25

!

Try

> x <- as.numeric(1:5)
> numericDeriv(quote(x^2),"x")

since the author of numericDeriv has forgotten some coercions.

-- 
Brian D. Ripley,                  ripley at 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 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list