Victor Landeiro
2009-Jul-25 18:36 UTC
[R] how to avoid a for looping break after an error message
Hi all, I wrote a piece of code that generates simulated variables. after variable generation I use them in several analyzes. However, when I use a for to repeat the procedure 1000 times I get an erro message in one of the "for" steps, precisely at this time: gls.temp<- gls(y2 ~ x2,correlation=corExp(form=~coord2[,1]+coord2[,2])) # coord 2 are spatial coordinates and the error message is: Error in gls(y2 ~ x2, correlation = corExp(form = ~coord2[, 1] + coord2[, : false convergence (8) I don´t know what causes this error. My problem is that this error stops all the calculations during the for looping and all previous results are lost. Can anyone help me to avoid that my looping stops after the error message? Thanks in advance, Victor Landeiro PS: A reproducible example is above, but there is a chance that the error message do not appears, and i reaches 1000: for(i in 1:1000){ x<-sample(seq(10,n*10,10)+runif(n,-2,2)) coord<-cbind(seq(10,n*10,10),rep(1,n)) x<-sort(x) colnames(coord)<-c("X","Y") y=(1+0.8*x)+runif(n,-(n*3),(n*3)) x2<-rep(x,each=nn)+rnorm(n*nn,0,2) y2<-rep(y,each=nn)+rnorm(n*nn,0,2) coord2<-cbind(rep(coord[,1],each=nn)+rnorm(n*nn),rep(1,n*nn)) gls<- gls(y2 ~ x2,correlation=corGaus(form=~coord2[,1]+coord2[,2])) print(i) } -- Victor Lemes Landeiro Instituto Nacional de Pesquisas da Amazônia - INPA - CPEc (Ecologia) Av. André Araujo, 2936, Petrópolis CEP: 69067-375 Manaus, Amazonas, Brasil Telefones: INPA (92) 3643 1912 Casa (92) 3646 3942 ou 3304 3942 Celular (92) 88311121 www.inpa.gov.br http://ppbio.inpa.gov.br http://ppbio.inpa.gov.br/Port/public/disciplinas2/Introducao%20ao%20R%202009.pdf [[alternative HTML version deleted]]
Victor Landeiro wrote:> > Hi all, > I wrote a piece of code that generates simulated variables. after variable > generation I use them in several analyzes. > However, when I use a for to repeat the procedure 1000 times I get an erro > message in one of the "for" steps, precisely at this time: > gls.temp<- gls(y2 ~ x2,correlation=corExp(form=~coord2[,1]+coord2[,2])) # > coord 2 are spatial coordinates > and the error message is: > Error in gls(y2 ~ x2, correlation = corExp(form = ~coord2[, 1] + coord2[, > : false convergence (8) > I don?t know what causes this error. My problem is that this error stops > all > the calculations during the for looping and all previous results are lost. > Can anyone help me to avoid that my looping stops after the error message? > Thanks in advance, > Victor Landeiro > > <snip> > >You could wrap the offending function in the try statement: gls.temp <- try( gls(y2 ~ x2,correlation=corExp(form=~coord2[,1]+coord2[,2])) ) Then you want to check the class of gls.temp- if it is a "try-error", the data you are expecting will not be there: if( class(gls.temp) == 'try-error' ){ # You had an error, maybe just skip this iteration using next }else{ # It's all good- do your normal calculations } Hope that helps! -Charlie ----- Charlie Sharpsteen Undergraduate Environmental Resources Engineering Humboldt State University -- View this message in context: http://www.nabble.com/how-to-avoid-a-for-looping-break-after-an-error-message-tp24660446p24660575.html Sent from the R help mailing list archive at Nabble.com.