?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: rvarad...@jhmi.edu Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html ---------------------------------------------------------------------------- -------- -----Original Message----- From: r-help-boun...@r-project.org [mailto:r-help-boun...@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]] ______________________________________________ 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.