[Rcpp-devel] Incorrect results from RcppArmadillo::sample

Balamuta, James Joseph balamut2 at illinois.edu
Sun Oct 18 19:08:48 CEST 2015


Greetings and Salutations,

At this code:
        Rcpp::NumericVector JJ(15); // creates a vector with 15 spaces
        JJ[0] = 15; // fills only the first space

You have only initiated slot 0 to 15. The remaining slots (1-14) are uninitialized at 0.

You probably want to use:
        Rcpp::IntegerVector JJ = seq_len( 10 ) ; // creates 1:10 vector

Why?

Z[i] <- sample(15, 1, replace = FALSE, prob = (exp(prob - deno)))

The 15 really is being converted to a vector ranging from 1:15.

Sincerely,

JJB

From: rcpp-devel-bounces at lists.r-forge.r-project.org [mailto:rcpp-devel-bounces at lists.r-forge.r-project.org] On Behalf Of Shuai Wang
Sent: Sunday, October 18, 2015 11:13 AM
To: rcpp-devel at lists.r-forge.r-project.org; Shuai Wang <wangshuai901 at gmail.com>
Subject: [Rcpp-devel] Incorrect results from RcppArmadillo::sample

Dear list,


I am a newbie to rcpp and currently I am trying to translate some R code into C++ code.

In particular, when I was trying to translate the following R code into C++ code:

    Z[i] <- sample(15, 1, replace = FALSE, prob = (exp(prob - deno)))

I found some errors in my C code, which troubles me a lot.

Let's elaborate a little bit.

I found that Rcpp does not provide a sample function, so I follow the instructions here<https://urldefense.proofpoint.com/v2/url?u=http-3A__gallery.rcpp.org_articles_using-2Dthe-2DRcpp-2Dbased-2Dsample-2Dimplementation_&d=BQMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=Oj62bnDE1oueLU-seL9f0p1xxu4Hvw2JDuP8BUw91c8&m=u0jDj4G8ZcHg4HCnGUSLEH17Y_2FH4CvUX-OVZ6OfcU&s=hh6uiDHmyqu_RGmNuCJSz_eLVL316VejIhtlWtDKZ4c&e=> to use
RcppArmadillo::sample.

However, even though the first parameter of sample function in R can be a integer:

    function (x, size, replace = FALSE, prob = NULL)
    {
        if (length(x) == 1L && is.numeric(x) && x >= 1) {
            if (missing(size))
                size <- x
            sample.int<https://urldefense.proofpoint.com/v2/url?u=http-3A__sample.int&d=BQMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=Oj62bnDE1oueLU-seL9f0p1xxu4Hvw2JDuP8BUw91c8&m=u0jDj4G8ZcHg4HCnGUSLEH17Y_2FH4CvUX-OVZ6OfcU&s=iBH3eCHMnRMHQ2mwiTNPV9k72ZAypzKfwhCrOe3o0fc&e=>(x, size, replace, prob)
        }
        else {
            if (missing(size))
                size <- length(x)
            x[sample.int<https://urldefense.proofpoint.com/v2/url?u=http-3A__sample.int&d=BQMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=Oj62bnDE1oueLU-seL9f0p1xxu4Hvw2JDuP8BUw91c8&m=u0jDj4G8ZcHg4HCnGUSLEH17Y_2FH4CvUX-OVZ6OfcU&s=iBH3eCHMnRMHQ2mwiTNPV9k72ZAypzKfwhCrOe3o0fc&e=>(length(x), size, replace, prob)]
        }
    }

I figure out that the first parameter of RcppArmadillo::sample cannot be a integer, it only accepts
a vector. So given my R code, I translate it into the following C code.

        Rcpp:NumericVector JJ(15);
        JJ[0] = 15;

        Z[i-1] = RcppArmadillo::sample(JJ, 1, false, (exp(prob - deno)))[0];


Then I found some errors in the above code, say, while in the original R code, Z[i] is a random number between 1 and 15, in the above C code, Z[i-1] is either 0 or 15.

Am I clear? Could anyone give me some help on this issue? Thank you a lot!


Shuai


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20151018/85f8b589/attachment.html>


More information about the Rcpp-devel mailing list