From ripley at stats.ox.ac.uk  Fri Nov  5 06:57:56 2004
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Fri Nov  5 06:58:02 2004
Subject: [Rd] covariance bug (PR#7342)
In-Reply-To: <20041105035524.BD85210FA8@slim.kubism.ku.dk>
Message-ID: <Pine.LNX.4.44.0411050549130.10397-100000@gannet.stats>

Please do read the posting guide and the FAQ.  E.g. the posting guide says

  Common posting mistakes:
  ...
  Finding a bug in an old version of R that has been fixed in the most 
  recent version.

This was a bug in 1.8.0, but the current version of R is 2.0.0 with 2.0.1 
in beta test.  We can't fix bugs retrospectively.

If you take a look at the `bugs fixed' lists since 1.8.0 you will think
you were remiss not to have upgrade a long time ago.

On Fri, 5 Nov 2004 christianlederer@t-online.de wrote:

> Full_Name: Christian Lederer
> Version: 1.8.0
> OS: Linux
> Submission from: (NULL) (217.229.7.13)
> 
> 
> R-1.8.0 seems to calculate wrong covariances, when the argument of cov() 
> is a matrix or a data frame.
> The following should produce a matrix of zeroes and NaNs:

Actually of zeroes and NAs, and it does in current R.

> x <- matrix(c(NA ,NA ,0.9068995 ,NA ,-0.3116229,
>               -0.06011117 ,0.7310134 ,NA ,1.738362 ,0.6276125,
>               0.6615581 ,NA ,NA ,-2.646011 ,-2.126105,
>               NA ,1.081825 ,NA ,1.253795 ,1.520708,
>               0.2822814 ,NA ,NA ,NA ,NA,
>               0.03291028 ,NA ,NA ,NA ,NA,
>               NA ,NA ,NA ,-0.5462126 ,-0.1997394,
>               NA ,-0.3419413 ,-0.2675226 ,-1.000133 ,-0.1346234,
>               NA ,NA ,-0.411743 ,1.301612 ,NA,
>               0.922197 ,NA ,0.9513522 ,0.2357021 ,NA),
>             nrow=10, ncol=5)
> 
> c1 <- cov(x, use="pairwise.complete")
> 
> c2 <- matrix(nrow=5, ncol=5)
> for (i in 1:5) 
> {
>     for (j in 1:5) 
>     {
>         c2[i,j] <- cov(x[,i], x[,j], use="pairwise.complete")
>     }
> }
> 
> c2-c1
> 
> Instead, R-1.8.0 produces this result:
> 
>             [,1]        [,2]       [,3]          [,4]        [,5]
> [1,]  0.00000000 -0.03053828         NA -0.0144996353 -0.03485883
> [2,] -0.03053828 -0.01649857         NA  0.0137259383 -0.02960707
> [3,]          NA          NA -0.1296134            NA          NA
> [4,] -0.01449964  0.01372594         NA -0.0003152629  0.08717648
> [5,] -0.03485883 -0.02960707         NA  0.0871764791  0.04961190
> 
> This happens as well under Linux (Suse 9.1) as well as under Windows NT.
> 
> Under 1.9.1 (Linux) and 1.9.0 (Windows) i get the expected matrix of
> zeroes and NaNs.
> 
> This example is not very special. Under R-1.8.0 cov produced wrong result
> for any random matrix i tried.
> 
> Doesn't this mean, that *any* result obtained under R 1.8.0 is unreliable?

>From cov(x, use="pairwise.complete"), yes.  But that is a very unusual 
usage.

> By the way, i just recompiled R-1.8.0 from source under Linux and tried
> 'make check'. All tests were ok.
> Does there exist a more detailed set of tests, which could insure that
> at least the most basic R functions work correctly?

This is not a `most basic R function'.  Look in the test directory in the 
sources to see that we do have an extensive series of tests.

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


[Rd] covariance bug (PR#7342)
Prof Brian Ripley