0

我从 Dirichlet 分布生成 100 个随机数,然后我需要使用输出从 Gamma 分布生成。这是代码:

a <- rdirichlet(100, c(1,1,1))
b <- c(3,3,3)

sapply(a, function(x) {rgamma(100, shape=2, rate =(b%*%a)) })

请注意,伽马分布的速率是向量 b 和 a 的点积(这是 Dirichlet 的输出)。

我收到此错误消息:

Error in b %*% a : non-conformable arguments
4

1 回答 1

1

我怀疑你想要。我的图书馆至少有四个不同的rdirichlet功能包。):

library(MCMCpack)
apply(a,  1, function(x) {rgamma(100, shape=2, rate =(b %*% x)) })

当向量传递给 %*% 时,需要具有相同的长度,并且 sapply 传递的是单个元素而不是长度为 3 的行。(表达式中也没有“x”。)

c(3,3,3) %*% 1
#Error in c(3, 3, 3) %*% 1 : non-conformable arguments

 str(a %*% b) 
 # vectors can be assumed to be column matrices in the second position
 num [1:100, 1] 3 3 3 3 3 3 3 3 3 3 ...

或者:

> str(b %*% t(a))
# .... or assumed to be row matrices in the first position.
 num [1, 1:100] 3 3 3 3 3 3 3 3 3 3 ...
于 2013-06-10T23:18:53.947 回答