# [R] Question related to combination and the corresponding probability

Michael Bedward michael.bedward at gmail.com
Tue Nov 9 07:38:06 CET 2010

```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