[R] *Building* a covariance matrix efficiently

Tsjerk Wassenaar tsjerkw at gmail.com
Mon Mar 14 15:13:44 CET 2011


deaRs,

I want to build a covariance matrix out of the data from a binary
file, that I can read in chunk by chunk, with each chunk containing a
single observation vector X. I wonder how to do that most efficiently,
avoiding the calculation of the full symmetric matrices XX'. The
trivial non-optimal approach boils down to something like:

Q <- matrix(rnorm(100000),ncol=200)
M <- matrix(0,ncol=200,nrow=200)
for (i in 1:nrow(Q))
  M <- M + tcrossprod(Q[i,])

I would appreciate pointers to help me fill this lacuna in my R skills :)

Cheers,

Tsjerk

-- 
Tsjerk A. Wassenaar, Ph.D.

post-doctoral researcher
Molecular Dynamics Group
* Groningen Institute for Biomolecular Research and Biotechnology
* Zernike Institute for Advanced Materials
University of Groningen
The Netherlands



More information about the R-help mailing list