Extending deriv3()

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Extending deriv3()

Thomas Yee
Hello,

I was wondering if the functions deriv3(), deriv() etc. could be extended
to handle psigamma() and its special cases (digamma(), trigamma()
etc.). From the error message it seems that 'psigamma' needs to be
added to the derivatives table.
This might be easy since psigamma() has a deriv argument.

Additionally, this error message is also obtained when requesting for
the Hessian of the gamma and lgamma functions:

d3 = deriv(~  gamma(y), namev="y", hessian= TRUE)
d3 = deriv(~ lgamma(y), namev="y", hessian= TRUE)

Another class of special functions worth adding are the Bessel functions.

Thanks

Thomas

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Extending deriv3()

Prof Brian Ripley
On Mon, 15 Oct 2007, Thomas Yee wrote:

> Hello,
>
> I was wondering if the functions deriv3(), deriv() etc. could be extended
> to handle psigamma() and its special cases (digamma(), trigamma()
> etc.). From the error message it seems that 'psigamma' needs to be
> added to the derivatives table.
> This might be easy since psigamma() has a deriv argument.

If you look at ?deriv you will see that it only knows about functions *of
one argument* and operators.  So it would be easy to add digamma(x) and
psigamma(x) (and I will do so shortly), it would not be so easy to add
psigamma(x, deriv).

> Additionally, this error message is also obtained when requesting for
> the Hessian of the gamma and lgamma functions:
>
> d3 = deriv(~  gamma(y), namev="y", hessian= TRUE)
> d3 = deriv(~ lgamma(y), namev="y", hessian= TRUE)
>
> Another class of special functions worth adding are the Bessel functions.

Well, you can always submit a patch ....

Note that deriv() in R differs from that in S in being done in C and hence
not being user-extensible.  A long time ago that had an advantage: S's
deriv could be very slow and take a lot of memory by the standards of the
early 1990's.  Rather than work on adding yet more special cases it would
seem better to work on making it user-extensible.

--
Brian D. Ripley,                  [hidden email]
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Extending deriv3()

Gabor Grothendieck
If you are modifying it it would also be nice to add
{ to the derivative table so one can write this:

    f <- function(x) x*x
    deriv(body(f), "x", func = TRUE)

Currently, one must do:

    deriv(body(f)[[2]], "x", func = TRUE)


On 10/15/07, Prof Brian Ripley <[hidden email]> wrote:

> On Mon, 15 Oct 2007, Thomas Yee wrote:
>
> > Hello,
> >
> > I was wondering if the functions deriv3(), deriv() etc. could be extended
> > to handle psigamma() and its special cases (digamma(), trigamma()
> > etc.). From the error message it seems that 'psigamma' needs to be
> > added to the derivatives table.
> > This might be easy since psigamma() has a deriv argument.
>
> If you look at ?deriv you will see that it only knows about functions *of
> one argument* and operators.  So it would be easy to add digamma(x) and
> psigamma(x) (and I will do so shortly), it would not be so easy to add
> psigamma(x, deriv).
>
> > Additionally, this error message is also obtained when requesting for
> > the Hessian of the gamma and lgamma functions:
> >
> > d3 = deriv(~  gamma(y), namev="y", hessian= TRUE)
> > d3 = deriv(~ lgamma(y), namev="y", hessian= TRUE)
> >
> > Another class of special functions worth adding are the Bessel functions.
>
> Well, you can always submit a patch ....
>
> Note that deriv() in R differs from that in S in being done in C and hence
> not being user-extensible.  A long time ago that had an advantage: S's
> deriv could be very slow and take a lot of memory by the standards of the
> early 1990's.  Rather than work on adding yet more special cases it would
> seem better to work on making it user-extensible.
>
> --
> Brian D. Ripley,                  [hidden email]
> 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
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Extending deriv3()

Prof Brian Ripley
In reply to this post by Prof Brian Ripley
BTW, this has come up several times before, e.g.

http://tolstoy.newcastle.edu.au/R/e2/help/07/08/22447.html

is a call for patches.

On Mon, 15 Oct 2007, Prof Brian Ripley wrote:

> On Mon, 15 Oct 2007, Thomas Yee wrote:
>
>> Hello,
>>
>> I was wondering if the functions deriv3(), deriv() etc. could be extended
>> to handle psigamma() and its special cases (digamma(), trigamma()
>> etc.). From the error message it seems that 'psigamma' needs to be
>> added to the derivatives table.
>> This might be easy since psigamma() has a deriv argument.
>
> If you look at ?deriv you will see that it only knows about functions *of
> one argument* and operators.  So it would be easy to add digamma(x) and
> psigamma(x) (and I will do so shortly), it would not be so easy to add
> psigamma(x, deriv).

I've now implemented that in R-devel, including the 'not so easy' case.

>> Additionally, this error message is also obtained when requesting for
>> the Hessian of the gamma and lgamma functions:
>>
>> d3 = deriv(~  gamma(y), namev="y", hessian= TRUE)
>> d3 = deriv(~ lgamma(y), namev="y", hessian= TRUE)
>>
>> Another class of special functions worth adding are the Bessel functions.
>
> Well, you can always submit a patch ....
>
> Note that deriv() in R differs from that in S in being done in C and hence
> not being user-extensible.  A long time ago that had an advantage: S's
> deriv could be very slow and take a lot of memory by the standards of the
> early 1990's.  Rather than work on adding yet more special cases it would
> seem better to work on making it user-extensible.
>
>

--
Brian D. Ripley,                  [hidden email]
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel