[R] automate curve drawing on nls() object

array chip arrayprofile at yahoo.com
Wed May 19 18:53:53 CEST 2010


I do know it, but wanted to spend a little more effort to make generalized function for this type of plot.

Thanks

--- On Tue, 5/18/10, Shi, Tao <shidaxia at yahoo.com> wrote:

> From: Shi, Tao <shidaxia at yahoo.com>
> Subject: Re: [R] automate curve drawing on nls() object
> To: "array chip" <arrayprofile at yahoo.com>, r-help at r-project.org
> Date: Tuesday, May 18, 2010, 9:25 PM
> In this case, Ben's approach is the
> way to go.  
> 
> I'm curious how you fit nls without knowing the model
> formula beforehand?
> 
> ...Tao
> 
> 
> 
> 
> 
> ----- Original Message ----
> > From: array chip <arrayprofile at yahoo.com>
> > To: r-help at r-project.org;
> TaoShi <shidaxia at yahoo.com>
> > Sent: Tue, May 18, 2010 5:22:47 PM
> > Subject: Re: [R] automate curve drawing on nls()
> object
> > 
> > well, this is not going automate enough because you
> have to know how the model 
> > (formula) looks like, and how many parameters there
> are in the model beforehand 
> > to do what you are suggesting.
> 
> Thanks
> 
> 
> --- On Tue, 5/18/10, 
> > Shi, Tao <
> > href="mailto:shidaxia at yahoo.com">shidaxia at yahoo.com>
> wrote:
> 
> > 
> > From: Shi, Tao <
> > href="mailto:shidaxia at yahoo.com">shidaxia at yahoo.com>
> > Subject: Re: 
> > [R] automate curve drawing on nls() object
> > To: "array chip" <
> > ymailto="mailto:arrayprofile at yahoo.com"
> 
> > href="mailto:arrayprofile at yahoo.com">arrayprofile at yahoo.com>,
> 
> > ymailto="mailto:r-help at r-project.org"
> 
> > href="mailto:r-help at r-project.org">r-help at r-project.org
> > Date: 
> > Tuesday, May 18, 2010, 7:42 PM
> > 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 <
> > href="mailto:arrayprofile at yahoo.com">arrayprofile at yahoo.com>
> > > 
> > To: 
> > href="mailto:r-help at r-project.org">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.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
> > 
> > 
> > ______________________________________________
> > 
> > > 
> > ymailto="mailto:
> > href="mailto:R-help at r-project.org">R-help at r-project.org"
> > 
> > 
> > > href="mailto:
> > href="mailto:R-help at r-project.org">R-help at r-project.org">
> > 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="
> > href="https://stat.ethz.ch/mailman/listinfo/r-help"
> target=_blank 
> > >https://stat.ethz.ch/mailman/listinfo/r-help"
> > target=_blank 
> > 
> > > >
> > target=_blank >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.
> > 
> > 
> > 
> >       
> > 
> 
> 
>       
> 






More information about the R-help mailing list