使用均值向量调用 rnorm

发布于 2024-09-14 16:46:37 字数 307 浏览 4 评论 0原文

当我调用 rnorm 并将单个值作为平均值传递时,很明显会发生什么:从 Normal(10,1) 生成一个值。

y <- rnorm(20, mean=10, sd=1)

但是,我看到整个向量被传递给 rnorm (或 rcauchy 等)的示例;在这种情况下,我不确定 R 机器到底做了什么。例如:

a = c(10,22,33,44,5,10,30,22,100,45,97)
y <- rnorm(a, mean=a, sd=1)

有什么想法吗?

When I call rnorm passing a single value as mean, it's obvious what happens: a value is generated from Normal(10,1).

y <- rnorm(20, mean=10, sd=1)

But, I see examples of a whole vector being passed to rnorm (or rcauchy, etc..); in this case, I am not sure what the R machinery really does. For example:

a = c(10,22,33,44,5,10,30,22,100,45,97)
y <- rnorm(a, mean=a, sd=1)

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

佼人 2024-09-21 16:46:38

rnorm 生成的随机数数量等于 a 的长度。来自?rnorm

n:观察次数。如果
'长度(n)> 1',取长度
为所需数量。

要了解将 a 传递给 Mean 参数时发生的情况,如果我们更改示例会更容易:

a = c(0, 10, 100)
y = rnorm(a, mean=a, sd=1)
[1] -0.4853138  9.3630421 99.7536461

因此,我们生成具有平均值 length(a) 随机数a[i]

The number of random numbers rnorm generates equals the length of a. From ?rnorm:

n: number of observations. If
‘length(n) > 1’, the length is taken
to be the number required.

To see what is happening when a is passed to the mean argument, it's easier if we change the example:

a = c(0, 10, 100)
y = rnorm(a, mean=a, sd=1)
[1] -0.4853138  9.3630421 99.7536461

So we generate length(a) random numbers with mean a[i].

隔岸观火 2024-09-21 16:46:38

一个更好的例子:

a <- c(0,10,100)
b <- c(2,4,6)
y <- rnorm(6,a,b)
y

结果

[1]  -1.2261425  10.1596462 103.3857481  -0.7260817   7.0812499  97.8964131

如您所见,对于y的第一个和第四个元素,rnorm采用<的第一个元素em>a 作为 meanb 的第一个元素sd

对于y的第二个和第五个元素,rnorm采用a的第二个元素> 作为 meanb 的第二个元素作为 sd

对于y的第三个和第六个元素,rnorm采用a的第三个元素> 作为meanb的第三个元素作为sd

您可以在 rnorm 的第一个参数中尝试不同的数字,看看发生了什么

例如,如果您在调用 rnorm 时使用 5 而不是 6 作为第一个参数,会发生什么?

a better example:

a <- c(0,10,100)
b <- c(2,4,6)
y <- rnorm(6,a,b)
y

result

[1]  -1.2261425  10.1596462 103.3857481  -0.7260817   7.0812499  97.8964131

as you can see, for the first and fourth element of y, rnorm takes the first element of a as the mean and the first element of b as the sd.

For the second and fifth element of y, rnorm takes the second element of a as the mean and the second element of b as the sd.

For the third and sixth element of y, rnorm takes the third element of a as the mean and the third element of b as the sd.

you can experiment with diferent number in the first argument of rnorm to see what is happening

For example, what is happening if you use 5 instead 6 as the first argument in calling rnorm?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文