[R] Catching an error with lm()

Ajay Narottam Shah ajayshah at mayin.org
Tue May 24 12:35:05 CEST 2005


I'm in a situation where I do a few thousand regressions, and some of
them are bad data. How do I get back an error value (return code such
as NULL) from lm(), instead of an error _message_?

Here's an example:

> x <- c(NA, 3, 4)
> y <- c(2, NA, NA)
> d <- lm(y ~ x)
Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
        0 (non-NA) cases
> str(d)
Error in str(d) : Object "d" not found

My question is: How do I force lm() to quietly send back an error code
like NULL? I am happy to then look at is.null(d) and handle it
accordingly. I am stuck because when things go wrong, there is no
object d to analyse!

(My production situation is a bit more complex. It is costly for me to
first verify that the data is sound. I'd like to toss it into lm() and
get an error code for null data).

Ajay Shah                                                   Consultant
ajayshah at mayin.org                      Department of Economic Affairs
http://www.mayin.org/ajayshah           Ministry of Finance, New Delhi

More information about the R-help mailing list