Reputation: 1145
I'm generating 100 random numbers from Dirichlet distribution, and then I need to use the output to generate from Gamma distribution. Here is the code:
a <- rdirichlet(100, c(1,1,1))
b <- c(3,3,3)
sapply(a, function(x) {rgamma(100, shape=2, rate =(b%*%a)) })
Note here that the rate for gamma distribution is the dot product of vectors b and a (which is the output from Dirichlet).
I'm getting this error message:
Error in b %*% a : non-conformable arguments
Upvotes: 0
Views: 697
Reputation: 263301
I suspect you want. My library has at least four different packages with rdirichlet
functions.):
library(MCMCpack)
apply(a, 1, function(x) {rgamma(100, shape=2, rate =(b %*% x)) })
When vectors are passed to %*% then need to be the same length and sapply was passing single elements rather than length-3 rows. (You also didn't have "x" in the expression.)
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 ...
OR:
> 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 ...
Upvotes: 1