Skip to content

Obtaining a derivative of nls() SSlogis function

4 messages · David Winsemius, Gabor Grothendieck, Katrina Bennett

#
On Nov 17, 2011, at 4:40 PM, Katrina Bennett wrote:

            
nls.fn is not a function or an expression. It has been evaluated and  
now it's a vectpr, and not what `deriv` is "expecting". If you want to  
use `deriv` or `D` you must first read the help page:

?deriv

And then construct a proper expresssion, call, or function...

 > nls.expr <- expression( 84.951/((1 + exp(( 66.90742- x)/ -6.3))) )
 > D(nls.expr, "x")
-(84.951 * (exp((66.90742 - x)/-6.3) * (1/6.3))/((1 + exp((66.90742 -
     x)/-6.3)))^2)
 > deriv(nls.expr, "x")
expression({
     .expr4 <- exp((66.90742 - x)/-6.3)
     .expr5 <- 1 + .expr4
     .value <- 84.951/.expr5
     .grad <- array(0, c(length(.value), 1L), list(NULL, c("x")))
     .grad[, "x"] <- -(84.951 * (.expr4 * (1/6.3))/.expr5^2)
     attr(.value, "gradient") <- .grad
     .value
})
#
On Thu, Nov 17, 2011 at 4:40 PM, Katrina Bennett <kebennett at alaska.edu> wrote:
You can do that with plain R:
deriv wrt asym is 1/((1 + exp((xmid - x.seq)/scal)))
deriv wrt xmid is -(asym * (exp((xmid - x.seq)/scal) * (1/scal))/((1 +
exp((xmid -      x.seq)/scal)))^2)
deriv wrt x.seq is asym * (exp((xmid - x.seq)/scal) * (1/scal))/((1 +
exp((xmid -      x.seq)/scal)))^2
deriv wrt scal is asym * (exp((xmid - x.seq)/scal) * ((xmid -
x.seq)/scal^2))/((1 +      exp((xmid - x.seq)/scal)))^2