[Rd] rbinom in RcppArmadillo?

Dirk Eddelbuettel edd at debian.org
Wed Jan 15 04:32:58 CET 2014


On 14 January 2014 at 16:40, boiled_wonderland wrote:
| What is the RcppArmadillo way to make binomial draws from a vector of probs,
| similar to what rbinom does in R?

1)  It is pretty common list etiquette in these quarters to post with a name
    and affiliation.  You may find list member more willing to help you if
    you adhere to common behaviour.

2)  RcppArmadillo is a member of the Rcpp family; discussions for Rcpp and
    related packages are happening at the rcpp-devel list. Conrad, the
    Armadillo author, also reads that list.

3)  RcppArmadillo is a wrapper for Armadillo, and 
       http://arma.sourceforge.net/docs.html
    is the main source of documentation. You will see that Armadillo supports
    from uniform and normal distributions.

4)  Rcpp, however, supports just about all distributions which R supports.
    So you can draw via Rcpp and instantiate an Armadillo object with the
    values. At the C level, some distributions have an additional parameter
    for degrees of freedom, the header file should be informative. Binomial
    is easier.  Here is a quick example just with Rcpp types:

    R> cppFunction('NumericVector nb(int n) { return(rbinom(n,1,0.5));}')
    R> set.seed(42); nb(4)               # draw 4 via C++
    [1] 1 1 0 1
    R> set.seed(42); rbinom(4,1,0.5)     # reset seed, draw 4 from R
    [1] 1 1 0 1
    R> 

Dirk

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com



More information about the R-devel mailing list