Christiana Anagnostou
2012-Dec-14 07:09 UTC
[R] Define a custom-built loss function and combine it with nls()
Dear R helpers, For an allometric analysis (allometric equation y = a*x^b) I would like to apply a non-linear regression instead of using log-log transformations of the measured parameters x and y and a Model II linear regression. Since both of the variables x and y are random, I would like to apply a Model II non-linear analog of either Reduced Major Axis or Major Axis Regression. The allometric equation for the non-linear regression model would be: y = a*x^b, with y as the dimension of a body part (dependent variable) and x a dimension of body size (independent variable). With applying a non-linear regression I would like to get estimates for the parameters a and b. Fortunately, I already have a recommended loss function, proposed by Ebert & Russel 1994, J. Theor. Biol.: loss = abs(y*abs(x-(y/a)^(1/b))-a/(b+1)*abs(x^(b+1)-(y/a)^((b+1)/b))) How may I define the loss function above and combine it with a non-linear regression nls() in R? Example for a data frame with the measured variables x and y: x<-1:10 y<-c(0.3,0.7,0.9,1.3,2.0,2.4,3.3,3.8,5.0,5.8) d<-data.frame(x,y) Non-linear regression with inbuilt loss-function (sum of squared residuals): nlmodel<-nls(y~a*x^b,data=d,start=list(a=1,b=0.1),trace=T) Many thanks!!! Christiana -- Christiana Anagnostou ***************************************** Department of Evolutionary Ecology and Genetics Zoological Institute Christian-Albrechts-University of Kiel Am Botanischen Garten 9 24118 Kiel Germany Tel: +49-431-8804145 (work) Tel: +49-431-5709649 (home) Fax: +49-431-8802403 Email: canagnostou@zoologie.uni-kiel.de Web: www.uni-kiel.de/zoologie/evoecogen/ [[alternative HTML version deleted]]
Bert Gunter
2012-Dec-14 17:12 UTC
[R] Define a custom-built loss function and combine it with nls()
Inline. Note: My comments subject to confirmation by true experts. On Thu, Dec 13, 2012 at 11:09 PM, Christiana Anagnostou < canagnostou@zoologie.uni-kiel.de> wrote:> Dear R helpers, > > For an allometric analysis (allometric equation y = a*x^b) I would like > to apply a non-linear regression instead of using log-log > transformations of the measured parameters x and y and a Model II linear > regression. Since both of the variables x and y are random, I would like > to apply a Model II non-linear analog of either Reduced Major Axis or > Major Axis Regression. > > The allometric equation for the non-linear regression model would be: y > = a*x^b, with y as the dimension of a body part (dependent variable) and > x a dimension of body size (independent variable). With applying a > non-linear regression I would like to get estimates for the parameters a > and b. > > Fortunately, I already have a recommended loss function, proposed by > Ebert & Russel 1994, J. Theor. Biol.: > > loss = abs(y*abs(x-(y/a)^(1/b))-a/(b+1)*abs(x^(b+1)-(y/a)^((b+1)/b))) > > How may I define the loss function above and combine it with a > non-linear regression nls() in R? >-- Doubt that you can. You'll need to use an explicit optimization function. See ?optim and the CRAN Optimization task view. Note that you cannot use a gradient-based optimizer as your function is not continuously differentiable (due to abs()). -- Bert> > > > Example for a data frame with the measured variables x and y: > > x<-1:10 > > y<-c(0.3,0.7,0.9,1.3,2.0,2.4,3.3,3.8,5.0,5.8) > > d<-data.frame(x,y) > > Non-linear regression with inbuilt loss-function (sum of squared > residuals): > > nlmodel<-nls(y~a*x^b,data=d,start=list(a=1,b=0.1),trace=T) > > Many thanks!!! > > Christiana > > -- > Christiana Anagnostou > ***************************************** > Department of Evolutionary Ecology and Genetics > Zoological Institute > Christian-Albrechts-University of Kiel > Am Botanischen Garten 9 > 24118 Kiel > Germany > Tel: +49-431-8804145 (work) > Tel: +49-431-5709649 (home) > Fax: +49-431-8802403 > Email: canagnostou@zoologie.uni-kiel.de > Web: www.uni-kiel.de/zoologie/evoecogen/ > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm [[alternative HTML version deleted]]