【发布时间】:2021-02-07 16:13:52
【问题描述】:
我正在尝试在循环函数中应用索引来创建一个新的数据框,以从本质上操纵交叉验证结果。我在实际使用这些索引来应用到我的循环函数时遇到了问题。
错误发生在oppo,这是我尝试提取每个折叠的索引的地方。 oppo 应该代表折叠 1-5 的所有索引,除了折叠 i 之外。
创建数据框的可重现示例
#data
attach(PimaIndiansDiabetes)
data=PimaIndiansDiabetes
#create training and testing sets
set.seed(101)
sample <- sample.int(n = nrow(data), size = floor(.7*nrow(data)), replace = F)
train <- data[sample, ]
test <- data[-sample, ]
#create simple RF
ctrl <- trainControl(method="cv", number=5, classProbs = TRUE, summaryFunction = twoClassSummary, savePredictions = TRUE)
rf_model <- train(diabetes ~., data=train,
metric="ROC",
trControl=ctrl)
#reformat the dataframe of interest
cv_dataframe <- rf_model$pred %>% filter(mtry==2)
cv_dataframe$Resample <- sub("Fold", "", cv_dataframe$Resample)
在我的循环函数中,我想设置 i = 1:5,并为除 i 之外的所有内容获取 rowIndex。所以对于下面的数据框,
head(cv_dataframe)
pred obs neg pos rowIndex mtry Resample
#1 neg neg 0.540 0.460 1 2 1
#2 neg pos 0.544 0.456 11 2 1
..
#3 neg neg 0.926 0.074 5 2 2
#4 pos neg 0.182 0.818 16 2 2
..
#5 neg neg 0.764 0.236 17 2 3
#6 neg neg 0.780 0.220 26 2 3
在我为Resample==1 提取rowIndex 后,我想将!rowIndex 应用于train 并得到一个数据帧的输出train 但只有与Resample 2 到5 匹配的索引。然后我想在train 上预测rowIndex,其中Resample==1。这是我尝试过的:
cv_performance <- as.data.frame(t(sapply(sort(unique(cv_dataframe$Resample)),
function(i) {
#extract indices where Resample is opposite of i
oppo <- cv_dataframe$rowIndex[!cv_dataframe$Resample==i] ##HERE IS THE ERROR
#ask it to paste df of folds 2-5
print(train[oppo,])
#now look at results where test fold is opposite of oppo
test_prob_cv <- as.data.frame(predict(rf_model, #original model
newdata = train[!oppo,], #data of leftover fold
type = "prob"))
})))
但我认为问题在于oppo,因为我不能将它用作索引列表。
【问题讨论】:
标签: r function loops cross-validation threshold