[Rd] cor( x, y , method = "spearman" ) incorrect if any( is.na(c( x, y (PR#6448)

cberry at tajo.ucsd.edu cberry at tajo.ucsd.edu
Wed Jan 21 00:20:32 MET 2004


> version
         _                
platform i686-pc-linux-gnu
arch     i686             
os       linux-gnu        
system   i686, linux-gnu  
status                    
major    1                
minor    8.1              
year     2003             
month    11               
day      21               
language R                
> cor( 1:3, rep(NA,3) ) # OK
Error in cor(1:3, rep(NA, 3)) : missing observations in cov/cor
> cor( 1:3, rep(NA,3), method="spearman" ) # OUCH!!
[1] 1
> 

I think this would fix cor( ), but have not looked at the C-code for
additional gotcha's:

    if (method != "pearson") {
        Rank <- function(u) if (is.matrix(u)) 
            apply(u, 2, rank)
        else rank(u)
+       x.na <- is.na(x)
        x <- Rank(x)
+       is.na( x ) <- x.na
        if (!is.null(y)) {
+           y.na <- is.na(y)
            y <- Rank(y)
+           is.na(y) <- y.na
        }
    }

Chuck


Charles C. Berry                        (858) 534-2098 
                                         Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	         UC San Diego
http://hacuna.ucsd.edu/members/ccb.html  La Jolla, San Diego 92093-0717



More information about the R-devel mailing list