2016-06-01 950 views
1

我正在尝试编写一个循环以进行重复的K-fold交叉验证。基本上尝试执行10倍交叉验证并重复该过程10次以获得预测结果和10个AUC值。重复K-折交叉验证的循环

我似乎在循环中遗漏了一些东西,它允许将计算出的预测移动到为k倍结果创建的空数据框的相应列中。我只在我的输出中得到最后的k-fold分数...而不是全部10.我仍然必须得到每个k-fold验证的auc值。

有没有办法将auc计算合并到循环中以获取值?如果有人能指导我这一点,将不胜感激。


library(cvTools) 
library (glmnet) 
#library(pROC) 

k <- 10 #the number of folds 
x <- structure(list(PC1 = c(-2.03456672313651, -1.73707505007147, 
-2.03456672313652, -0.255368300655119, -1.73707505007143, -2.03456672313651, 
-0.37500359723752, -2.03456672313651, -2.03456672313651, 3.47288460329945, 
-0.734187869112349, -0.0134149056651377, 0.0942929078885968, 
-2.0345667231365, -2.03456672313651), PC2 = c(0.112471741011579, 
0.133858302549922, 0.1124717410116, 2.61374131070885, 0.133858302549994, 
0.11247174101158, -0.158995891265301, 0.11247174101159, 0.112471741011592, 
-0.260528749768208, -0.503925189558291, 0.194756984230433, 0.318778158034713, 
0.112471741011598, 0.11247174101159), PC3 = c(2.44850389170835, 
2.3403087394181, 2.44850389170835, -2.46949441441314, 2.34030873941815, 
2.44850389170834, 0.123937826076267, 2.44850389170836, 2.44850389170835, 
-0.367483430521022, -0.155846438581532, 0.509441984698824, 0.612816030555617, 
2.44850389170836, 2.44850389170835), PC4 = c(0.112471741011652, 
0.133858302549981, 0.11247174101165, 0.00436673840662417, 0.133858302549995, 
0.112471741011656, -0.158995891265306, 0.112471741011666, 0.112471741011661, 
-0.260528749768211, -0.290253126970872, -2.28110627358792, 0.318778158034689, 
0.11247174101168, 0.11247174101167), PC5 = c(0.112471741011684, 
0.13385830255004, 0.112471741011692, 0.00436673840662224, 0.133858302550053, 
0.112471741011681, -0.158995891265284, 0.112471741011697, 0.112471741011696, 
-0.260528749768212, 1.20999715739728, -1.91404159432553, 0.318778158034758, 
0.112471741011709, 0.112471741011692)), .Names = c("PC1", "PC2", 
"PC3", "PC4", "PC5"), row.names = c("O35245", "O35286", "O54949", 
"O54991", "O88569", "P14733", "P16054", "P21619", "P24369", "P37889", 
"P40201", "P57080", "P60843", "P63085", "P99029"), class = "data.frame") 

folds <- cvFolds(NROW(x), K=k) 

mypreds <- data.frame(matrix(0, nrow(x),ncol = 10)) # create a dataframe to store results of all 10 k-fold repetititions 
row.names(mypreds) <- row.names(x) # row names for the dataframe 
names(mypreds) <- paste("K", (1:10), sep = "") # column names 

set.seed(123) 

j <- 1 
nsim = 10 # number of repetitions 

x$kfoldlpred <- rep(0,nrow(x)) # append a column to original dataframe to temporarily store results of each k-fold 

# the loop for repeated cross-validation 
repeatcv <- function(){ 
    while (j <= nsim){ 
    for(i in 1:k){ 
     train <- x[folds$subsets[folds$which != i], ] #Set the training set 
     train_response <- responseY1[folds$subsets[folds$which != i]] # set the training set response 

     validation <- x[folds$subsets[folds$which == i], ] #Set the validation set 

     lasso_newglm <- glmnet(as.matrix(train), train_response, alpha = 1,family = "binomial") #Get your new logistic regression model (just fit on the train data) 
     lasso_cvglm <- cv.glmnet(as.matrix(train), train_response, alpha = 1, family = "binomial",type.measure = "deviance") 
     lasso_newpred <- predict(lasso_newglm,newx = as.matrix(validation), type = "response", s = c(lasso_cvglm$lambda.min)) #Get the predicitons for the validation set (from the model just fit on the train data) 

     x[folds$subsets[folds$which == i],]$kfoldlpred <- lasso_newpred 
    } 
    mypreds[,i] <- x$kfoldlpred 
    j <- j+1 
    } 
    return(mypreds) 
} 

回答

1

caret包提供重复交叉验证开箱。下面是一个最小的工作示例:

library(caret) 
model <- train(x = iris[51:150,1:2], 
       y = factor(iris[51:150,5]), 
       method = 'glmnet', 
       metric='ROC', 
       trControl = trainControl(method = 'repeatedcv', # repeated cross validation 
             number = 10, # nr of partitions 
             repeats = 10, # nr of repeats 
             classProbs = T, 
             summaryFunction = twoClassSummary)) 

model$resample为您提供了AUC在所有分区和重复(含10个分区和10个重复其10 * 10 = 100个值):

> model$resample 
    ROC Sens Spec  Resample 
1 0.90 0.8 0.8 Fold05.Rep10 
2 0.98 1.0 0.8 Fold04.Rep10 
3 0.80 1.0 0.2 Fold01.Rep09 
4 0.64 0.4 0.8 Fold08.Rep07 
5 0.86 0.8 0.8 Fold05.Rep06 
[...] 

BTW:如果你还需要绘制所有分区和重复的ROC曲线,请参阅this question

+0

谢谢你的回复。然而,我仍然试图在使用脱字符包之前将其作为第一步进行自我编码,这将是下一步。我一定会用你的例子作为指导。 – DataStudent