Dear R People: When the D function is used for a symbolic derivative, an expression is returned, which is fine. How do you change that expression to a function, please? I've been experimenting with substitute and deparse, but no success yet. This is R 1.8.0 for Windows. thanks in advance for the help! Sincerely, Erin Hodgess mailto: hodgess at gator.uhd.edu
Would it work to use deriv with the optional function.arg argument? In some ways deriv is just a wrapper around repeated calls to D. (It does a bit more than that but you could think of it that way.) Erin Hodgess <hodgess at gator.uhd.edu> writes:> Dear R People: > > When the D function is used for a symbolic derivative, > an expression is returned, which is fine. > > How do you change that expression to a function, please? > > I've been experimenting with substitute and deparse, but no success > yet. > > This is R 1.8.0 for Windows. > > thanks in advance for the help! > > Sincerely, > Erin Hodgess > mailto: hodgess at gator.uhd.edu > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help-- Douglas Bates bates at stat.wisc.edu Statistics Department 608/262-2598 University of Wisconsin - Madison http://www.stat.wisc.edu/~bates/
You should be able to use deriv(..., func=TRUE) to get a function returned, instead of an expression. If you need to use D, here's a rather clumsy way that seems to work:> dx2x <- D(expression(x^2), name="x"); dx2x2 * x> eval(parse(text=paste("f <- function(x){",paste(deparse(dx2x), collapse=";"), "}")))> ffunction(x){ 2 * x } HTH, Andy> From: Erin Hodgess [mailto:hodgess at gator.uhd.edu] > > Dear R People: > > When the D function is used for a symbolic derivative, > an expression is returned, which is fine. > > How do you change that expression to a function, please? > > I've been experimenting with substitute and deparse, but no > success yet. > > This is R 1.8.0 for Windows. > > thanks in advance for the help! > > Sincerely, > Erin Hodgess > mailto: hodgess at gator.uhd.edu
I can think of 2 ways: f <- function(x) { } body(f) <- D(expression(log(x)), "x") Or maybe g <- deriv(expression(log(x)), "x", function.arg = TRUE) f <- function(x) { attr(g(x), "gradient") } Or something along those lines. -roger Erin Hodgess wrote:> Dear R People: > > When the D function is used for a symbolic derivative, > an expression is returned, which is fine. > > How do you change that expression to a function, please? > > I've been experimenting with substitute and deparse, but no success > yet. > > This is R 1.8.0 for Windows. > > thanks in advance for the help! > > Sincerely, > Erin Hodgess > mailto: hodgess at gator.uhd.edu > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >