From Martin Maechler <maechler@stat.math.ethz.ch>  Fri May 19 14:31:00 2000
From: Martin Maechler <maechler@stat.math.ethz.ch> (Martin Maechler)
Date: Fri, 19 May 2000 15:31:00 +0200 (CEST)
Subject: [Rd] variance of a scalar (PR#546)
In-Reply-To: <200005190848.JAA22344@toucan.stats.ox.ac.uk>
References: <200005190848.JAA22344@toucan.stats.ox.ac.uk>
Message-ID: <14629.16916.782701.119727@lynne.ethz.ch>

>>>>> "BDR" == Prof Brian Ripley <ripley@stats.ox.ac.uk> writes:

    >> From: J.C.Rougier@durham.ac.uk Date: Fri, 19 May 2000 10:42:04 +0200
    >> (MET DST)
    >> 
    >> I was surprised to find that the variance of a scalar, using var(),
    >> is NA.  Surely this should be zero?

    BDR> Do you mean a vector of length 1? (S has no scalars.)  No, it
    BDR> should be NA, as sum((x-xbar)^2)/(n-1) is NaN, or, in statistical
    BDR> terms, one has no idea at all of the variability from a single
    BDR> sample.

    BDR> S gives NA too.

Now I'm becoming really picky ....

There *is* a bug[let] but not the way you thought :
As we have seen,  when  length(x) == 1,
var(x) should be semantically equivalent to  
	      (x - x)^2 / (1 - 1) == 0 / 0 = NaN
as Brian says above.

Now the bug is:  R returns NA, and not NaN.
whereas e.g. S-plus 5.1 (and probably all the other
 ``non-R S language implementations'') return NaN, but it's not immediately
visible since only R prints NaN's as "NaN" whereas ``S'' prints NaNs as "NA".

I'm about to fix this bug (in src/main/cov.c) and also mention the
"1/(n-1)" denominator in the documentation, since it seems that (some)one could
think of other denominators.

Martin

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


[Rd] variance of a scalar (PR#546)
Martin Maechler