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

Gabor Grothendieck ggrothendieck at gmail.com
Fri Nov 18 16:02:58 CET 2011

```On Thu, Nov 17, 2011 at 4:40 PM, Katrina Bennett <kebennett at alaska.edu> 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.
>
> I've tried to do this with Ryacas as well, but I'm lost.
>
> Can anyone help?
>

You can do that with plain R:

> e <- quote(asym/((1+exp((xmid-x.seq)/scal))))
> for(v in all.vars(e)) cat("deriv wrt", v, "is", format(D(e, v)), "\n")
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

--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

```