Re: [R] Query about using try block

From: talepanda <talepanda_at_gmail.com>
Date: Tue 23 Jan 2007 - 05:11:20 GMT

Usually (that is, not limited in R language), when error occurs in try, stacks are rollbacked, so the variables defined in try no longer exists after calling try.

One non-elegant solution is:

fit<-NULL
try ( (fit = lm(y~x, data = data_fitting)), silent =T) if(!is.null(fit)){
coeffs = as.list(coef(fit))
## other subsequent processes
}

On 1/23/07, lalitha viswanath <lalithaviswanath@yahoo.com> wrote:
> Hi
> Thanks for your response.
> However I seem to be doing something wrong regarding
> the try block resulting in yet another error described
> below.
>
> I have a function that takes in a file name and
> does the fit for the data in that file.
> Hence based on your input, I tried
>
> try ( (fit = lm(y~x, data = data_fitting)), silent =
> T);
>
>
> I left the subsequent lines of my code unchanged.
> coeffs = as.list(coef(fit);
> lambda = exp(coeffs$x)....
>
> After the change using try, when I tried to resume
> processing under R as follows
> source("fitting.R")
> for filename in list { process(filename); }
> It says "Cannot find object "fit" "...(in the line
> trying to get the coefficients...)
>
> Am I closing the try block in the wrong place?
> This function does some post processing on the
> coefficients returned by coef(fit), puts them in a
> list and sends it to another function.
> (i.e. around 6 lines of code after the call to fit).
> Thanks
> Lalitha
> --- Andreas Hary <andreashary@googlemail.com> wrote:
>
> > Look at ?try
> >
> > Your code will probably need to be something like
> > the following:
> >
> > fit <- list()
> > for(fileId in 1:n){
> > try(fit[i] <- lm(formula,data=???,...), silent=F)
> > #or silent=T if you would like to be made aware
> > of problems
> > }
> >
> > Best wishes,
> >
> > Andreas
> >
> >
> >
> >
> > lalitha viswanath wrote:
> > > Hi
> > > I am a newbie to R and am using the lm function
> > to
> > > fit my data.
> > > This optimization is to be performed for around
> > 45000
> > > files not all of which lend themselves to
> > > optimization. Some of these will and do crash.
> > >
> > > However, How do I ensure that the program simply
> > goes
> > > to the next file in line without exiting the code
> > with
> > > the error
> > > "Error in lm.fit(x, y, offset = offset,
> > singular.ok =
> > > singular.ok, ...) :
> > > NA/NaN/Inf in foreign function call (arg
> > 4)"
> > > everytime it encounters troublesome data?
> > >
> > > I would greatly appreciate your input as it would
> > > avoid me having to manually type
> > > for fileId in (c(4351:46000)) { ... }
> > > for fileId in (c(5761:46000)) { ... }, etc...
> > >
> > > Thanks
> > > Lalitha
> > >
> > >
> > >
> > >
> >
> ____________________________________________________________________________________
> > > Now that's room service! Choose from over 150,000
> > hotels
> > >
> > > ______________________________________________
> > > R-help@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.
> > >
> >
> > --
> > =============================
> > Andreas Hary
> > Flat 5, 70 Finsbury Park Road
> > Lond, N4 2JX, UK
> >
> > Email: andreashary@gmail.com
> > Mobile: 07906 860 987
> >
>
>
>
>
> ____________________________________________________________________________________
> Want to start your own business?
>
> ______________________________________________
> R-help@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.
>



R-help@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. Received on Tue Jan 23 16:17:18 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 23 Jan 2007 - 09:30:33 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.