[R] automate curve drawing on nls() object

array chip arrayprofile at yahoo.com
Wed May 19 01:13:33 CEST 2010


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:
substitute(parse(text=as.character(obj$call$formula[3]),srcfile=NULL),as.list(coef(obj)))

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.

where
     
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?

Thanks

John



More information about the R-help mailing list