klaus.steenberg.larsen at risoe.dk
2008-Oct-02 09:06 UTC
[R] possible bug in function 'var' in R 2.7.2?
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: NA 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, Klaus Details on R versions installed and platform: Platform: Windows XP Prof, English, ver. 5.1 R2.6.0: "LC_COLLATE=Danish_Denmark.1252;LC_CTYPE=Danish_Denmark.1252;LC_MONETARY=Danish_Denmark.1252;LC_NUMERIC=C;LC_TIME=Danish_Denmark.1252" R2.7.2: "LC_COLLATE=Danish_Denmark.1252;LC_CTYPE=Danish_Denmark.1252;LC_MONETARY=Danish_Denmark.1252;LC_NUMERIC=C;LC_TIME=Danish_Denmark.1252" Klaus Steenberg Larsen Scientist Phone direct +45 4677 4157 klaus.steenberg.larsen@risoe.dk Biosystems Division Risø National Lab. for sustainable Energy Risø DTU Building 309, P.O. Box 49 DK-4000 Roskilde, Denmark Tel +45 4677 4100 Fax +45 4677 4160 www.risoe.dk>From 1 January 2007, Risø National Laboratory, the Danish Institute for Food and Veterinary Research,the Danish Institute for Fisheries Research, the Danish National Space Center and the Danish Transport Research Institute have been merged with the Technical University of Denmark (DTU) with DTU as the continuing unit. [[alternative HTML version deleted]]
<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: > > NA > > 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 ifthey are all NA.> > I prefer the output of 2.6.0. Is this a bug in 2.7.2 or is it a deliberatechange 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, > > KlausThis 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. Ben Bolker