Rcpp R sample equivalent from a NumericVector

拥有回忆 提交于 2019-12-13 08:53:47

问题


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; }


回答1:


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;
}


来源:https://stackoverflow.com/questions/26384959/rcpp-r-sample-equivalent-from-a-numericvector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!