Skip to content
Prev 255747 / 398506 Next

Deriving formula with deriv

On Apr 4, 2011, at 6:35 AM, kitty wrote:

            
For one thing you are not specifying what variable you want to  
differentiate with-respect-to: Assuming this to be `A` then:

  express <-
      expression( A/(1+exp((4*mu/A)*(lambda-time))+ 2))
  # The quotes looked "wrong" inside an expression, so I removed them

  d1<-deriv(express, "A")  #  but the diff w.r.t variable needs to be  
quoted.
   d1
expression({
     .expr1 <- 4 * mu
     .expr3 <- lambda - time
     .expr5 <- exp(.expr1/A * .expr3)
     .expr7 <- 1 + .expr5 + 2
     .value <- A/.expr7
     .grad <- array(0, c(length(.value), 1L), list(NULL, c("A")))
     .grad[, "A"] <- 1/.expr7 + A * (.expr5 * (.expr1/A^2  
* .expr3))/.expr7^2
     attr(.value, "gradient") <- .grad
     .value
})


All this should have been clear if you had looked at the examples in  
help(deriv).
That , too, is clearly exemplified in the help page.