2016-04-19 9 views
1

Rキャレット(参照番号:this example)を使用してクロス検証を行いたいが、CVモデルを作成するためのトレーニングでデータのサブセットのみを使用する。それでも、計算された制約のためにトレーニングに使用できない数百万のサンプルであっても、左の被験者のすべてのデータをテストする必要があるため、左のCVパーティションを全体として使用する必要があります。Rキャレット:トレーニングのためにデータサブセットを使用してクロス検証を行いますか?

私はこれを達成するためにcaret::traincaret::trainControlsubsetindexパラメータを使用して、最小限の2クラス分類の例を作成しました。私の見解では、これで問題は解決するはずですが、実際にはという評価は、依然として対象外の方法で行われています。。たぶん、このタスクの経験を持つ誰かがこの上でいくつかの光を当てることができます:

library(plyr) 
library(caret) 
library(pROC) 
library(ggplot2) 

# with diamonds we want to predict cut and look at results for different colors = subjects 
d <- diamonds 
d <- d[d$cut %in% c('Premium', 'Ideal'),] # make a 2 class problem 
d$cut <- factor(d$cut) 
indexes_data <- c(1,5,6,8:10) 
indexes_labels <- 2 

# population independent CV indexes for trainControl 
index <- llply(unique(d[,3]), function(cls) c(which(d[,3]!=cls))) 
names(index) <- paste0('sub_', unique(d[,3])) 
str(index) # indexes used for training models with CV = OK 

m3 <- train(x = d[,indexes_data], 
      y = d[,indexes_labels], 
      method = 'glm', 
      metric = 'ROC', 
      subset = sample(nrow(d), 5000), # does this subset the data used for training and obtaining models, but not the left out partition used for estimating CV performance? 
      trControl = trainControl(returnResamp = 'final', 
            savePredictions = T, 
            classProbs = T, 
            summaryFunction = twoClassSummary, 
            index = index)) 
str(m3$resample) # all samples used once = OK 

# performance over all subjects 
myRoc <- roc(predictor = m3$pred[,3], response = m3$pred$obs) 

プロット(myRoc、メイン= 'すべて')

個々の被験者のパフォーマンス

l_ply(ユニーク(M3 $ PRED (予測子= pred_sub [、3]、応答= pred_sub $ obs)$ {$ resample}、.fun = function(cls){ pred_sub < - m3 $ pred [m3 $ pred $ Resample == cls、] myRoc < プロット(myRoc、main = cls) })

お時間をありがとうございます!同時にcaret::trainControlindexindexOutパラメータの両方を使用して

答えて

1

は、トリック(ヒントin this questionマックスのおかげで)やっているようです。ここで更新されたコードは次のとおりです。まだ

library(plyr) 
library(caret) 
library(pROC) 
library(ggplot2) 
str(diamonds) 

# with diamonds we want to predict cut and look at results for different colors = subjects 
d <- diamonds 
d <- d[d$cut %in% c('Premium', 'Ideal'),] # make a 2 class problem 
d$cut <- factor(d$cut) 
indexes_data <- c(1,5,6,8:10) 
indexes_labels <- 2 

# population independent CV partitions for training and left out partitions for evaluation 
indexes_populationIndependence_subjects <- 3 
index <- llply(unique(d[,indexes_populationIndependence_subjects]), function(cls) c(which(d[,indexes_populationIndependence_subjects]!=cls))) 
names(index) <- paste0('sub_', unique(d[,indexes_populationIndependence_subjects])) 
indexOut <- llply(index, function(part) (1:nrow(d))[-part]) 
names(indexOut) <- paste0('sub_', unique(d[,indexes_populationIndependence_subjects])) 
# subsample partitions for training 
index <- llply(index, function(i) sample(i, 1000)) 

m3 <- train(x = d[,indexes_data], 
      y = d[,indexes_labels], 
      method = 'glm', 
      metric = 'ROC', 
      trControl = trainControl(returnResamp = 'final', 
            savePredictions = T, 
            classProbs = T, 
            summaryFunction = twoClassSummary, 
            index = index, 
            indexOut = indexOut)) 
m3$resample # seems OK 
str(m3$pred) # seems OK 
myRoc <- roc(predictor = m3$pred[,3], response = m3$pred$obs) 
plot(myRoc, main = 'all') 
# analyze results per subject 
l_ply(unique(m3$pred$Resample), .fun = function(cls) { 
    pred_sub <- m3$pred[m3$pred$Resample==cls,] 
    myRoc <- roc(predictor = pred_sub[,3], response = pred_sub$obs) 
    plot(myRoc, main = cls) 
}) 

、私は誰もが自分の考えを共有してください詳細についての知識を持っているので、もしこれが実際には、人口に依存しない方法に推定をしている場合は絶対にわかりません!

関連する問題