简体   繁体   English

如何使用 R 从给定分布中模拟 m 个大小为 n 的随机样本?

[英]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.这将xi列放入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名为V1V2 、...、 Vm随机样本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 R 中创建 m 个大小为 n 正态分布的样本 - Create m samples with size n Normal Distribution in R 如何计算期望值和方差,然后从R中的此分布模拟500个样本 - How do I calculate expected value and variance, then simulate 500 samples from this distribution in R 从均值5和标准差3的正态分布模拟5000个大小为5的样本 - Simulate 5000 samples of size 5 from a normal distribution with mean 5 and standard deviation 3 如何从R中的向量中绘制N个随机样本? - How to draw N random samples from a vector in R? 如何从R中大小增加的数据集中获取随机样本? - How to take random samples from data set with increasing size in R? 如何从多元正态分布模拟R中的200个矩阵(20 * 100)? - How can I simulate 200 matrices (20*100) in R from a multivariate normal distribution? 在 R 中使用自定义 pdf 从分布中生成随机样本? - Generate random samples from a distribution with custom pdf in R? 如何从R中大小为N的数据帧中获取大小为n的所有可能的子样本? - How to obtain all possible sub-samples of size n from a dataframe of size N in R? 如何将随机分布转换为 R 中的(预定义)integer 值? - How can i transform random distribution to (predefined) integer values in R? 根据N(0,1)分布生成1000个大小为4,$ Z_1,Z_2,Z_3,Z_4 $的随机样本,并计算W = $ Z_ {1} ^ 2 + Z_ {2} ^ 2 + Z_ {3} ^ 2 + Z_ {4} ^ 2 $ - Generate 1000 random samples of size 4, $Z_1, Z_2, Z_3, Z_4$ from N(0,1) distribution and calculate W=$Z_{1}^2+Z_{2}^2+Z_{3}^2+Z_{4}^2$
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM