[英]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. 您的函数存在一些问题,导致不同的错误。
// [[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
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. 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);
. 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. 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)'
. 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)
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));
// [[Rcpp::export]]
above your function. // [[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. 我注意到您中的三个参数尚未使用
lastPaid
, percentile4Capping
和verbose
-我假设您还没有机会在函数体中实现它们。 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.