[R] number of observations used in cor when use="pairwise.obs"

William Dunlap wdunlap at tibco.com
Wed Dec 16 20:55:22 CET 2009


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of 
> Jean-Christophe Domenge
> Sent: Wednesday, December 16, 2009 11:48 AM
> To: r-help
> Subject: [R] number of observations used in cor when 
> use="pairwise.obs"
> 
> Dear R gurus,
> to compute the correlation matrix of "n" variables with 
> "n_obs" observations
> each,
> possibly including NA, I use cor(M,  use="pairwise.obs")
> where m is a "n" x "nobs" matrix.
> 
> Now I want to know the number of observations actually used in this
> computation,
> namely for each pair of columns in M, say pair (i,j),  I want 
> to compute
> sum( !is.na(M[,i])  &  !is.na(M[,j]) ).
> I can think of several ways of constructing all pairs (i,j) 
> with i<j, then
> computing the sum
> above, but for n=2000 and nobs=700 the computation time is 
> prohibitive.

Try
   crossprod(!is.na(M))

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> 
> Is there an efficient way to solve this?
> 
> Many thanks,
> 
> jc
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 




More information about the R-help mailing list