From maechler at stat.math.ethz.ch  Sat Sep 13 16:29:03 2008
From: maechler at stat.math.ethz.ch (Martin Maechler)
Date: Sat, 13 Sep 2008 16:29:03 +0200
Subject: [Rd] .. var() fails on NA in R 2.7.2 but not R 2.6.1
In-Reply-To: <3c12769c0809051550j6b52f6a5y2329de79b25d66de@mail.gmail.com>
References: <20080905223827.GA61846@piskorski.com>
	<3c12769c0809051548m2aa78da8i296e1a607576789@mail.gmail.com>
	<3c12769c0809051550j6b52f6a5y2329de79b25d66de@mail.gmail.com>
Message-ID: <18635.52783.33920.673292@cmath-4.math.ethz.ch>

Hi,
I'm getting back to this topic, from a slightly different angle:

>>>>> "HenricN" == Henric Nilsson (Public) <nilsson.henric at gmail.com>
>>>>>     on Sat, 06 Sep 2008 03:47:06 +0200 writes:

    HenricN> Andrew Piskorski wrote:
    >> I recently started using R 2.7.2, and noticed a surprising change in
    >> the behavior of var() on NA data:
    >>
    >> R 2.6.1 (Patched), 2007-11-26, svn.rev 43541, x86_64-unknown-linux-gnu:
    >>
    >> > var(rep(NA,3))
    >> [1] NA

  [.................]

After some R-core internal discussion,
I have implemented the following two changes in 
R-devel {to be R 2.8.0 soon}, and I'm hereby "RFCing" i.e.,
requesting for (your) comments: 

1) cov() & cor()  gain a new option 'use = "everything"',
  *and* that is the new default in the cases where "all.obs" has been the
  default, i.e. including the default uses  of var() or sd()
  {Andy had stdev(), but that's not a standard R function}.

  Explicit use of  use = "all.obs" (or other previously existing
  options of 'use') will remain unchanged of course.

  The new implementation (of use = "everything"), as committed to R-devel
  a few days ago,  basically replaces all error messages
    "missing observations in cov/cor"  (and *only* those, at the moment!)
  by returning  NA  in the corresponding places
  (many, if we have a cov/var/cor *matrix*).

2)  cov() & cor()  gain a new option 'use = "na.or.complete"',
              {better proposals for this "na.or.complete" are still
               welcome, but please think twice: we have partial matching...}
  *and* that is the new default in the cases where "complete.obs" has been the
  default, notably for var(*, na.rm=TRUE).
  Consequently, all of
       var(double(0))
       var(double(0), na.rm=TRUE)
  and  var(NA, na.rm=TRUE)
  now return  NA  instead of an error
  { "empty 'x'" or  "no complete element pairs" }

  This will entail that

    i.  {empty vector <==> {vector of all NAs (= empty after na.omit)}
   ii.  var(<empty>) |->  NA

  by default.

---- 

Feedback is very welcome!

Martin Maechler, ETH Zurich and R core team.



[Rd] .. var() fails on NA in R 2.7.2 but not R 2.6.1
Martin Maechler