[英]How can I simulate m random samples of size n from a given distribution with R?
I know how to generate a random sample of size n from a standard statistical distribution, say exponential.我知道如何从标准统计分布中生成大小为 n 的随机样本,比如指数分布。 But if I want to generate m such random samples of size n (ie m vectors of dimension n) how can I do it?但是,如果我想生成 m 个这样的大小为 n 的随机样本(即 m 个维度为 n 的向量),我该怎么做?
To create a n
by m
matrix containing m
samples of size n
you can use:要创建包含m
个大小为n
样本的n
× m
矩阵,您可以使用:
x <- replicate(m, rnorm(n, ...))
Obviously substituting rnorm
with other distributions if desired.如果需要,显然可以用其他分布代替rnorm
。 If you then want to store these in separate individual vectors then you can use如果您想将这些存储在单独的单独向量中,那么您可以使用
v <- x[ , i]
This puts the i
th column of x
into v
, which corresponds to the i
th sample.这将x
第i
列放入v
,这对应于第i
个样本。 It may be easier/quicker to just use a simple for loop altogether though:不过,完全使用简单的 for 循环可能更容易/更快:
for(i in 1:m){
name <- paste("V", i, sep = "")
assign(name, rnorm(n, ...))
}
This generates a random sample at each iteration, and for stage i
, names the sample Vi
.这会在每次迭代时生成一个随机样本,并且对于阶段i
,将样本命名为Vi
。 By the end of it you'll have m
random samples named V1
, V2
, ..., Vm
.到最后,您将拥有m
名为V1
、 V2
、...、 Vm
随机样本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.