Liao, Hongsheng
2009-Jul-30 17:14 UTC
[R] Continue to finish for loop even there is an error in one of rounds.
I am trying to fit a logistic model to my 10 year data (1999-2008) by year. Codes like below: Year <- c(1999: 2008) for(y in 1:length(year)) { file.input <- paste("C:\\", year[y], "\\data.csv", sep="") table <- read.csv(file=fileinput, header=TRUE, as.is=TRUE, na.strings=c("")) initial <- getInitial(percent ~ SSlogis(age, Asym, xmid, scal), data = table) fm1 <- nls(percent ~ SSlogis(age, Asym, xmid, scal), data = table) summary <- summary(fm1) parameters <- summary$parameters file.output <- paste("C:\\", year[y], "\\parameters.csv", sep="") write.csv(parameters, file=file.output, row.names=FALSE) } However, when one of 10 years can't be converged, such as 2000, the loop stops in that year (2000) even though the rest years (2001 to 2008) can be converged. I would really appreciate if someone helps me on how to force the loop function to finish all 10 rounds even when errors occur due to no convergences for some of years. Hongsheng (Hank) Liao Lab Manager Center for Quantitative Fisheries Ecology Old Dominion University Phone: 757-683-4571 [[alternative HTML version deleted]]
Scott Sherrill-Mix
2009-Jul-30 18:26 UTC
[R] Continue to finish for loop even there is an error in one of rounds.
You probably want to wrap the nls(...) in a 'try' or 'tryCatch' function. Scott Scott Sherrill-Mix Department of Microbiology University of Pennsylvania 402B Johnson Pavilion 3610 Hamilton Walk Philadelphia, PA 19104-6076 Phone: 215-573-3141 On Thu, Jul 30, 2009 at 1:14 PM, Liao, Hongsheng<HLiao at odu.edu> wrote:> I am trying to fit a logistic model to my 10 year data (1999-2008) by year. ?Codes like below: > > Year <- c(1999: 2008) > for(y in 1:length(year)) { > ? ? ? ?file.input <- paste("C:\\", year[y], "\\data.csv", sep="") > table <- read.csv(file=fileinput, header=TRUE, as.is=TRUE, na.strings=c("")) > ? ? ? ?initial <- getInitial(percent ~ SSlogis(age, Asym, xmid, scal), data = table) > fm1 <- nls(percent ~ SSlogis(age, Asym, xmid, scal), data = table) > ? ? ? ?summary <- summary(fm1) > ? ? ? ?parameters <- summary$parameters > ? ? ? ?file.output <- paste("C:\\", year[y], "\\parameters.csv", sep="") > ? ? ? ?write.csv(parameters, file=file.output, row.names=FALSE) > } > > However, when one of 10 years can't be converged, such as 2000, the loop stops in that year (2000) even though the rest years (2001 to 2008) can be converged. I would really appreciate if someone helps me on how to force the loop function to finish all 10 rounds even when errors occur due to no convergences for some of years. > > > Hongsheng (Hank) Liao > Lab Manager > Center for Quantitative Fisheries Ecology > Old Dominion University > Phone: 757-683-4571 > > > > > ? ? ? ?[[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. >