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
maybe something along these lines could be helpful: x <- c(0.3, 0.1, 0.2) ########### nx <- length(x) perms <- as.matrix(expand.grid(lapply(1:nx, function(i) c(FALSE, TRUE)))) apply(perms, 1, function(ind) prod(c(x[ind], (1 - x[!ind])))) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://www.med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Serguei Kaniovski" <kaniovsk at wifo.ac.at> To: <r-help at stat.math.ethz.ch> Sent: Wednesday, November 23, 2005 2:01 PM Subject: [R] vector of permutated products> 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 >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
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