Re: [R] Calculate variance/covariance with complex numbers
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(1)+1i*rnorm(1),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 __ R-help@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.
Re: [R] Calculate variance/covariance with complex numbers
On Sun, 28 Mar 2010, Gang Chen wrote: 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(1)+1i*rnorm(1),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) Do you agree? Argh! Sorry for the 'thinko' (like a 'typo', but a misapplication of the brain rather than the fingers)! I'd use this: crossprod( sweep( xri, 2, colMeans(xri), - ))/(nrow(xri)-1) or crossprod(xri - rep(colMeans(xri),each=nrow(xri)))/(nrow(xri)-1) Chuck Gang On Sat, Mar 27, 2010 at 7:07 PM, Charles C. Berry cbe...@tajo.ucsd.edu wrote: On Sat, 27 Mar 2010, Gang Chen wrote: Anybody knows what functions can be used to calculate variance/covariance with complex numbers? var and cov don't seem to work: How about? xri - matrix(rnorm(1)+1i*rnorm(1),nc=2) crossprod(xri-colMeans(xri))/(nrow(xri)-1) HTH, Chuck a 1 V1 0.00810014+0.00169366i V2 0.00813054+0.00158251i V3 0.00805489+0.00163295i V4 0.00809141+0.00159533i V5 0.00813976+0.00161850i var(a) 1 1 1.141556e-09 Warning message: In var(a) : imaginary parts discarded in coercion cov(a) 1 1 1.141556e-09 Warning message: In cov(a) : imaginary parts discarded in coercion Thanks in advance, Gang __ R-help@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. Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cbe...@tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 __ R-help@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. Charles C. Berry(858) 534-2098 Dept of Family/Preventive Medicine E mailto:cbe...@tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 __ R-help@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.
Re: [R] Calculate variance/covariance with complex numbers
Rolf Turner wrote: On 28/03/2010, at 11:50 AM, Ben Bolker wrote: Gang Chen gangchen6 at gmail.com writes: Anybody knows what functions can be used to calculate variance/covariance with complex numbers? var and cov don't seem to work: How about: y - complex(real=5:1,imag=2:6) z - complex(real=1:5,imag=6:10) complex.var - function(x) { mx - mean(x) n - length(x) mean((x-mx)^2)*n/(n-1) } SNIP Don't you want variances to be positive (non-negative) reals? IIRC the population variance of a complex random variable is E(|X - mu|^2) not E((X - mu)^2) So shouldn't your complex.var function have Mod(x-mx)^2 where you currently have (x-mx)^2 ??? cheers, Rolf Probably. To be honest, I wasn't thinking about it that carefully. It crossed my mind to wonder what the appropriate definition was of the variance for complex numbers, but I didn't bother to go find out. I guess it goes to show (yet again) that advice may be worth what you pay for it ... cheers Ben signature.asc Description: OpenPGP digital signature __ R-help@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.
Re: [R] Calculate variance/covariance with complex numbers
On 03/29/2010 04:17 AM, Charles C. Berry wrote: ... Argh! Sorry for the 'thinko' (like a 'typo', but a misapplication of the brain rather than the fingers)! Well, a typo is the _result_ of a misapplication of the fingers to a keyboard, so I would expect that if the result of thinking can be called a meme, then the appropriate term for the mistake would be a memo. I have seen plenty of examples. Jim __ R-help@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.
Re: [R] Calculate variance/covariance with complex numbers
Gang Chen gangchen6 at gmail.com writes: Anybody knows what functions can be used to calculate variance/covariance with complex numbers? var and cov don't seem to work: How about: y - complex(real=5:1,imag=2:6) z - complex(real=1:5,imag=6:10) complex.var - function(x) { mx - mean(x) n - length(x) mean((x-mx)^2)*n/(n-1) } complex.cov - function(x,y) { mx - mean(x) my - mean(y) n - length(x) mean((x-mx)*(y-my))*n/(n-1) } complex.var(z) complex.cov(y,z) ## check that they agree with the usual defs for y,z real: z - 1:5 var(z)-complex.var(z) y - c(6,5,4,2,3) cov(y,z)-complex.cov(y,z) __ R-help@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.
Re: [R] Calculate variance/covariance with complex numbers
On Sat, 27 Mar 2010, Gang Chen wrote: Anybody knows what functions can be used to calculate variance/covariance with complex numbers? var and cov don't seem to work: How about? xri - matrix(rnorm(1)+1i*rnorm(1),nc=2) crossprod(xri-colMeans(xri))/(nrow(xri)-1) HTH, Chuck a 1 V1 0.00810014+0.00169366i V2 0.00813054+0.00158251i V3 0.00805489+0.00163295i V4 0.00809141+0.00159533i V5 0.00813976+0.00161850i var(a) 1 1 1.141556e-09 Warning message: In var(a) : imaginary parts discarded in coercion cov(a) 1 1 1.141556e-09 Warning message: In cov(a) : imaginary parts discarded in coercion Thanks in advance, Gang __ R-help@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. Charles C. Berry(858) 534-2098 Dept of Family/Preventive Medicine E mailto:cbe...@tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 __ R-help@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.
Re: [R] Calculate variance/covariance with complex numbers
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(1)+1i*rnorm(1),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) Do you agree? Gang On Sat, Mar 27, 2010 at 7:07 PM, Charles C. Berry cbe...@tajo.ucsd.edu wrote: On Sat, 27 Mar 2010, Gang Chen wrote: Anybody knows what functions can be used to calculate variance/covariance with complex numbers? var and cov don't seem to work: How about? xri - matrix(rnorm(1)+1i*rnorm(1),nc=2) crossprod(xri-colMeans(xri))/(nrow(xri)-1) HTH, Chuck a 1 V1 0.00810014+0.00169366i V2 0.00813054+0.00158251i V3 0.00805489+0.00163295i V4 0.00809141+0.00159533i V5 0.00813976+0.00161850i var(a) 1 1 1.141556e-09 Warning message: In var(a) : imaginary parts discarded in coercion cov(a) 1 1 1.141556e-09 Warning message: In cov(a) : imaginary parts discarded in coercion Thanks in advance, Gang __ R-help@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. Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cbe...@tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 __ R-help@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.