Bill Shipley
2009-Mar-12 18:13 UTC
[R] avoiding termination of nls given convergence failure
Hello. I have a script in which I repeatedly fit a nonlinear regression to
a series of data sets using nls and the port algorithm from within a loop.
The general structure of the loop is:
for(i in 1:n){
… extract relevant vectors of dependent and independent variables …
… estimate starting values for Amax and Q.LCP…
fit<-nls(photosynthesis~fit.Mitcherlich(irradiance,Amax,LCP,Q.LCP),data=temp
,
start=list(Amax=Astart,Q.LCP=x,LCP=33),control=list(maxiter=100,tol=5e-4),
na.action=na.omit,trace=T,algorithm="port",lower=c(0,0,0))
…
}
Despite trying to estimate good starting values, the nls function
occasionally experiences problems with convergence. When this happens the
function stops and prints an error message, thus preventing the loop from
continuing. Is there some what of detecting the convergence problem while
preventing the nls function from stopping when this happens, so that the
loop can continue?
Bill Shipley
North American Editor, Annals of Botany
Département de biologie
Université de Sherbrooke
Sherbrooke (Québec) J1K 2R1
Canada
(819) 821-8000, poste 62079
(819) 821-8049 FAX
http://pages.usherbrooke.ca/jshipley/recherche/
[[alternative HTML version deleted]]
Joshua Reich
2009-Mar-12 18:18 UTC
[R] avoiding termination of nls given convergence failure
Hi Bill, You should have a look at the try() function that lets you enclose an expression and handle failure in a way that prevents the entire execution from halting. Josh On Thu, Mar 12, 2009 at 1:13 PM, Bill Shipley <bill.shipley@usherbrooke.ca>wrote:> Hello. I have a script in which I repeatedly fit a nonlinear regression to > a series of data sets using nls and the port algorithm from within a loop. > The general structure of the loop is: > > for(i in 1:n){ > > … extract relevant vectors of dependent and independent variables … > > … estimate starting values for Amax and Q.LCP… > > > > > > fit<-nls(photosynthesis~fit.Mitcherlich(irradiance,Amax,LCP,Q.LCP),data=temp > , > > > start=list(Amax=Astart,Q.LCP=x,LCP=33),control=list(maxiter=100,tol=5e-4), > > na.action=na.omit,trace=T,algorithm="port",lower=c(0,0,0)) > > … > > } > > > > Despite trying to estimate good starting values, the nls function > occasionally experiences problems with convergence. When this happens the > function stops and prints an error message, thus preventing the loop from > continuing. Is there some what of detecting the convergence problem while > preventing the nls function from stopping when this happens, so that the > loop can continue? > > > > Bill Shipley > > North American Editor, Annals of Botany > > Département de biologie > > Université de Sherbrooke > > Sherbrooke (Québec) J1K 2R1 > > Canada > > (819) 821-8000, poste 62079 > > (819) 821-8049 FAX > > > > http://pages.usherbrooke.ca/jshipley/recherche/ > > > > > [[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. > >-- Joshua Reich 646 256 4763 i2pi simplicity . depth . scalability [[alternative HTML version deleted]]
Gabor Grothendieck
2009-Mar-12 18:23 UTC
[R] avoiding termination of nls given convergence failure
If the goal here is to repeatedly run a particular model from different starting values then the nls2 package will do that for you. On Thu, Mar 12, 2009 at 2:13 PM, Bill Shipley <bill.shipley at usherbrooke.ca> wrote:> Hello. ?I have a script in which I repeatedly fit a nonlinear regression to > a series of data sets using nls and the port algorithm from within a loop. > The general structure of the loop is: > > ?for(i in 1:n){ > > ? extract relevant vectors of dependent and independent variables ? > > ? estimate starting values for Amax and Q.LCP? > > > > > fit<-nls(photosynthesis~fit.Mitcherlich(irradiance,Amax,LCP,Q.LCP),data=temp > , > > > start=list(Amax=Astart,Q.LCP=x,LCP=33),control=list(maxiter=100,tol=5e-4), > > ? ? ? ? ? ?na.action=na.omit,trace=T,algorithm="port",lower=c(0,0,0)) > > ? > > } > > > > Despite trying to estimate good starting values, the nls function > occasionally experiences problems with convergence. ?When this happens the > function stops and prints an error message, thus preventing the loop from > continuing. ?Is there some what of detecting the convergence problem while > preventing the nls function from stopping when this happens, so that the > loop can continue? > > > > Bill Shipley > > North American Editor, Annals of Botany > > D?partement de biologie > > Universit? de Sherbrooke > > Sherbrooke (Qu?bec) J1K 2R1 > > Canada > > (819) 821-8000, poste 62079 > > (819) 821-8049 FAX > > > > http://pages.usherbrooke.ca/jshipley/recherche/ > > > > > ? ? ? ?[[alternative HTML version deleted]] > > > ______________________________________________ > R-help at 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. > >
Ravi Varadhan
2009-Mar-12 18:24 UTC
[R] avoiding termination of nls given convergence failure
?try
For example,
for (i in 1:n) {
try (fit <- nls(...), silent=TRUE)
if (class(fit) != "try-error")
dowhateverthatneedstobedonewiththeresults
else fit <- NA
}
Ravi.
----------------------------------------------------------------------------
-------
Ravi Varadhan, Ph.D.
Assistant Professor, The Center on Aging and Health
Division of Geriatric Medicine and Gerontology
Johns Hopkins University
Ph: (410) 502-2619
Fax: (410) 614-9625
Email: rvaradhan at jhmi.edu
Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html
----------------------------------------------------------------------------
--------
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On
Behalf Of Bill Shipley
Sent: Thursday, March 12, 2009 2:13 PM
To: R-help
Subject: [R] avoiding termination of nls given convergence failure
Hello. I have a script in which I repeatedly fit a nonlinear regression to
a series of data sets using nls and the port algorithm from within a loop.
The general structure of the loop is:
for(i in 1:n){
extract relevant vectors of dependent and independent variables
estimate starting values for Amax and Q.LCP
fit<-nls(photosynthesis~fit.Mitcherlich(irradiance,Amax,LCP,Q.LCP),data=temp
,
start=list(Amax=Astart,Q.LCP=x,LCP=33),control=list(maxiter=100,tol=5e-4),
na.action=na.omit,trace=T,algorithm="port",lower=c(0,0,0))
}
Despite trying to estimate good starting values, the nls function
occasionally experiences problems with convergence. When this happens the
function stops and prints an error message, thus preventing the loop from
continuing. Is there some what of detecting the convergence problem while
preventing the nls function from stopping when this happens, so that the
loop can continue?
Bill Shipley
North American Editor, Annals of Botany
Dipartement de biologie
Universiti de Sherbrooke
Sherbrooke (Quibec) J1K 2R1
Canada
(819) 821-8000, poste 62079
(819) 821-8049 FAX
http://pages.usherbrooke.ca/jshipley/recherche/
[[alternative HTML version deleted]]