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
array chip <arrayprofile <at> yahoo.com> writes:> Hi, I would like to use the curve() function to draw the > predicted curve from an nls() object. for example: >Is there a reason you don't want to use plot() and predict() ???> dd<-read.table("dd.txt",sep='\t',header=T,row.names=1)note that this is not a reproducible example ...> 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) >predframe <- data.frame(x=seq(1,20000,length.out=101)) predframe <- cbind(predframe,y=predict(obj,newdata=predframe)) with(predframe,plot(y~x,type="l"))
I can't directly answer your question regarding 'expression', but can you just replace b, c,d, and e with coef(obj)[1], coef(obj)[2], ... etc. You still can automate the whole process this way, right? ----- Original Message ----> From: array chip <arrayprofile at yahoo.com> > To: r-help at r-project.org > Sent: Tue, May 18, 2010 4:13:33 PM > Subject: [R] automate curve drawing on nls() object > > 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.9373131curve(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 ______________________________________________> ymailto="mailto:R-help at r-project.org" > href="mailto:R-help at r-project.org">R-help at r-project.org mailing list> href="https://stat.ethz.ch/mailman/listinfo/r-help" target=_blank > >https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting> guide http://www.R-project.org/posting-guide.htmland provide commented,> minimal, self-contained, reproducible code.
Maybe Matching Threads
- file descriptor leak in getSrcLines in R 2.10.0 svn 48590
- Issue with getParserData in R3.0.0
- [External] Re: Segfault when parsing UTF-8 text with srcrefs
- sys.source() does not provide the parsing info to eval()
- Segfault when parsing UTF-8 text with srcrefs