正在运行Rcpp代码块中止R会话



R会话终止时表示发生了致命错误。我对C++和Rcpp知之甚少,也不知道问题的根源是什么。

Rcpp::cppFunction("
NumericVector cumyRes(double a, double b, double timedt, NumericVector dt, 
NumericVector ProbMDset, NumericVector MainMDset, 
NumericVector decPoints, double LP, double LT,
double p1, double pA, int ii, double x1, double x2){
bool repFlag = false;
int n = dt.size();
double inity = 0;
NumericVector out(n);
std::unordered_set<double> sampleSetMd(MainMDset.begin(), MainMDset.end());
std::unordered_set<double> sampleSetProb(ProbMDset.begin(), ProbMDset.end());
std::unordered_set<double> sampleSetDec(decPoints.begin(), decPoints.end());

for (int i = 1; i < n; ++i){
ii = ii + 1;
double d = dt[ii];
out[ii] = inity + rgamma(1, a * timedt, b)[0];
inity = out[ii];

if (sampleSetDec.find(d) != sampleSetDec.end()) {
if (sampleSetProb.find(d + LT) != sampleSetProb.end() ||
sampleSetMd.find(d + LT) != sampleSetMd.end()) {
repFlag = inity >= LP;
} else if (sampleSetMd.find(d) != sampleSetMd.end() && repFlag) {
double genRanProb = rbinom(1, 1, (1 - p1))[0];
for (int j = ii; ii < (ii+x1); ++j){
out[j] = inity * genRanProb;
}
inity = inity * genRanProb;
ii = ii + x1 - 1;
} else if (sampleSetProb.find(d) != sampleSetProb.end() && repFlag) {
double genRanProb = rbinom(1, 1, pA)[0];
for (int j = ii; ii < (ii+x2); ++j){
out[j] = inity * genRanProb;
}
inity = inity * genRanProb;
ii = ii + x2 - 1;
}}}
return out;
}")

需要查看错误才能完全确定这是唯一的问题。但是类型铸造有几个问题。

for (int j = ii; ii < (ii+x1); ++j)

您声明了double x1int ii,这将不起作用,因为您正试图将ii隐式转换为double

再往下一点你就有了

ii = ii + x1 - 1;

再次尝试将int隐式转换为double

与相同

for (int j = ii; ii < (ii+x2); ++j){

ii = ii + x2 - 1;

TL;DR-C++是一种静态类型的语言。这种隐式类型转换是行不通的。

最新更新