R-beta: deriv()

Thomas Lumley thomas at biostat.washington.edu
Sat Jul 5 20:19:46 CEST 1997


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 at stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=



More information about the R-help mailing list