[R] t-test via matrix operations
ripley@stats.ox.ac.uk
ripley at stats.ox.ac.uk
Wed Aug 14 18:21:27 CEST 2002
On Wed, 14 Aug 2002, Kort, Eric wrote:
> I need to calculate a large number of t statistics, and would like to do so via matrix operations. So far I have figured out a way to calculate the mean of each row of the matrix:
>
> d <- matrix(runif(100000,1,10), 1000, 10) # some test data
> s <- rep(1,ncol(d)) # a sum vector to use for matrix multiplication
> means <- (d%*%s)/ncol(d)
>
> This is at least 1 order of magnitude faster than iterating through the
rows and using the mean function (<1/100th seconds vs. 13/100th seconds
on my computer).
>
> Two questions:
> 1) Do you see a way to further optimize/simplify/elegantize this
calculation of the row means?
function rowMeans
> 2) How do I proceed to calculate the row variances with matrix
operations (specifically, how can I use matrix operations to subtract each
element of each row from the corresponding row mean)?
sweep does the subtraction, but I would make use of rowmeans(d^2) and
rowmeans(d) in the classic hand-calculation formula.
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list