Is it possible to compute the variance of every column in a matrix without a loop? -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
compute variance of every column in a matrix without a loop
6 messages · Francisco J Molina, Peter Dalgaard, Jonathan Baron +2 more
Francisco J Molina <FJMolina at lbl.gov> writes:
Is it possible to compute the variance of every column in a matrix without a loop?
apply(m,2,var)
O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Francisco J Molina wrote:
Is it possible to compute the variance of every column in a matrix without a loop?
What about apply(X, 2, var) Uwe Ligges -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On 03/16/02 19:43, Francisco J Molina wrote:
Is it possible to compute the variance of every column in a matrix without a loop?
If the matrix is m1, apply(m1,2,var) or, with missing data, apply(m1,2,var,na.rm=T) In general, apply() and related functions are well worth understanding.
Jonathan Baron, Professor of Psychology, University of Pennsylvania Home page: http://www.sas.upenn.edu/~baron -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Sat, 16 Mar 2002, Francisco J Molina wrote:
Is it possible to compute the variance of every column in a matrix without a loop?
No, but you can make it look as if there's no loop. You can hide the for() loop inside a function with apply(m, 2, var) if the reason you want to avoid a loop is to make your code look simpler. In 1.5.0 you will be able to do colMeans(m*m)-colMeans(m)^2 if the reason is to have the loop take place in compiled code and be faster. -thomas -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Thomas Lumley wrote:
On Sat, 16 Mar 2002, Francisco J Molina wrote:
Is it possible to compute the variance of every column in a matrix without a loop?
No, but you can make it look as if there's no loop. You can hide the for() loop inside a function with apply(m, 2, var) if the reason you want to avoid a loop is to make your code look simpler. In 1.5.0 you will be able to do colMeans(m*m)-colMeans(m)^2
..., but only for well conditioned problems, because using Steiner's theorem is risky from the computational point of view. Try this extreme example several times: m <- rnorm(10, 1e8, 1e-7) # large mean, small variance mean(m^2) - mean(m)^2 # typical results: -4, -2, 0, 2, 4 var(m) # reasonable, about 1e-14 BTW: Of course this is a computational problem that is *not* related to R!
if the reason is to have the loop take place in compiled code and be faster.
Uwe Ligges -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._