简体   繁体   English

来自NumericVector的Rcpp R样本等效项

[英]Rcpp R sample equivalent from a NumericVector

I have created a NumericVector and I need to sample one random Integer from it. 我已经创建了一个NumericVector,我需要从中采样一个随机的Integer。 I tried to use various RcppArmarillo functions but it failed to works for me. 我尝试使用各种RcppArmarillo函数,但对我而言无效。 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; 返回 } }

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. 您需要在文件中添加// [[Rcpp::depends(RcppArmadillo)]] ,通常将其放置在#include语句之后。 Without this you will get a compilation error - fatal error: RcppArmadilloExtensions/sample.h: No such file or directory 没有这个,您将得到一个编译错误- fatal error: RcppArmadilloExtensions/sample.h: No such file or directory
  2. RcppArmadillo::sample(...) may or may not have resulted in an error for you. RcppArmadillo::sample(...)可能会或可能不会导致您出错。 In this Rcpp Gallery post , the authors use RcppArmadillo::sample presumably without issue. 此Rcpp Gallery帖子中 ,作者大概使用RcppArmadillo::sample可能没有问题。 However, I received the following error message: error: reference to 'RcppArmadillo' is ambiguous out=RcppArmadillo::sample(historicalStatus,1); 但是,我收到以下错误消息: error: reference to 'RcppArmadillo' is ambiguous out=RcppArmadillo::sample(historicalStatus,1); . I resolved this by using Rcpp::RcppArmadillo:sample instead; 我通过使用Rcpp::RcppArmadillo:sample来解决此问题; although this seems strange to me considering that the declaration using namespace Rcpp; 考虑到using namespace Rcpp;进行声明,这对我来说似乎很奇怪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)' . 与基本R函数sample ,我认为您不能仅使用两个参数来调用RcppArmadillo::sample这样做会导致此错误: 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) 通过为替换参数提供布尔值可以解决此问题: 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' . 进行上述更改之后,我得到以下错误: error: invalid user-defined conversion from 'Rcpp::Vector<13, Rcpp::PreserveStorage>' to 'int' This is easily fixed by adding an Rcpp::as , ie out=as<int>(Rcpp::RcppArmadillo::sample(historicalStatus,1,false)); 通过添加Rcpp::as轻松解决,即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. 我不确定您是否打算将其作为独立函数导出到R环境中,但是如果是这样,则需要在函数上方// [[Rcpp::export]]

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. 我注意到您中的三个参数尚未使用lastPaidpercentile4Cappingverbose -我假设您还没有机会在函数体中实现它们。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM