Dear friends, If I have a table like this, first row A B C D ... are different levels of the variable, first column 0 1 2 4 ... are the levels of the "numbers", the numbers inside the table are the probabilities of the "number" occuring. A B C D ... 0 0.2 0.3 0.1 0.05 1 0.1 0.1 0.2 0.2 2 0.02 0.2 0 0.1 4 0.3 0.01 0.01 0.4 ... How can I use R to do the simulation and get a table like this, first row A B C D ... are different levels of the variable, the numbers inside the table are the "numbers" simulated from the probailties table above? A B C D ... 0 4 2 0 2 2 0 1 0 1 4 1 2 2 0 0 ... Thanks for help! Kelvin
If the trials are not connected then I would consider melting the table using melt() from the reshape package. And then using lapply() with the function random.function <- function(my.prob, number.of.observations = 10) { sum(rbinom(number.of.observations, 1, my.prob)) } in case the trials are connected, by column, than you could use apply(the.data.table, 2, a.function) on it. Where "a.function" will to multinum distribution (for which I don't remember the function at the moment, but it can be searched). Best, Tal. ----------------Contact Details:------------------------------------------------------- Contact me: Tal.Galili@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com/ (English) ---------------------------------------------------------------------------------------------- On Wed, Jan 13, 2010 at 7:20 PM, Kelvin <6kelvin6@gmail.com> wrote:> Dear friends, > > If I have a table like this, first row A B C D ... are different > levels of the variable, first column 0 1 2 4 ... are the levels of the > "numbers", the numbers inside the table are the probabilities of the > "number" occuring. > > A B C D ... > 0 0.2 0.3 0.1 0.05 > 1 0.1 0.1 0.2 0.2 > 2 0.02 0.2 0 0.1 > 4 0.3 0.01 0.01 0.4 > ... > > How can I use R to do the simulation and get a table like this, first > row A B C D ... are different levels of the variable, the numbers > inside the table are the "numbers" simulated from the probailties > table above? > > A B C D ... > 0 4 2 0 > 2 2 0 1 > 0 1 4 1 > 2 2 0 0 > ... > > > Thanks for help! > > > Kelvin > > ______________________________________________ > 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]]
Try this: dat <- data.frame(x=11:14, pa=1:4/10, pb=4:1/10) f <- function(numreps, data){ pmat <- as.matrix(data[-1]) x <- data[,1] result <- matrix(0, nrow=numreps, ncol=ncol(pmat)) colnames(result) <- c("A", "B") for(i in seq_len(numreps)){ result[i,] <- apply(pmat, 2, function(p) sample(x, 1, prob=p)) } result } f(5, dat) -Peter Ehlers Kelvin wrote:> Dear friends, > > If I have a table like this, first row A B C D ... are different > levels of the variable, first column 0 1 2 4 ... are the levels of the > "numbers", the numbers inside the table are the probabilities of the > "number" occuring. > > A B C D ... > 0 0.2 0.3 0.1 0.05 > 1 0.1 0.1 0.2 0.2 > 2 0.02 0.2 0 0.1 > 4 0.3 0.01 0.01 0.4 > ... > > How can I use R to do the simulation and get a table like this, first > row A B C D ... are different levels of the variable, the numbers > inside the table are the "numbers" simulated from the probailties > table above? > > A B C D ... > 0 4 2 0 > 2 2 0 1 > 0 1 4 1 > 2 2 0 0 > ... > > > Thanks for help! > > > Kelvin > > ______________________________________________ > 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. > >-- Peter Ehlers University of Calgary 403.202.3921