[R] Calculate variance/covariance with complex numbers

Ben Bolker bolker at ufl.edu
Sun Mar 28 18:18:36 CEST 2010


Gang Chen <gangchen6 <at> gmail.com> writes:

> 
> Thanks a lot for the help, Ben Bolker and Chuck!
> 
> Chuck, it seems your version needs a little modification. Instead of this:
> 
> > xri <- matrix(rnorm(10000)+1i*rnorm(10000),nc=2)
> > crossprod(xri-colMeans(xri))/(nrow(xri)-1)
> 
> it seems to me it should be this (maybe there is a more elegant
> modification than mine?):
> > crossprod(t(apply(xri, 1, '-', colMeans(xri))))/(nrow(xri)-1)
> 

   Not sure, but you could take a look at sweep() and scale( ,scale=FALSE)
for slightly more specialized/efficient ways of doing the column centering,
i.e.

  crossprod(scale(xri,scale=FALSE))/(nrow(xri)-1)

 (warning, almost completely untested)

  Ben



More information about the R-help mailing list