[R] Help with tryCatch with a for loop

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Wed Nov 9 22:38:27 CET 2011


Without a sample data set that fails the first pass and succeeds on the second pass this is a pain to test.

Don't forget to read the posting guide... Reproducible sample code isn't too reproducible without some specified data or autogenerated data.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

Spencer S <scheidt14 at gmail.com> wrote:

>Hello all,
>
>I'm a beginner in R working on a script that will produce a set of
>models
>(linear, polynomial and logistic) for each location in a dataset. 
>However,
>the self-starting logistic model often fails - if this happens I would
>like
>to just skip to the next iteration of the loop using tryCatch.  
>
>I've looked at a few examples and read the help file, but didn't
>understand
>tryCatch in the context of my script.  Any help or suggestions (even
>telling
>me where to insert the tryCatch command) would be much appreciated!!
>
>Below is the script I am currently working on:
>
>data<-read.csv(file.choose(),sep=",",header=T)
>#File name is cbc.subset
>
>data$location.code = as.character(data$location.code)
>
>locs = unique(location.code)
>
>pdf("mygraphs.pdf",height=8, width=10)
>par(mfrow=c(3,4))
>
>for(s in 1:length(locs)){
>  #To plot data from a particular stateroute:
>  sub.ECDO<-data[data$location.code == locs[s],]
>  plot(abund~year, data=sub.ECDO, main=locs[s])
>
>  #To plot the linear model for the specified location:
>  lmodel<-lm(abund~year, data=sub.ECDO)
>  abline(lmodel$coefficients[1],lmodel$coefficients[2],lty=2)
> 
>  #To plot the polynomial model for the specified location:
>  polymodel<-lm(abund~year+I(year^2), data=sub.ECDO)
>  xv<-seq(min(sub.ECDO$year),max(sub.ECDO$year),0.1)
>  yv<-predict(polymodel,list(year=xv))
>  lines(xv,yv)
>
>  #To plot the logistic model
>  #####tryCatch
>  logis<-nls(abund~SSlogis(year,a,b,c),data=sub.ECDO)
>  yv<-predict(logis,list(year=xv))
>  lines(xv,yv)
>
>  #To find which model is the best fit:
>  if ("logis" %in% ls()) {
>    AIC.results = AIC(lmodel,polymodel,logis)
>  } else {
>    AIC.results = AIC(lmodel,polymodel)
>  }
>  
>  #Add text to plot
>  text(min(sub.ECDO$year)+2,0.9*max(sub.ECDO$abund),paste("linear =
>",AIC.results[1,2],"\npolynomial = ",AIC.results[2,2],"\nlogistic =
>",AIC.results[3,2],sep=""))
>  
>  rm(logis)
>  rm(polymodel)
>  rm(lmodel)
>  
>}
>
>dev.off()
>
>Thank you!
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/Help-with-tryCatch-with-a-for-loop-tp4020475p4020475.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>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.



More information about the R-help mailing list