[R] Obtaining a derivative of nls() SSlogis function

David Winsemius dwinsemius at comcast.net
Fri Nov 18 14:31:22 CET 2011


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

> Hello, I am wondering if someone can help me. I have the following  
> function
> that I derived using nls() SSlogis. I would like to find its  
> derivative. I
> thought I had done this using deriv(), but for some reason this isn't
> working out for me.
>
> Here is the function:
> asym <- 84.951
> xmid <- 66.90742
> scal <- -6.3
>
> x.seq <- seq(1, 153,, 153)
> nls.fn <- asym/((1+exp((xmid-x.seq)/scal)))
>
> try #1
> deriv(nls.fn)
> #get an Error in .Internal(deriv.default(expr, namevec,  
> function.arg, tag,
> hessian)) : 'namevec' is missing
>
> try #2
> deriv(nls.fn, namevec=c("asym", "xmid", "scal"))
> #this doesn't seem to give me the expression, and the gradients are  
> zero.

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
})

-- 
David.

>
> I've tried to do this with Ryacas as well, but I'm lost.
>
> Can anyone help?
>
> Thank you,
>
> Katrina
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list