vendredi 6 mai 2016

Perform and test random demand generation with Vectorize for data.table

I have a data.table inv containing demand data per period and article and another data.table arts containing information about the gamma distribution function of demand for each article (with shape and rate parameters).

I want to create all random demand in one step for all periods and all articles and I am not sure whether my code does it properly.

library(data.table)
artID <- LETTERS[1:5]
inv <- CJ(period=1:10, ar=artID, d=0); setkey(inv,period,ar)
arts <- data.table(ar=artID, shape=seq(0.6,1,0.1), rate=seq(6,10)); setkey(arts,ar)

set.seed(1000)

vrgamma <- Vectorize(rgamma, USE.NAMES = TRUE, SIMPLIFY=TRUE)
inv[,d:=vrgamma(10, shape=arts[,shape], rate=arts[,rate])]
# Warning message: In `[.data.table`(inv, , `:=`(d, vrgamma(10, shape = arts[, shape],  :
# 5 column matrix RHS of := will be treated as one vector

So I get a warning, but I have random numbers in every period for every article. But I don't know whether they are the in the right place. And since it is random, I cannot look at the values and see whether they are right.

So here are my questions

  1. Do you think my solution works?
  2. Can you tell me how to test whether my solution works?
  3. Can I get rid of the warning with as.vector without side effects?
  4. Can you think of a better way to tackle my problem?

Aucun commentaire:

Enregistrer un commentaire