An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20111117/30dac9f1/attachment.pl>
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:
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
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
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20111118/8155a99d/attachment.pl>