Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have created a NumericVector and I need to sample one random Integer from it. I tried to use various RcppArmarillo functions but it failed to works for me. The function is below:

//#include <algorithm>
#include <RcppArmadilloExtensions/sample.h>
using namespace Rcpp;
using namespace arma;
using namespace std;

int simulateNextStepC(double currentAmount, double lastPaid, int currentStatus, int currentMaturity, NumericMatrix amountLinkMatrix, NumericMatrix statusMatrix, double  percentile4Capping=1, bool verbose=false)
{
int nrow = amountLinkMatrix.nrow(), outsize;

bool check;
LogicalVector positionsToSample(nrow); 

for(int i=0;i< nrow;i++) {
check=false;
check=((statusMatrix(i,currentMaturity)==currentStatus)&&(is_finite(statusMatrix(i,currentMaturity+1))));
positionsToSample[i]=check;
}

outsize=sum(positionsToSample);

IntegerVector historicalStatus(max(outsize,1));
int out;
if(outsize==0) 
out=currentStatus; 
else { 
   for(int i=0, j=0; i<nrow; i++) {
     if(positionsToSample[i]){
       historicalStatus[j]=statusMatrix(i,currentMaturity+1);
       j++;
     }
   }
   out=RcppArmadillo::sample(historicalStatus,1); // SAMPLING HERE
};

return out; }

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
134 views
Welcome To Ask or Share your Answers For Others

1 Answer

There are a few problems with your function, resulting in different errors.

  1. You need to have // [[Rcpp::depends(RcppArmadillo)]] in your file, typically placed after your #include statements. Without this you will get a compilation error - fatal error: RcppArmadilloExtensions/sample.h: No such file or directory
  2. RcppArmadillo::sample(...) may or may not have resulted in an error for you. In this Rcpp Gallery post, the authors use RcppArmadillo::sample presumably without issue. However, I received the following error message: error: reference to ‘RcppArmadillo’ is ambiguous out=RcppArmadillo::sample(historicalStatus,1);. I resolved this by using Rcpp::RcppArmadillo:sample instead; although this seems strange to me considering that the declaration using namespace Rcpp; was in place.
  3. Unlike the base R function sample, I don't think you can call RcppArmadillo::sample with only two arguments - doing so resulted in this error: error: no matching function for call to ‘sample(Rcpp::IntegerVector&, int)’. This was resolved by supplying a boolean to the replacement argument: Rcpp::RcppArmadillo::sample(historicalStatus,1,false)
  4. After making the above changes, I got the following error: error: invalid user-defined conversion from ‘Rcpp::Vector<13, Rcpp::PreserveStorage>’ to ‘int’. This is easily fixed by adding an Rcpp::as, i.e. out=as<int>(Rcpp::RcppArmadillo::sample(historicalStatus,1,false));
  5. I'm not sure if you intended to export this to your R environment as a stand-alone function, but if so, you need a // [[Rcpp::export]] above your function.

I noticed that three of you arguments were unused - lastPaid, percentile4Capping, and verbose - I'm assuming that you just had not yet had a chance to implement them in the body of your function. I couldn't test this on actual data since you did not provide any in your question, but after making the changes noted above, this compiled for me:

#include <RcppArmadilloExtensions/sample.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
using namespace arma;
using namespace std;
// [[Rcpp::export]]
int simulateNextStepC(double currentAmount, double lastPaid, 
                      int currentStatus, int currentMaturity, 
                      NumericMatrix amountLinkMatrix, 
                      NumericMatrix statusMatrix, 
                      double percentile4Capping=1, 
                      bool verbose=false)
{
  int nrow = amountLinkMatrix.nrow(), outsize;

  bool check;
  LogicalVector positionsToSample(nrow); 

  for(int i=0; i<nrow; i++) {
    check = false;
    check = ((statusMatrix(i,currentMaturity)==currentStatus) &&
             (is_finite(statusMatrix(i,currentMaturity+1))));
    positionsToSample[i] = check;
  }

  outsize = sum(positionsToSample);

  IntegerVector historicalStatus(max(outsize,1));
  int out;
  if( outsize==0 ) {
    out=currentStatus; 
  } else { 
    for(int i=0, j=0; i<nrow; i++) {
      if( positionsToSample[i] ) {
        historicalStatus[j]=statusMatrix(i,currentMaturity+1);
        j++;
      }
    }
    out=as<int>(Rcpp::RcppArmadillo::sample(historicalStatus,1,false));
  }
  return out;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...