[R] vector of permutated products

Liaw, Andy andy_liaw at merck.com
Wed Nov 23 14:40:12 CET 2005


Here's one possibility:

> x <- c(0.3, 0.1, 0.2)
> ## Create the permutation matrix:
> m <- do.call("expand.grid", lapply(x, function(x) c(x, 1-x)))
> ## get the row product:
> apply(m, 1, prod)
    1     2     3     4     5     6     7     8 
0.006 0.014 0.054 0.126 0.024 0.056 0.216 0.504 
> ## Alternatively:
> exp(rowSums(log(m)))
    1     2     3     4     5     6     7     8 
0.006 0.014 0.054 0.126 0.024 0.056 0.216 0.504 

HTH,
Andy

> From: Serguei Kaniovski
> 
> Given an x-vector with, say, 3 elements, I would like to compute the 
> following vector of permutated products
> 
> (1-x1)*(1-x2)*(1-x3)
> (1-x1)*(1-x2)*x3
> (1-x1)*x2*(1-x3)
> x1*(1-x2)*(1-x3)
> (1-x1)*x2*x3
> x1*(1-x2)*x3
> x1*x2*(1-x3)
> x1*x2*x3
> 
> Now, I already have the correctly sorted matrix of 
> permutations! So, the 
> input looks something like:
> 
> #input
> x<-c(0.3,0.1,0.2)
> Nx<-length(x)
> Ncomb<-2^Nx
> permat<-matrix(c(1,1,1,1,1,0,1,0,1,1,0,0,0,1,1,0,1,0,0,0,1,0,0
> ,0),Ncomb,Nx)
> 
> I code the rest as follows:
> 
> #correct but clumsy code
> temp1<-t(matrix(rep(x,2^3),3,2^3))
> temp2<-t(matrix(rep(1-x,2^3),3,2^3))
> result<-apply(permat*temp1-(permat-1)*temp2,1,prod)
> 
> But I would like to do without temp1 and temp2. To have something like
> 
> result<-apply(permat*x-(permat-1)*x,1,prod)
> 
> My problem is that permat*x does not produce permat*temp1 due 
> to the way 
> the element-by-element multiplication works in R. Can you help to 
> simplify the above code?
> 
> Thank you in advance,
> Serguei Kaniovski
> -- 
> ___________________________________________________________________
> 
> Austrian Institute of Economic Research (WIFO)
> 
> Name: Serguei Kaniovski			P.O.Box 91
> Tel.: +43-1-7982601-243			Arsenal Objekt 20
> Fax:  +43-1-7989386			1103 Vienna, Austria
> Mail: Serguei.Kaniovski at wifo.ac.at
> 
http://www.wifo.ac.at/Serguei.Kaniovski

______________________________________________
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




More information about the R-help mailing list