以下 R 代碼塊在速度方面效率非常低,我需要它顯著加快,因為在原始問題中length(dt)它非常大。
您能幫我將以下 R 代碼塊轉換為 C 以便在 R 中使用 RCPP 函式嗎?我對 C 的了解非常接近0并且無法弄清楚如何進行轉換。
inity <- 0
cumy <- rep(0,length = length(dt))
dec.index <- 1
startingPoint <- 2
temp <- numeric()
repFlag <- F
for (i in 2:length(dt)){
cumy[i] <- inity rgamma(n = 1, shape = a*timedt, scale = b)
inity <- cumy[i]
if (dt[i] %in% decPoints){
if (dt[i] %in% LTset){
repFlag <- ifelse(cumy[i] >= LP, T, F)
} else if (dt[i] %in% MainMDset && repFlag == T){
genRanProb <- rbinom(1,1,(1-p1))
cumy[i] <- inity*genRanProb
inity <- cumy[i]
} else if (dt[i] %in% ProbMDset && repFlag == T){
genRanProb <- rbinom(1,1,pA)
cumy[i] <- inity*genRanProb
inity <- cumy[i]
}
}
}
如果要運行代碼,可以使用以下值:
a <- 0.2
b <- 1
ph <- 1000
timedt <- 1
oppInt <- 90
dt <- seq(0,ph,timedt)
LT <- 30
MainMDset <- seq(oppInt, ph, oppInt)
ProbMDset <- c(0,seq((oppInt oppInt/2), ph, oppInt))
LTset <- sort(c(ProbMDset, MainMDset))
LTset <- LTset - LT
decPoints <- sort(c(LTset, ProbMDset, MainMDset))
decPoints <- decPoints[-which(decPoints < 0)]
decPoints[1] <- 1
p1 <- 0
pA <- 0.5
LP <- 40
更新:到目前為止,我的嘗試以錯誤告終,如圖所示。
NumericVector cumyRes(double a, double b, double timedt, NumericVector dt, NumericVector ProbMDset, NumericVector MainMDset, NumericVector decPoints, double LP, double LT){
bool repFlag = false;
int n = dt.size();
double inity = 0;
NumericVector out(n);
unordered_set<double> sampleSetMd(MainMDset.begin(), MainMDset.end());
unordered_set<double> sampleSetProb(ProbMDset.begin(), ProbMDset.end());
unordered_set<double> sampleSetDec(decPoints.begin(), decPoints.end());
for (int i = 1; i < n; i){
out[i] <- inity rgamma(1, a*timedt, 1/b)[0];
inity <- out[i];
if (sampleSetDec.find(dt[i])){
if (out[i] >= LP){
repFlag = true;
} else {
repFlag = false;
}
}
return out;
}
}
/*** R
cumyRes(0.2, 1, 1, c(50,100,150,200), c(25,75,125,175), c(20, 45, 70, 95, 120, 145, 170, 195), 40, 30)
*/

uj5u.com熱心網友回復:
您的 C 代碼中有幾個錯誤,可能太多了,無法在簡明的答案中列出。但是,以下更正似乎遵循您的邏輯,并編譯以在更短的時間內為您的 R 回圈提供類似的答案:
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){
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){
double d = dt[i];
out[i] = inity rgamma(1, a * timedt, b)[0];
inity = out[i];
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];
out[i] = inity * genRanProb;
inity = out[i];
} else if (sampleSetProb.find(d) != sampleSetProb.end() && repFlag) {
double genRanProb = rbinom(1, 1, pA)[0];
out[i] = inity * genRanProb;
inity = out[i];
}}}
return out;
}")
您可以使用以下方法對其進行測驗:
cumyRes(a, b, timedt, dt, ProbMDset, MainMDset, decPoints, LP, LT, p1, pA)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/512543.html
標籤:C rfor循环if 语句rcpp
上一篇:R中forloop中的函式
下一篇:如何從For回圈中獲取回傳值并將其傳遞給.body(StringBody(session=>在Gatling中使用Scala
