所以,到目前爲止,我只寫了一些R代碼(準確地說2個項目),這可能可以證明這個問題的愚蠢程度,這對於一個經驗豐富的程序員來說似乎是合理的。如何訪問嵌套的foreach循環中的計數器?
我想平行我的K倍交叉驗證代碼,其目的是找到用於最終模型的最佳變量集合。
的代碼是有點像這樣
child <- foreach(i=icount(ncol(parentModel)-1),.combine = 'rbind') %:%{
childModel<-parentModel
childModel[,i]<-NULL
filteredTestMTM <-foreach(j = icount(nFolds),.combine = c, .export = c("DataSplit","getProbabilityThreshold","SharpeRatio")) %dopar% {
splitData <- DataSplit(childModel, nFolds = nFolds, testFold=j)
testData<-splitData$testData
trainingData<-splitData$trainingData
trainingMTM <- trainingData[,ncol(trainingData)]
testMTM <- testData[,ncol(testData)]
Trade <- (trainingMTM > 0.001)*1.0 #mtmThreshold to be used here instead of 0.001
trainingData <- trainingData[,1:(ncol(trainingData)-1),drop=FALSE]
trainingData <- cbind(trainingData, Trade)
logmodel <- glm(Trade ~ .,data=trainingData, family = "binomial"(link="logit"))
trainingData <- trainingData[,1:(ncol(trainingData)-1),drop=FALSE]
trainingResults <- predict(logmodel, newdata=trainingData, type="response")
probabilityThreshold <- getProbabilityThreshold(trainingResults, trainingMTM, 0.001) #new Probability function to be defined to use optimParam
tR <- predict(logmodel, newdata=testData, type="response")
tMTM <- testMTM * ((tR>probabilityThreshold)*1.0)
return(tMTM)
}
totalSharpe <- (mean(filteredTestMTM)/sd(filteredTestMTM))
if (is.nan(totalSharpe)) {
totalSharpe = 0.0
}
return(c(totalSharpe,i))
}
總之 - 我走parentModel,逐個取出變量,遊程k折交叉驗證,並收集結果。但我不斷收到錯誤
Error in `[<-.data.frame`(`*tmp*`, , i, value = NULL) :
object 'i' not found
任何人都可以請幫助我嗎?
編輯:我在Windows 7
我已經嘗試過只並行化一個循環,並行化外循環比內循環並行化得到更好的結果。我只是想檢查嵌套並行化是否會更好。 – Smit