Jean-Christophe Domenge
2009-Dec-16 19:47 UTC
[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. Is there an efficient way to solve this? Many thanks, jc [[alternative HTML version deleted]]
William Dunlap
2009-Dec-16 19:55 UTC
[R] number of observations used in cor when use="pairwise.obs"
> -----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. >
Maybe Matching Threads
- cor, cov, method "pairwise.complete.obs"
- strange behavior of cor() with pairwise.complete.obs
- bug? in stats::cor for use=complete.obs with NAs
- Wrong result with cor(x, y, method="spearman", use="complete.obs") with NA's???
- Error in cor.default(x1, x2) : missing observations in cov/cor