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.
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.
My apologies for not including some test data. Attached is a sample dataset that fails with the first stateroute and works with the second. http://r.789695.n4.nabble.com/file/n4021696/testdata.csv testdata.csv -- View this message in context: http://r.789695.n4.nabble.com/Help-with-tryCatch-with-a-for-loop-tp4020475p4021696.html Sent from the R help mailing list archive at Nabble.com.
If your loop is of the form
for(i in 1:n) {
doSomethingSafe()
x <- doSomethingThatFailsSometimes()
doSomethingWithX(x) # cannot do this if previous line fails
doSomethingElseSafe()
}
and you want it to somehow recover when
doSomethingThatFailsSometimes() throws an error,
then change it to use try() with
for(i in 1:n) {
doSomethingSafe()
tmp <- try({
x <- doSomethingThatFailsSometimes()
doSomethingWithX(x)
})
if (inherits(try, "try-error")) {
recoverFromFailure()
}
doSomethingElseSafe()
}
or to use tryCatch() with
for(i in 1:n) {
doSomethingSafe()
tryCatch({
x <- doSomethingThatFailsSometimes()
doSomethingWithX(x)
}, error = function(e) {
message("Had a problem at iteration ", i, ": ",
conditionMessage(e))
recoverFromFailure()
})
doSomethingElseSafe()
}
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at
r-project.org] On Behalf Of Spencer S
> Sent: Wednesday, November 09, 2011 2:57 PM
> To: r-help at r-project.org
> Subject: Re: [R] Help with tryCatch with a for loop
>
> My apologies for not including some test data.
> Attached is a sample dataset that fails with the first stateroute and works
> with the second.
>
> http://r.789695.n4.nabble.com/file/n4021696/testdata.csv testdata.csv
>
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/Help-with-tryCatch-with-a-for-loop-
> tp4020475p4021696.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.