[R] Plot nls line on plot?

David Winsemius dwinsemius at comcast.net
Thu Oct 8 21:55:37 CEST 2009


On Oct 8, 2009, at 3:39 PM, Douglas M. Hultstrand wrote:

> Hello,
>
> I have a non-linear function (exponential) that I am trying to  
> display the line with the data in a plot, is there a command similar  
> to abline() for the function I created, if not what is the best way  
> to display the fitted line.  Example code below.
>
> ### Example
> z <- c(1,10,15,20,25,30,35,40,45,50,55,60)
> p <- c(.02,.08,.2,.45,1.03,2.36,5.37,12.24,27.85,63.39,144.27,328.35)
> cnt <- length(z)
>
> fit <- nls(p~(b*exp(m*z)), start=list(m=.1645,b=.017),  
> control=list(maxiter=200))
> plot(p~z)

Perhaps instead:

  plot(z, predict(fit), type="l")
  points(z,p)

# not a very interesting exercise given the utter simplicity of the  
data, but it should point the way forward.

-- 

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list