Re: [Rd] extending the derivs table/fools rushing in

2008-08-14 Thread Gabor Grothendieck
While you are at it could you add { to the
table so that this works:

 # this is ok
 f - function(x) x*x
 D(body(f), x)
x + x

 # but not g which is same as f
 # except it has { ... } surrounding its body
 g - function(x) { x*x }
 D(body(g), x)
Error in D(body(g), x) : Function '`{`' is not in the derivatives table


2008/8/14 Ben Bolker [EMAIL PROTECTED]:

  I added plogis to the derivative table in the
 development version of R; the patch against yesterday's
 R-devel src/deriv/main.c is available at
 http://www.zoology.ufl.edu/bolker/deriv_patch.txt .

  I pretty much followed the framework of the other symbols;
 here was my incantation

 -   } else if (CAR(expr) == PlogisSymbol) {
 -   ans = simplify(TimesSymbol,
 -  PP_S(TimesSymbol,
 -   PP_S2(ExpSymbol,
 -  PP_S2(MinusSymbol,CADR(expr))),
 -   PP_S(PowerSymbol,
 -PP_S(PlusSymbol,
 - Constant(1.),
 - PP_S2(ExpSymbol,
 -PP_S2(MinusSymbol,CADR(expr,
 -Constant(-2.))),
 -  PP(D(CADR(expr),var)));
 -   UNPROTECT(8);

 It seems to work:

 D(quote(plogis(a)),a)
 exp(-a) * (1 + exp(-a))^-2
 D(quote(plogis(a+b*x)),x)
 exp(-(a + b * x)) * (1 + exp(-(a + b * x)))^-2 * b

  Any thoughts?  I'm sure there's a cleverer way to do this ...

  Ben Bolker

  PS I get a minor build error at the end of the compilation
 (on Ubuntu 8.10):

 make[2]: *** No rule to make target `VR.ts', needed by
 `stamp-recommended'.  Stop.
 make[2]: Leaving directory
 `/usr/local/src/R/R-devel/src/library/Recommended'
 make[1]: *** [recommended-packages] Error 2
 make[1]: Leaving directory
 `/usr/local/src/R/R-devel/src/library/Recommended'
 make: *** [stamp-recommended] Error 2

  ... don't know if that is important or not.



 __
 R-devel@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-devel



__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] extending the derivs table/fools rushing in

2008-08-14 Thread Ben Bolker
 http://www.zoology.ufl.edu/bolker/deriv_patch2.txt

 has this change added as well.
 However, I'm not as confident that this is the right thing
to do?  Should curly brackets even be appearing in mathematical
expressions?

  Ben

Gabor Grothendieck wrote:
 While you are at it could you add { to the
 table so that this works:
 
 # this is ok
 f - function(x) x*x
 D(body(f), x)
 x + x
 
 # but not g which is same as f
 # except it has { ... } surrounding its body
 g - function(x) { x*x }
 D(body(g), x)
 Error in D(body(g), x) : Function '`{`' is not in the derivatives table
 
 
 2008/8/14 Ben Bolker [EMAIL PROTECTED]:
  I added plogis to the derivative table in the
 development version of R; the patch against yesterday's
 R-devel src/deriv/main.c is available at
 http://www.zoology.ufl.edu/bolker/deriv_patch.txt .

  I pretty much followed the framework of the other symbols;
 here was my incantation

 -   } else if (CAR(expr) == PlogisSymbol) {
 -   ans = simplify(TimesSymbol,
 -  PP_S(TimesSymbol,
 -   PP_S2(ExpSymbol,
 -  PP_S2(MinusSymbol,CADR(expr))),
 -   PP_S(PowerSymbol,
 -PP_S(PlusSymbol,
 - Constant(1.),
 - PP_S2(ExpSymbol,
 -PP_S2(MinusSymbol,CADR(expr,
 -Constant(-2.))),
 -  PP(D(CADR(expr),var)));
 -   UNPROTECT(8);

 It seems to work:

 D(quote(plogis(a)),a)
 exp(-a) * (1 + exp(-a))^-2
 D(quote(plogis(a+b*x)),x)
 exp(-(a + b * x)) * (1 + exp(-(a + b * x)))^-2 * b

  Any thoughts?  I'm sure there's a cleverer way to do this ...

  Ben Bolker

  PS I get a minor build error at the end of the compilation
 (on Ubuntu 8.10):

 make[2]: *** No rule to make target `VR.ts', needed by
 `stamp-recommended'.  Stop.
 make[2]: Leaving directory
 `/usr/local/src/R/R-devel/src/library/Recommended'
 make[1]: *** [recommended-packages] Error 2
 make[1]: Leaving directory
 `/usr/local/src/R/R-devel/src/library/Recommended'
 make: *** [stamp-recommended] Error 2

  ... don't know if that is important or not.



 __
 R-devel@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-devel






signature.asc
Description: OpenPGP digital signature
__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] extending the derivs table/fools rushing in

2008-08-14 Thread Prof Brian Ripley
The derivative of plogis is surely dlogis.  (And yes, there is a good 
reason why we have such a function: take a look at its C code.)


That means we would need an entry for dlogis too, I guess. I am not 
convinced that there is a real need for these (and where does this stop?) 
What would be much more useful is to make this user-extensible (as Bill 
Venables pointed out a decade ago).  [pd]norm were added in 2002 to 
support MASS, the ability to do all of MASS in R being a goal at the time.


On Thu, 14 Aug 2008, Ben Bolker wrote:



 I added plogis to the derivative table in the
development version of R; the patch against yesterday's
R-devel src/deriv/main.c is available at
http://www.zoology.ufl.edu/bolker/deriv_patch.txt .

 I pretty much followed the framework of the other symbols;
here was my incantation

-   } else if (CAR(expr) == PlogisSymbol) {
-   ans = simplify(TimesSymbol,
-  PP_S(TimesSymbol,
-   PP_S2(ExpSymbol,
-  PP_S2(MinusSymbol,CADR(expr))),
-   PP_S(PowerSymbol,
-PP_S(PlusSymbol,
- Constant(1.),
- PP_S2(ExpSymbol,
-PP_S2(MinusSymbol,CADR(expr,
-Constant(-2.))),
-  PP(D(CADR(expr),var)));
-   UNPROTECT(8);

It seems to work:


D(quote(plogis(a)),a)

exp(-a) * (1 + exp(-a))^-2

D(quote(plogis(a+b*x)),x)

exp(-(a + b * x)) * (1 + exp(-(a + b * x)))^-2 * b

 Any thoughts?  I'm sure there's a cleverer way to do this ...

 Ben Bolker

 PS I get a minor build error at the end of the compilation
(on Ubuntu 8.10):

make[2]: *** No rule to make target `VR.ts', needed by
`stamp-recommended'.  Stop.
make[2]: Leaving directory
`/usr/local/src/R/R-devel/src/library/Recommended'
make[1]: *** [recommended-packages] Error 2
make[1]: Leaving directory
`/usr/local/src/R/R-devel/src/library/Recommended'
make: *** [stamp-recommended] Error 2

 ... don't know if that is important or not.


A local-to-you problem.






--
Brian D. Ripley,  [EMAIL PROTECTED]
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, UKFax:  +44 1865 272595

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] extending the derivs table/fools rushing in

2008-08-14 Thread Ben Bolker
Prof Brian Ripley wrote:
 The derivative of plogis is surely dlogis.  (And yes, there is a good
 reason why we have such a function: take a look at its C code.)

  Doh.
 
 That means we would need an entry for dlogis too, I guess. I am not
 convinced that there is a real need for these (and where does this
 stop?) What would be much more useful is to make this user-extensible
 (as Bill Venables pointed out a decade ago).  [pd]norm were added in
 2002 to support MASS, the ability to do all of MASS in R being a goal at
 the time.

   I agree it would be great to make this user-extensible, but it's
probably a bit beyond me ... I found a web-reference of Venables saying

 There is a detailed example towards the end of Ch. 9 of VR on how
 to extend D() and make.call(), which are the work horses for
 deriv(), to handle new functions. The new functions handled there
 are dnorm() and pnorm(), but I() would be even easier, of course.

http://www.math.yorku.ca/Who/Faculty/Monette/pub/s-old/0690.html

 ... but this is from 1997 therefore presumably MASS3? or MASS2? --
I can't find my copy of MASS3 at the moment, and don't own MASS2 ...

   The reason behind this is that I was trying to write a simple
analytic derivative calculator for formulae of the form (e.g.)

y ~ dbinom(prob=plogis(a+b*x),size=N)

Obviously in this case I could just tell people to write the
formula out as

y ~ dbinom(prob=1/(1+exp(-(a+b*x))),size=N)

 ...

  Ben Bolker



signature.asc
Description: OpenPGP digital signature
__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] extending the derivs table/fools rushing in

2008-08-14 Thread Gabor Grothendieck
You could see if Ryacas package can do what you want:

http://ryacas.googlecode.com

2008/8/14 Ben Bolker [EMAIL PROTECTED]:
 Prof Brian Ripley wrote:
 The derivative of plogis is surely dlogis.  (And yes, there is a good
 reason why we have such a function: take a look at its C code.)

  Doh.

 That means we would need an entry for dlogis too, I guess. I am not
 convinced that there is a real need for these (and where does this
 stop?) What would be much more useful is to make this user-extensible
 (as Bill Venables pointed out a decade ago).  [pd]norm were added in
 2002 to support MASS, the ability to do all of MASS in R being a goal at
 the time.

   I agree it would be great to make this user-extensible, but it's
 probably a bit beyond me ... I found a web-reference of Venables saying

 There is a detailed example towards the end of Ch. 9 of VR on how
 to extend D() and make.call(), which are the work horses for
 deriv(), to handle new functions. The new functions handled there
 are dnorm() and pnorm(), but I() would be even easier, of course.

 http://www.math.yorku.ca/Who/Faculty/Monette/pub/s-old/0690.html

  ... but this is from 1997 therefore presumably MASS3? or MASS2? --
 I can't find my copy of MASS3 at the moment, and don't own MASS2 ...

   The reason behind this is that I was trying to write a simple
 analytic derivative calculator for formulae of the form (e.g.)

 y ~ dbinom(prob=plogis(a+b*x),size=N)

 Obviously in this case I could just tell people to write the
 formula out as

 y ~ dbinom(prob=1/(1+exp(-(a+b*x))),size=N)

  ...

  Ben Bolker


 __
 R-devel@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-devel



__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel