After scouring the online R resources and help pages, I still need clarification on the function rmultinomial(). I would like to create a vector, say of 100 elements, where every element in the vector can take on the value of 0, 1 or 2, and where each of those values have a specific probability. ie. the probability a given element in the vector = 0 is 0.06, 1 = 0.38, 2 = 0.56 (probabilities sum to 1). Can I use rmultinomial() function to do this? The following code does not seem to produce the result I need, but this sort of code is all I could find the R "help" pages:> rmultinomial(100,c(0.06,0.38,0.56))[1] 3 29 68> rmultinomial(100,c(0.06,0.38,0.56),long=TRUE)[1] 3 3 2 2 3 2 3 3 2 3 2 3 3 3 2 2 3 3 2 3 1 2 3 3 3 3 3 3 2 3 3 2 3 3 3 2 3 2 2 3 2 3 2 3 3 3 2 1 3 3 1 [52] 2 3 2 2 3 3 2 2 2 1 3 3 2 3 3 3 3 2 3 3 3 3 2 3 2 3 3 2 3 3 2 3 3 2 3 2 3 3 2 3 3 3 2 3 2 2 2 2 2 Also, I don't really understand the difference between the default long=FALSE and long=TRUE. The R "help" simply states that you use "long TRUE to choose one generator, FALSE to choose another one"; however I could not find any documentation that described what the difference between those generators is. Any clarification would be greatly appreciated! Thanks, Mary
Mary Black wrote:> After scouring the online R resources and help pages, I still need > clarification on the function rmultinomial(). I would like to create > a vector, say of 100 elements, where every element in the vector can > take on the value of 0, 1 or 2, and where each of those values have a > specific probability. ie. the probability a given element in the > vector = 0 is 0.06, 1 = 0.38, 2 = 0.56 (probabilities sum to 1). Can > I use rmultinomial() function to do this? > > The following code does not seem to produce the result I need, but > this sort of code is all I could find the R "help" pages: > >> rmultinomial(100,c(0.06,0.38,0.56)) > [1] 3 29 68 >> rmultinomial(100,c(0.06,0.38,0.56),long=TRUE) > [1] 3 3 2 2 3 2 3 3 2 3 2 3 3 3 2 2 3 3 2 3 1 2 3 3 3 3 3 3 2 3 3 2 3 > 3 3 2 3 2 2 3 2 3 2 3 3 3 2 1 3 3 1 [52] 2 3 2 2 3 3 2 2 2 1 3 3 2 3 > 3 3 3 2 3 3 3 3 2 3 2 3 3 2 3 3 2 3 3 2 3 2 3 3 2 3 3 3 2 3 2 2 2 2 2 >You didn't tell us which package has this 'rmultinomial' function. The function 'rmultinom' sounds like it (part of the stats package), but has no argument 'long'. You can just use the 'sample' function to do this. sample(0:2, 100, prob = c(.06,.38,.56), replace = TRUE) Best, Erik Iverson> > Also, I don't really understand the difference between the default > long=FALSE and long=TRUE. The R "help" simply states that you use > "long TRUE to choose one generator, FALSE to choose another one"; > however I could not find any documentation that described what the > difference between those generators is. Any clarification would be > greatly appreciated! > > Thanks, > > Mary > > ______________________________________________ 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.
Mary -- The dmultinomial function (try ?Multinomial, noting that it is an upper case M) has a "log" option, which, if set to TRUE, returns logarithms of probabilities, but that is for computing probabilities, not generating samples. Perhaps the "long" you referred to is a misprint for "log?" In any case, try ?Multinomial, and give rmultinom() another try. Note, however, that its output gives the _number_ of each of the sampled items produced in a single sample, not the sequence of draws. If you need the sequence, then I think that the reply from Erik Iverson tells you how best to proceed. Ben -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Mary Black Sent: Tuesday, March 18, 2008 5:29 PM To: r-help at r-project.org Subject: [PS] [R] rmultinomial() function After scouring the online R resources and help pages, I still need clarification on the function rmultinomial(). I would like to create a vector, say of 100 elements, where every element in the vector can take on the value of 0, 1 or 2, and where each of those values have a specific probability. ie. the probability a given element in the vector = 0 is 0.06, 1 = 0.38, 2 = 0.56 (probabilities sum to 1). Can I use rmultinomial() function to do this? The following code does not seem to produce the result I need, but this sort of code is all I could find the R "help" pages:> rmultinomial(100,c(0.06,0.38,0.56))[1] 3 29 68> rmultinomial(100,c(0.06,0.38,0.56),long=TRUE)[1] 3 3 2 2 3 2 3 3 2 3 2 3 3 3 2 2 3 3 2 3 1 2 3 3 3 3 3 3 2 3 3 2 3 3 3 2 3 2 2 3 2 3 2 3 3 3 2 1 3 3 1 [52] 2 3 2 2 3 3 2 2 2 1 3 3 2 3 3 3 3 2 3 3 3 3 2 3 2 3 3 2 3 3 2 3 3 2 3 2 3 3 2 3 3 3 2 3 2 2 2 2 2 Also, I don't really understand the difference between the default long=FALSE and long=TRUE. The R "help" simply states that you use "long TRUE to choose one generator, FALSE to choose another one"; however I could not find any documentation that described what the difference between those generators is. Any clarification would be greatly appreciated! Thanks, Mary ______________________________________________ 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.
Seemingly Similar Threads
- dmultinomial
- with data in the form of an R data objecte: Monte Carlo simulation in R
- R version of SAS Proc Varclus
- Troubles with the function rmultinom.c of the R's Random Number Generator
- Error message for infinite probability parameters in rbinom() and rmultinom()