Can someone help me understand the following: > D(expression(dnorm(x, mean)), "mean") [1] 0 > sessionInfo() R version 2.2.1, 2005-12-20, i386-pc-mingw32 attached base packages: [1] "methods" "stats" "graphics" "grDevices" "utils" "datasets" [7] "base" By my computations, this should be something like ((mean-x)/sd^2)*dnorm(...). Thanks for your help. Spencer Graves
dnorm() is an internal function, so I don't see how D (or deriv) can do anything with it symbolically. Am I missing something? -- Bert> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Spencer Graves > Sent: Wednesday, January 25, 2006 2:43 PM > To: r-help at stat.math.ethz.ch > Subject: [R] D(dnorm...)? > > Can someone help me understand the following: > > > D(expression(dnorm(x, mean)), "mean") > [1] 0 > > sessionInfo() > > R version 2.2.1, 2005-12-20, i386-pc-mingw32 > > attached base packages: > [1] "methods" "stats" "graphics" "grDevices" "utils" > "datasets" > [7] "base" > > By my computations, this should be something like > ((mean-x)/sd^2)*dnorm(...). > > Thanks for your help. > Spencer Graves > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
Yes Bert, this time you are missing something (unusually) ... As Brian Ripley pointed out 'dnorm' is in the derivative table, *but* only as a function of one variable. So if you want to find the derivative of dnorm(x, mean, sigma) you have to write it as 1/sigma * dnorm((x - mu)/sigma). Here is a little example:> D(Quote(pnorm((x-mu)/sigma)), "x")dnorm((x - mu)/sigma) * (1/sigma)> D(D(Quote(pnorm((x-mu)/sigma)), "x"), "mu")(x - mu)/sigma * (dnorm((x - mu)/sigma) * (1/sigma)) * (1/sigma) --- Like Brian, I recall the suggestion that we make D(...) extensible. I still think it is a good idea and worth considering. Under one scheme you would specify an object such as Fnorm <- structure(quote(pnorm(x, mu, sigma)), deriv = list(x = Quote(dnorm(x, mu, sigma)/sigms), mu = Quote(-dnorm(x, mu, sigma)/sigma), sigma = Quote(-(x - mu)*dnorm(x, mu, sigma)/sigma^2), class = "dfunction") ane write a generic "differentiate" function with a "dfunction" method and "D" as the default. I don't think it's quite that easy, but the plan is clear enough. Bill. -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Berton Gunter Sent: Thursday, 26 January 2006 8:58 AM To: 'Spencer Graves'; r-help at stat.math.ethz.ch Subject: Re: [R] D(dnorm...)? dnorm() is an internal function, so I don't see how D (or deriv) can do anything with it symbolically. Am I missing something? -- Bert> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Spencer Graves > Sent: Wednesday, January 25, 2006 2:43 PM > To: r-help at stat.math.ethz.ch > Subject: [R] D(dnorm...)? > > Can someone help me understand the following: > > > D(expression(dnorm(x, mean)), "mean") > [1] 0 > > sessionInfo() > > R version 2.2.1, 2005-12-20, i386-pc-mingw32 > > attached base packages: > [1] "methods" "stats" "graphics" "grDevices" "utils" > "datasets" > [7] "base" > > By my computations, this should be something like > ((mean-x)/sd^2)*dnorm(...). > > Thanks for your help. > Spencer Graves > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Hi Spencer, I think if you have a problem that needs a lot of symbolic manipulation you are probably better off driving it directly from something like Maple or Mathematica (I prefer maple, actually) than trying to drive it from R. It just gets too clumsy. On the other hand it is very handy having a simple differentiator available in R, like D(...) for small jobs that are not worth taking to a big system like Maple. The point I was trying to make in the previous message was that with a little thought it could be made a lot more convenient. This arose in connexion with a real problem. We needed to differentiate a pdf that had the normal density function in it, but was otherwise quite simple and we had to hack the code in another system (not unlike R, as it happens) to handle it. The hack was quite small and it became clear that with a slight change of design users would not need to do hacks like this for simple extensions such as the one we needed. As it was a hack, we only put it in for the standard density and I suspect that is the reason why even now the derivative tables in both R and the other system (not unlike R) only handle normal density and distribution funcitons in one variable. I'm sort of avoiding your question, because I don't know how hard it would be to link R with Yacas, either, but if you really wanted to go that way I see that Yacas can be driven over the net via a Java applet. Something like this might provide the simplest link, if not the most efficient. But note that Yacas uses the eccentric Mathematica notation, where the functions are Capitalised, for example, as nouns are in German. That's a small bother you could do without, too. Regards, Bill Venables. -----Original Message----- From: Spencer Graves [mailto:spencer.graves at pdf.com] Sent: Thursday, 26 January 2006 2:14 PM To: Venables, Bill (CMIS, Cleveland) Cc: gunter.berton at gene.com; r-help at stat.math.ethz.ch; ripley at stats.ox.ac.uk Subject: Re: [R] D(dnorm...)? Hello, Bill: I'm not qualified to make this suggestion since I'm incapable of turning it into reality, but what about creating a link between R and one of the Mathematica clones like Yacas? I can immagine that it could be substantially more difficult than linking R to other software like Excel, but ... . Spencer Graves Bill.Venables at csiro.au wrote:> Yes Bert, this time you are missing something (unusually) ... > > As Brian Ripley pointed out 'dnorm' is in the derivative table, *but* > only as a function of one variable. So if you want to find the > derivative of > > dnorm(x, mean, sigma) > > you have to write it as 1/sigma * dnorm((x - mu)/sigma). Here is a > little example: > > >>D(Quote(pnorm((x-mu)/sigma)), "x") > > dnorm((x - mu)/sigma) * (1/sigma) > > >>D(D(Quote(pnorm((x-mu)/sigma)), "x"), "mu") > > (x - mu)/sigma * (dnorm((x - mu)/sigma) * (1/sigma)) * (1/sigma) > > --- > > Like Brian, I recall the suggestion that we make D(...) extensible. I > still think it is a good idea and worth considering. Under one scheme > you would specify an object such as > > Fnorm <- structure(quote(pnorm(x, mu, sigma)), > deriv = > list(x = Quote(dnorm(x, mu, sigma)/sigms), > mu = Quote(-dnorm(x, mu, sigma)/sigma), > sigma = Quote(-(x - mu)*dnorm(x, mu, sigma)/sigma^2), > class = "dfunction") > > ane write a generic "differentiate" function with a "dfunction" method > and "D" as the default. > > I don't think it's quite that easy, but the plan is clear enough. > > Bill. > > > > > > -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Berton Gunter > Sent: Thursday, 26 January 2006 8:58 AM > To: 'Spencer Graves'; r-help at stat.math.ethz.ch > Subject: Re: [R] D(dnorm...)? > > > dnorm() is an internal function, so I don't see how D (or deriv) cando> anything with it symbolically. Am I missing something? > > -- Bert > > > > >>-----Original Message----- >>From: r-help-bounces at stat.math.ethz.ch >>[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Spencer Graves >>Sent: Wednesday, January 25, 2006 2:43 PM >>To: r-help at stat.math.ethz.ch >>Subject: [R] D(dnorm...)? >> >> Can someone help me understand the following: >> >> > D(expression(dnorm(x, mean)), "mean") >>[1] 0 >> > sessionInfo() >> >>R version 2.2.1, 2005-12-20, i386-pc-mingw32 >> >>attached base packages: >>[1] "methods" "stats" "graphics" "grDevices" "utils" >> "datasets" >>[7] "base" >> >> By my computations, this should be something like >>((mean-x)/sd^2)*dnorm(...). >> >> Thanks for your help. >> Spencer Graves >> >>______________________________________________ >>R-help at stat.math.ethz.ch mailing list >>https://stat.ethz.ch/mailman/listinfo/r-help >>PLEASE do read the posting guide! >>http://www.R-project.org/posting-guide.html >> > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html
While symbolic computation is handy, I actually think a more pressing addition to R is some kind of automatic differentiation facility, particularly 'reverse mode' AD, which can be spectacular. There are free tools available for it as well, though I don't know how well developed they are. See: http://www-unix.mcs.anl.gov/autodiff/AD_Tools/ I admit this is not quite the same thing, but for statistical computations this is, in my experience, the key thing you need. (Well, for frequentist estimation at any rate...) There are commercial systems that use this idea already, of course. Two that I know of are 'ADMB' (and its associated 'ADMB-RE' for random effects) estimation and of course the 'S-NUOPT' module for another system not unlike R. ADMB is, frankly, difficult to use but it performs so well and so quickly once you get it going nothing else seems to come close to it. I has become almost a de-facto standard at the higher end of the fishery stock assessment game, for example, where they are always fitting huge, highly complex and very non-linear models. Bill V. -----Original Message----- From: Berwin A Turlach [mailto:berwin at bossiaea.maths.uwa.edu.au] On Behalf Of Berwin A Turlach Sent: Thursday, 26 January 2006 4:50 PM To: Spencer Graves Cc: Venables, Bill (CMIS, Cleveland); r-help at stat.math.ethz.ch; gunter.berton at gene.com; ripley at stats.ox.ac.uk Subject: Re: [R] D(dnorm...)? G'day Spencer,>>>>> "SG" == Spencer Graves <spencer.graves at pdf.com> writes:SG> I'm not qualified to make this suggestion since I'm incapable SG> of turning it into reality, [...] This statement applies to me too, nevertheless I would like to point out the following GPL library: http://www.gnu.org/software/libmatheval/ I am wondering since some time how hard it would be to incorporate that library into R and let it provide symbolic differentiation capabilities for R. Cheers, Berwin