[R] confidence interval for nls

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Oct 7 15:02:25 CEST 2004

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

> Do I have the right impression that it's currently not possible to
> produce confidence intervals for the nls predictions using R?
> I had a course were we used SAS PROC nlin and there you could get
> intervals for the parameters and the prediction but I do not have
> access to SAS.
> Would it be difficult to implement, I tried to dig into the help pages
> of nls, vcov and nlsModel but I could not really make sense out of
> this?

It's pretty trivial (if you stick with the linear approximation), once
you realize that predict.nls actually returns the gradient in an

se.fit <- sqrt(apply(attr(predict(fm,list(Time = tt)),"gradient"),1, 
                  function(x) sum(vcov(fm)*outer(x,x))))
matplot(tt, predict(fm,list(Time = tt))+
               outer(se.fit,qnorm(c(.5, .025,.975))),type="l")

points(demand ~ Time, data = BOD)

One slight issue is that it doesn't work if "newdata" is omitted, but
then you can easily get the gradient from fm$m$gradient()

   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