[R] vector of permutated products

Serguei Kaniovski kaniovsk at wifo.ac.at
Wed Nov 23 14:01:32 CET 2005


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




More information about the R-help mailing list