[R] Catch errors

Stephen Tucker brown_emu at yahoo.com
Tue Aug 7 00:41:45 CEST 2007


That's because tag <- 1 is evaluated in a local environment (of the function)
- once function(err) exits, the information is lost. Try R's <<- operator:

tag <- 0
tryCatch(fit.lme <- lme(Beta ~ Trust*Sex*Freq, random = ~1|Subj,  
         Model), error=function(err) tag <<- 1)



--- Gang Chen <gangchen at mail.nih.gov> wrote:

> I wanted something like this:
> 
>  >tag <- 0;
>  >tryCatch(fit.lme <- lme(Beta ~ Trust*Sex*Freq, random = ~1|Subj,  
> Model), error=function(err) tag <- 1);
> 
> but it seems not working because 'tag' does not get value of 1 when  
> error occurs. How can I make it work?
> 
> Thanks,
> Gang
> 
> 
> On Aug 6, 2007, at 1:44 PM, Stephen Tucker wrote:
> 
> > ?try
> >
> > or
> >
> > ?tryCatch
> > http://www.maths.lth.se/help/R/ExceptionHandlingInR/
> >
> > for example...
> >
> > tryCatch(lme(Y ~ X1*X2, random = ~1|subj, Model[i]),
> >          error=function(err) return(0))
> >
> > (you can do something with 'err' or just return 0 as above)
> >
> > --- Gang Chen <gangchen at mail.nih.gov> wrote:
> >
> >> I run a linear mixed-effects model in a loop
> >>
> >> for (i in 1:N) {
> >>     fit.lme <- lme(Y ~ X1*X2, random = ~1|subj, Model[i]);
> >> }
> >>
> >> As the data in some iterations are all (or most) 0's, leading to the
> >> following error message from lme:
> >>
> >> Error in chol((value + t(value))/2) : the leading minor of order 1 is
> >> not positive definite
> >>
> >> What is a good way to catch the error without spilling on the screen
> >> so that I can properly stuff the corresponding output with some
> >> artificial value such as 0?
> >>
> >> Thanks,
> >> Gang
> >>
> >> ______________________________________________
> >> R-help at stat.math.ethz.ch 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.
> >>
> >
> >
> >
> >
> > ______________________________________________________________________ 
> > ______________
> > Looking for a deal? Find great prices on flights and hotels with  
> > Yahoo! FareChase.
> > http://farechase.yahoo.com/
> 



      ____________________________________________________________________________________



More information about the R-help mailing list