英文:
Behavour of the rpois function
问题
以下是翻译后的代码部分:
我正在尝试理解一项大学测试的提出的解决方案。
让我假设我们已经创建了一个随机变量,使用以下代码:
set.seed(123)
R <- 5
X <- rexp(R, 2)
所以 `X` 的内容是
0.42172863 0.28830514 0.66452743 0.01578868 0.02810549
在问题的解决方案中,我发现
Y <- rpois(R, exp(X / 4))
其中 `exp(X / 4)` 的内容是
1.111191 1.074737 1.180729 1.003955 1.007051
与我的预期相反,第二个参数是一个数组,而不是一个标量。
如果我计算
print(rpois(R, 1.111191))
print(rpois(R, 1.074737))
print(rpois(R, 1.180729))
print(rpois(R, 1.003955))
print(rpois(R, 1.007051))
我得到
2 1 1 3 1
1 1 0 2 0
0 1 3 3 2
1 4 1 1 1
1 0 0 3 2
而对于 `rpois(R, exp(X / 4))`,我得到
1 2 0 1 2
这两个结果有什么关系?
这是一个我无法在任何地方找到解释的行为。
英文:
I'm trying to understand a proposed solution for a University test.
Let me assume that we have created a random variable with
set.seed(123)
R <- 5
X <- rexp(R, 2)
So the content of X
is
0.42172863 0.28830514 0.66452743 0.01578868 0.02810549
In the solutions of the problem I find
Y <- rpois(R, exp(X / 4))
where the content of exp(X / 4)
is
1.111191 1.074737 1.180729 1.003955 1.007051
where, contrary to my expectations the second argument is an array instead of being a scalar.
If I calculate
print(rpois(R, 1.111191))
print(rpois(R, 1.074737))
print(rpois(R, 1.180729))
print(rpois(R, 1.003955))
print(rpois(R, 1.007051))
I get
2 1 1 3 1
1 1 0 2 0
0 1 3 3 2
1 4 1 1 1
1 0 0 3 2
while for rpois(R, exp(X / 4))
I get
1 2 0 1 2
How are the two results related?
It's a behaviour I can't find explained anywhere.
答案1
得分: 4
R会在合理的情况下将其函数矢量化。
特别是,在函数调用rpois(R, lambda)
中,R
指定要获取的样本数,而lambda
是均值的矢量,它会被重复使用以匹配R
。换句话说,如果lambda
是单个值,那么每个泊松抽样都将使用相同的均值;如果它是长度为R
的矢量,那么矢量的每个元素将用于相应的泊松抽样。
因此,Y <- rpois(R, exp(X / 4))
的等效代码将是
Y <- c(
rpois(1, exp(X[1]/4),
rpois(1, exp(X[2]/4),
rpois(1, exp(X[3]/4),
...
)
我们还可以使用for
循环来实现这个:
Y <- numeric(R) ## 分配一个长度为R的数值矢量
for (i in seq(R)) {
Y[i] <- rpois(1, exp(X[i]/4))
}
在可用的情况下使用矢量化版本是最佳实践;它更快,需要更少的代码(因此更容易理解)。
英文:
R makes its functions vectorized wherever it's reasonable to do so.
In particular, in the function call rpois(R, lambda)
, R
specifies the number of samples to take, and lambda
is the vector of means, which is recycled to match R
. In other words, if lambda
is a single value then the same mean will be used for each Poisson draw; if it is a vector of length R
, then each element of the vector will be used for the corresponding Poisson draw.
So the equivalent of Y <- rpois(R, exp(X / 4))
would be
Y <- c(
rpois(1, exp(X[1]/4),
rpois(1, exp(X[2]/4),
rpois(1, exp(X[3]/4),
...
)
We could also do this with a for
loop:
Y <- numeric(R) ## allocate a length-R numeric vector
for (i in seq(R)) {
Y[i] <- rpois(1, exp(X[i]/4))
}
Using the vectorized version whenever it's available is best practice; it's faster and requires less code (therefore easier to understand).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论