[R] confidence interval for nls

Peter Dalgaard p.dalgaard at biostat.ku.dk
Fri Oct 8 11:59:57 CEST 2004


Henrik Andersson <h.andersson at nioo.knaw.nl> writes:

> I tried the example and it works fine,
> 
> but why o why, do I not get any gradient from another prediction?


Apparently this depends on whether you supply an RHS which returns the
gradient. The built-in SSxxxx all do that.
 
> ##plot(yran~x)
> mich <- function(x,K,rmax) rmax*x/(x+K)
> mm.nls <- nls(yran~mich(x,K,rmax),start=list(K=5,rmax=3))

So: Here you need to add a gradient computation to mich(). One simple
way (and pretty much what nls does internally) is

mich <- function(x,K,rmax)
           numericDeriv(quote(rmax*x/(x+K)),c("K","rmax"),
                       parent.frame())

(well, figuring out the parent.frame() bit wasn't all *that* trivial!)

Or, nice when dealing with simple functions:

mmDeriv <- deriv(quote(rmax*x/(x+K)),c("K","rmax"))
mich <- function(x,K,rmax) eval.parent(mmDeriv)

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list