[R] parameter estimates from nls

Douglas Bates bates at stat.wisc.edu
Wed Jan 8 20:04:03 CET 2003

mmiller3 at iupui.edu (Michael A. Miller) writes:

> >>>>> "David" == David Parkhurst <parkhurs at indiana.edu> writes:
>     > How can I get at the estimated parameter values from a
>     > non-linear model fitted by nls in R, so as to plot the
>     > fitted curve?  If I have f.t <- nls(f~a*exp(b*t), ...)
>     > then type names(f.t), all that shows up is m for model,
>     > data, and call.  I don't see how to get at a and b, other
>     > than to print them.
> They can be gotten with a coef (or coefficients) call to the fit
> result: coef(f.t)['a']

Exactly - the general rule is to use the extractor functions like
coef() rather than relying on specific names in the fitted model
objects.  In this particular case you don't even need to extract the
fitted parameter values.  The easiest way to plot the fitted curve is
to use the predict method for the nls object as shown in the last part
of example(Puromycin)

Prmycn> conc <- seq(0, 1.2, len = 101)

Prmycn> lines(conc, predict(fm1, list(conc = conc)), lty = 1, 
    col = 1)

In your case just establish a vector of values of t, say tvals, then

plot(tvals, predict(f.t, list(t = tvals)))


lines(tvals, predict(f.t, list(t = tvals)))

More information about the R-help mailing list