Kate Hsu
2010-Nov-09 06:05 UTC
[R] Question related to combination and the corresponding probability
Dear r users, I have 4 variables x1,x2,x3,x4 and each one has two levels, for example Y and N. For x1: prob(Y)=0.6, prob(N)=0.4; For x2: prob(Y)=0.5, prob(N)=0.5; For x3: prob(Y)=0.8, prob(N)=0.2; For x4: prob(Y)=0.9, prob(N)=0.1; Therefore, the sample space for (x1, x2, x3, x4)={YYYY, YYYN, YYNY,......} (16 possible combination) and the corresponding probabilities are {(0.6)(0.5)(0.8)(0.9), (0.6)(0.5)(0.8)(0.1), (0.6)(0.5)(0.2)(0.9),...} I want to create a matrix includes all possible outcomes and the corresponding probabilities. That is AYYYY (0.6)(0.5)(0.8)(0.9) YYYN (0.6)(0.5)(0.8)(0.1) YYNY (0.6)(0.5)(0.2)(0.9) . . . . . . Any efficient way to do this? Thanks, Kate [[alternative HTML version deleted]]
Michael Bedward
2010-Nov-09 06:38 UTC
[R] Question related to combination and the corresponding probability
Don't know if this is "efficient" but I think it works... yn <- c("Y", "N") X <- expand.grid(x1=yn, x2=yn, x3=yn, x4=yn) Yp <- c(0.6, 0.5, 0.8, 0.9) X$prob <- apply(X, 1, function(x) cumprod(ifelse(x == "Y", Yp, 1-Yp))[length(x)]) Michael On 9 November 2010 17:05, Kate Hsu <yhsu.rhelp at gmail.com> wrote:> Dear r users, > > I have 4 variables x1,x2,x3,x4 and each one has two levels, for example Y > and N. > > For x1: prob(Y)=0.6, prob(N)=0.4; > ?For x2: prob(Y)=0.5, prob(N)=0.5; > For x3: prob(Y)=0.8, prob(N)=0.2; > ?For x4: prob(Y)=0.9, prob(N)=0.1; > > Therefore, the sample space for (x1, x2, x3, x4)={YYYY, YYYN, YYNY,......} > (16 possible combination) and the corresponding probabilities are > {(0.6)(0.5)(0.8)(0.9), (0.6)(0.5)(0.8)(0.1), (0.6)(0.5)(0.2)(0.9),...} > I want to create a matrix includes all possible outcomes and the > corresponding probabilities. > That is > > A> YYYY ?(0.6)(0.5)(0.8)(0.9) > YYYN ?(0.6)(0.5)(0.8)(0.1) > YYNY (0.6)(0.5)(0.2)(0.9) > ? . ? ? ? ? ? ?. > ? . ? ? ? ? ? ?. > ? . ? ? ? ? ? ?. > > Any efficient way to do this? > > Thanks, > > Kate > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >