It does! thank you very much. Sigalit.
On 10/17/07, Peter McMahan <[EMAIL PROTECTED]> wrote: > > There are two things that will help you out. > > First, instead of getting the individual z1 and x1 one at a time and > then multiplying them you can call rbinom(200,1,p) to get a vector of > 200 independent draws. So you could replace your for loop with: > > z1 <- rbinom(200,1,.6) > x1 <- rbinom(200,1,.95) > y1 <- z1*x1 > > R will multiply vectors element-wise unless you use matrix multiply > so this will get you the same result much faster and easier. > > Second, if you don't care about saving the z1 and x1 vectors, the > replicate() function ("help(replicate)") can run the "experiment" any > number of times and return it to you as yet another vector. These two > lines > > yMeans1 <- replicate(200,mean(rbinom(200,1,.6)*rbinom(200,1,.95))) > yMeans2 <- replicate(200,mean(rbinom(200,1,.6)*rbinom(200,1,.8))) > > will give you the end results you're looking for, with yMeans1 being > a vector of means of 200 independent runs of the full 200 draws. The > first argument is the number of times to replicate, the second is the > expression you want to run multiple times. > > Hope this helps. > > On Oct 17, 2007, at 3:03 AM, sigalit mangut-leiba wrote: > > > hello, > > I want to simulate 200 times the mean of a joint probability (y1) > > and 200 > > times the mean of another joint distribution (y2), > > that is I'm expecting to get 200 means of y1 and 200 means of y2. > > y1 and y2 are probabilities that I calculate from the marginal > > prob. (z1 and > > z2 respectively) multiple by the conditional prob. (x1 and x2 > > respectively), > > which I generaterd from the binomial distribution. > > here are the results for one mean of y1 and one mean of y2: > > > >> y1 <- 0 > >> for (i in 1:200){ > > + z1[i] <- rbinom(1, 1, .6) > > + x1[i] <- rbinom(1, 1, .95) > > + y1[i] <- z1[i]*x1[i] > > + } > >> mean(y1) > > [1] 0.605 > >> y2 <- 0 > >> for (j in 1:200){ > > + z2[j] <- rbinom(1, 1, .6) > > + x2[j] <- rbinom(1, 1, .8) > > + y2[j] <- z2[j]*x2[j] > > + } > >> mean(y2) > > [1] 0.515 > >> > > and this I want to simulate 200 time, in order to see the > > distribution of y1 > > and the distribution of y2. > > How can I repeat it and see all the results? (all the 200 means of > > y1 and > > y2) > > Thank you, > > Sigalit. > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help@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. > > [[alternative HTML version deleted]] ______________________________________________ R-help@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.