问题
The problem is that I have a variable arma::mat prob_vec
and want something equivalent to rmultinom(1, 1, prob_vec)
in R.
I found the rmultinom
function provided by RcppArmadillo has a weird argument requirement which is different from that in R! So it won't pass the compilation.
I just wanna know how to draw the desired sample in RcppArmadillo, or equivalently in Armadillo. If I need to get the pointer or convert my prob_vec
variable, please tell me how.
Many thanks!
回答1:
Your friendly neighbourhood co-author of RcppArmadillo
here: I can assure you that it does not provide rmultinom
, but Rcpp does. In fact, it simply passes through to R itself as a quick grep
would have told you:
inline void rmultinom(int n, double* prob, int k, int* rn)
{ return ::rmultinom(n, prob, k, rn); }
So I would suggest your first write a five-line C program against the R API to make sure you know how to have rmultinom
do what you want it to do, and then use Rcpp and RcppArmadillo to do the same thing on the data in your vector.
来源:https://stackoverflow.com/questions/14653336/how-do-i-draw-multinomial-distributed-samples-with-rcpparmadillo