# Re: [R] need suggestion about building formual

From: Spencer Graves <spencer.graves_at_pdf.com>
Date: Wed 12 Oct 2005 - 10:54:43 EST

Have you considered writing a function to do the complex math and then calling nls referring to that function? Consider the following (not tried):

expg <- function(a., x, G0, R, T){

exp((a.[1]+(a.[2]*x)^(1/2)-G0)/(R*T))
}

mynls<-nls(formula=y~expg(a.=c(a, b), x=x, G0=G0, R=R, T=T),

data=mydata,...)

If "nls" stops prematurely, I then write another function, "SSE" to compute the sum of squares of deviations from y and then ask "optim" to minimize "SSE", using "hessian=TRUE". If you try this and have trouble making it work, please send another post.

spencer graves

Simple wrote:

> Thanks for your kind respond. Although the answer didn't solve my question
> clearly,maybe I still not understand the art of R.
>
> I'm sorry that I had not talked the problem clearly, maybe a example with more
> detail will be suitable as suggested in the the posting guide.
>
> In function fitting program, such as Sigmaplot, a fitting formula, can be
> write in separate form:
> G=a+(b*x)^(1/2)
> k=exp((G-G0)/(R*T))
> fit k to y
>
> of course,in R's nls, can write as:
> mynls<-nls(formula=y~exp((a+(b*x)^(1/2)-G0)/(R*T)),data=mydata,...)
>
> In this example, the formula is simple and acceptable. However, when the
> formula is more complexity,writing all in one formula,the readability will be
> damaged.So I'm looking for a way to write simple and readable code in this
> situation.
>
>
> Spencer Graves wrote:
>

```>>	  I'm not certain what you are asking.
>>
>>	  You can build expressions in R as character strings and then execute
>>them.  Example:
>>
>>expr <- paste("two <-", 1, "+", 1)
>>eval(parse(text=expr))
>>two
>>
>>guide, "www.R-project.org/posting-guide.html".  It can help increase the
>>chances of a quick and useful reply.
>>
>>	  spencer graves
>>
>>Simple wrote:
>>
>>>hi,
>>>I'm an newbie for R,I want do some fitting in R.
>>>
>>>I wander if it is possible to write a few of equations but only one
>>>formual when fitting
>>>
>>>Currently,My problem is,in R, is there methods combination a few
>>>equations into one formual?
>>>For example,
>>>y=f1(k);
>>>k=f2(t);
>>>t=f3(x);
>>>although it is certain that the can be equations turn into one formual as
>>>y~f(x),but write such a complexity string make me painful.
>>>
>>>I have searched the web and found out there were only examples with one
>>>formual.any suggestion?
>>>
>>>I hope that I have omit something.
```

>
>
>
> ------------------------------------------------------------------------
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
```--
Spencer Graves, PhD
Senior Development Engineer
PDF Solutions, Inc.
333 West San Carlos Street Suite 700
San Jose, CA 95110, USA

spencer.graves@pdf.com
www.pdf.com <http://www.pdf.com>
Tel:  408-938-4420
Fax: 408-280-7915

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help