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? 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)? Thank you, Eric Kort -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 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 therows 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 thiscalculation of the row means? function rowMeans> 2) How do I proceed to calculate the row variances with matrixoperations (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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Dear R experts and Omega experts, I am trying to run the R from Java. Now, I am facing the problem: UnsatisfiedLinkError: SJava.dll, please see the detail Error message below: Path is /cygdrive/c/Tcl/bin:.:/cygdrive/c/ora9iclient/jdk/jre/bin/classic:/cygdr ive/c/Documents and Settings/pgu/Desktop/oldmachine/rtools:/cygdrive/e/R/gcc/bin :/cygdrive/c/WINNT/system32:/cygdrive/c/WINNT:/cygdrive/c/WINNT/system32/WBEM:/c ygdrive/c/ora9iclient/bin:/cygdrive/c/ora9iclient/jdk/bin:/cygdrive/c/Perl/bin:/ cygdrive/f/Mingw/bin:/cygdrive/c/Program Files/HTML Help Workshop:/cygdrive/e/r- 1.5.1/library/SJava/libs:/cygdrive/e/r-1.5.1/src/gnuwin32 Hello... Exception in thread "main" java.lang.UnsatisfiedLinkError: E:\R-1.5.1\library\SJ ava\libs\SJava.dll: Can't find dependent libraries at java.lang.ClassLoader$NativeLibrary.load(Native Method) at java.lang.ClassLoader.loadLibrary0(Unknown Source) at java.lang.ClassLoader.loadLibrary(Unknown Source) at java.lang.Runtime.loadLibrary0(Unknown Source) at java.lang.System.loadLibrary(Unknown Source) at org.omegahat.R.Java.RForeignReference.<clinit>(RForeignReference.java :25) As one can see, that I do have the SJava.dll directory (e/r-1.5.1/library/SJava/libs) in the path. Please help. Thanks. OS: Window 2000 R version: R1.5.1 (compiled from Source code) RSJava version: 0.65 Thanks in advance for your help. Pauline -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Eric Kort <Eric.Kort at vai.org> wrote:> 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)?Brian D. Ripley <ripley at stats.ox.ac.uk> replied:> sweep does the subtraction, but I would make use of rowmeans(d^2) and > rowmeans(d) in the classic hand-calculation formula.Here are four useful functions that build on rowMeans and friends: colVars <- function(x, na.rm=FALSE, dims=1, unbiased=TRUE, SumSquares=FALSE, twopass=FALSE) { if (SumSquares) return(colSums(x^2, na.rm, dims)) N <- colSums(!is.na(x), FALSE, dims) Nm1 <- if (unbiased) N-1 else N if (twopass) {x <- if (dims==length(dim(x))) x - mean(x, na.rm=na.rm) else sweep(x, (dims+1):length(dim(x)), colMeans(x,na.rm,dims))} (colSums(x^2, na.rm, dims) - colSums(x, na.rm, dims)^2/N) / Nm1 } rowVars <- function(x, na.rm=FALSE, dims=1, unbiased=TRUE, SumSquares=FALSE, twopass=FALSE) { if (SumSquares) return(rowSums(x^2, na.rm, dims)) N <- rowSums(!is.na(x), FALSE, dims) Nm1 <- if (unbiased) N-1 else N if (twopass) {x <- if (dims==0) x - mean(x, na.rm=na.rm) else sweep(x, 1:dims, rowMeans(x,na.rm,dims))} (rowSums(x^2, na.rm, dims) - rowSums(x, na.rm, dims)^2/N) / Nm1 } colStdevs <- function(x, ...) sqrt(colVars(x, ...)) rowStdevs <- function(x, ...) sqrt(rowVars(x, ...)) ----------------------------------------------------------------------------- And in case any core member is inspired to put these into base, here's the .Rd: \name{colVars} \alias{colVars} \alias{rowVars} \alias{colStdevs} \alias{rowStdevs} \title{Column and Row Variances} \description{Variances, or standard deviations by column (or row) of an array.} \usage{ colVars (x, na.rm=FALSE, dims=1, unbiased=TRUE, SumSquares=FALSE, twopass=FALSE) rowVars (x, na.rm=FALSE, dims=1, unbiased=TRUE, SumSquares=FALSE, twopass=FALSE) colStdevs(x, ...) rowStdevs(x, ...) } \arguments{ \item{x}{A numeric array (or a dataframe to convert to a matrix).} \item{na.rm}{Logical: Remove NA's?} \item{dims}{Number of dimensions to sum over [colSums] or leave alone [rowSums]. Only useful when x is a multidimensional array.} \item{unbiased}{Logical: Use (N-1) in the denominator when calculating variance?} \item{SumSquares}{Logical: If TRUE, colVars just returns sums of squares.} \item{twopass}{Logical: If TRUE, colVars uses the corrected two-pass algorithm of Chan Golub & LeVeque, which is slower but less subject to roundoff error.} \item{...}{colStdevs/rowStdevs take the same arguments as colVars/rowVars.} } \details{On a matrix: colVars (x, na.rm) == apply(x, 2, var, na.rm=na.rm) rowVars (x, na.rm) == apply(x, 1, var, na.rm=na.rm) } \value{ A vector or array with dimensionality length(dim(x))-dims [colVars] or dims [rowVars]. Dimnames of the remaining dimensions are preserved. When the result is 1-dimensional, it is always demoted to a vector. } \author{Originally by Douglas Bates <bates at stat.wisc.edu> as package "MatUtils". Modified, expanded, and renamed by David Brahm <brahm at alum.mit.edu>, with help of course from the R-help gurus.} \seealso{\code{\link{apply}}} \examples{ x <- matrix(1:12, 3,4, dimnames=list(letters[1:3], LETTERS[1:4])) x[2,2] <- NA colVars(x) } \keyword{array} \keyword{arith} -- -- David Brahm (brahm at alum.mit.edu) -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._