R-beta: deriv()

Thomas Lumley (thomas@biostat.washington.edu)
Sat, 5 Jul 1997 11:19:46 -0700 (PDT)


Date: Sat, 5 Jul 1997 11:19:46 -0700 (PDT)
From: Thomas Lumley <thomas@biostat.washington.edu>
To: r-testers <r-testers@stat.math.ethz.ch>
Subject: R-beta: deriv()
In-Reply-To: <9704300754.AA19200@alpha.luc.ac.be>


The deriv() function does not set the class of the result to "expression".
This means that eval() must be used twice to evaluate the function.

Using the example on p436 of Chambers & Hastie:
> deriv(formula,"alpha")->a
> eval(a)
expression({
        .expr1 <- D * alpha
        .expr2 <- exp(.expr1)
        .expr3 <- 1 + .expr2
        .value <- log(.expr3) - .expr1
        .grad <- array(0, c(length(.value), 1), list(NULL, c("alpha")))
        .grad[, "alpha"] <- .expr2 * D/.expr3 - D
        attr(.value, "gradient") <- .grad
        .value
})
> eval(eval(a))
[1] 0.3132617
attr(,"gradient")
          alpha
[1,] -0.2689414
> class(a)<-"expression" 
> eval(a)
[1] 0.3132617
attr(,"gradient")
          alpha
[1,] -0.2689414


Thomas Lumley
------------------------------------------------------+------
Biostatistics		: "Never attribute to malice what  :
Uni of Washington	:  can be adequately explained by  :
Box 357232		:  incompetence" - Hanlon's Razor  :
Seattle WA 98195-7232	:				   :
------------------------------------------------------------

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=