来自 NumericVector 的 Rcpp R 样本等效项 [英] Rcpp R sample equivalent from a NumericVector
问题描述
我创建了一个 NumericVector,我需要从中抽取一个随机整数.我尝试使用各种 RcppArmarillo 函数,但它对我不起作用.功能如下:
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
};
返回;}
推荐答案
您的函数存在一些问题,导致不同的错误.
There are a few problems with your function, resulting in different errors.
- 您的文件中需要有
//[[Rcpp::depends(RcppArmadillo)]]
,通常放在#include
语句之后.没有这个你会得到一个编译错误 -fatal error: RcppArmadilloExtensions/sample.h: No such file or directory
RcppArmadillo::sample(...)
可能会或可能不会导致您出错.在这篇 Rcpp Gallery 帖子中,作者使用RcppArmadillo::sample
大概没有问题.但是,我收到以下错误消息:error: reference to ‘RcppArmadillo’ is ambiguous out=RcppArmadillo::sample(historicalStatus,1);
.我通过使用Rcpp::RcppArmadillo:sample
解决了这个问题;尽管考虑到声明using namespace Rcpp;
已经到位,这对我来说似乎很奇怪.- 与基本 R 函数
sample
不同,我认为您不能仅使用两个参数调用RcppArmadillo::sample
- 这样做会导致此错误:错误:没有匹配的函数调用'sample(Rcpp::IntegerVector&, int)'
.这是通过为替换参数提供一个布尔值来解决的:Rcpp::RcppArmadillo::sample(historicalStatus,1,false)
- 进行上述更改后,出现以下错误:
error: invalid user-defined conversion from ‘Rcpp::Vector<13, Rcpp::PreserveStorage>’ to ‘int’
.这可以通过添加Rcpp::as
轻松解决,即out=as<int>(Rcpp::RcppArmadillo::sample(historicalStatus,1,false));
- 我不确定您是否打算将其作为独立函数导出到 R 环境,但如果是这样,您需要上面的
//[[Rcpp::export]]
您的功能.
- 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.
我注意到你们中的三个参数没有使用 - lastPaid
、percentile4Capping
和 verbose
- 我假设你只是没有但有机会在您的函数体中实现它们.我无法在实际数据上对此进行测试,因为您没有在问题中提供任何内容,但在进行上述更改后,为我编译:
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;
}
这篇关于来自 NumericVector 的 Rcpp R 样本等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!