问题
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.
- 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
RcppArmadillo::sample(...)
may or may not have resulted in an error for you. In this Rcpp Gallery post, the authors useRcppArmadillo::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 usingRcpp::RcppArmadillo:sample
instead; although this seems strange to me considering that the declarationusing namespace Rcpp;
was in place.- Unlike the base R function
sample
, I don't think you can callRcppArmadillo::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)
- 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 anRcpp::as
, i.e.out=as<int>(Rcpp::RcppArmadillo::sample(historicalStatus,1,false));
- 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