kjetikj@astro.uio.no
2000-Nov-26 20:21 UTC
[Rd] Problem with NAs using chisq.test() (PR#748)
Full_Name: Kjetil Kjernsmo Version: 1.1.1.1 (patched the rpois :-)) OS: osf4.0e Submission from: (NULL) (129.240.28.227) Dear all, I have just gotten through a debugging session of my code, being confused for a few days. I sent what I thought was a straightforward matrix to chisq.test() and once in a while got the error message:> chisq.test(t2)Error in chisq.test(t2) : missing value where logical needed Now, it turned out that a fairly deep bug in my code occasionally produced NAs in the matrix, and that was the source of the confusion. Looking at the code, it is apparent that the test if (any(x < 0) || any(is.na(x))) stop("all entries of x must be nonnegative and finite") was designed to catch this, but it doesn't seem to, because the first any()-call is NA and so when using the operator ||, well, I bet you understand this better than I... :-) I figured, perhaps turn this around: if (any(is.na(x)) || any(x < 0)) stop("all entries of x must be nonnegative and finite") That seems to work:> chisq.test(t2)Error in chisq.test(t2) : all entries of x must be nonnegative and finite Now, I hope this was a true bug, not just one of my misunderstandings, and that this is a real fix too.... :-) Kjetil -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Sun, 26 Nov 2000 kjetikj@astro.uio.no wrote:> Full_Name: Kjetil Kjernsmo > > > Now, it turned out that a fairly deep bug in my code occasionally produced > NAs in the matrix, and that was the source of the confusion. Looking at > the code, it is apparent that the test > if (any(x < 0) || any(is.na(x))) > stop("all entries of x must be nonnegative and finite") > was designed to catch this, but it doesn't seem to, because the first > any()-call is NA and so when using the operator ||, well, I bet you > understand this better than I... :-) > > I figured, perhaps turn this around: > if (any(is.na(x)) || any(x < 0)) > stop("all entries of x must be nonnegative and finite") > That seems to work: > > chisq.test(t2) > Error in chisq.test(t2) : all entries of x must be nonnegative and finite > > Now, I hope this was a true bug, not just one of my misunderstandings, and that > this is a real fix too.... :-)This is a real fix; it may be a real bug. any(x<0) correctly returns NA with NAs in x, that is, it doesn't know whether there are any negative entries because some are missing. any(x<0) || any(is.na(x)) returns NA because of short-circuit evaluation. That is, we do sequential if()s on each element from left to right. The first one fails because of NAs. Reversing the expression is more sensible because you would expect to rule out NAs before testing for x<0. However, it would also be reasonable for NA || B to evaluate B and return TRUE or NA depending on the value. This is what S-PLUS 5 does, so at least it's an incompatibility. The R documentation says The longer form [ie ||]evaluates left to right examining only the first element of each vector. Evaluation proceeds only until the result is determined. which at least seems incomplete as NA | TRUE gives TRUE, but NA || TRUE gives an error. -thomas Thomas Lumley Assistant Professor, Biostatistics University of Washington, Seattle -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._