I need to generate a LOT of random numbers from a negative binomial distribution, all with different distribution parameters (the parameters I am using are mu and size for rnbinom). I need a faster way of doing this.
So right now I have 3 very large matrices: mean, exp and size. All of them have the same dimension and distinct numbers. I need to return a matrix M, where M(i, j) being a number randomly generated with NegativeBinomial(mu=mean(i, j)^exp(i, j), size=size(i, j)). Here is my current way of doing it:
f <- function(mean, exp, size) rnbinom(1, mu=mean^exp, size=l2fc)
M <- structure(mapply(f, mean, exp, size), dim=dim(mean))
From what I can tell mapply is already about as fast as it can be with R's internal vector optimization. Is there anyway to optimize this even further? The dim of mean/exp/size is roughly about 10000*25000, and it takes as long as an hour to finish running the entire code.