Katrina Bennett
2011-Nov-17 21:40 UTC
[R] Obtaining a derivative of nls() SSlogis function
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? Thank you, Katrina [[alternative HTML version deleted]]
David Winsemius
2011-Nov-18 13:31 UTC
[R] Obtaining a derivative of nls() SSlogis function
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
I think you need to make an expression. I tried> nls.fn <- asym/((1+exp((xmid-x.seq)/scal)))Error: object 'asym' not found> nls.fn <- expression(asym/((1+exp((xmid-x.seq)/scal)))) > D(nls.fn,"asym")1/((1 + exp((xmid - x.seq)/scal)))>Does that help? Maybe there are other approaches too. JN On 11/18/2011 06:00 AM, r-help-request at r-project.org wrote:> Message: 88 > Date: Thu, 17 Nov 2011 12:40:59 -0900 > From: Katrina Bennett <kebennett at alaska.edu> > To: r-help at r-project.org > Subject: [R] Obtaining a derivative of nls() SSlogis function > Message-ID: > <CA+SNM83UtZMd1HDFGf2BioLPWGwhS-OWRMaHoV4yKQtvSwy4tQ at mail.gmail.com> > Content-Type: text/plain > > 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? > > Thank you, > > Katrina
Gabor Grothendieck
2011-Nov-18 15:02 UTC
[R] Obtaining a derivative of nls() SSlogis function
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