[R] Linear Model "NA" Value Test

(Ted Harding) Ted.Harding at manchester.ac.uk
Mon Sep 21 23:02:45 CEST 2009


On 21-Sep-09 20:38:25, Douglas M. Hultstrand wrote:
> Hello,
> 
> I am deriving near real-time liner relationships based on 5-min
> precipitation data, sometimes the non-qced data result in a
> slope of NA. I am trying to read the coefficient (in this example x)
> to see if it is equal to NA, if it is equal to NA assign it a value
> of 1. I am having trouble with the if statement not recognizing the
> coefficient or "NA" value in the test.
> 
> Any thoughts, I supplied a really basic example below.
> 
> Thank you,
> Doug
> 
>#####################
>###### Example ######
>#####################
> x=c(1,1,1)
> y=c(1,1,1)
> fit <- lm(y~x) fit
> 
> Call:
> lm(formula = y ~ x)
> Coefficients:
> (Intercept)            x
> 1           NA
> 
> coef <- coef(fit) fit$coef[[2]]
> [1] NA
> if("fit$coef[[2]]" == "NA") {.cw = 1}

I take it you know why you are getting the NA in that example
(i.e. because the routine evaluates 0/0 with result NA).

But the test for "NA" is not "== NA", which itself always ireturns NA.
"==" is a test for equality of value. NA is not a value.

The best way to test whether something is NA is to use

  is.na(something)

which returns TRUE if "something" is NA. The logic of "NA" is that
it can be interpreted as "could be anything, and we don't know what".
So if

  x <- NA ; y <- NA

then "x == y" will return NA because
a) x could be anything; y could be the same anything or a different one.
b) Depending on what the unkown values of x and y "really are",
   "x == y" could be TRUE or FALSE, but we don't know which.

So, withing the domain of logical values, "x == y" could be either
TRUE or FALSE and we don't know which, so it could be anything
in that domain. Hence "x == y" returns NA. That's logical ...

Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 21-Sep-09                                       Time: 22:02:42
------------------------------ XFMail ------------------------------




More information about the R-help mailing list