[R] automate curve drawing on nls() object

From: array chip <arrayprofile_at_yahoo.com>
Date: Tue, 18 May 2010 16:13:33 -0700 (PDT)

Hi, I would like to use the curve() function to draw the predicted curve from an nls() object. for example:

dd<-read.table("dd.txt",sep='\t',header=T,row.names=1) obj<-nls(y~c+(d-c)/(1+(x/e)^b),data=dd,start=list(b=-1, c=0, d=100, e=150)) coef(obj)

          b c d e  -1.1416422 0.6987028 102.8613176 135.9373131 curve(0.699+(102.86-0.699)/(1+(x/135.94)^(-1.1416)),1,20000)

Now I am going to have a lot of datasets to do this, so certainly I would like to automate this. Suppose that I can create a character string for the formula, but I am not sure how to pass that character string into the curve() to make it work. The help page of curve() says the first argument is "an expression written as a function of x, or alternatively the name of a function which will be plotted". I tried the following, for example:

substitute(expression(c + (d - c)/(1 + (x/e)^b)),as.list(coef(obj))) will return:
"expression(0.698704171233635 + (102.861317499063 - 0.698704171233635)/(1 + (x/135.937317917920)^-1.14164217993857))"

so I tried:
curve(substitute(expression(c + (d - c)/(1 + (x/e)^b)), as.list(coef(obj))), 1,20000)

but it returns an error:
"Error in xy.coords(x, y, xlabel, ylabel, log) :
  'x' and 'y' lengths differ"

Any suggestions?

A related question:

If I do this:
substitute(expression(c + (d - c)/(1 + (x/e)^b)),as.list(coef(obj)))

I will get:
"expression(0.698704171233635 + (102.861317499063 - 0.698704171233635)/(1 + (x/135.937317917920)^-1.14164217993857))"

But if I do:

I only get:
"parse(text = as.character(obj$call$formula[3]), srcfile = NULL)" as a result, not have b,c,d,e replaced by coefficient values.


parse(text=as.character(obj$call$formula[3]),srcfile=NULL) returns the wanted expression:
"expression(c + (d - c)/(1 + (x/e)^b))"

Why is that?



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. Received on Tue 18 May 2010 - 23:18:40 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 19 May 2010 - 00:30:22 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive