Hello, suppose I have a list with matrices: a=list(x1=matrix(rnorm(10),5,2),x2=matrix(rnorm(10),5,2),x3=matrix(rnorm(10),5,2)) I want to compute for all combination of xi and xj (x1,x2 x1,x3 and x2,x3) a value. This value is given for the pair x1,x2 by trace(x1%*%t(x1)%*%x2%*%t(x2)) / trace(x1%*%t(x1))*trace(x2%*%t(x2)) I know that product matrices t(xi)%*%xi can be obtained by: aa=lapply(a,crossprod) but I do not know how to "mix" the values in aa to obtain the desired values. Is there a way to do it without for loop ? Thanks in advances, sincerely St??phane DRAY -------------------------------------------------------------------------------------------------- D??partement des Sciences Biologiques Universit?? de Montr??al, C.P. 6128, succursale centre-ville Montr??al, Qu??bec H3C 3J7, Canada Tel : (514) 343-6111 poste 1233 Fax : (514) 343-2293 E-mail : stephane.dray at umontreal.ca -------------------------------------------------------------------------------------------------- Web http://www.steph280.freesurf.fr/
Hi! Not sure if I got your point. But if it is to compute apply a function to all pairs (x1,x2);(x1,x3);(x2,x3) where x1,x2,x3 are stored in a list you can take look at the sources of function listdist which computes an object of class dist from a list, in the package "pairseqsim" available from bioconducor, to get an idea how to do this. /E *********** REPLY SEPARATOR *********** On 20.09.2004 at 13:24 Stephane DRAY wrote:>Hello, > >suppose I have a list with matrices: > >a=list(x1=matrix(rnorm(10),5,2),x2=matrix(rnorm(10),5,2),x3=matrix(rnorm(10),5,2)) > >I want to compute for all combination of xi and xj (x1,x2 x1,x3 and x2,x3) >a value. >This value is given for the pair x1,x2 by trace(x1%*%t(x1)%*%x2%*%t(x2)) / >trace(x1%*%t(x1))*trace(x2%*%t(x2)) > >I know that product matrices t(xi)%*%xi can be obtained by: > >aa=lapply(a,crossprod) > >but I do not know how to "mix" the values in aa to obtain the desired >values. > >Is there a way to do it without for loop ? > >Thanks in advances, > >sincerely > >St??phane DRAY >-------------------------------------------------------------------------------------------------- > >D??partement des Sciences Biologiques >Universit?? de Montr??al, C.P. 6128, succursale centre-ville >Montr??al, Qu??bec H3C 3J7, Canada > >Tel : (514) 343-6111 poste 1233 Fax : (514) 343-2293 >E-mail : stephane.dray at umontreal.ca >-------------------------------------------------------------------------------------------------- > >Web >http://www.steph280.freesurf.fr/ > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Stephane: 1. apply() functions ARE (implicit) loops. 2. ?outer might be useful, but I suspect would be even more inefficient ... so I think the answer is, no, you must loop either explicitly or implicitly. However, my guess is that whatever you're trying to do is either built into R or can be done much more efficiently. Explicit matrix multiplication is almost always avoidable in statistics. -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Stephane DRAY > Sent: Monday, September 20, 2004 10:25 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Multiple operations on list > > Hello, > > suppose I have a list with matrices: > > a=list(x1=matrix(rnorm(10),5,2),x2=matrix(rnorm(10),5,2),x3=ma > trix(rnorm(10),5,2)) > > I want to compute for all combination of xi and xj (x1,x2 > x1,x3 and x2,x3) > a value. > This value is given for the pair x1,x2 by > trace(x1%*%t(x1)%*%x2%*%t(x2)) / > trace(x1%*%t(x1))*trace(x2%*%t(x2)) > > I know that product matrices t(xi)%*%xi can be obtained by: > > aa=lapply(a,crossprod) > > but I do not know how to "mix" the values in aa to obtain the > desired values. > > Is there a way to do it without for loop ? > > Thanks in advances, > > sincerely > > St?phane DRAY > -------------------------------------------------------------- > ------------------------------------ > > D?partement des Sciences Biologiques > Universit? de Montr?al, C.P. 6128, succursale centre-ville > Montr?al, Qu?bec H3C 3J7, Canada > > Tel : (514) 343-6111 poste 1233 Fax : (514) 343-2293 > E-mail : stephane.dray at umontreal.ca > -------------------------------------------------------------- > ------------------------------------ > > Web > http://www.steph280.freesurf.fr/ > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
You could do it with a double sapply like this: tr <- function(x) sum(diag(x)) sapply(a, function(x1) sapply(a, function(x2) tr(x1%*%t(x1)%*%x2%*%t(x2)) / (tr(x1%*%t(x1))*tr(x2%*%t(x2))))) Date: Mon, 20 Sep 2004 13:24:49 -0400 From: Stephane DRAY <stephane.dray at umontreal.ca> To: <r-help at stat.math.ethz.ch> Subject: [R] Multiple operations on list Hello, suppose I have a list with matrices: a=list(x1=matrix(rnorm(10),5,2),x2=matrix(rnorm(10),5,2),x3=matrix(rnorm(10),5,2)) I want to compute for all combination of xi and xj (x1,x2 x1,x3 and x2,x3) a value. This value is given for the pair x1,x2 by trace(x1%*%t(x1)%*%x2%*%t(x2)) / trace(x1%*%t(x1))*trace(x2%*%t(x2)) I know that product matrices t(xi)%*%xi can be obtained by: aa=lapply(a,crossprod) but I do not know how to "mix" the values in aa to obtain the desired values. Is there a way to do it without for loop ? Thanks in advances, sincerely Stéphane DRAY -------------------------------------------------------------------------------------------------- Département des Sciences Biologiques Université de Montréal, C.P. 6128, succursale centre-ville Montréal, Québec H3C 3J7, Canada Tel : (514) 343-6111 poste 1233 Fax : (514) 343-2293 E-mail : stephane.dray at umontreal.ca
Hope this does what you want:> library(gregmisc) > cmb <- combinations(length(aa), 2) > apply(cmb, 1, function(i) sum(diag(aa[[i[1]]] %*% aa[[i[2]]])) /+ (sum(diag(aa[[i[1]]])) * sum(diag(aa[[i[2]]])))) [1] 0.5363973 0.3336318 0.6593545 Andy> From: Stephane DRAY > > Hello, > > suppose I have a list with matrices: > > a=list(x1=matrix(rnorm(10),5,2),x2=matrix(rnorm(10),5,2),x3=ma > trix(rnorm(10),5,2)) > > I want to compute for all combination of xi and xj (x1,x2 > x1,x3 and x2,x3) > a value. > This value is given for the pair x1,x2 by > trace(x1%*%t(x1)%*%x2%*%t(x2)) / > trace(x1%*%t(x1))*trace(x2%*%t(x2)) > > I know that product matrices t(xi)%*%xi can be obtained by: > > aa=lapply(a,crossprod) > > but I do not know how to "mix" the values in aa to obtain the > desired values. > > Is there a way to do it without for loop ? > > Thanks in advances, > > sincerely > > St??phane DRAY > -------------------------------------------------------------- > ------------------------------------ > > D??partement des Sciences Biologiques > Universit?? de Montr??al, C.P. 6128, succursale centre-ville > Montr??al, Qu??bec H3C 3J7, Canada > > Tel : (514) 343-6111 poste 1233 Fax : (514) 343-2293 > E-mail : stephane.dray at umontreal.ca > -------------------------------------------------------------- > ------------------------------------ > > Web > http://www.steph280.freesurf.fr/ > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >