[R] Ack! Odd correlation matrix problem

Spencer Graves spencer.graves at pdf.com
Sat Apr 16 05:44:17 CEST 2005


Does the following answer the question: 

 > set.seed(1)
 > B <- matrix(rnorm(21), ncol=3)
 > diag(B) <- NA
 > cor(B, use="complete.obs")
          [,1]      [,2]      [,3]
[1,] 1.0000000 0.4992305 0.9149359
[2,] 0.4992305 1.0000000 0.1433292
[3,] 0.9149359 0.1433292 1.0000000
 > cor(B, use="pairwise.complete.obs")
         [,1]     [,2]     [,3]
[1,] 1.000000 0.432209 0.410608
[2,] 0.432209 1.000000 0.309776
[3,] 0.410608 0.309776 1.000000
 > cor(B[,1:2], use="complete.obs")
         [,1]     [,2]
[1,] 1.000000 0.432209
[2,] 0.432209 1.000000

      hope this helps.  spencer graves

Chris Bergstresser wrote:

> Hi all --
>
>    Obviously, I'm missing something frightfully basic with the 
> following.  What's likely going wrong?
>
> > cr = cor(cluster.data, use = "complete.obs");
> > cr["tax", "spend"]
> [1] -0.6138096
> > cor(cluster.data[["tax"]], cluster.data[["spend"]],
> +     use = "complete.obs")
> [1] -0.4925006
>
> > df = data.frame(tax = cluster.data$tax,
> +                 spend = cluster.data$spend);
> > cr = cor(df, use = "complete.obs");
> > cr["tax", "spend"]
> [1] -0.4925006
> > cor(df[["tax"]], df[["spend"]], use = "complete.obs")
> [1] -0.4925006
>
> -- Chris
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html




More information about the R-help mailing list