[英]Generating multinomial random data in R
I am trying to generate data from a multinomial distribution in R using the function rmultinom, but I am having some problems.我正在尝试使用 function rmultinom 从 R 中的多项分布生成数据,但我遇到了一些问题。 The fact is that I want a data frame of 50 rows and 20 columns and a total sum of the outcomes equal to 3 times n*p.事实是我想要一个 50 行和 20 列的数据框,并且结果的总和等于 3 倍 n*p。
I am using this code:我正在使用这段代码:
p <- 20
n <- 50
N <- 3*(n*p)
prob_true <- rep(1/p, p)
a <- rmultinom(50, N, prob_true)
But I get some very strange results and a data frame with 20 rows and 50 columns.但是我得到了一些非常奇怪的结果和一个 20 行 50 列的数据框。 How can I solve this problem?我怎么解决这个问题?
Thanks in advance!提前致谢!
The help available at ?rmultinom
says that n
in rmultinom(n, size, prob)
is: ?rmultinom
提供的帮助说 rmultinom( n
rmultinom(n, size, prob)
中的 n 是:
"number of random vectors to draw" “要绘制的随机向量数”
And size
is: "specifying the total number of objects that are put into K boxes in the typical multinomial experiment"而size
是:“指定典型多项式实验中放入K个盒子的物体总数”
And the help says that the output is: "For rmultinom(), an integer K xn matrix where each column is a random vector generated according to the desired multinomial law, and hence summing to size"帮助说 output 是:“对于 rmultinom(),一个 integer K xn 矩阵,其中每列是根据所需多项式定律生成的随机向量,因此求和大小”
So you're asking for 50 vectors/variables with a total number of "objects" equal to 3000, so each column is drawn as a vector that sums to 3000.因此,您要求 50 个向量/变量,“对象”总数等于 3000,因此每列都绘制为总和为 3000 的向量。
colSums(a)
does result in 3000
. colSums(a)
确实导致3000
。
Do you want your vectors/variables as rows?你想要你的向量/变量作为行吗? Then this would work just by transposing a
:然后这将通过转置a
来工作:
t(a)
but if you want 20 columns, each that is its own variable, you would need to switch your n
and p
(I also subbed in n
in the rmultinom
call):但是如果你想要 20 列,每列都是它自己的变量,你需要切换你的n
和p
(我也在rmultinom
调用中替换了n
):
n <- 20
p <- 50
N <- 3*(n*p)
prob_true <- rep(1/p, p)
a <- rmultinom(n, N, prob_true)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.