[R] possible bug in function 'var' in R 2.7.2?
bolker at ufl.edu
Thu Oct 2 15:07:02 CEST 2008
<klaus.steenberg.larsen <at> risoe.dk> writes:
> Dear R-Help,
> I have used R2.6.0 until I recently installed also R2.7.2 (see details below)
> In R 2.6.0, the following script using the function 'var' (cor(stats)):
> x.test <- c(NA, NA, NA, NA)
> var(x.test, na.rm=T)
> gives the output:
> In R2.7.2 the output of the same script generates an error message and stops R:
> 'Error in var(x.test, na.rm = T) : no complete element pairs'
> R2.7.2 can handle it if there is just one non-NA value in the list but not if
they are all NA.
> I prefer the output of 2.6.0. Is this a bug in 2.7.2 or is it a deliberate
change compare to previous versions?
> Or is there a way to make R2.7.2 give NA as output?
> Thank you for any helo/comments!
> Best regards,
This is a deliberate change, but the behavior will
(more or less) revert in version 2.8.0.
From the NEWS file for 2.7 (in bug fixes):
o co[rv](use = "complete.obs") now always gives an error if there
are no complete cases: they used to give NA if
method = "pearson" but an error for the other two methods.
(Note that this is pretty arbitrary, but zero-length vectors
always give an error so it is at least consistent.)
Since sd(na.rm=TRUE) and var(na.rm=TRUE) both call cov(use =
"complete.obs"), this applies also to them.
cor(use="pair") used to give diagonal 1 even if the variable
was completely missing for the rank methods but NA for the
Pearson method: it now gives NA in all cases.
cor(use="pair") for the rank methods gave a matrix result with
dimensions > 0 even if one of the inputs had 0 columns.
From the NEWS file for the development version
o var(),cov(),cor() etc now by default (when 'use' is not specified)
return NA in many cases where they signalled an error before.
I don't know of a really easy way to make the behavior revert, perhaps
the easiest workaround is to make a 'my.var' function that first
tests if(all(is.na(x))) -- if you want to live really dangerously
you could even call it 'var' and have it mask the built-in function,
but that's probably a bad idea.
More information about the R-help