generate sets of normal distribution values within for loop

时间:2015-04-24 21:36:20

标签: r

I'm trying to capture 500 samples of a linear model, where each sample contains 20 error terms that are randomly-generated values of a normal distribution.

Because I'm interested in results per sample, I don't want to just generate a vector of 500 * 20 = 10000 values of a normal distribution.

My code is:

for (i in 1:500) {
    e <- rnorm(n = 20, mean = 0, sd = 4)
}

The problem is that this code generates 20 values, once. So each of the 500 samples have the same 20 error terms. How can I generate 20 new values for each iteration of the 500-iteration for-loop?

3 个答案:

答案 0 :(得分:5)

我认为你对你的循环实际上在做什么感到困惑。它会将新分配的随机向量连续500次重新分配给变量e。这意味着每次迭代e都会被新的随机向量覆盖。因此,在循环结束后,您最终得到一个现在分配给e的随机向量。您基本上只是以非常低效的方式定义e:)

我认为,你想做的事情很可能是这样的:

nrSamples = 500
e <- list(mode="vector",length=nrSamples)
for (i in 1:nrSamples) {
  e[[i]] <- rnorm(n = 20, mean = 0, sd = 4)
}

首先将e定义为最多可包含500个向量的列表。在循环中,首先创建随机向量,然后将其分配到列表中的相应位置。每个唯一的listindex完成500次。

您现在可以按如下方式访问随机向量:

> vector_1 <- e[[1]]
> vector_7 <- e[[7]]
> vector_1
 [1]  3.8713046  3.4672930  4.2840856  4.0388847 -3.0535864 -4.1402421 -2.7912700 -1.2332116  3.2628433  3.5377208
[11] -1.0929493  0.6466984 -5.5490625 -7.3033997  1.0898727  0.2001674  2.2646435  0.1623863  2.2611607 -1.1867225
> vector_7
 [1]  0.8199701 -3.1517209 -1.1319827  6.3150359 -3.7589505  1.4065123 -0.5410125 -3.0186291  6.6353592 -0.5002009
[11] -3.7416365  5.5324850 -2.2105955 -1.0931199 -2.0189795 -5.4934535  2.4210809  1.0956980 -7.6284702 -1.3574990

如您所见,随机向量不相同。它们是彼此随机独立生成的。为了访问随机向量的各个元素,您可以这样做:

> vector_7[[3]]
[1] -1.131983
> # OR
> e[[7]][[3]]
[1] -1.131983

答案 1 :(得分:3)

Buffer是一个矢量化函数。

因此,rnorm()或类似内容应该有效。

matrix(rnorm(500 * 20, 0, 4), nrow = 500))

答案 2 :(得分:2)

运行代码,循环结束将返回一组20个数字:

for (i in 1:500) {
    e <- rnorm(n = 20, mean = 0, sd = 4)
}

> e
[1]  7.48112400 -3.76594695 -1.55396151 -0.88205322  1.00736518  1.61904598 -4.69739057 -0.65291410
[9] -1.11921165  1.35657106 -8.33957962 -1.80607461 -0.05524872 -1.79938725 -0.98579993  6.32969133
[17]  2.83715482 -1.56407249 -6.56056515  0.65830884

你需要创建一个列表并将e的每次迭代存储在其中,正如@infominer建议的那样:

e <- list()

for (i in 1:500) {
    e[i] <- list(rnorm(n = 20, mean = 0, sd = 4))
}